Class CmsStaticResourceHandler

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String HANDLER_NAME
      The handler name.
      static java.lang.String STATIC_RESOURCE_PREFIX
      The static resource prefix '/handleStatic'.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected boolean allowServePrecompressedResource​(javax.servlet.http.HttpServletRequest request, java.lang.String url)
      Returns whether this servlet should attempt to serve a precompressed version of the given static resource.
      protected int getCacheTime​(java.lang.String filename)
      Calculates the cache lifetime for the given filename in seconds.
      java.lang.String[] getHandlerNames()
      Returns the handler name.
      static java.lang.String getStaticResourceContext​(java.lang.String opencmsContext, java.lang.String opencmsVersion)
      Returns the context for static resources served from the class path, e.g.
      static java.net.URL getStaticResourceURL​(java.lang.String resourcePath)
      Returns the URL to a static resource.
      void handle​(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.String name)
      Handles an OpenCms request.
      static boolean isStaticResourceUri​(java.lang.String path)
      Returns if the given URI points to a static resource.
      static boolean isStaticResourceUri​(java.net.URI uri)
      Returns if the given URI points to a static resource.
      static java.lang.String removeStaticResourcePrefix​(java.lang.String path)
      Removes the static resource path prefix.
      protected void setResponseHeaders​(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.String filename, java.net.URL resourceURL)
      Sets the response headers.
      protected void writeStaticResourceResponse​(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.net.URL resourceUrl)
      Writes the contents of the given resourceUrl in the response.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getStaticResourceContext

        public static java.lang.String getStaticResourceContext​(java.lang.String opencmsContext,
                                                                java.lang.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 java.net.URL getStaticResourceURL​(java.lang.String resourcePath)
        Returns the URL to a static resource.

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

        public static boolean isStaticResourceUri​(java.lang.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​(java.net.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

        public static java.lang.String removeStaticResourcePrefix​(java.lang.String path)
        Removes the static resource path prefix.

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

        protected boolean allowServePrecompressedResource​(javax.servlet.http.HttpServletRequest request,
                                                          java.lang.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​(java.lang.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,
                                          java.lang.String filename,
                                          java.net.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,
                                                   java.net.URL resourceUrl)
                                            throws java.io.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:
        java.io.IOException - in case writing the response fails