Handler
, GzipFactory
, HandlerContainer
, org.eclipse.jetty.util.component.Destroyable
, org.eclipse.jetty.util.component.LifeCycle
public class GzipHandler extends HandlerWrapper implements GzipFactory
The GzipHandler can be applied to the entire server (a gzip.mod
is included in
the jetty-home
) or it may be applied to individual contexts.
Both Request uncompress and Response compress are gated by a configurable
DispatcherType
check on the GzipHandler.
(This is similar in behavior to a Filter
configuration
you would find in a Servlet Descriptor file (WEB-INF/web.xml
)
(Default: DispatcherType.REQUEST
).
Requests with a Content-Encoding
header with the value gzip
will
be uncompressed by a GzipHttpInputInterceptor
for any API that uses
ServletRequest.getInputStream()
or ServletRequest.getReader()
.
Response compression has a number of checks before GzipHandler will perform compression.
Accept-Encoding
header that specifies
gzip
value?
HttpServletRequest.getMethod()
allowed by the configured HTTP Method Filter.
GET
)
Content-Length
header present, and does its
value meet the minimum gzip size requirements?
DEFAULT_MIN_GZIP_SIZE
)
Accept
header present and does it contain the
required gzip
value?
When you encounter a configurable filter in the GzipHandler (method, paths, user-agent, mime-types, etc) that has both Included and Excluded values, note that the Included values always win over the Excluded values.
Important note about Default Values: It is important to note that the GzipHandler will automatically configure itself from the MimeType present on the Server, System, and Contexts and the ultimate set of default values for the various filters (paths, methods, mime-types, etc) can be influenced by the available mime types to work with.
ETag (or Entity Tag) information: any Request headers for If-None-Match
or
If-Match
will be evaluated by the GzipHandler to determine if it was involved
in compression of the response earlier. This is usually present as a --gzip
suffix
on the ETag that the Client User-Agent is tracking and handed to the Jetty server.
The special --gzip
suffix on the ETag is how GzipHandler knows that the content
passed through itself, and this suffix will be stripped from the Request header values
before the request is sent onwards to the specific webapp / servlet endpoint for
handling.
If a ETag is present in the Response headers, and GzipHandler is compressing the
contents, it will add the --gzip
suffix before the Response headers are committed
and sent to the User Agent.
This implementation relies on an Jetty internal HttpOutput.Interceptor
mechanism to allow for effective and efficient compression of the response on all Output API usages:
ServletOutputStream
- Obtained from ServletResponse.getOutputStream()
using the traditional Blocking I/O techniques
WriteListener
- Provided to
ServletOutputStream.setWriteListener(javax.servlet.WriteListener)
using the new (since Servlet 3.1) Async I/O techniques
PrintWriter
- Obtained from ServletResponse.getWriter()
using Blocking I/O techniques
Historically the compression of responses were accomplished via
Servlet Filters (eg: GzipFilter
) and usage of HttpServletResponseWrapper
.
Since the introduction of Async I/O in Servlet 3.1, this older form of Gzip support
in web applications has been problematic and bug ridden.
AbstractHandler.ErrorDispatchHandler
org.eclipse.jetty.util.component.AbstractLifeCycle.AbstractLifeCycleListener
Modifier and Type | Field | Description |
---|---|---|
static int |
DEFAULT_MIN_GZIP_SIZE |
|
static java.lang.String |
DEFLATE |
|
static java.lang.String |
GZIP |
FAILED, RUNNING, STARTED, STARTING, STOPPED, STOPPING
_handler
Constructor | Description |
---|---|
GzipHandler() |
Instantiates a new GzipHandler.
|
Modifier and Type | Method | Description |
---|---|---|
void |
addExcludedAgentPatterns(java.lang.String... patterns) |
Add excluded to the User-Agent filtering.
|
void |
addExcludedMethods(java.lang.String... methods) |
Add excluded to the HTTP methods filtering.
|
void |
addExcludedMimeTypes(java.lang.String... types) |
Adds excluded MIME types for response filtering.
|
void |
addExcludedPaths(java.lang.String... pathspecs) |
Adds excluded Path Specs for request filtering.
|
void |
addIncludedAgentPatterns(java.lang.String... patterns) |
Adds included User-Agents for filtering.
|
void |
addIncludedMethods(java.lang.String... methods) |
Adds included HTTP Methods (eg: POST, PATCH, DELETE) for filtering.
|
void |
addIncludedMimeTypes(java.lang.String... types) |
Add included MIME types for response filtering
|
void |
addIncludedPaths(java.lang.String... pathspecs) |
Add included Path Specs for filtering.
|
protected void |
doStart() |
|
boolean |
getCheckGzExists() |
Deprecated.
feature will be removed in Jetty 10.x, with no replacement.
|
int |
getCompressionLevel() |
|
java.util.zip.Deflater |
getDeflater(Request request,
long content_length) |
|
java.util.EnumSet<DispatcherType> |
getDispatcherTypes() |
Get the Set of
DispatcherType that this Filter will operate on. |
java.lang.String[] |
getExcludedAgentPatterns() |
Get the current filter list of excluded User-Agent patterns
|
java.lang.String |
getExcludedMethodList() |
Get the excluded filter list of HTTP methods in CSV format
|
java.lang.String[] |
getExcludedMethods() |
Get the current filter list of excluded HTTP methods
|
java.lang.String[] |
getExcludedMimeTypes() |
Get the current filter list of excluded MIME types
|
java.lang.String[] |
getExcludedPaths() |
Get the current filter list of excluded Path Specs
|
java.lang.String[] |
getIncludedAgentPatterns() |
Get the current filter list of included User-Agent patterns
|
java.lang.String |
getIncludedMethodList() |
Get the included filter list of HTTP methods in CSV format
|
java.lang.String[] |
getIncludedMethods() |
Get the current filter list of included HTTP Methods
|
java.lang.String[] |
getIncludedMimeTypes() |
Get the current filter list of included MIME types
|
java.lang.String[] |
getIncludedPaths() |
Get the current filter list of included Path Specs
|
int |
getInflateBufferSize() |
Get the size (in bytes) of the
Inflater buffer used to inflate
compressed requests. |
java.lang.String[] |
getMethods() |
Deprecated.
use
getIncludedMethods() instead. (Will be removed in Jetty 10) |
int |
getMinGzipSize() |
Get the minimum size, in bytes, that a response
Content-Length must be
before compression will trigger. |
protected org.eclipse.jetty.http.HttpField |
getVaryField() |
|
void |
handle(java.lang.String target,
Request baseRequest,
HttpServletRequest request,
HttpServletResponse response) |
Handle a request.
|
protected boolean |
isAgentGzipable(java.lang.String ua) |
Test if the provided User-Agent is allowed based on the User-Agent filters.
|
boolean |
isMimeTypeGzipable(java.lang.String mimetype) |
Test if the provided MIME type is allowed based on the MIME type filters.
|
protected boolean |
isPathGzipable(java.lang.String requestURI) |
Test if the provided Request URI is allowed based on the Path Specs filters.
|
boolean |
isSyncFlush() |
Is the
Deflater running Deflater.SYNC_FLUSH or not. |
void |
recycle(java.util.zip.Deflater deflater) |
|
void |
setCheckGzExists(boolean checkGzExists) |
Deprecated.
feature will be removed in Jetty 10.x, with no replacement.
|
void |
setCompressionLevel(int compressionLevel) |
Set the Compression level that
Deflater uses. |
void |
setDispatcherTypes(java.util.EnumSet<DispatcherType> dispatchers) |
Set of supported
DispatcherType that this filter will operate on. |
void |
setDispatcherTypes(DispatcherType... dispatchers) |
Set the list of supported
DispatcherType that this filter will operate on. |
void |
setExcludedAgentPatterns(java.lang.String... patterns) |
Set the excluded filter list of User-Agent patterns (replacing any previously set)
|
void |
setExcludedMethodList(java.lang.String csvMethods) |
Set the excluded filter list of HTTP Methods (replacing any previously set)
|
void |
setExcludedMethods(java.lang.String... methods) |
Set the excluded filter list of HTTP methods (replacing any previously set)
|
void |
setExcludedMimeTypes(java.lang.String... types) |
Set the excluded filter list of MIME types (replacing any previously set)
|
void |
setExcludedPaths(java.lang.String... pathspecs) |
Set the excluded filter list of Path specs (replacing any previously set)
|
void |
setIncludedAgentPatterns(java.lang.String... patterns) |
Set the included filter list of User-Agent patterns (replacing any previously set)
|
void |
setIncludedMethodList(java.lang.String csvMethods) |
Set the included filter list of HTTP Methods (replacing any previously set)
|
void |
setIncludedMethods(java.lang.String... methods) |
Set the included filter list of HTTP methods (replacing any previously set)
|
void |
setIncludedMimeTypes(java.lang.String... types) |
Set the included filter list of MIME types (replacing any previously set)
|
void |
setIncludedPaths(java.lang.String... pathspecs) |
Set the included filter list of Path specs (replacing any previously set)
|
void |
setInflateBufferSize(int size) |
Set the size (in bytes) of the
Inflater buffer used to inflate comrpessed requests. |
void |
setMinGzipSize(int minGzipSize) |
Set the minimum response size to trigger dynamic compression
|
void |
setSyncFlush(boolean syncFlush) |
Set the
Deflater flush mode to use. |
doError, doStop, dumpThis, getServer
expandHandler, findContainerOf, getChildHandlerByClass, getChildHandlers, getChildHandlersByClass, setServer
addLifeCycleListener, getState, getState, getStopTimeout, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, start, stop
addBean, addBean, addBean, addEventListener, addManaged, contains, dump, dump, dump, dump, dump, dumpBeans, dumpObject, dumpStdErr, getBean, getBeans, getBeans, getContainedBeans, getContainedBeans, isManaged, manage, removeBean, removeBeans, removeEventListener, setBeans, setStopTimeout, start, stop, unmanage, updateBean, updateBean, updateBeans
destroy, expandChildren, getHandler, getHandlers, insertHandler, setHandler
public static final java.lang.String GZIP
public static final java.lang.String DEFLATE
public static final int DEFAULT_MIN_GZIP_SIZE
public void addExcludedAgentPatterns(java.lang.String... patterns)
patterns
- Regular expressions matching user agents to excludeaddIncludedAgentPatterns(String...)
public void addExcludedMethods(java.lang.String... methods)
methods
- The methods to exclude in compressionaddIncludedMethods(String...)
public java.util.EnumSet<DispatcherType> getDispatcherTypes()
DispatcherType
that this Filter will operate on.DispatcherType
this filter will operate onpublic void setDispatcherTypes(java.util.EnumSet<DispatcherType> dispatchers)
DispatcherType
that this filter will operate on.dispatchers
- the set of DispatcherType
that this filter will operate onpublic void setDispatcherTypes(DispatcherType... dispatchers)
DispatcherType
that this filter will operate on.dispatchers
- the list of DispatcherType
that this filter will operate onpublic void addExcludedMimeTypes(java.lang.String... types)
Deprecation Warning: For backward compatibility the MIME types parameters may be comma separated strings, but this will not be supported in future versions of Jetty.
types
- The mime types to exclude (without charset or other parameters).addIncludedMimeTypes(String...)
public void addExcludedPaths(java.lang.String... pathspecs)
There are 2 syntaxes supported, Servlet url-pattern
based, and
Regex based. This means that the initial characters on the path spec
line are very strict, and determine the behavior of the path matching.
'^'
the spec is assumed to be
a regex based path spec and will match with normal Java regex rules.'/'
then spec is assumed to be
a Servlet url-pattern rules path spec for either an exact match
or prefix based match.'*.'
then spec is assumed to be
a Servlet url-pattern rules path spec for a suffix based match.Note: inclusion takes precedence over exclude.
pathspecs
- Path specs (as per servlet spec) to exclude. If a
ServletContext is available, the paths are relative to the context path,
otherwise they are absolute.addIncludedPaths(String...)
public void addIncludedAgentPatterns(java.lang.String... patterns)
patterns
- Regular expressions matching user agents to includeaddExcludedAgentPatterns(String...)
public void addIncludedMethods(java.lang.String... methods)
methods
- The HTTP methods to include in compression.addExcludedMethods(String...)
public boolean isSyncFlush()
Deflater
running Deflater.SYNC_FLUSH
or not.Deflater.SYNC_FLUSH
is used, else Deflater.NO_FLUSH
setSyncFlush(boolean)
public void setSyncFlush(boolean syncFlush)
Deflater
flush mode to use. Deflater.SYNC_FLUSH
should be used if the application wishes to stream the data, but this may
hurt compression performance.syncFlush
- True if Deflater.SYNC_FLUSH
is used, else Deflater.NO_FLUSH
isSyncFlush()
public void addIncludedMimeTypes(java.lang.String... types)
types
- The mime types to include (without charset or other parameters)
For backward compatibility the mimetypes may be comma separated strings, but this
will not be supported in future versions.addExcludedMimeTypes(String...)
public void addIncludedPaths(java.lang.String... pathspecs)
There are 2 syntaxes supported, Servlet url-pattern
based, and
Regex based. This means that the initial characters on the path spec
line are very strict, and determine the behavior of the path matching.
'^'
the spec is assumed to be
a regex based path spec and will match with normal Java regex rules.'/'
then spec is assumed to be
a Servlet url-pattern rules path spec for either an exact match
or prefix based match.'*.'
then spec is assumed to be
a Servlet url-pattern rules path spec for a suffix based match.Note: inclusion takes precedence over exclusion.
pathspecs
- Path specs (as per servlet spec) to include. If a
ServletContext is available, the paths are relative to the context path,
otherwise they are absoluteprotected void doStart() throws java.lang.Exception
doStart
in class AbstractHandler
java.lang.Exception
@Deprecated public boolean getCheckGzExists()
public int getCompressionLevel()
public java.util.zip.Deflater getDeflater(Request request, long content_length)
getDeflater
in interface GzipFactory
public java.lang.String[] getExcludedAgentPatterns()
getIncludedAgentPatterns()
public java.lang.String[] getExcludedMethods()
getIncludedMethods()
public java.lang.String[] getExcludedMimeTypes()
getIncludedMimeTypes()
public java.lang.String[] getExcludedPaths()
getIncludedPaths()
public java.lang.String[] getIncludedAgentPatterns()
getExcludedAgentPatterns()
public java.lang.String[] getIncludedMethods()
getExcludedMethods()
public java.lang.String[] getIncludedMimeTypes()
getExcludedMimeTypes()
public java.lang.String[] getIncludedPaths()
getExcludedPaths()
@Deprecated public java.lang.String[] getMethods()
getIncludedMethods()
instead. (Will be removed in Jetty 10)public int getMinGzipSize()
Content-Length
must be
before compression will trigger.setMinGzipSize(int)
protected org.eclipse.jetty.http.HttpField getVaryField()
public int getInflateBufferSize()
Inflater
buffer used to inflate
compressed requests.public void setInflateBufferSize(int size)
Inflater
buffer used to inflate comrpessed requests.size
- size in bytes of the buffer, or 0 for no inflation.public void handle(java.lang.String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException
Handler
handle
in interface Handler
handle
in class HandlerWrapper
target
- The target of the request - either a URI or a name.baseRequest
- The original unwrapped request object.request
- The request either as the Request
object or a wrapper of that request. The
HttpConnection.getCurrentConnection()
.getHttpChannel()
.getRequest()
method can be used access the Request object if required.response
- The response as the Response
object or a wrapper of that request. The
HttpConnection.getCurrentConnection()
.getHttpChannel()
.getResponse()
method can be used access the Response object if required.java.io.IOException
- if unable to handle the request or response processingServletException
- if unable to handle the request or response due to underlying servlet issueprotected boolean isAgentGzipable(java.lang.String ua)
ua
- the user agentpublic boolean isMimeTypeGzipable(java.lang.String mimetype)
isMimeTypeGzipable
in interface GzipFactory
mimetype
- the MIME type to testprotected boolean isPathGzipable(java.lang.String requestURI)
requestURI
- the request uripublic void recycle(java.util.zip.Deflater deflater)
recycle
in interface GzipFactory
@Deprecated public void setCheckGzExists(boolean checkGzExists)
*.gz
file for the incoming file exists.checkGzExists
- whether to check if a static gz file exists for
the resource that the DefaultServlet may serve as precompressed.public void setCompressionLevel(int compressionLevel)
Deflater
uses.compressionLevel
- The compression level to use to initialize Deflater.setLevel(int)
Deflater.setLevel(int)
public void setExcludedAgentPatterns(java.lang.String... patterns)
patterns
- Regular expressions list matching user agents to excludesetIncludedAgentPatterns(String...)
public void setExcludedMethods(java.lang.String... methods)
methods
- the HTTP methods to excludesetIncludedMethods(String...)
public void setExcludedMimeTypes(java.lang.String... types)
types
- The mime types to exclude (without charset or other parameters)setIncludedMimeTypes(String...)
public void setExcludedPaths(java.lang.String... pathspecs)
pathspecs
- Path specs (as per servlet spec) to exclude. If a
ServletContext is available, the paths are relative to the context path,
otherwise they are absolute.setIncludedPaths(String...)
public void setIncludedAgentPatterns(java.lang.String... patterns)
patterns
- Regular expressions matching user agents to includesetExcludedAgentPatterns(String...)
public void setIncludedMethods(java.lang.String... methods)
methods
- The methods to include in compressionsetExcludedMethods(String...)
public void setIncludedMimeTypes(java.lang.String... types)
types
- The mime types to include (without charset or other parameters)setExcludedMimeTypes(String...)
public void setIncludedPaths(java.lang.String... pathspecs)
pathspecs
- Path specs (as per servlet spec) to include. If a
ServletContext is available, the paths are relative to the context path,
otherwise they are absolutesetExcludedPaths(String...)
public void setMinGzipSize(int minGzipSize)
minGzipSize
- minimum response size in bytespublic void setIncludedMethodList(java.lang.String csvMethods)
csvMethods
- the list of methods, CSV formatsetExcludedMethodList(String)
public java.lang.String getIncludedMethodList()
getExcludedMethodList()
public void setExcludedMethodList(java.lang.String csvMethods)
csvMethods
- the list of methods, CSV formatsetIncludedMethodList(String)
public java.lang.String getExcludedMethodList()
getIncludedMethodList()
Copyright © 1995–2018 Webtide. All rights reserved.