I’m looking for a way to inspect the contents of a HttpServletResponse to sign them with a MD5 hash.
The pseudocode might look like this
process(Response response, Request request){
defaultProcessingFor(response,request);
dispatcher.handle(response,request);
// Here I want to read the contents of the Response object (now filled with data) to create a MD5 hash with them and add it to a header.
}
Is that possible?
Yes, that’s possible. You need to decorate the response with help of
HttpServletResponseWrapperwherein you replace theServletOutputStreamwith a custom implementation which writes the bytes to both the MD5 digest and the “original” outputstream. Finally provide an accessor to obtain the final MD5 sum.Update I just for fun played a bit round it, here’s a kickoff example:
The response wrapper:
The MD5 outputstream:
How to use it: