You need to buffer the whole response yourself, by default Undertow will just sent the
bytes straight to the socket, so the headers will already be sent by the time the response
is finished.
You will need to write a StreamSinkConduit implementation that buffers the full response,
then when terminateWrites() is called you sign the request using the buffered data, and
then try and write out the buffered data. If it can't all be written out you finish
writing it in the flush() method.
Stuart
----- Original Message -----
From: "Vikas Nagaraj"
<Vikas.Nagaraj(a)safenet-inc.com>
To: undertow-dev(a)lists.jboss.org
Sent: Thursday, 18 February, 2016 6:10:49 AM
Subject: [undertow-dev] How to get response bytes before they are sent
Hello,
Is it possible to get the full content of a response body before it’s sent?
I’m trying to sign the response and add this signature in a header. There
was a similar question on this list a couple of weeks ago, but that one was
about signing the incoming request. My current approach is, in
HttpHandler.handleRequest():
- Create a javax.crypto.Signature object
- Use HttpServerExchange. addResponseWrapper() to add a StreamSinkConduit
that updates the Signature object as write() is called
- Use HttpServerExchange.addResponseCommitListener() to add a callback that
calls Signature.sign() when the response is ready to be sent
Good in theory, but I’m finding that the sign happens before the updates –
looks like the Conduit is only active as the response is being sent. Is
there something else I should wrap to get the bytes as they’re being added
to the response? Even better would be if I could read them directly from the
ResponseCommitListener.beforeCommit() callback. I can see the number of
bytes that have been written, but not the actual data.
Note: I’m using Undertow 1.2.x, as we’re on WildFly 9.
--
Vikas Nagaraj
vikas.nagaraj(a)safenet-inc.com
The information contained in this electronic mail transmission
may be privileged and confidential, and therefore, protected
from disclosure. If you have received this communication in
error, please notify us immediately by replying to this
message and deleting it from your computer without copying
or disclosing it.
_______________________________________________
undertow-dev mailing list
undertow-dev(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/undertow-dev