You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by bu...@apache.org on 2004/04/02 09:37:26 UTC

DO NOT REPLY [Bug 28152] New: - problem with wrapper and dofilter

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=28152>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=28152

problem with wrapper and dofilter

           Summary: problem with wrapper and dofilter
           Product: Struts
           Version: 1.1 Final
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: Blocker
          Priority: Other
         Component: Tiles framework
        AssignedTo: struts-dev@jakarta.apache.org
        ReportedBy: tekn0@libero.it


I need to capture all the response data that comes back from the servlet. 
So, I have a simple filter that wraps the response object, and sends the 
wrapped object into filterChain.doFilter() method.
I've set up the web.xml so that any servlet access should trigger my filter 
(the <url-pattern> is *.jsp, *.html and *.do). 
When the browser makes a .html request, everything works as expected.
After the filterChain.doFilter(), I can take a look at the wrapped response 
object and print the contents of the response.
However, when I access a .jsp page or .do action, the output stream of my 
wrapper is empty after the last call to <tiles:insert> tag.
If I don't wrap the response, then everything seems to work fine.

I've enclosed the source code & the output that demonstrates the problem.

Is this a bug in Struts ? If this is a bug, is there a patch available?

Thanks


------ Begin ResponseWrapper.java ------
public class ResponseWrapper extends HttpServletResponseWrapper {
	private PrintWriter printWriter; 
	private ResponseOutputStream outputStream;

	public ResponseWrapper(ServletResponse response) throws 
java.io.IOException { 
		super((HttpServletResponse) response);
		
		outputStream = new ResponseOutputStream
(response.getOutputStream());
		printWriter = new PrintWriter(outputStream);
	}

	public ServletOutputStream getOutputStream() throws 
java.io.IOException {
		return outputStream;
	}

	public PrintWriter getWriter() throws java.io.IOException {
		return printWriter;
	}
}
------ End ResponseWrapper.java ------

------ Begin ResponseOutputSteam.java ------
public class ResponseOutputStream extends ServletOutputStream {
	private OutputStream outputStream;
	private ByteArrayOutputStream arrayStream;
	private boolean closed = false;
     
	public ResponseOutputStream(OutputStream stream) {
		outputStream = stream;
		arrayStream = new ByteArrayOutputStream();
	}

	public void write(int i) throws java.io.IOException {
		arrayStream.write(i);
	}

	public void close() throws java.io.IOException {
		if (!closed) {
		  processStream();
		  outputStream.close();
		  closed = true;
		}
	 }
	 	
	public void flush() throws java.io.IOException {
		if (arrayStream.size() != 0) {
			if (!closed) {
				processStream();
				arrayStream = new ByteArrayOutputStream();
			}
		}
	}

	private void processStream() throws java.io.IOException {
		outputStream.write(replaceContent(arrayStream.toByteArray()));
		outputStream.flush();
	}
  
	public byte[] replaceContent(byte[] bytesContent) {
		byte[] returnContent = new byte[bytesContent.length];
		
		int j = 0;

		for (int i = 0; i < bytesContent.length; i++) {
			if (bytesContent[i] != '\n' && bytesContent[i] !
= '\t') {
				returnContent[j++] = bytesContent[i];
			}
		}
		
		return returnContent;
  	}
}
------ End ResponseOutputSteam.java ------

------ Begin ResponseFilter.java ------
public class ResponseFilter implements Filter {
	private FilterConfig filterConfig = null;
	
	public void doFilter(
			ServletRequest request, 
			ServletResponse response,
			FilterChain chain)
			throws IOException, ServletException {
		ResponseWrapper responseWrapper = new ResponseWrapper
(response); 
		chain.doFilter(request, responseWrapper);  
		responseWrapper.getOutputStream().close();
	}

	public void destroy() {
		this.filterConfig =null;
	}

	public void init(FilterConfig filterConfig) {
		this.filterConfig = filterConfig;
	}
}
------ End ResponseFilter.java ------

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org