tags:

views:

478

answers:

2

Thanks to everyone in advance -

So I went ahead and implemented this code: http://stackoverflow.com/questions/701681/how-can-i-read-an-httpservletreponses-output-stream

Which gives me the body of the response (html etc), but I am not getting any headers at all - is this possible to do with HttpServletResponseWrapper? I need to capture the entire request to all for header modification etc.

Thanks,

Sam

A: 

A simple solution is to use Tomcat's Request Dumper Valve. This dumps all meta information including header fields for each incoming HttpServletRequest and outgoing HttpServletReply.

Stephen C
Thanks for the reply - I actually need this to allow for modification of the headers on the way to the client -Thanks though
Sam Ingrassia
+2  A: 

If you want to capture headers, your best approach is to override all addHeader() / setHeader() (Date, int and String) in your HttpServletResponseWrapper and store them in a HashMap or Properties instance (or MultiMap if you're using Google Collections).
Then you can do whatever you want with them in your filter after response is processed. You'll need to expose them somehow in your wrapper as well (e.g. write a getHeaders() method) because regular HttpServletResponse does not.

ChssPly76
Thanks for the reply-I went ahead and overrode all the header modification methods - and tested it out with a simple jsp containing "response.addHeader("myheader","headervalue");" and that header gets captured, but it looks like the following headers are not being set via those methods:Status=OK - 200Server=Apache-Coyote/1.1Content-Type=text/htmlContent-Length=0Date=Sun, 02 Aug 2009 02:05:36 GMTIt looks like tomcat is setting those somewhere else, am I missing something?Thanks again for your help!
Sam Ingrassia
Those are set by your server and/or servlet container. You should be able to _override_ them (meaning if you set, say, content type to something else, server will keep it that way), but you won't capture them in your wrapper because they are added after you filterChain (and your filter) is already processed.
ChssPly76
I tested out that out and it looks like the headers are not being overwritten or replaced, check out this example:Status=OK - 200Server=Apache-Coyote/1.1Status=500Content-Type=text/htmlContent-Length=0Date=Sun, 02 Aug 2009 02:40:30 GMTThe status 500 is what I am setting after I call doFilter(), but as you can see its not replacing the status=200 header which tomcat is setting (i think) - any ideas?Thanks again for your help
Sam Ingrassia
You've chosen a wrong header to test :-) Sorry I haven't mentioned that before - you've said "headers" in the question and I didn't think you'd be changing status. Status is the first thing that gets set - via setStatus() or sendError(), so you'll need to override those methods as well if you want to change it. Keep in mind that those methods have very specific behavior dealing with committed response: http://java.sun.com/javaee/5/docs/api/javax/servlet/http/HttpServletResponse.htmlThen there's also sendRedirect() and setContentType(), though latter _should_ be overridable via normal header
ChssPly76
Thanks again for your help - its been incredibly helpful!
Sam Ingrassia