• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

How do I avoid IllegalState when calling getReader in chain?

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


in the filter:

 
Richard Johnson
Greenhorn
Posts: 10
  • 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
  • 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.
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic