HttpRequestHandler that serves static resources in an optimized way
according to the guidelines of Page Speed, YSlow, etc.
The "locations" property takes a list of Spring
Resource locations from which static resources are allowed to
be served by this handler. Resources could be served from a classpath location,
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 CacheControl | |
| private Function | |
| private final List | |
| private final List | |
| private final List | |
| private static final Log | |
| private Map | |
| private boolean | |
| private ResourceResolverChain | |
| private ResourceHttpMessageWriter | |
| private ResourceLoader | |
| private final List | |
| private final List | |
| private static final Set | |
| private ResourceTransformerChain | |
| 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 CacheControl | getCacheControl()
Return the |
| public Function | Returns: the HTTP ETag generator functionReturn the HTTP ETag generator function to be used when serving resources. |
| public List | |
| public List | |
| private MediaType | |
| public Map | |
| protected Mono | |
| public ResourceHttpMessageWriter | |
| private String | |
| public List | |
| public List | |
| public Mono | handle(ServerWebExchange
the current server exchange exchange)Implements org. Processes a resource request. |
| protected void | initAllowedLocations()
Look for a |
| 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 | setCacheControl(CacheControl cacheControl)
Set the |
| 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(ServerWebExchange
current exchange exchange, Resource the identified resource (never resource, MediaType null)the resource's media type (never mediaType)null)Set headers on the response. |
| public void | setLocations(List<Resource> locations)
Set the |
| public void | setLocationValues(List<String> locationValues)
Accepts a list of String-based location values to be resolved into
|
| public void | setMediaTypes(Map<String, MediaType>
media type mappings mediaTypes)Add mappings between file extensions extracted from the filename of 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 | setResourceHttpMessageWriter(ResourceHttpMessageWriter httpMessageWriter)
Configure the |
| public void | setResourceLoader(ResourceLoader resourceLoader)
Provide the ResourceLoader to load |
| public void | setResourceResolvers(List<ResourceResolver> resourceResolvers)
Configure the list of |
| public void | setResourceTransformers(List<ResourceTransformer> resourceTransformers)
Configure the list of |
| public void | setUseLastModified(boolean useLastModified)
Set whether we should look at the |
| public String |
| cacheControl | back to summary |
|---|---|
| private CacheControl cacheControl
| |
| etagGenerator | back to summary |
|---|---|
| private Function<Resource, String> etagGenerator
| |
| 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 | |
| mediaTypes | back to summary |
|---|---|
| private Map<String, MediaType> mediaTypes
| |
| optimizeLocations | back to summary |
|---|---|
| private boolean optimizeLocations | |
| resolverChain | back to summary |
|---|---|
| private ResourceResolverChain resolverChain
| |
| resourceHttpMessageWriter | back to summary |
|---|---|
| private ResourceHttpMessageWriter resourceHttpMessageWriter
| |
| resourceLoader | back to summary |
|---|---|
| private ResourceLoader resourceLoader
| |
| resourceResolvers | back to summary |
|---|---|
| private final List<ResourceResolver> resourceResolvers | |
| resourceTransformers | back to summary |
|---|---|
| private final List<ResourceTransformer> resourceTransformers | |
| SUPPORTED_METHODS | back to summary |
|---|---|
| private static final Set<HttpMethod> SUPPORTED_METHODS | |
| transformerChain | back to summary |
|---|---|
| private ResourceTransformerChain transformerChain
| |
| useLastModified | back to summary |
|---|---|
| private boolean useLastModified | |
| ResourceWebHandler | back to summary |
|---|---|
| public ResourceWebHandler() | |
| 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) | |
| getCacheControl | back to summary |
|---|---|
| public CacheControl getCacheControl() Return the
| |
| 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 Note that if 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
| |
| getLocationValues | back to summary |
|---|---|
| public List Return the configured location values.
| |
| getMediaType | back to summary |
|---|---|
private MediaType getMediaType(Resource resource)
| |
| getMediaTypes | back to summary |
|---|---|
| public Map Return the
| |
| getResource | back to summary |
|---|---|
| protected Mono | |
| getResourceHttpMessageWriter | back to summary |
|---|---|
| public ResourceHttpMessageWriter getResourceHttpMessageWriter() Return the configured resource message writer.
| |
| getResourcePath | back to summary |
|---|---|
| private String getResourcePath(ServerWebExchange exchange) | |
| 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. | |
| handle | back to summary |
|---|---|
| public Mono Implements org. Processes a resource request. Checks for the existence of the requested resource in the configured list of locations.
If the resource does not exist, a
| |
| initAllowedLocations | back to summary |
|---|---|
| protected void initAllowedLocations() Look for a | |
| 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() | |
| setCacheControl | back to summary |
|---|---|
| public void setCacheControl(CacheControl cacheControl) Set the | |
| 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(ServerWebExchange exchange, Resource resource, MediaType mediaType) throws IOException Set headers on the response. Called for both GET and HEAD requests.
| |
| setLocations | back to summary |
|---|---|
| public void setLocations(List<Resource> locations) Set the | |
| setLocationValues | back to summary |
|---|---|
| public void setLocationValues(List<String> locationValues) Accepts a list of String-based location values to be resolved into
| |
| setMediaTypes | back to summary |
|---|---|
| public void setMediaTypes(Map<String, MediaType> mediaTypes) Add mappings between file extensions extracted from the filename of static
Use of this method is typically not necessary since mappings can be
also 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
| |
| setResourceHttpMessageWriter | back to summary |
|---|---|
| public void setResourceHttpMessageWriter(ResourceHttpMessageWriter httpMessageWriter) Configure the By default a | |
| setResourceLoader | back to summary |
|---|---|
| public void setResourceLoader(ResourceLoader resourceLoader) Provide the ResourceLoader to load
| |
| 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. | |
| 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- | |