This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem filtering servlet response

 
KC Smith
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to filter my http response coming out of the app server - I've tested with both ATG and jboss. The problem is that when using the filter with a response wrapper the response disappears. In ATG I get a blank page, and jboss a 404.

The filter is very simple at this stage, simply getting the response and wrapping it for modification, and chaining the filter. Eventually I want to modify the html.

I can see the html output in the console, so I know the filter executes, but the page never makes it to the browser. If I change the chain.doFilter(request, buffer) to chain.doFilter(request, response) it works fine - but then I won't be able to modify the response...

I'd appreciate any thoughts, I'm running out of ideas! Thanks.

doFilter():

PrintWriter out = response.getWriter();
ResponseBuffer buffer = new ResponseBuffer((HttpServletResponse) response);
chain.doFilter(request, buffer);

// get page content
String contents = buffer.toString();
log.info(contents);

// TODO stuff here

// send the page to the browser
CharArrayWriter caw = new CharArrayWriter();
caw.write(contents);
response.setContentLength(caw.toString().length());
out.write(caw.toString());
out.close();
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a sample app that captures the text (writes it to System.out) and also sends it to the browser.

The code is quick and dirty but you're welcome to it.
http://simple.souther.us/capture.war

In short, it uses the wrapper pattern.
 
KC Smith
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the sample. Strange, that works in jboss but not ATG. They use the same filter mappings.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't touched ATG Dynamo in years.
What version of the servlet spec does it comply with?
Filters were introduced in 2.3.
 
KC Smith
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's ATG 6.1, uses the 2.3 spec but the old 1.3.1 JDK. That should not cause any trouble. The atg application is Struts, and the filter is set catch any .do requests and post-process the response. Your code displays the html in the console, but the result never makes it to the browser.

It seems like a chaining problem, but the application chains correctly if the original response is used in the chain.doFilter, not the wrapper. Strange...
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at my filter code.
It's possible that I'm using some 1.4 functionality.

Try recompiling it with 1.3.
If it barfs, then you know where to start looking.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic