Summary: | 7.0.21 change can use undesired Writer object. Wrapper circumvention. Servlet 2.3 | ||
---|---|---|---|
Product: | Tomcat 7 | Reporter: | Brad Plies <bplies> |
Component: | Servlet & JSP API | Assignee: | Tomcat Developers Mailing List <dev> |
Status: | RESOLVED DUPLICATE | ||
Severity: | normal | CC: | bplies |
Priority: | P2 | ||
Version: | 7.0.21 | ||
Target Milestone: | --- | ||
Hardware: | PC | ||
OS: | All |
Description
Brad Plies
2011-11-08 20:32:59 UTC
Attempted example // See: http://download.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponseWrapper.html class MyWrapper extends HttpServletResponseWrapper { public MyWrapper(HttpServletResponse response) { super(response); } // This was working fine with Tomcat 7.0.20 public void sendResponse() throws IOException { this.pw.flush(); this.pw.close(); ServletOutputStream realStream = super.getOutputStream(); byte[] byteArray = basos.baos.toByteArray(); realStream.write(byteArray); realStream.flush(); } // NOTE: Alternate Writer & OutputStream MyOutputStream basos = new MyOutputStream(); PrintWriter pw = new PrintWriter(basos); // NOTE: Overriding default behavior of Wrapper, returning alternates public ServletOutputStream getOutputStream() throws IOException { return basos; } public PrintWriter getWriter() throws IOException { return pw; } } } public class MyFilter implements Filter { @Override public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { final HttpServletRequest httpRequest = (HttpServletRequest) request; final MyWrapper wrappedResponse = new MyWrapper((HttpServletResponse) response); chain.doFilter(httpRequest, wrappedResponse); // ... Something down the chain called wrappedResponse.sendRedirect() if (someCondition) { // Override redirect and handle response directly or something response.sendErrorPage(...); // NOTE: Original response object, discarding anything written to the wrapped buffer // !!! Blows up in Tomcat 7.0.21 because sendRedirect() circumvented the Wrapper's Writer } else { // Accept the wrapped Response as the output // Set response output to be same as wrappedResponse output } } } There are a bunch of issues related to this for which no simple solution is available since application provided wrappers are unlikely to be available at the point where the redirect needs to take place. See the duplicate for details of the fix. *** This bug has been marked as a duplicate of bug 51952 *** |