Class GzipHandler

All Implemented Interfaces:
Handler, Handler.Container, GzipFactory, Handler.Singleton, Request.Handler, Container, Destroyable, Dumpable, Dumpable.DumpableContainer, LifeCycle, Invocable

public class GzipHandler extends Handler.Wrapper implements GzipFactory
  • Field Details

  • Constructor Details

    • GzipHandler

      public GzipHandler()
      Instantiates a new GzipHandler.
    • GzipHandler

      public GzipHandler(Handler handler)
      Instantiates a new GzipHandler.
      Parameters:
      handler - the handler to wrap
  • Method Details

    • doStart

      protected void doStart() throws Exception
      Description copied from class: ContainerLifeCycle
      Starts the managed lifecycle beans in the order they were added.
      Overrides:
      doStart in class Handler.Abstract
      Throws:
      AbstractLifeCycle.StopException - If thrown, the lifecycle will immediately be stopped.
      Exception - If there was a problem starting. Will cause a transition to FAILED state
    • doStop

      protected void doStop() throws Exception
      Description copied from class: ContainerLifeCycle
      Stops the managed lifecycle beans in the reverse order they were added.
      Overrides:
      doStop in class Handler.Abstract
      Throws:
      Exception - If there was a problem stopping. Will cause a transition to FAILED state
    • getVary

      public HttpField getVary()
      Returns:
      The VARY field to use.
    • setVary

      public void setVary(HttpField vary)
      Parameters:
      vary - The VARY field to use. It if is not an instance of PreEncodedHttpField, then it will be converted to one.
    • addExcludedMethods

      public void addExcludedMethods(String... methods)
      Add excluded to the HTTP methods filtering.
      Parameters:
      methods - The methods to exclude in compression
      See Also:
    • addExcludedMimeTypes

      public void addExcludedMimeTypes(String... types)
      Adds excluded MIME types for response filtering.

      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.

      Parameters:
      types - The mime types to exclude (without charset or other parameters).
      See Also:
    • addExcludedPaths

      public void addExcludedPaths(String... pathspecs)
      Adds excluded Path Specs for request filtering.

      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.

      • If the spec starts with '^' the spec is assumed to be a regex based path spec and will match with normal Java regex rules.
      • If the spec starts with '/' then spec is assumed to be a Servlet url-pattern rules path spec for either an exact match or prefix based match.
      • If the spec starts with '*.' then spec is assumed to be a Servlet url-pattern rules path spec for a suffix based match.
      • All other syntaxes are unsupported

      Note: inclusion takes precedence over exclude.

      Parameters:
      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.
      For backward compatibility the pathspecs may be comma separated strings, but this will not be supported in future versions.
      See Also:
    • addExcludedInflationPaths

      public void addExcludedInflationPaths(String... pathspecs)
      Adds excluded Path Specs for request filtering on request inflation.

      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.

      • If the spec starts with '^' the spec is assumed to be a regex based path spec and will match with normal Java regex rules.
      • If the spec starts with '/' then spec is assumed to be a Servlet url-pattern rules path spec for either an exact match or prefix based match.
      • If the spec starts with '*.' then spec is assumed to be a Servlet url-pattern rules path spec for a suffix based match.
      • All other syntaxes are unsupported

      Note: inclusion takes precedence over exclude.

      Parameters:
      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.
      For backward compatibility the pathspecs may be comma separated strings, but this will not be supported in future versions.
      See Also:
    • addIncludedMethods

      public void addIncludedMethods(String... methods)
      Adds included HTTP Methods (eg: POST, PATCH, DELETE) for filtering.
      Parameters:
      methods - The HTTP methods to include in compression.
      See Also:
    • isSyncFlush

      public boolean isSyncFlush()
      Is the Deflater running Deflater.SYNC_FLUSH or not.
      Returns:
      True if Deflater.SYNC_FLUSH is used, else Deflater.NO_FLUSH
      See Also:
    • setSyncFlush

      public void setSyncFlush(boolean syncFlush)
      Set the 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.
      Parameters:
      syncFlush - True if Deflater.SYNC_FLUSH is used, else Deflater.NO_FLUSH
      See Also:
    • addIncludedMimeTypes

      public void addIncludedMimeTypes(String... types)
      Add included MIME types for response filtering
      Parameters:
      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.
      See Also:
    • addIncludedPaths

      public void addIncludedPaths(String... pathspecs)
      Add included Path Specs for filtering.

      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.

      • If the spec starts with '^' the spec is assumed to be a regex based path spec and will match with normal Java regex rules.
      • If the spec starts with '/' then spec is assumed to be a Servlet url-pattern rules path spec for either an exact match or prefix based match.
      • If the spec starts with '*.' then spec is assumed to be a Servlet url-pattern rules path spec for a suffix based match.
      • All other syntaxes are unsupported

      Note: inclusion takes precedence over exclusion.

      Parameters:
      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 absolute
    • addIncludedInflationPaths

      public void addIncludedInflationPaths(String... pathspecs)
      Add included Path Specs for filtering on request inflation.

      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.

      • If the spec starts with '^' the spec is assumed to be a regex based path spec and will match with normal Java regex rules.
      • If the spec starts with '/' then spec is assumed to be a Servlet url-pattern rules path spec for either an exact match or prefix based match.
      • If the spec starts with '*.' then spec is assumed to be a Servlet url-pattern rules path spec for a suffix based match.
      • All other syntaxes are unsupported

      Note: inclusion takes precedence over exclusion.

      Parameters:
      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 absolute
    • getDeflaterEntry

      public CompressionPool<Deflater>.Entry getDeflaterEntry(Request request, long contentLength)
      Specified by:
      getDeflaterEntry in interface GzipFactory
    • getExcludedMethods

      public String[] getExcludedMethods()
      Get the current filter list of excluded HTTP methods
      Returns:
      the filter list of excluded HTTP methods
      See Also:
    • getExcludedMimeTypes

      public String[] getExcludedMimeTypes()
      Get the current filter list of excluded MIME types
      Returns:
      the filter list of excluded MIME types
      See Also:
    • getExcludedPaths

      public String[] getExcludedPaths()
      Get the current filter list of excluded Path Specs
      Returns:
      the filter list of excluded Path Specs
      See Also:
    • getExcludedInflationPaths

      public String[] getExcludedInflationPaths()
      Get the current filter list of excluded Path Specs for request inflation.
      Returns:
      the filter list of excluded Path Specs
      See Also:
    • getIncludedMethods

      public String[] getIncludedMethods()
      Get the current filter list of included HTTP Methods
      Returns:
      the filter list of included HTTP methods
      See Also:
    • getIncludedMimeTypes

      public String[] getIncludedMimeTypes()
      Get the current filter list of included MIME types
      Returns:
      the filter list of included MIME types
      See Also:
    • getIncludedPaths

      public String[] getIncludedPaths()
      Get the current filter list of included Path Specs
      Returns:
      the filter list of included Path Specs
      See Also:
    • getIncludedInflationPaths

      public String[] getIncludedInflationPaths()
      Get the current filter list of included Path Specs for request inflation.
      Returns:
      the filter list of included Path Specs
      See Also:
    • getMinGzipSize

      public int getMinGzipSize()
      Get the minimum size, in bytes, that a response Content-Length must be before compression will trigger.
      Returns:
      minimum response size (in bytes) that triggers compression
      See Also:
    • getInflateBufferSize

      public int getInflateBufferSize()
      Get the size (in bytes) of the Inflater buffer used to inflate compressed requests.
      Returns:
      size in bytes of the buffer, or 0 for no inflation.
    • setInflateBufferSize

      public void setInflateBufferSize(int size)
      Set the size (in bytes) of the Inflater buffer used to inflate comrpessed requests.
      Parameters:
      size - size in bytes of the buffer, or 0 for no inflation.
    • handle

      public boolean handle(Request request, Response response, Callback callback) throws Exception
      Description copied from interface: Request.Handler

      Invoked to handle the passed HTTP request and response.

      The request is accepted by returning true, then handling must be concluded by completing the passed callback. The handling may be asynchronous, i.e. this method may return true and complete the given callback later, possibly from a different thread. If this method returns false, then the callback must not be invoked and any mutation on the response reversed.

      Exceptions thrown by this method may be subsequently handled by an error Request.Handler, if present, otherwise a default HTTP 500 error is generated and the callback completed while writing the error response.

      The simplest implementation is:

       public boolean handle(Request request, Response response, Callback callback)
       {
           callback.succeeded();
           return true;
       }
       

      A HelloWorld implementation is:

       public boolean handle(Request request, Response response, Callback callback)
       {
           response.write(true, ByteBuffer.wrap("Hello World\n".getBytes(StandardCharsets.UTF_8)), callback);
           return true;
       }
       
      Specified by:
      handle in interface Request.Handler
      Overrides:
      handle in class Handler.Wrapper
      Parameters:
      request - the HTTP request to handle
      response - the HTTP response to handle
      callback - the callback to complete when the handling is complete
      Returns:
      True if and only if the request will be handled, a response generated and the callback eventually called. This may occur within the scope of the call to this method, or asynchronously some time later. If false is returned, then this method must not generate a response, nor complete the callback.
      Throws:
      Exception - if there is a failure during the handling. Catchers cannot assume that the callback will be called and thus should attempt to complete the request as if a false had been returned.
      See Also:
    • isMimeTypeDeflatable

      protected boolean isMimeTypeDeflatable(MimeTypes mimeTypes, String requestURI)
    • isMimeTypeDeflatable

      public boolean isMimeTypeDeflatable(String mimetype)
      Test if the provided MIME type is allowed based on the MIME type filters.
      Specified by:
      isMimeTypeDeflatable in interface GzipFactory
      Parameters:
      mimetype - the MIME type to test
      Returns:
      true if allowed, false otherwise
    • isPathDeflatable

      protected boolean isPathDeflatable(String requestURI)
      Test if the provided Request URI is allowed based on the Path Specs filters.
      Parameters:
      requestURI - the request uri
      Returns:
      whether compressing is allowed for the given the path
    • isPathInflatable

      protected boolean isPathInflatable(String pathInContext)
      Test if the provided Request URI is allowed to be inflated based on the Path Specs filters.
      Parameters:
      pathInContext - the request path in context
      Returns:
      whether decompressing is allowed for the given the path.
    • setExcludedMethods

      public void setExcludedMethods(String... methods)
      Set the excluded filter list of HTTP methods (replacing any previously set)
      Parameters:
      methods - the HTTP methods to exclude
      See Also:
    • setExcludedMimeTypes

      public void setExcludedMimeTypes(String... types)
      Set the excluded filter list of MIME types (replacing any previously set)
      Parameters:
      types - The mime types to exclude (without charset or other parameters)
      See Also:
    • setExcludedMimeTypesList

      public void setExcludedMimeTypesList(String csvTypes)
      Set the excluded filter list of MIME types (replacing any previously set)
      Parameters:
      csvTypes - The list of mime types to exclude (without charset or other parameters), CSV format
      See Also:
    • setExcludedPaths

      public void setExcludedPaths(String... pathspecs)
      Set the excluded filter list of Path specs (replacing any previously set)
      Parameters:
      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.
      See Also:
    • setExcludedInflatePaths

      public void setExcludedInflatePaths(String... pathspecs)
      Set the excluded filter list of Path specs (replacing any previously set)
      Parameters:
      pathspecs - Path specs (as per servlet spec) to exclude from inflation. If a ServletContext is available, the paths are relative to the context path, otherwise they are absolute.
      See Also:
    • setIncludedMethods

      public void setIncludedMethods(String... methods)
      Set the included filter list of HTTP methods (replacing any previously set)
      Parameters:
      methods - The methods to include in compression
      See Also:
    • setIncludedMimeTypes

      public void setIncludedMimeTypes(String... types)
      Set the included filter list of MIME types (replacing any previously set)
      Parameters:
      types - The mime types to include (without charset or other parameters)
      See Also:
    • setIncludedMimeTypesList

      public void setIncludedMimeTypesList(String csvTypes)
      Set the included filter list of MIME types (replacing any previously set)
      Parameters:
      csvTypes - The list of mime types to include (without charset or other parameters), CSV format
      See Also:
    • setIncludedPaths

      public void setIncludedPaths(String... pathspecs)
      Set the included filter list of Path specs (replacing any previously set)
      Parameters:
      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 absolute
      See Also:
    • setIncludedInflatePaths

      public void setIncludedInflatePaths(String... pathspecs)
      Set the included filter list of Path specs (replacing any previously set)
      Parameters:
      pathspecs - Path specs (as per servlet spec) to include for inflation. If a ServletContext is available, the paths are relative to the context path, otherwise they are absolute
      See Also:
    • setMinGzipSize

      public void setMinGzipSize(int minGzipSize)
      Set the minimum response size to trigger dynamic compression.

      Sizes below BREAK_EVEN_GZIP_SIZE will result a compressed response that is larger than the original data.

      Parameters:
      minGzipSize - minimum response size in bytes (not allowed to be lower then BREAK_EVEN_GZIP_SIZE)
    • setIncludedMethodList

      public void setIncludedMethodList(String csvMethods)
      Set the included filter list of HTTP Methods (replacing any previously set)
      Parameters:
      csvMethods - the list of methods, CSV format
      See Also:
    • getIncludedMethodList

      public String getIncludedMethodList()
      Get the included filter list of HTTP methods in CSV format
      Returns:
      the included filter list of HTTP methods in CSV format
      See Also:
    • setExcludedMethodList

      public void setExcludedMethodList(String csvMethods)
      Set the excluded filter list of HTTP Methods (replacing any previously set)
      Parameters:
      csvMethods - the list of methods, CSV format
      See Also:
    • getExcludedMethodList

      public String getExcludedMethodList()
      Get the excluded filter list of HTTP methods in CSV format
      Returns:
      the excluded filter list of HTTP methods in CSV format
      See Also:
    • getDeflaterPool

      public DeflaterPool getDeflaterPool()
      Get the DeflaterPool being used. The default value of this is null before starting, but after starting if it is null it will be set to the default DeflaterPool which is stored as a bean on the server.
      Returns:
      the DeflaterPool being used.
    • getInflaterPool

      public InflaterPool getInflaterPool()
      Get the InflaterPool being used. The default value of this is null before starting, but after starting if it is null it will be set to the default InflaterPool which is stored as a bean on the server.
      Returns:
      the DeflaterPool being used.
    • setDeflaterPool

      public void setDeflaterPool(DeflaterPool deflaterPool)
      Set the DeflaterPool to be used. This should be called before starting. If this value is null when starting the default pool will be used from the server.
      Parameters:
      deflaterPool - the DeflaterPool to use.
    • setInflaterPool

      public void setInflaterPool(InflaterPool inflaterPool)
      Set the InflaterPool to be used. This should be called before starting. If this value is null when starting the default pool will be used from the server.
      Parameters:
      inflaterPool - the InflaterPool to use.
    • toString

      public String toString()
      Overrides:
      toString in class AbstractLifeCycle