Class CmsStaticResourceHandler

java.lang.Object
org.opencms.main.CmsStaticResourceHandler
All Implemented Interfaces:
I_CmsRequestHandler

public class CmsStaticResourceHandler extends Object implements I_CmsRequestHandler
Handles the requests for static resources located in the classpath.

  • Field Details

  • Constructor Details

  • Method Details

    • getStaticResourceContext

      public static String getStaticResourceContext(String opencmsContext, String opencmsVersion)
      Returns the context for static resources served from the class path, e.g. "/opencms/handleStatic/v5976v".

      Parameters:
      opencmsContext - the OpenCms context
      opencmsVersion - the OpenCms version
      Returns:
      the static resource context
    • getStaticResourceURL

      public static URL getStaticResourceURL(String resourcePath)
      Returns the URL to a static resource.

      Parameters:
      resourcePath - the static resource path
      Returns:
      the resource URL
    • isStaticResourceUri

      public static boolean isStaticResourceUri(String path)
      Returns if the given URI points to a static resource.

      Parameters:
      path - the path to test
      Returns:
      true in case the given URI points to a static resource
    • isStaticResourceUri

      public static boolean isStaticResourceUri(URI uri)
      Returns if the given URI points to a static resource.

      Parameters:
      uri - the URI to test
      Returns:
      true in case the given URI points to a static resource
    • removeStaticResourcePrefix

      Removes the static resource path prefix.

      Parameters:
      path - the path
      Returns:
      the modified path
    • getHandlerNames

      public String[] getHandlerNames()
      Description copied from interface: I_CmsRequestHandler
      Returns the handler name.

      Specified by:
      getHandlerNames in interface I_CmsRequestHandler
      Returns:
      the handler name
      See Also:
    • handle

      public void handle(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, String name) throws IOException
      Description copied from interface: I_CmsRequestHandler
      Handles an OpenCms request.

      Specified by:
      handle in interface I_CmsRequestHandler
      Parameters:
      request - the current request
      response - the current response
      name - the handler name to invoke
      Throws:
      IOException - in case an error occurs
      See Also:
    • allowServePrecompressedResource

      protected boolean allowServePrecompressedResource(javax.servlet.http.HttpServletRequest request, String url)
      Returns whether this servlet should attempt to serve a precompressed version of the given static resource. If this method returns true, the suffix .gz is appended to the URL and the corresponding resource is served if it exists. It is assumed that the compression method used is gzip. If this method returns false or a compressed version is not found, the original URL is used.

      The base implementation of this method returns true if and only if the request indicates that the client accepts gzip compressed responses and the filename extension of the requested resource is .js, .css, or .html.

      Parameters:
      request - the request for the resource
      url - the URL of the requested resource
      Returns:
      true if the servlet should attempt to serve a precompressed version of the resource, false otherwise
    • getCacheTime

      protected int getCacheTime(String filename)
      Calculates the cache lifetime for the given filename in seconds. By default filenames containing ".nocache." return 0, filenames containing ".cache." return one year, all other return the value defined in the web.xml using resourceCacheTime (defaults to 1 hour).

      Parameters:
      filename - the file name
      Returns:
      cache lifetime for the given filename in seconds
    • setResponseHeaders

      protected void setResponseHeaders(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, String filename, URL resourceURL)
      Sets the response headers.

      Parameters:
      request - the request
      response - the response
      filename - the file name
      resourceURL - the resource URL
    • writeStaticResourceResponse

      protected void writeStaticResourceResponse(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, URL resourceUrl) throws IOException
      Writes the contents of the given resourceUrl in the response. Can be overridden to add/modify response headers and similar.

      Parameters:
      request - the request for the resource
      response - the response
      resourceUrl - the url to send
      Throws:
      IOException - in case writing the response fails