public class GzipFilter extends UserAgentFilter
minGzipSize
initParameter or the minGzipSize is 0(default)mimeTypes
initParameter or
if no mimeTypes are defined the content-type is not "application/gzip"If both gzip and deflate are specified in the accept-encoding header, then gzip will be used.
Compressing the content can greatly improve the network bandwidth usage, but at a cost of memory and
CPU cycles. If this filter is mapped for static content, then use of efficient direct NIO may be
prevented, thus use of the gzip mechanism of the DefaultServlet
is
advised instead.
This filter extends UserAgentFilter
and if the the initParameter excludedAgents
is set to a comma separated list of user agents, then these agents will be excluded from gzip content.
Init Parameters:
bufferSize The output buffer size. Defaults to 8192. Be careful as values <= 0 will lead to anIllegalArgumentException
. See:GZIPOutputStream.GZIPOutputStream(java.io.OutputStream, int)
and:DeflaterOutputStream.DeflaterOutputStream(java.io.OutputStream, Deflater, int)
minGzipSize Content will only be compressed if content length is either unknown or greater thanminGzipSize
. deflateCompressionLevel The compression level used for deflate compression. (0-9). See:Deflater.Deflater(int, boolean)
deflateNoWrap The noWrap setting for deflate compression. Defaults to true. (true/false) See:Deflater.Deflater(int, boolean)
mimeTypes Comma separated list of mime types to compress. See description above. excludedAgents Comma separated list of user agents to exclude from compression. Does aString.contains(CharSequence)
to check if the excluded agent occurs in the user-agent header. If it does -> no compression excludeAgentPatterns Same as excludedAgents, but accepts regex patterns for more complex matching. excludePaths Comma separated list of paths to exclude from compression. Does aString.startsWith(String)
comparison to check if the path matches. If it does match -> no compression. To match subpaths useexcludePathPatterns
instead. excludePathPatterns Same as excludePath, but accepts regex patterns for more complex matching. vary Set to the value of the Vary header sent with responses that could be compressed. By default it is set to 'Vary: Accept-Encoding, User-Agent' since IE6 is excluded by default from the excludedAgents. If user-agents are not to be excluded, then this can be set to 'Vary: Accept-Encoding'. Note also that shared caches may cache copies of a resource that is varied by User-Agent - one per variation of the User-Agent, unless the cache does some normalization of the UA string.
Modifier and Type | Field and Description |
---|---|
protected int |
_bufferSize |
protected javax.servlet.ServletContext |
_context |
protected int |
_deflateCompressionLevel |
protected boolean |
_deflateNoWrap |
protected Set<Pattern> |
_excludedAgentPatterns |
protected Set<String> |
_excludedAgents |
protected Set<Pattern> |
_excludedPathPatterns |
protected Set<String> |
_excludedPaths |
protected Set<String> |
_mimeTypes |
protected int |
_minGzipSize |
protected String |
_vary |
static String |
DEFLATE |
static String |
ETAG |
static String |
ETAG_DEFLATE |
static String |
ETAG_GZIP |
static String |
GZIP |
Constructor and Description |
---|
GzipFilter() |
Modifier and Type | Method and Description |
---|---|
protected void |
configureWrappedResponse(CompressedResponseWrapper wrappedResponse) |
protected CompressedResponseWrapper |
createWrappedResponse(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
String compressionType) |
void |
destroy() |
void |
doFilter(javax.servlet.ServletRequest req,
javax.servlet.ServletResponse res,
javax.servlet.FilterChain chain) |
void |
init(javax.servlet.FilterConfig filterConfig) |
getUserAgent, getUserAgent
public static final String GZIP
public static final String ETAG_GZIP
public static final String DEFLATE
public static final String ETAG_DEFLATE
public static final String ETAG
protected javax.servlet.ServletContext _context
protected int _bufferSize
protected int _minGzipSize
protected int _deflateCompressionLevel
protected boolean _deflateNoWrap
protected String _vary
public void init(javax.servlet.FilterConfig filterConfig) throws javax.servlet.ServletException
init
in interface javax.servlet.Filter
init
in class UserAgentFilter
javax.servlet.ServletException
UserAgentFilter.init(javax.servlet.FilterConfig)
public void destroy()
destroy
in interface javax.servlet.Filter
destroy
in class UserAgentFilter
UserAgentFilter.destroy()
public void doFilter(javax.servlet.ServletRequest req, javax.servlet.ServletResponse res, javax.servlet.FilterChain chain) throws IOException, javax.servlet.ServletException
doFilter
in interface javax.servlet.Filter
doFilter
in class UserAgentFilter
IOException
javax.servlet.ServletException
UserAgentFilter.doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
protected CompressedResponseWrapper createWrappedResponse(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, String compressionType)
protected void configureWrappedResponse(CompressedResponseWrapper wrappedResponse)
Copyright © 1995-2013 Mort Bay Consulting. All Rights Reserved.