DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT <http://nagoya.apache.org/bugzilla/show_bug.cgi?id=16760>. ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND INSERTED IN THE BUG DATABASE.
http://nagoya.apache.org/bugzilla/show_bug.cgi?id=16760 Wrapping response in filter not possible Summary: Wrapping response in filter not possible Product: Tomcat 4 Version: 4.1.18 Platform: PC OS/Version: Windows NT/2K Status: NEW Severity: Blocker Priority: Other Component: Catalina AssignedTo: [EMAIL PROTECTED] ReportedBy: [EMAIL PROTECTED] The usage of a wrapped response which is derived from HttpServletResponseWrapper is no longer possible with Tomcat 4.1.18. In Tomcat 4.1.12 however, the following coding works fine: // -- Response wrapper class class ResponseWrapper extends HttpServletResponseWrapper { private PrintWriter printWriter; private ReplaceContentOutputStream outputStream; public ResponseWrapper(HttpServletResponse response) throws IOException { super(response); this.outputStream = new ReplaceContentOutputStream(response.getOutputStream ()); this.printWriter = new PrintWriter(this.outputStream); } public ServletOutputStream getOutputStream() throws IOException { return this.outputStream; } public PrintWriter getWriter() throws IOException { return this.printWriter; } } // -- Wrapped output stream class ReplaceContentOutputStream extends ServletOutputStream { private OutputStream outputStream; private ByteArrayOutputStream byteArrayOutputStream; private boolean closed = false; private boolean transformOnCloseOnly = false; public ReplaceContentOutputStream(OutputStream outputStream) { this.outputStream = outputStream; this.byteArrayOutputStream = new ByteArrayOutputStream(); } public void write(int i) throws IOException { this.byteArrayOutputStream.write(i); } public void close() throws IOException { if (!closed) { processStream(); this.outputStream.close(); closed = true; } } public void flush() throws IOException { if (this.byteArrayOutputStream.size() != 0) { if (!this.transformOnCloseOnly) { processStream(); this.byteArrayOutputStream = new ByteArrayOutputStream(); } } } public byte[] replaceContent(byte[] bytes) throws IOException { String newString = new String(bytes); int replacePosition = newString.indexOf("<%placeholder%>"); if (replacePosition != -1) { newString = newString.substring(0, replacePosition) + "replaced" + newString.substring(replacePosition + "<%placeholder%>".length()); } return newString.getBytes(); } public void processStream() throws IOException { byte[] bytes = replaceContent(this.byteArrayOutputStream.toByteArray()); this.outputStream.write(bytes); } public void setTransformOnCloseOnly() { this.transformOnCloseOnly = true; } } The filter itself is set up to process each request on *.jsp. When the wrapped response is passed in to the doFilter method, neither getServletOutputStream() nor getWriter() is called (extract from doFilter()): ... ResponseWrapper wrappedResponse = new ResponseWrapper(this.response); this.filterChain.doFilter(this.request, wrappedResponse); wrappedResponse.getOutputStream().close(); ... Thus, the wrapped response itself is entirely empty. --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]