Class ResourceHandler

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

public class ResourceHandler extends Handler.Wrapper
Resource Handler will serve static content and handle If-Modified-Since headers. No caching is done. Requests for resources that do not exist are let pass (Eg no 404's).
  • Constructor Details

    • ResourceHandler

      public ResourceHandler()
    • ResourceHandler

      public ResourceHandler(Handler handler)
  • Method Details

    • newResourceService

      protected ResourceService newResourceService()
    • getResourceService

      public ResourceService getResourceService()
    • doStart

      public 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
    • getHttpContentFactory

      public HttpContent.Factory getHttpContentFactory()
    • newHttpContentFactory

      protected HttpContent.Factory newHttpContentFactory()
    • setupWelcomeFactory

      protected ResourceService.WelcomeFactory setupWelcomeFactory()
    • 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:
    • getBaseResource

      public Resource getBaseResource()
      Returns:
      Returns the resourceBase.
    • getByteBufferPool

      public ByteBufferPool getByteBufferPool()
    • getCacheControl

      public String getCacheControl()
      Get the cacheControl header to set on all static content..
      Returns:
      the cacheControl header to set on all static content.
    • getGzipEquivalentFileExtensions

      public List<String> getGzipEquivalentFileExtensions()
      Returns:
      file extensions that signify that a file is gzip compressed. Eg ".svgz"
    • getMimeTypes

      public MimeTypes getMimeTypes()
    • getStyleSheet

      public Resource getStyleSheet()
      Returns:
      Returns the stylesheet as a Resource.
    • getWelcomeFiles

      public List<String> getWelcomeFiles()
    • isAcceptRanges

      public boolean isAcceptRanges()
      Returns:
      If true, range requests and responses are supported
    • isDirAllowed

      public boolean isDirAllowed()
      Returns:
      If true, directory listings are returned if no welcome file is found. Else 403 Forbidden.
    • isEtags

      public boolean isEtags()
      Returns:
      True if ETag processing is done
    • isUseFileMapping

      public boolean isUseFileMapping()
    • getPrecompressedFormats

      public List<CompressedContentFormat> getPrecompressedFormats()
      Returns:
      Precompressed resources formats that can be used to serve compressed variant of resources.
    • getWelcomeMode

      public ResourceService.WelcomeMode getWelcomeMode()
    • setAcceptRanges

      public void setAcceptRanges(boolean acceptRanges)
      Parameters:
      acceptRanges - If true, range requests and responses are supported
    • setBaseResource

      public void setBaseResource(Resource base)
      Parameters:
      base - The resourceBase to server content from. If null the context resource base is used.
    • setBaseResourceAsString

      public void setBaseResourceAsString(String base)
      Parameters:
      base - The resourceBase to server content from. If null the context resource base is used. If non-null the Resource is created from ResourceFactory.of(org.eclipse.jetty.util.component.Container) for this context.
    • setCacheControl

      public void setCacheControl(String cacheControl)
      Set the cacheControl header to set on all static content..
      Parameters:
      cacheControl - the cacheControl header to set on all static content.
    • setDirAllowed

      public void setDirAllowed(boolean dirAllowed)
      Parameters:
      dirAllowed - If true, directory listings are returned if no welcome file is found. Else 403 Forbidden.
    • setEtags

      public void setEtags(boolean etags)
      Parameters:
      etags - True if ETag processing is done
    • setGzipEquivalentFileExtensions

      public void setGzipEquivalentFileExtensions(List<String> gzipEquivalentFileExtensions)
      Set file extensions that signify that a file is gzip compressed. Eg ".svgz".
      Parameters:
      gzipEquivalentFileExtensions - file extensions that signify that a file is gzip compressed. Eg ".svgz"
    • setPrecompressedFormats

      public void setPrecompressedFormats(CompressedContentFormat... precompressedFormats)
      Parameters:
      precompressedFormats - The list of precompresed formats to serve in encoded format if matching resource found. For example serve gzip encoded file if ".gz" suffixed resource is found.
    • setPrecompressedFormats

      public void setPrecompressedFormats(List<CompressedContentFormat> precompressedFormats)
      Parameters:
      precompressedFormats - The list of precompresed formats to serve in encoded format if matching resource found. For example serve gzip encoded file if ".gz" suffixed resource is found.
    • setEncodingCacheSize

      public void setEncodingCacheSize(int encodingCacheSize)
    • getEncodingCacheSize

      public int getEncodingCacheSize()
    • setMimeTypes

      public void setMimeTypes(MimeTypes mimeTypes)
    • setUseFileMapping

      public void setUseFileMapping(boolean useFileMapping)
    • setWelcomeMode

      public void setWelcomeMode(ResourceService.WelcomeMode welcomeMode)
    • setStyleSheet

      public void setStyleSheet(Resource styleSheet)
      Parameters:
      styleSheet - The location of the style sheet to be used as a String.
    • setWelcomeFiles

      public void setWelcomeFiles(String... welcomeFiles)
    • setWelcomeFiles

      public void setWelcomeFiles(List<String> welcomeFiles)