public class ResourceHttpRequestHandler extends WebContentGenerator implements HttpRequestHandler, InitializingBean
HttpRequestHandler
that serves static resources optimized for superior browser performance
(according to the guidelines of Page Speed, YSlow, etc.) by allowing for flexible cache settings
("cacheSeconds" property, last-modified support).
The "locations" property takes a list of Spring Resource
locations
from which static resources are allowed to be served by this handler. For a given request, the
list of locations will be consulted in order for the presence of the requested resource, and the
first found match will be written to the response, with Expires
and Cache-Control
headers set as configured. The handler also properly evaluates the Last-Modified
header
(if present) so that a 304
status code will be returned as appropriate, avoiding unnecessary
overhead for resources that are already cached by the client. The use of Resource
locations
allows resource requests to easily be mapped to locations other than the web application root. For
example, resources could be served from a classpath location such as "classpath:/META-INF/public-web-resources/",
allowing convenient packaging and serving of resources such as a JavaScript library from within jar files.
To ensure that users with a primed browser cache get the latest changes to application-specific resources upon deployment of new versions of the application, it is recommended that a version string is used in the URL mapping pattern that selects this handler. Such patterns can be easily parameterized using Spring EL. See the reference manual for further examples of this approach.
Rather than being directly configured as a bean, this handler will typically be configured
through use of the <mvc:resources/>
XML configuration element.
METHOD_GET, METHOD_HEAD, METHOD_POST
Constructor and Description |
---|
ResourceHttpRequestHandler() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Invoked by a BeanFactory after it has set all bean properties supplied
(and satisfied BeanFactoryAware and ApplicationContextAware).
|
protected MediaType |
getMediaType(Resource resource)
Determine an appropriate media type for the given resource.
|
protected Resource |
getResource(HttpServletRequest request) |
void |
handleRequest(HttpServletRequest request,
HttpServletResponse response)
Processes a resource request.
|
protected boolean |
isInvalidPath(String path)
Identifies invalid resource paths.
|
protected String |
processPath(String path)
Process the given resource path to be used.
|
protected void |
setHeaders(HttpServletResponse response,
Resource resource,
MediaType mediaType)
Set headers on the given servlet response.
|
void |
setLocations(List<Resource> locations)
Set a
List of Resource paths to use as sources
for serving static resources. |
protected void |
writeContent(HttpServletResponse response,
Resource resource)
Write the actual content out to the given servlet response,
streaming the resource's content.
|
applyCacheSeconds, applyCacheSeconds, cacheForSeconds, cacheForSeconds, checkAndPrepare, checkAndPrepare, getCacheSeconds, getSupportedMethods, isAlwaysMustRevalidate, isRequireSession, isUseCacheControlHeader, isUseCacheControlNoStore, isUseExpiresHeader, preventCaching, setAlwaysMustRevalidate, setCacheSeconds, setRequireSession, setSupportedMethods, setUseCacheControlHeader, setUseCacheControlNoStore, setUseExpiresHeader
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext
getApplicationContext, getMessageSourceAccessor, initApplicationContext, requiredContextClass, setApplicationContext
public void setLocations(List<Resource> locations)
List
of Resource
paths to use as sources
for serving static resources.public void afterPropertiesSet() throws Exception
InitializingBean
This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.
afterPropertiesSet
in interface InitializingBean
Exception
- in the event of misconfiguration (such
as failure to set an essential property) or if initialization fails.public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
Checks for the existence of the requested resource in the configured list of locations.
If the resource does not exist, a 404
response will be returned to the client.
If the resource exists, the request will be checked for the presence of the
Last-Modified
header, and its value will be compared against the last-modified
timestamp of the given resource, returning a 304
status code if the
Last-Modified
value is greater. If the resource is newer than the
Last-Modified
value, or the header is not present, the content resource
of the resource will be written to the response with caching headers
set to expire one year in the future.
handleRequest
in interface HttpRequestHandler
request
- current HTTP requestresponse
- current HTTP responseServletException
- in case of general errorsIOException
- in case of I/O errorsprotected Resource getResource(HttpServletRequest request)
protected String processPath(String path)
The default implementation replaces any combination of leading '/' and
control characters (00-1F and 7F) with a single "/" or "". For example
" // /// //// foo/bar"
becomes "/foo/bar"
.
protected boolean isInvalidPath(String path)
StringUtils.cleanPath(java.lang.String)
.
valid URL
or would represent one after the leading slash is removed.
Note: this method assumes that leading, duplicate '/' or control characters (e.g. white space) have been trimmed so that the path starts predictably with a single '/' or does not have one.
path
- the path to validatetrue
if the path is invalid, false
otherwiseprotected MediaType getMediaType(Resource resource)
resource
- the resource to checknull
if none foundprotected void setHeaders(HttpServletResponse response, Resource resource, MediaType mediaType) throws IOException
response
- current servlet responseresource
- the identified resource (never null
)mediaType
- the resource's media type (never null
)IOException
- in case of errors while setting the headersprotected void writeContent(HttpServletResponse response, Resource resource) throws IOException
response
- current servlet responseresource
- the identified resource (never null
)IOException
- in case of errors while writing the content