• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do I avoid IllegalState when calling getReader in chain?

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


in the filter:

 
Richard Johnson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic