Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I avoid IllegalState when calling getReader in chain?

 
Richard Johnson
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 155
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Richard Johnson
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 155
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


in the filter:

 
Richard Johnson
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 155
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic