title: Servlet filters and response wrapper
body: <p>Trying to mess around with the response content I tried <a href="http://docs.oracle.com/cd/B14099_19/web.1012/b14017/filters.htm#i1000578" rel="nofollow noreferrer">this filter tutorial</a></p>
  
  <p>So I defined my classes as</p>
  
  <pre><code>public class FilterServletOutputStream extends ServletOutputStream
  {
      //=======================================================================
      private DataOutputStream stream; 
      //=======================================================================
      public FilterServletOutputStream(OutputStream output) { 
        stream = new DataOutputStream(output); 
      }
      //=======================================================================
      @Override
      public void write(int b) throws IOException  {
          stream.write(b); 
      }
      //=======================================================================
      @Override
      public void write(byte[] b) throws IOException  { 
          stream.write(b); 
      }
      //=======================================================================
      @Override
      public void write(byte[] b, int off, int len) throws IOException  {
          stream.write(b,off,len); 
      }
      //=======================================================================
  }
  
  
  
  
  public class GenericResponseWrapper extends HttpServletResponseWrapper 
  { 
    private ByteArrayOutputStream output;
    private int contentLength;
    private String contentType;
    public GenericResponseWrapper(HttpServletResponse response) { 
      super(response);
      output=new ByteArrayOutputStream();
    } 
    public byte[] getData() { 
      return output.toByteArray(); 
    } 
    @Override
    public ServletOutputStream getOutputStream() 
    { 
      return new FilterServletOutputStream(output); 
    } 
  
    @Override
    public PrintWriter getWriter() 
    { 
      return new PrintWriter(getOutputStream(),true); 
    } 
  
    @Override
    public void setContentLength(int length) { 
      this.contentLength = length;
      super.setContentLength(length); 
    } 
  
    public int getContentLength() { 
      return contentLength; 
    } 
    @Override
    public void setContentType(String type) { 
      this.contentType = type;
      super.setContentType(type); 
    } 
    @Override
    public String getContentType() { 
      return contentType; 
    } 
  } 
  </code></pre>
  
  <p>And then my filter.</p>
  
  <pre><code>public void  doFilter(ServletRequest request, ServletResponse response, FilterChain   chain) throws IOException, ServletException
  {
      //===================================================================
      GenericResponseWrapper wrapper = new GenericResponseWrapper((HttpServletResponse) response);
      chain.doFilter(request, wrapper);
      OutputStream out = response.getOutputStream();
      out.write(wrapper.getData());
      out.write("test content".getBytes());
      out.close();
      //===================================================================
  }
  </code></pre>
  
  <p>The filter seems to be ok. I can read "test content" but it seems <code>wrapper.getData()</code> returns 0 bytes. When I call <code>chain.doFilter(..)</code> usually is a servlet that some times dispatches to a jsp page. Tried a URL of a JSP plain page but it does not seem to write data.
  The filter is configured to catch ALL requests and it does it well.</p>
  
  <p>What can I be doing so wrong? The only difference is that I added <code>@override</code>. But I tried without them also.</p>
  
  <p>Added a JSP.</p>
  
  <pre><code>  &lt;%@page contentType="text/html" pageEncoding="UTF-8" session="false"%&gt;
    &lt;!DOCTYPE html&gt;
    &lt;html&gt;
    &lt;head&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
    &lt;style&gt;
    &lt;%-- @include file="/static/css/divs.css" --%&gt;
    &lt;%-- @include file="/static/css/links.css" --%&gt;
    &lt;/style&gt;
    &lt;title&gt;title&lt;/title&gt;
    &lt;/head&gt;
    &lt;body style="margin-top: 0px; margin-left: 0px; margin-right: 0px"&gt;
    &lt;%--@include file="publicheader.jsp" --%&gt;
    &lt;div class="desktopwebpagecontainer"&gt;
    main page.
    &lt;/div&gt;
    &lt;%--@include file="publicfooter.jsp" --%&gt;
    &lt;/body&gt;
    &lt;/html&gt;
  </code></pre>