Results 1 to 2 of 2
  1. #1
    touchsky88 is offline Member
    Join Date
    Jan 2008
    Posts
    2
    Rep Power
    0

    Default How to avoid response being commited in filter

    Recently dealing with a problem that the ServletResponse being comminted in Filter class after chain.doFilter() being called. Once ServletResponse is being commited, it won't allow us to manipulate the response any more. After resolved this issue, thought that worth to publish this online to share with every body.

    Example:
    YourFilter - your filter class.
    YourController - your controller class that chain.doFilter() goes to
    YourMockServletResponse - your mockup ServletResponse class which will act like ServletResponse.
    YourResponseWrapper - your response wrapper
    YourFilter implements Filter
    {
    public void doFilter(request, response, chain)
    {
    YourMockServleResponse mockRes = new YourMockServletResponse();
    YourResponseWrapper buffer = new YourResponseWrapper ();
    //Now you are passing in your mock up response object,
    //so the actual response is intact.
    chain.doFilter(request, buffer);
    String yourData1 = buffer.toString();
    //you can actually make multiple calls on chain.doFilter()
    //by resetting request attribute.
    request.setAttribute("myTest", "myTest");
    buffer.reSet();
    chain.doFilter(request, buffer);
    String yourData2 = buffer.toString();
    String yourData = maniputateYourData(data1, data2);
    //Manipulate yourData1 and yourData2 to deal with your business
    //After you done, output your data
    //Now we are using the actual response
    PrintWriter writer = response.getWriter();
    writer.write(yourData);
    writer.close.
    }
    }

    Here is the rest of the class example.
    public class YourResponseWrapper extends HttpServletResponseWrapper
    {

    private CharArrayWriter output;

    public String toString()
    {
    return output.toString();
    }

    public StringResponseWrapper(HttpServletResponse response)
    {
    super(response);
    output = new CharArrayWriter();
    }

    public PrintWriter getWriter()
    {
    return new PrintWriter(output);
    }
    }

    public class MockServletResponse implements HttpServletResponse
    {
    private ByteArrayOutputStream outputStream = null;
    private PrintWriter out = null;

    public ByteArrayOutputStream getOutputStreamNotServlet() throws IOException
    {
    if (this.out != null) this.out.flush();
    if (this.outputStream != null) this.outputStream.flush();
    return this.outputStream;
    }

    /**
    * Used to close the outout streams if you have used the getWriter(), if not
    * the output streams will not be created.
    *
    */
    public void closeOutputStream() throws IOException
    {
    if (this.out != null) this.out.close();
    if (this.outputStream != null) this.outputStream.close();
    }

    public PrintWriter getWriter() throws IOException
    {
    if (this.outputStream == null)
    {
    this.outputStream = new ByteArrayOutputStream();
    this.out = new PrintWriter(outputStream);
    }

    return this.out;
    }

    //You need to implement the rest abstract methods inherited from parent.
    }

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Thanks for the info, but code tags would improve readability.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

Similar Threads

  1. how to avoid input errors?
    By Sinnergy in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 12:25 AM
  2. avoid legitimate spamming
    By hervey in forum Suggestions & Feedback
    Replies: 1
    Last Post: 10-29-2008, 09:20 AM
  3. Avoid/Eliminate Duplicate Code
    By kicker in forum New To Java
    Replies: 6
    Last Post: 09-11-2008, 05:14 AM
  4. web content filter or internet filter
    By sundarjothi in forum Advanced Java
    Replies: 3
    Last Post: 05-15-2008, 12:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •