private void doGzipResponse ( final HttpServletRequest req , final HttpServletResponse response , final FilterChain chain ) throws IOException , ServletException { LOG . debug ( "Applying gzip on resource: " + req . getRequestURI ( ) ) ; response . setHeader ( HttpHeader . CONTENT_ENCODING . toString ( ) , "gzip" ) ; final ByteArrayOutputStream baos = new ByteArrayOutputStream ( ) ; final CountingOutputStream countingStream = new CountingOutputStream ( new GZIPOutputStream ( new BufferedOutputStream ( baos ) ) ) ; // final GZIPOutputStream gzout = new GZIPOutputStream(new BufferedOutputStream(baos)); // Perform gzip operation in-memory before sending response final HttpServletResponseWrapper wrappedResponse = new RedirectedStreamServletResponseWrapper ( countingStream , response ) ; chain . doFilter ( req , wrappedResponse ) ; // close underlying stream countingStream . close ( ) ; response . setContentLength ( countingStream . getCount ( ) ) ; // avoid NO CONTENT error thrown by jetty when gzipping empty response if ( countingStream . getCount ( ) > 0 ) { IOUtils . write ( baos . toByteArray ( ) , response . getOutputStream ( ) ) ; } }
Performs actual gzip of the filtered content .