HttpRequestHandler that serves static resources in an optimized way
according to the guidelines of Page Speed, YSlow, etc.
The properties "locations" and "locationValues" accept locations from which static resources can be served by this handler. This can be relative to the root of the web application, or from the classpath, e.g. "classpath:/META-INF/public-web-resources/", allowing convenient packaging and serving of resources such as .js, .css, and others in jar files.
This request handler may also be configured with a
resourcesResolver and
resourceTransformer chains to support
arbitrary resolution and transformation of resources being served. By default,
a PathResourceResolver simply finds resources based on the configured
"locations". An application can configure additional resolvers and transformers
such as the VersionResourceResolver which can resolve and prepare URLs
for resources with a version in the URL.
This 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.
| Modifier and Type | Field and Description |
|---|---|
| private ContentNegotiationManager | |
| private CorsConfiguration | |
| private StringValueResolver | |
| private Function | |
| private final Map | |
| private final List | |
| private final List | |
| private final List | |
| private static final Log | |
| private final Map | |
| private boolean | |
| private ResourceResolverChain | |
| private ResourceHttpMessageConverter | |
| private ResourceRegionHttpMessageConverter | |
| private final List | |
| private final List | |
| private ResourceTransformerChain | |
| private static final String | |
| private UrlPathHelper | |
| private boolean |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | afterPropertiesSet()
Implements org. Invoked by the containing This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set. |
| private String | |
| private String | |
| public ContentNegotiationManager | getContentNegotiationManager()
Deprecated
as of 5.2.4
Return the configured content negotiation manager. |
| public CorsConfiguration | getCorsConfiguration(HttpServletRequest request)
Implements org. Return the specified CORS configuration. |
| public Function | Returns: the HTTP ETag generator functionReturn the HTTP ETag generator function to be used when serving resources. |
| public List | getLocations()
Return the configured |
| protected MediaType | Returns: the corresponding media type, ornull if none foundthe current request request, Resource the resource to check resource)Determine the media type for the given request and the resource matched to it. |
| public Map | |
| private static String | |
| protected Resource | |
| public ResourceHttpMessageConverter | |
| public ResourceRegionHttpMessageConverter | |
| public List | |
| public List | |
| public UrlPathHelper | |
| public void | handleRequest(HttpServletRequest
current HTTP request request, HttpServletResponse current HTTP response response)Implements org. Processes a resource request. |
| protected void | initAllowedLocations()
Look for a |
| protected PathExtensionContentNegotiationStrategy | initContentNegotiationStrategy()
Deprecated
as of 5.2.4 this method returns
null, and if a
subclass returns an actual instance, the instance is used only as a
source of media type mappings, if it contains any.
Initialize the strategy to use to determine the media type for a resource. |
| private boolean | Returns: true if the path is invalid, false otherwisethe path to validate path)Check whether the given path contains invalid escape sequences. |
| protected boolean | Returns: true if the path is invalid, false otherwisethe path to validate path)Identifies invalid resource paths. |
| public boolean | isOptimizeLocations()
Return whether to optimize the specified locations through an existence check on startup, filtering non-existing directories upfront so that they do not have to be checked on every resource access. |
| public boolean | isUseLastModified()
Return whether the |
| private String | |
| protected String | |
| private void | |
| public void | setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager)
Deprecated
as of 5.2.4 in favor of using
setMediaTypes(Map)
with mappings possibly obtained from
ContentNegotiationManager#getMediaTypeMappings().
Configure a |
| public void | setCorsConfiguration(CorsConfiguration corsConfiguration)
Specify the CORS configuration for resources served by this handler. |
| public void | setEmbeddedValueResolver(StringValueResolver resolver)
Implements org. Set the StringValueResolver to use for resolving embedded definition values. |
| public void | setEtagGenerator(Function<Resource, String>
the HTTP ETag generator function to use. etagGenerator)Configure a generator function that will be used to create the ETag information,
given a |
| protected void | setHeaders(HttpServletResponse
current servlet response response, Resource the identified resource (never resource, MediaType null)the resource's media type (never mediaType)null)Set headers on the given servlet response. |
| public void | setLocations(List<Resource> locations)
Configure locations to serve resources from as pre-resourced Resource's. |
| public void | |
| public void | setMediaTypes(Map<String, MediaType>
media type mappings mediaTypes)Add mappings between file extensions, extracted from the filename of a
static |
| public void | setOptimizeLocations(boolean optimizeLocations)
Set whether to optimize the specified locations through an existence check on startup, filtering non-existing directories upfront so that they do not have to be checked on every resource access. |
| public void | setResourceHttpMessageConverter(ResourceHttpMessageConverter messageConverter)
Configure the |
| public void | setResourceRegionHttpMessageConverter(ResourceRegionHttpMessageConverter messageConverter)
Configure the |
| public void | setResourceResolvers(List<ResourceResolver> resourceResolvers)
Configure the list of |
| public void | setResourceTransformers(List<ResourceTransformer> resourceTransformers)
Configure the list of |
| public void | setUrlPathHelper(UrlPathHelper urlPathHelper)
Provide a reference to the |
| public void | setUseLastModified(boolean useLastModified)
Set whether we should look at the |
| public String |
| contentNegotiationManager | back to summary |
|---|---|
| private ContentNegotiationManager contentNegotiationManager
| |
| corsConfiguration | back to summary |
|---|---|
| private CorsConfiguration corsConfiguration
| |
| embeddedValueResolver | back to summary |
|---|---|
| private StringValueResolver embeddedValueResolver
| |
| etagGenerator | back to summary |
|---|---|
| private Function<Resource, String> etagGenerator
| |
| locationCharsets | back to summary |
|---|---|
| private final Map<Resource, Charset> locationCharsets | |
| locationResources | back to summary |
|---|---|
| private final List<Resource> locationResources | |
| locationsToUse | back to summary |
|---|---|
| private final List<Resource> locationsToUse | |
| locationValues | back to summary |
|---|---|
| private final List<String> locationValues | |
| logger | back to summary |
|---|---|
| private static final Log logger Hides org. | |
| mediaTypes | back to summary |
|---|---|
| private final Map<String, MediaType> mediaTypes | |
| optimizeLocations | back to summary |
|---|---|
| private boolean optimizeLocations | |
| resolverChain | back to summary |
|---|---|
| private ResourceResolverChain resolverChain
| |
| resourceHttpMessageConverter | back to summary |
|---|---|
| private ResourceHttpMessageConverter resourceHttpMessageConverter
| |
| resourceRegionHttpMessageConverter | back to summary |
|---|---|
| private ResourceRegionHttpMessageConverter resourceRegionHttpMessageConverter
| |
| resourceResolvers | back to summary |
|---|---|
| private final List<ResourceResolver> resourceResolvers | |
| resourceTransformers | back to summary |
|---|---|
| private final List<ResourceTransformer> resourceTransformers | |
| transformerChain | back to summary |
|---|---|
| private ResourceTransformerChain transformerChain
| |
| URL_RESOURCE_CHARSET_PREFIX | back to summary |
|---|---|
| private static final String URL_RESOURCE_CHARSET_PREFIX | |
| urlPathHelper | back to summary |
|---|---|
| private UrlPathHelper urlPathHelper
| |
| useLastModified | back to summary |
|---|---|
| private boolean useLastModified | |
| ResourceHttpRequestHandler | back to summary |
|---|---|
| public ResourceHttpRequestHandler() | |
| afterPropertiesSet | back to summary |
|---|---|
| public void afterPropertiesSet() throws Exception Implements org. Doc from org. Invoked by the containing This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set. | |
| cleanDuplicateSlashes | back to summary |
|---|---|
| private String cleanDuplicateSlashes(String path) | |
| cleanLeadingSlash | back to summary |
|---|---|
| private String cleanLeadingSlash(String path) | |
| getContentNegotiationManager | back to summary |
|---|---|
| public ContentNegotiationManager getContentNegotiationManager()
Deprecated as of 5.2.4 Return the configured content negotiation manager.
| |
| getCorsConfiguration | back to summary |
|---|---|
| public CorsConfiguration getCorsConfiguration(HttpServletRequest request) Implements org. Return the specified CORS configuration.
| |
| getEtagGenerator | back to summary |
|---|---|
| public Function Return the HTTP ETag generator function to be used when serving resources. | |
| getLocations | back to summary |
|---|---|
| public List Return the configured Note that the returned list is fully initialized only after
initialization via Note As of 5.3.11 the list of locations may be filtered to
exclude those that don't actually exist and therefore the list returned from this
method may be a subset of all given locations. See
| |
| getMediaType | back to summary |
|---|---|
| protected MediaType getMediaType(HttpServletRequest request, Resource resource) Determine the media type for the given request and the resource matched to it. This implementation tries to determine the MediaType using one of the following lookups based on the resource filename and its path extension:
| |
| getMediaTypes | back to summary |
|---|---|
| public Map Return the
| |
| getPath | back to summary |
|---|---|
| private static String getPath(HttpServletRequest request) | |
| getResource | back to summary |
|---|---|
protected Resource getResource(HttpServletRequest request) throws IOException
| |
| getResourceHttpMessageConverter | back to summary |
|---|---|
| public ResourceHttpMessageConverter getResourceHttpMessageConverter() Return the configured resource converter.
| |
| getResourceRegionHttpMessageConverter | back to summary |
|---|---|
| public ResourceRegionHttpMessageConverter getResourceRegionHttpMessageConverter() Return the configured resource region converter.
| |
| getResourceResolvers | back to summary |
|---|---|
| public List Return the list of configured resource resolvers. | |
| getResourceTransformers | back to summary |
|---|---|
| public List Return the list of configured resource transformers. | |
| getUrlPathHelper | back to summary |
|---|---|
| public UrlPathHelper getUrlPathHelper() The configured
| |
| handleRequest | back to summary |
|---|---|
| public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException Implements org. Processes a resource request. Finds the requested resource under one of the configured locations.
If the resource does not exist,
| |
| initAllowedLocations | back to summary |
|---|---|
| protected void initAllowedLocations() Look for a | |
| initContentNegotiationStrategy | back to summary |
|---|---|
| protected PathExtensionContentNegotiationStrategy initContentNegotiationStrategy()
Deprecated as of 5.2.4 this method returns Initialize the strategy to use to determine the media type for a resource.
| |
| isInvalidEncodedPath | back to summary |
|---|---|
| private boolean isInvalidEncodedPath(String path) Check whether the given path contains invalid escape sequences.
| |
| isInvalidPath | back to summary |
|---|---|
| protected boolean isInvalidPath(String path) Identifies invalid resource paths. By default, rejects:
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.
| |
| isOptimizeLocations | back to summary |
|---|---|
| public boolean isOptimizeLocations() Return whether to optimize the specified locations through an existence check on startup, filtering non-existing directories upfront so that they do not have to be checked on every resource access.
| |
| isUseLastModified | back to summary |
|---|---|
| public boolean isUseLastModified() Return whether the
| |
| locationToString | back to summary |
|---|---|
| private String locationToString(List<Resource> locations) | |
| processPath | back to summary |
|---|---|
| protected String processPath(String path) Process the given resource path. The default implementation replaces:
| |
| resolveResourceLocations | back to summary |
|---|---|
| private void resolveResourceLocations() | |
| setContentNegotiationManager | back to summary |
|---|---|
| public void setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager)
Deprecated as of 5.2.4 in favor of using Configure a
| |
| setCorsConfiguration | back to summary |
|---|---|
| public void setCorsConfiguration(CorsConfiguration corsConfiguration) Specify the CORS configuration for resources served by this handler. By default this is not set in which allows cross-origin requests. | |
| setEmbeddedValueResolver | back to summary |
|---|---|
| public void setEmbeddedValueResolver(StringValueResolver resolver) Implements org. Doc from org. Set the StringValueResolver to use for resolving embedded definition values.
| |
| setEtagGenerator | back to summary |
|---|---|
| public void setEtagGenerator(Function<Resource, String> etagGenerator) Configure a generator function that will be used to create the ETag information,
given a This function should return a String that will be used as an argument in
| |
| setHeaders | back to summary |
|---|---|
| protected void setHeaders(HttpServletResponse response, Resource resource, MediaType mediaType) throws IOException Set headers on the given servlet response. Called for GET requests as well as HEAD requests.
| |
| setLocations | back to summary |
|---|---|
| public void setLocations(List<Resource> locations) Configure locations to serve resources from as pre-resourced Resource's.
| |
| setLocationValues | back to summary |
|---|---|
| public void setLocationValues(List<String> locations) Configure String-based locations to serve resources from. For example, { For
| |
| setMediaTypes | back to summary |
|---|---|
| public void setMediaTypes(Map<String, MediaType> mediaTypes) Add mappings between file extensions, extracted from the filename of a
static Use of this method is typically not necessary since mappings are
otherwise determined via
| |
| setOptimizeLocations | back to summary |
|---|---|
| public void setOptimizeLocations(boolean optimizeLocations) Set whether to optimize the specified locations through an existence check on startup, filtering non-existing directories upfront so that they do not have to be checked on every resource access. The default is
| |
| setResourceHttpMessageConverter | back to summary |
|---|---|
| public void setResourceHttpMessageConverter(ResourceHttpMessageConverter messageConverter) Configure the By default a
| |
| setResourceRegionHttpMessageConverter | back to summary |
|---|---|
| public void setResourceRegionHttpMessageConverter(ResourceRegionHttpMessageConverter messageConverter) Configure the By default a
| |
| setResourceResolvers | back to summary |
|---|---|
| public void setResourceResolvers(List<ResourceResolver> resourceResolvers) Configure the list of By default | |
| setResourceTransformers | back to summary |
|---|---|
| public void setResourceTransformers(List<ResourceTransformer> resourceTransformers) Configure the list of By default no transformers are configured for use. | |
| setUrlPathHelper | back to summary |
|---|---|
| public void setUrlPathHelper(UrlPathHelper urlPathHelper) Provide a reference to the
| |
| setUseLastModified | back to summary |
|---|---|
| public void setUseLastModified(boolean useLastModified) Set whether we should look at the This option is enabled by default and should be turned off if the metadata of the static files should be ignored.
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non- | |