Class CompressionHandler

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

public class CompressionHandler extends Handler.Wrapper

CompressionHandler to provide compression of response bodies and decompression of request bodies.

Supports any arbitrary Content-Encoding via Compression implementations such as gzip, zstd, and brotli, discovered via ServiceLoader.

Configuration is handled by associating a CompressionConfig against a PathSpec. By default, if no configuration is specified, then a default CompressionConfig is assigned to the / PathSpec.

  • Field Details

    • HANDLER_ETAGS

      public static final String HANDLER_ETAGS
  • Constructor Details

    • CompressionHandler

      public CompressionHandler()
    • CompressionHandler

      public CompressionHandler(Handler handler)
  • Method Details

    • putCompression

      public Compression putCompression(Compression compression)
      Registers support for a Compression implementation to this Handler.
      Parameters:
      compression - the compression implementation.
      Returns:
      the previously registered compression with the same encoding name, can be null.
    • removeCompression

      public Compression removeCompression(String encodingName)
      Unregisters a specific Compression implementation.
      Parameters:
      encodingName - the encoding name of the compression to remove.
      Returns:
      the Compression that was removed, can be null if no Compression exists on that encoding name.
    • ensureConfiguration

      public CompressionConfig ensureConfiguration(PathSpec pathSpec)
      Obtain a CompressionConfig for the specified PathSpec.

      This is different from getConfiguration(PathSpec), which will return null if the mapping to the provided PathSpec does not exist.

      Parameters:
      pathSpec - the PathSpec to look for.
      Returns:
      the CompressionConfig associated with the PathSpec, mapping is created if it didn't previously exist.
    • ensureConfiguration

      public CompressionConfig ensureConfiguration(String pathSpecString)
      Obtain a CompressionConfig for the specified PathSpec.

      This is different from getConfiguration(PathSpec), which will return null if the mapping to the provided PathSpec does not exist.

      Parameters:
      pathSpecString - the string representation of the path spec.
      Returns:
      the CompressionConfig associated with the PathSpec, mapping is created if it didn't previously exist.
      See Also:
    • getConfiguration

      public CompressionConfig getConfiguration(PathSpec pathSpec)
      Get the CompressionConfig associated with this PathSpec
      Parameters:
      pathSpec - the PathSpec to look for
      Returns:
      the CompressionConfig mapped to the PathSpec, null if nothing is mapped to the PathSpec
    • getConfiguration

      public CompressionConfig getConfiguration(String pathSpecString)
      Get the CompressionConfig associated with this PathSpec
      Parameters:
      pathSpecString - the string representation of the path spec.
      Returns:
      the CompressionConfig mapped to the PathSpec, null if nothing is mapped to the PathSpec
    • putConfiguration

      public CompressionConfig putConfiguration(PathSpec pathSpec, CompressionConfig config)
      Establish a CompressionConfig associated with the specific PathSpec
      Parameters:
      pathSpec - the path spec to use as the key
      config - the config to use as the value
      Returns:
      the old CompressionConfig if one was previously set.
      See Also:
    • putConfiguration

      public CompressionConfig putConfiguration(String pathSpecString, CompressionConfig config)
      Establish a CompressionConfig associated with the specific PathSpec
      Parameters:
      pathSpecString - the string representation of the path spec.
      config - the config to use as the value
      Returns:
      the old CompressionConfig if one was previously set.
      See Also:
    • 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:
      Exception - If there was a problem starting. Will cause a transition to FAILED state
    • 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:
    • toString

      public String toString()
      Overrides:
      toString in class AbstractLifeCycle