Question

When I use ContentCachingResponseWrapper, the response data is not written to the original output stream. Shouldn’t the data be written to the original output stream while being written to the cache?

private class ResponseServletOutputStream extends ServletOutputStream {
        private final ServletOutputStream os;
        public ResponseServletOutputStream(ServletOutputStream os) {
            this.os = os;
        }
        @Override
        public void write(int b) throws IOException {
                       // os.write(b);
            content.write(b);
        }
        @Override
        public void write(byte[] b, int off, int len) throws IOException {
                       // os.write(b, off, len);
            content.write(b, off, len);
        }
               // ...
    }

Comment From: bclozel

No, there is a "copyBodyToResponse" method designed for that. This wrapper class can be used to buffer the response body and calculate a response header accordingly. You can see that in action in the ShallowEtagHeaderFilter - if we were to write the body to the actual response directly, we wouldn't be able to add a response header as it would be committed by the Servlet container.

For further questions please use StackOverflow.

Comment From: honhimW

I am aware there is a method called ‘copyBodyToResponse()’, but ContentCachingRequestWrapper and ContentCachingResponseWrapper have similar names but work differently. This caused some confusion because the name only implies caching the content and does not indicate that it needs to be edited further before being rewritten.