Package org.opencms.main
Class CmsStaticResourceHandler
- java.lang.Object
-
- org.opencms.main.CmsStaticResourceHandler
-
- All Implemented Interfaces:
I_CmsRequestHandler
public class CmsStaticResourceHandler extends java.lang.Object implements I_CmsRequestHandler
Handles the requests for static resources located in the classpath.
-
-
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'.
-
Constructor Summary
Constructors Constructor Description CmsStaticResourceHandler()
-
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
-
Methods inherited from interface org.opencms.main.I_CmsRequestHandler
getConfiguration, initParameters
-
-
-
-
Field Detail
-
HANDLER_NAME
public static final java.lang.String HANDLER_NAME
The handler name.- See Also:
- Constant Field Values
-
STATIC_RESOURCE_PREFIX
public static final java.lang.String STATIC_RESOURCE_PREFIX
The static resource prefix '/handleStatic'.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
CmsStaticResourceHandler
public CmsStaticResourceHandler()
-
-
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 contextopencmsVersion
- 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
-
getHandlerNames
public java.lang.String[] getHandlerNames()
Description copied from interface:I_CmsRequestHandler
Returns the handler name.- Specified by:
getHandlerNames
in interfaceI_CmsRequestHandler
- Returns:
- the handler name
- See Also:
I_CmsRequestHandler.getHandlerNames()
-
handle
public void handle(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.String name) throws java.io.IOException
Description copied from interface:I_CmsRequestHandler
Handles an OpenCms request.- Specified by:
handle
in interfaceI_CmsRequestHandler
- Parameters:
request
- the current requestresponse
- the current responsename
- the handler name to invoke- Throws:
java.io.IOException
- in case an error occurs- See Also:
I_CmsRequestHandler.handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.String)
-
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 resourceurl
- 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 requestresponse
- the responsefilename
- the file nameresourceURL
- 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 resourceresponse
- the responseresourceUrl
- the url to send- Throws:
java.io.IOException
- in case writing the response fails
-
-