0

I've been trying to figure this out for more than a day now. Looked all over the internet and used multiple references as examples.

I use a custom HttpServletResponseWrapper, and I override the getOutputStream() method. I'm essentially following this example: How to read and copy the HTTP servlet response output stream content for logging

Which is very similar, to other examples I used. But the result is the same. I'm successfully logging the response, but my JSP page that I'm redirected to is showing up empty.

Perhaps I'm not understanding some of the fundamentals? Any help whatsoever will be appreciated!

Thank you!

Below is the code I'm using from the example. The HTTPServeletResponseCopier adn ServletOutputStreamCopier can be found here: https://github.com/ukwa/interject/tree/master/interject-servlet-filter/src/main/java/uk/bl/wa/interject/filter

  @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
    if (response.getCharacterEncoding() == null) {
        response.setCharacterEncoding("UTF-8");
    }

    HttpServletResponseCopier responseCopier = new HttpServletResponseCopier((HttpServletResponse) response);

    try 
    {

        chain.doFilter(request, responseCopier);
        responseCopier.flushBuffer();

    } 
    catch (Exception e)
    {
        System.out.println("EXCEPTION ECNOUTERED - " + e.getMessage());
        e.printStackTrace();
    }
    finally 
    {
      byte[] copy = responseCopier.getCopy();
     System.out.println("Logging.....");
        System.out.println(new String(copy, response.getCharacterEncoding())); // Do your logging job here. This is just a basic example.
        System.out.println("Logging.....");

    }
}

Edit: I think I know what I'm doing wrong, but I don't know how to fix it.

Apparently, when dealing with a RESPONSE fIlter, you need to handle the response AFTER the filter-chain. Which is why my response is empty. What I tried doing was

"String responseString = new String(copy);
            response.getWriter().write(responseString);"

But this seems like a quick and easy work around that's not ACTUALLY solving the problem. Any thoughts?

Community
  • 1
  • 1
PoRco1x
  • 53
  • 3
  • Without showing your code, we cannot guess how you *exactly* wrote your filter – Serge Ballesta Aug 13 '15 at 15:38
  • @SergeBallesta Thanks for the response! I'm currently trying to get the code in the example above to work. The githum to the source code is here: https://github.com/ukwa/interject/tree/master/interject-servlet-filter/src/main/java/uk/bl/wa/interject/filter I figured I would try and make a working example run, but I didn't manage that either. – PoRco1x Aug 13 '15 at 15:47
  • Sorry about the constant edits, still getting used to "enter" being "Send" :P – PoRco1x Aug 13 '15 at 15:54
  • I would be more confident in BalusC code which looks simpler even it is a bit old. Did you try it directly ? – Serge Ballesta Aug 13 '15 at 17:52
  • Yes, they're actually both is code. The github version just contains some updated and additional code. I've tried both. – PoRco1x Aug 13 '15 at 20:08
  • I think I know what I'm doing wrong, but I don't know how to fix it. Apparently, when dealing with a RESPONSE fIlter, you need to handle the response AFTER the filter-chain. Which is why my response is empty. What I tried doing was "String responseString = new String(copy); response.getWriter().write(responseString);" But this seems like a quick and easy work around that's not ACTUALLY solving the problem. Any thoughts? – PoRco1x Aug 13 '15 at 20:08

1 Answers1

0

In ServletOutputStreamCopier you must write to original OutputStream and its copy at the same time. Take a look at the code of ServletOutputStreamCopier in the answer again: How to read and copy the HTTP servlet response output stream content for logging . The whole point of ServletOutputStreamCopier (from answer) is to write to original output stream (to the client) and at the same time write data to second stream which in turn you can use for your logic.

UPDATE: And yes you can just write after you are done with response body as in your last snippet. I think it's is ok also (just synchronous).