Class CompressionFilter
- java.lang.Object
-
- org.kohsuke.stapler.compression.CompressionFilter
-
- All Implemented Interfaces:
javax.servlet.Filter
public class CompressionFilter extends Object implements javax.servlet.Filter
Pimps upHttpServletResponse
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, sinceHttpServletResponse
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 fromServletResponse.getOutputStream()
(and it would also have to process the exception thrown from the app.) This filter does exactly that.- Author:
- Kohsuke Kawaguchi
-
-
Field Summary
Fields Modifier and Type Field Description static boolean
DISABLED
Disables the effect ofsetUncaughtExceptionHandler(javax.servlet.ServletContext, org.kohsuke.stapler.compression.UncaughtExceptionHandler)
, letting all errors be rethrown.
-
Constructor Summary
Constructors Constructor Description CompressionFilter()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static boolean
activate(javax.servlet.ServletRequest req)
Is this request already wrapped intoCompressionFilter
, activate that, so thatServletResponse.getOutputStream()
will return a stream that automatically handles compression.void
destroy()
void
doFilter(javax.servlet.ServletRequest _req, javax.servlet.ServletResponse _rsp, javax.servlet.FilterChain filterChain)
static UncaughtExceptionHandler
getUncaughtExceptionHandler(javax.servlet.ServletContext context)
static boolean
has(javax.servlet.ServletRequest req)
Is this request already wrapped intoCompressionFilter
?void
init(javax.servlet.FilterConfig filterConfig)
static void
setUncaughtExceptionHandler(javax.servlet.ServletContext context, UncaughtExceptionHandler handler)
-
-
-
Field Detail
-
DISABLED
public static boolean DISABLED
Disables the effect ofsetUncaughtExceptionHandler(javax.servlet.ServletContext, org.kohsuke.stapler.compression.UncaughtExceptionHandler)
, letting all errors be rethrown. Despite its name, this flag does not disableCompressionFilter
itself! Rather useDefaultScriptInvoker.COMPRESS_BY_DEFAULT
.
-
-
Method Detail
-
init
public void init(javax.servlet.FilterConfig filterConfig) throws javax.servlet.ServletException
- Specified by:
init
in interfacejavax.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 interfacejavax.servlet.Filter
- Throws:
IOException
javax.servlet.ServletException
-
destroy
public void destroy()
- Specified by:
destroy
in interfacejavax.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 intoCompressionFilter
?
-
activate
public static boolean activate(javax.servlet.ServletRequest req) throws IOException
Is this request already wrapped intoCompressionFilter
, activate that, so thatServletResponse.getOutputStream()
will return a stream that automatically handles compression.- Throws:
IOException
-
-