Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Modifying the response to a HTTP servlet using a filter

843842Jul 27 2010 — edited Aug 6 2010
I have attached code ([Code Link|http://dontnegmeout.com/CGIRewrite.zip]) if you want to give this a go. I have created a servlet based on the perl cgi web proxy from [http://www.jmarshall.com/tools/cgiproxy/|http://www.jmarshall.com/tools/cgiproxy/].

What I want to do is post process this using the Filter API, but have now failed on multiple attempts. In the toy example I provide, hypothetically if you searched for Barrack Obama, the pages returned would have the name Barrack Obama replaced with my name Anthony Brew.

My first attempt was to mirror what I found in http://java.sun.com/products/servlet/Filters.html for injecting a hit count onto a page. This fails as it seems the response is written to response.getOutputStream(), and from there I dont seem to be able to read the text no matter what I do (even when I wrap the method in HttpServletResponseWrapper).

If you want to get the cgi script working you need to update the context.xml of tomcat 6 to <Context privileged="true"> .

in the com.modify.ModifyFilter class I have included the code that doesnt work
 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        PrintWriter out = response.getWriter();
        CharResponseWrapper wrapper = new CharResponseWrapper(
                (HttpServletResponse)response);
        chain.doFilter(request, wrapper);
        if(wrapper.getContentType() != null && wrapper.getContentType().equals("text/html")) {
            CharArrayWriter caw = new CharArrayWriter();
            String inner = wrapper.toString();

            caw.write((update == null)?inner:update.getTranTransformedString(inner));

            
            response.setContentLength(caw.toString().length());
            out.write(caw.toString());
        } else
            out.write(wrapper.toString());
        out.close();
    }
Any help / suggestions would be greatly appreciated, I am not sure how much hair I have left!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 3 2010
Added on Jul 27 2010
6 comments
523 views