*
The moose likes Servlets and the fly likes How do I avoid IllegalState when calling getReader in chain? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How do I avoid IllegalState when calling getReader in chain?" Watch "How do I avoid IllegalState when calling getReader in chain?" New topic
Author

How do I avoid IllegalState when calling getReader in chain?

Richard Johnson
Greenhorn

Joined: Mar 30, 2002
Posts: 10
Please help, this is driving me absolutely crazy!

All I want to do is intercept the request, print the header and body to the log and pass the request onto the servlet. So I use a filter chain. But if I call getReader to print the body, it then throws an IllegalStateException further up the chain.

How do I avoid this and print the body to the log, while leaving the streams intact? possible?

Thanks

Richard.
Brahim Bakayoko
Ranch Hand

Joined: Aug 29, 2003
Posts: 155
Use a HttpServletResponseWrapper and implement the getWriter method to return a CharArrayWriter.

When the request returns to the first filter, get the real PrintWriter and print the content of the CharArrayWriter.

This is the same technique use for compression filters.

You cannot obtain a response output more than once.
Either use what I said above or save a reference of the output in the request scope. If the later, make sure only the last to access the output object is committing the response.


SCJP, SCWCD, SCBCD, IBM CSD WebSphere v5, <br />A+, MCP 2000 and 2000 server, CST, and few incompleted certification tracks.<br /> <br />Ivory Coast<br /> <br />Analyze your web Request/Response @ <a href="http://webtools.servehttp.com" target="_blank" rel="nofollow">http://webtools.servehttp.com</a> down for a while...
Richard Johnson
Greenhorn

Joined: Mar 30, 2002
Posts: 10
Can you show me a code example? I'm not sure I've understood the fix. Here's my original code:

Brahim Bakayoko
Ranch Hand

Joined: Aug 29, 2003
Posts: 155


in the filter:

Richard Johnson
Greenhorn

Joined: Mar 30, 2002
Posts: 10
ok. thanks for the help on this one. I'm getting closer, but have one last issue to deal with. How do I override, getInputStream() and return a ServletInputStream? I'm using WAS 5.0. Here is what I have so far for my wrapper:

Brahim Bakayoko
Ranch Hand

Joined: Aug 29, 2003
Posts: 155
Originally posted by Richard Johnson:
ok. thanks for the help on this one. I'm getting closer, but have one last issue to deal with. How do I override, getInputStream() and return a ServletInputStream? I'm using WAS 5.0. Here is what I have so far for my wrapper:



I implemented my own ByteArrayReader and ByteArrayWriter.
There are no such classes in the java api.

And no, I will not give you the source code of my implementations.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do I avoid IllegalState when calling getReader in chain?