Skip to main content

Servlet Filter breaking Jsf Extensions/DynaFaces

1 reply [Last post]
Joined: 2008-08-13

I have been using dynafaces for a while and it is a great project. I been using ajaxZone and it is like magic just add the tag and presto everything contained within get ajaxified.
Unfortunately I recently need to add a application wide filter to do some post processing and this filter has broken the ajaxZone. I have tried everything to try to fix this but no luck. I have verified that the ajax request is being sent and a blank ajax response is returned and it seems to be a problem with the PostProcessingStream of my PostProcessingServletResponseWrapper not being written to. I can see that my PostProcessingStream in being wrapped in a noOpPrintWriter by AsyncResponse. Any insight would be greatly appreciated. My code is below


<br />
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {</p>
<p>      PostProcessingServletOutputStream postProcessingStream = new PostProcessingStream(servletResponse.getOutputStream());<br />
      PostProcessingServletResponseWrapper wrappedResp = new PostProcessingServletResponseWrapper(servletResponse, postProcessingStream);</p>
<p>      try{<br />
         chain.doFilter(servletRequest, , wrappedResp);<br />
      }finally{<br />
         wrappedResp.close();<br />
      }<br />
<p>public abstract class PostProcessingServletOutputStream extends ServletOutputStream {<br />
   /** original servlet Response Stream */<br />
   protected OutputStream origResponseStream;<br />
   /** buffer stream of output */<br />
   protected ByteArrayOutputStream bufferStream;<br />
   private boolean closed = false;</p>
<p>   public PostProcessingServletOutputStream(ServletOutputStream servletOutputStream)<br />
throws{<br />
      origResponseStream = servletOutputStream;<br />
      bufferStream = new ByteArrayOutputStream();<br />
<p>   public void write(int i) throws {<br />
      bufferStream.write(i);<br />
<p>   public void close() throws {<br />
      if (!closed) {<br />
         if (getPostProcessRequired() ){<br />
            doPostProcessStream();<br />
         }else<br />
            origResponseStream.write(bufferStream.toByteArray());<br />
         origResponseStream.flush();<br />
         origResponseStream.close();<br />
         closed = true;<br />
      }<br />
<p>   public void flush() throws {<br />
      if (bufferStream.size() != 0) {<br />
         if (!closed) {<br />
            if (getPostProcessRequired()){<br />
               doPostProcessStream();<br />
            }else<br />
               origResponseStream.write(bufferStream.toByteArray());<br />
            origResponseStream.flush();<br />
            bufferStream.reset();<br />
         }<br />
      }<br />
<p>   /** indicates if post processing is required */<br />
   public abstract boolean getPostProcessRequired();</p>
<p>   /** does post processing on response and write out to actual response */<br />
   public void doPostProcessStream() throws{<br />
      System.out.print(bufferStream.toByteArray());//prints nothing<br />
      ......<br />
      ......<br />
      ......<br />
      origResponseStream.write(postProcessed);<br />
<p>}<br />
public class PostProcessingServletResponseWrapper extends HttpServletResponseWrapper {<br />
    /** Using output stream flag. */<br />
    protected boolean usingOutputStream = false;<br />
    /** Using writer flag. */<br />
    protected boolean usingWriter = false;</p>
<p>   private PrintWriter wrappedWriter;<br />
   private PostProcessingServletOutputStream wrappedStream;</p>
<p>   public PostProcessingServletResponseWrapper(ServletResponse response, PostProcessingServletOutputStream postProcessingServletOutputStream) throws {<br />
      super((HttpServletResponse) response);<br />
      wrappedStream = postProcessingServletOutputStream;<br />
<p>   public PrintWriter getWriter() throws {<br />
            if (usingOutputStream)<br />
                throw new IllegalStateException("getOutputStream() was already called");<br />
            if (Globals.STRICT_SERVLET_COMPLIANCE) {<br />
                setCharacterEncoding(getCharacterEncoding());<br />
            }<br />
            if (wrappedWriter == null) {<br />
             wrappedWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter(wrappedStream, getCharacterEncoding())),false);<br />
            }<br />
            usingWriter = true;<br />
      return wrappedWriter;<br />
<p>   public ServletOutputStream getOutputStream() throws {<br />
        if (usingWriter)<br />
            throw new IllegalStateException("getWriter() was already called");<br />
        usingOutputStream = true;<br />
      return wrappedStream;<br />
   }<br />
   /** calls close on PostProcessingServletOutputStream to run doPostProcessStream if needed */<br />
   public void close() throws {<br />
      wrappedStream.close();<br />
<p>   }</p>
<p>   public void setContentType(String type) {<br />
      if(type!=null)<br />
         super.setContentType(contentType);<br />
   }<br />

Message was edited by: nonome

Message was edited by: nonome

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2004-05-06


I'll have to try this out before getting back to you.
In the mean time, can you try using fireAjaxtransaction to ire an individual
Ajax request with the servlet filter to see if the problem still persists?

Thanks, Roger.