Problem with wrapper and dofilter

Discussion in 'Java' started by tek.no.logik, Apr 5, 2004.

  1. tek.no.logik

    tek.no.logik Guest

    I need to capture all the response data that comes back from the
    servlet (Tomcat + Struts environment).
    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 or Tomcat ? 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 != '\n' && bytesContent !
    = '\t') {
    returnContent[j++] = bytesContent;
    }
    }

    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 ------

    --- Begin Filter configuring into web.xml ---
    <filter>
    <filter-name>ResponseFilter</filter-name>
    <filter-class>it.openkey.util.filters.ResponseFilter</filter-
    class>
    </filter>
    <filter-mapping>
    <filter-name>ResponseFilter</filter-name>
    <url-pattern>*.jsp</url-pattern>
    </filter-mapping>
    <filter-mapping>
    <filter-name>ResponseFilter</filter-name>
    <url-pattern>*.do</url-pattern>
    </filter-mapping>
    <filter-mapping>
    <filter-name>ResponseFilter</filter-name>
    <url-pattern>*.html</url-pattern>
    </filter-mapping>

    --- End Filter configuring into web.xml ---
     
    tek.no.logik, Apr 5, 2004
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phin
    Replies:
    2
    Views:
    548
    =?Utf-8?B?ZGhucml2ZXJzaWRl?=
    Jun 2, 2005
  2. Ken Carroll
    Replies:
    4
    Views:
    460
    Sudsy
    Sep 19, 2004
  3. Replies:
    0
    Views:
    458
  4. Fred Paris
    Replies:
    5
    Views:
    526
    benben
    Sep 10, 2005
  5. Jeff
    Replies:
    1
    Views:
    1,487
    Andrew Thompson
    Aug 9, 2007
  6. NeoGregorian
    Replies:
    1
    Views:
    468
    A.T.Hofkamp
    Sep 10, 2007
  7. Stephen

    Problem creating wrapper

    Stephen, Jan 7, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    192
    Stephen
    Jan 7, 2005
  8. Ferenc Engard

    tkdialog wrapper problem

    Ferenc Engard, Nov 13, 2003, in forum: Ruby
    Replies:
    3
    Views:
    139
    Hidetoshi NAGAI
    Nov 17, 2003
Loading...