public class ResourceWebHandler extends Object implements WebHandler, InitializingBean
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.
Constructor and Description |
---|
ResourceWebHandler() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Invoked by the containing
BeanFactory after it has set all bean properties
and satisfied BeanFactoryAware , ApplicationContextAware etc. |
CacheControl |
getCacheControl()
Return the
CacheControl instance to build
the Cache-Control HTTP response header. |
List<Resource> |
getLocations()
Return the
List of Resource paths to use as sources
for serving static resources. |
List<String> |
getLocationValues()
Return the configured location values.
|
Map<String,MediaType> |
getMediaTypes()
Return the
configured media type mappings. |
protected reactor.core.publisher.Mono<Resource> |
getResource(ServerWebExchange exchange) |
ResourceHttpMessageWriter |
getResourceHttpMessageWriter()
Return the configured resource message writer.
|
List<ResourceResolver> |
getResourceResolvers()
Return the list of configured resource resolvers.
|
List<ResourceTransformer> |
getResourceTransformers()
Return the list of configured resource transformers.
|
reactor.core.publisher.Mono<Void> |
handle(ServerWebExchange exchange)
Processes a resource request.
|
protected void |
initAllowedLocations()
Look for a
PathResourceResolver among the configured resource
resolvers and set its allowedLocations property (if empty) to
match the locations configured on this class. |
protected boolean |
isInvalidPath(String path)
Identifies invalid resource paths.
|
boolean |
isUseLastModified()
Return whether the
Resource.lastModified() information is used
to drive HTTP responses when serving static resources. |
protected String |
processPath(String path)
Process the given resource path.
|
void |
setCacheControl(CacheControl cacheControl)
Set the
CacheControl instance to build
the Cache-Control HTTP response header. |
protected void |
setHeaders(ServerWebExchange exchange,
Resource resource,
MediaType mediaType)
Set headers on the response.
|
void |
setLocations(List<Resource> locations)
Set the
List of Resource paths to use as sources
for serving static resources. |
void |
setLocationValues(List<String> locationValues)
Accepts a list of String-based location values to be resolved into
Resource locations. |
void |
setMediaTypes(Map<String,MediaType> mediaTypes)
Add mappings between file extensions extracted from the filename of static
Resource s and the media types to use for the response. |
void |
setResourceHttpMessageWriter(ResourceHttpMessageWriter httpMessageWriter)
Configure the
ResourceHttpMessageWriter to use. |
void |
setResourceLoader(ResourceLoader resourceLoader)
Provide the ResourceLoader to load
location values with. |
void |
setResourceResolvers(List<ResourceResolver> resourceResolvers)
Configure the list of
ResourceResolvers to use. |
void |
setResourceTransformers(List<ResourceTransformer> resourceTransformers)
Configure the list of
ResourceTransformers to use. |
void |
setUseLastModified(boolean useLastModified)
Set whether we should look at the
Resource.lastModified()
when serving resources and use this information to drive "Last-Modified"
HTTP response headers. |
String |
toString() |
public void setLocationValues(List<String> locationValues)
Resource
locations.public List<String> getLocationValues()
public void setLocations(@Nullable List<Resource> locations)
List
of Resource
paths to use as sources
for serving static resources.public List<Resource> getLocations()
List
of Resource
paths to use as sources
for serving static resources.
Note that if locationValues
are provided,
instead of loaded Resource-based locations, this method will return
empty until after initialization via afterPropertiesSet()
.
public void setResourceResolvers(@Nullable List<ResourceResolver> resourceResolvers)
ResourceResolvers
to use.
By default PathResourceResolver
is configured. If using this property,
it is recommended to add PathResourceResolver
as the last resolver.
public List<ResourceResolver> getResourceResolvers()
public void setResourceTransformers(@Nullable List<ResourceTransformer> resourceTransformers)
ResourceTransformers
to use.
By default no transformers are configured for use.
public List<ResourceTransformer> getResourceTransformers()
public void setCacheControl(@Nullable CacheControl cacheControl)
CacheControl
instance to build
the Cache-Control HTTP response header.@Nullable public CacheControl getCacheControl()
CacheControl
instance to build
the Cache-Control HTTP response header.public void setResourceHttpMessageWriter(@Nullable ResourceHttpMessageWriter httpMessageWriter)
ResourceHttpMessageWriter
to use.
By default a ResourceHttpMessageWriter
will be configured.
@Nullable public ResourceHttpMessageWriter getResourceHttpMessageWriter()
public void setMediaTypes(Map<String,MediaType> mediaTypes)
Resource
s and the media types to use for the response.
Use of this method is typically not necessary since mappings can be
also determined via MediaTypeFactory.getMediaType(Resource)
.
mediaTypes
- media type mappingspublic Map<String,MediaType> getMediaTypes()
configured
media type mappings.public void setResourceLoader(ResourceLoader resourceLoader)
location values
with.public boolean isUseLastModified()
Resource.lastModified()
information is used
to drive HTTP responses when serving static resources.public void setUseLastModified(boolean useLastModified)
Resource.lastModified()
when serving resources and use this information to drive "Last-Modified"
HTTP response headers.
This option is enabled by default and should be turned off if the metadata of the static files should be ignored.
useLastModified
- whether to use the resource last-modified information.public void afterPropertiesSet() throws Exception
InitializingBean
BeanFactory
after it has set all bean properties
and satisfied BeanFactoryAware
, ApplicationContextAware
etc.
This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
afterPropertiesSet
in interface InitializingBean
Exception
- in the event of misconfiguration (such as failure to set an
essential property) or if initialization fails for any other reasonprotected void initAllowedLocations()
PathResourceResolver
among the configured resource
resolvers and set its allowedLocations
property (if empty) to
match the locations
configured on this class.public reactor.core.publisher.Mono<Void> handle(ServerWebExchange exchange)
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.
handle
in interface WebHandler
exchange
- the current server exchangeMono<Void>
to indicate when request handling is completeprotected reactor.core.publisher.Mono<Resource> getResource(ServerWebExchange exchange)
protected String processPath(String path)
The default implementation replaces:
" / // 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 void setHeaders(ServerWebExchange exchange, Resource resource, @Nullable MediaType mediaType) throws IOException
exchange
- current exchangeresource
- the identified resource (never null
)mediaType
- the resource's media type (never null
)IOException