title: JAX-RS add headers to chunked response
body: <p>I'm having some trouble with my REST API service on my JBOSS 7.1.1 server.<br>
  Right now I use a RESTEasy implementation and I have the following filter to add CORS headers to the response:</p>
  
  <pre><code>@WebFilter(filterName = "HeaderFilter", urlPatterns = {"/*"})
  public class HeaderFilter implements Filter {
  
      public void init(FilterConfig arg0) throws ServletException{}
      public void destroy(){}
  
      public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException
      {
          //Just continue the request
          filterChain.doFilter(request, response);
  
          //After the request add the response headers
          HttpServletResponse responseObj = (HttpServletResponse)response;
          responseObj.addHeader("Access-Control-Allow-Origin", "*");
          responseObj.addHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS");
          responseObj.addHeader("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Content-Length");
      }
  }
  </code></pre>
  
  <p>My problem is that the server sometimes chooses to send the response in parts.<br>
  Via the Transfer-Encoding: chunked "protocol".<br>
  This doesn't matter and I have no problem with that but it doesn't add the CORS headers anymore when it does this. The client side of the application is now unable to use the data and I cannot figure out how to add the CORS headers to the final response.  </p>
  
  <p>Do you guys know how I could fix this issue?</p>
  
  <p><strong>EDIT:</strong> I'm using Jackson to turn my lazy loaded model into JSON by the way, so I don't think it's as easy as just setting the Content-Length.</p>