Class CompressionFilter

  • All Implemented Interfaces:
    javax.servlet.Filter

    public class CompressionFilter
    extends Object
    implements javax.servlet.Filter
    Pimps up HttpServletResponse so that it understands "Content-Encoding: gzip" and compress the response.

    When exceptions are processed within web applications, different unrelated parts of the webapp can end up calling ServletResponse.getOutputStream(). This fundamentally doesn't work with the notion that the application needs to process "Content-Encoding:gzip" on its own. Such app has to maintain a GZIP output stream on its own, since HttpServletResponse doesn't know that its output is written through a compressed stream.

    Another place this break-down can be seen is when a servlet throws an exception that the container handles. It tries to render an error page, but of course it wouldn't know that "Content-Encoding:gzip" is set, so it will fail to write in the correct format.

    The only way to properly fix this is to make HttpServletResponse smart enough that it returns the compression-transparent stream from ServletResponse.getOutputStream() (and it would also have to process the exception thrown from the app.) This filter does exactly that.

    Author:
    Kohsuke Kawaguchi
    • Constructor Detail

      • CompressionFilter

        public CompressionFilter()
    • Method Detail

      • init

        public void init​(javax.servlet.FilterConfig filterConfig)
                  throws javax.servlet.ServletException
        Specified by:
        init in interface javax.servlet.Filter
        Throws:
        javax.servlet.ServletException
      • doFilter

        public void doFilter​(javax.servlet.ServletRequest _req,
                             javax.servlet.ServletResponse _rsp,
                             javax.servlet.FilterChain filterChain)
                      throws IOException,
                             javax.servlet.ServletException
        Specified by:
        doFilter in interface javax.servlet.Filter
        Throws:
        IOException
        javax.servlet.ServletException
      • destroy

        public void destroy()
        Specified by:
        destroy in interface javax.servlet.Filter
      • setUncaughtExceptionHandler

        public static void setUncaughtExceptionHandler​(javax.servlet.ServletContext context,
                                                       UncaughtExceptionHandler handler)
      • getUncaughtExceptionHandler

        public static UncaughtExceptionHandler getUncaughtExceptionHandler​(javax.servlet.ServletContext context)
      • has

        public static boolean has​(javax.servlet.ServletRequest req)
        Is this request already wrapped into CompressionFilter?
      • activate

        public static boolean activate​(javax.servlet.ServletRequest req)
                                throws IOException
        Is this request already wrapped into CompressionFilter, activate that, so that ServletResponse.getOutputStream() will return a stream that automatically handles compression.
        Throws:
        IOException