public class ResourceWebHandler extends java.lang.Object implements WebHandler, InitializingBean, SmartInitializingSingleton
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 |
cacheControl |
private CompositeContentTypeResolver |
contentTypeResolver |
private java.util.List<Resource> |
locations |
private static Log |
logger |
private PathExtensionContentTypeResolver |
pathExtensionResolver |
private ResourceHttpMessageWriter |
resourceHttpMessageWriter |
private java.util.List<ResourceResolver> |
resourceResolvers |
private java.util.List<ResourceTransformer> |
resourceTransformers |
private static java.util.Set<java.lang.String> |
SUPPORTED_METHODS
Set of supported HTTP methods
|
Constructor and Description |
---|
ResourceWebHandler() |
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).
|
void |
afterSingletonsInstantiated()
Invoked right at the end of the singleton pre-instantiation phase,
with a guarantee that all regular singleton beans have been created
already.
|
private ResourceResolverChain |
createResolverChain() |
private ResourceTransformerChain |
createTransformerChain(ResourceResolverChain resolverChain) |
CacheControl |
getCacheControl() |
CompositeContentTypeResolver |
getContentTypeResolver()
Return the configured
CompositeContentTypeResolver . |
java.util.List<Resource> |
getLocations()
Return the
List of Resource paths to use as sources
for serving static resources. |
protected MediaType |
getMediaType(ServerWebExchange exchange,
Resource resource)
Determine the media type for the given request and the resource matched
to it.
|
protected <any> |
getResource(ServerWebExchange exchange) |
ResourceHttpMessageWriter |
getResourceHttpMessageWriter()
Return the configured resource message writer.
|
java.util.List<ResourceResolver> |
getResourceResolvers()
Return the list of configured resource resolvers.
|
java.util.List<ResourceTransformer> |
getResourceTransformers()
Return the list of configured resource transformers.
|
<any> |
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 PathExtensionContentTypeResolver |
initContentNegotiationStrategy() |
protected boolean |
isInvalidPath(java.lang.String path)
Identifies invalid resource paths.
|
protected java.lang.String |
processPath(java.lang.String path)
Process the given resource path to be used.
|
void |
setCacheControl(CacheControl cacheControl)
Set the
CacheControl instance to build
the Cache-Control HTTP response header. |
void |
setContentTypeResolver(CompositeContentTypeResolver contentTypeResolver)
Configure a
CompositeContentTypeResolver to help determine the
media types for resources being served. |
protected void |
setHeaders(ServerWebExchange exchange,
Resource resource,
MediaType mediaType)
Set headers on the response.
|
void |
setLocations(java.util.List<Resource> locations)
Set the
List of Resource paths to use as sources
for serving static resources. |
void |
setResourceHttpMessageWriter(ResourceHttpMessageWriter httpMessageWriter)
Configure the
ResourceHttpMessageWriter to use. |
void |
setResourceResolvers(java.util.List<ResourceResolver> resourceResolvers)
Configure the list of
ResourceResolver s to use. |
void |
setResourceTransformers(java.util.List<ResourceTransformer> resourceTransformers)
Configure the list of
ResourceTransformer s to use. |
java.lang.String |
toString() |
private static final java.util.Set<java.lang.String> SUPPORTED_METHODS
private static final Log logger
private final java.util.List<Resource> locations
private final java.util.List<ResourceResolver> resourceResolvers
private final java.util.List<ResourceTransformer> resourceTransformers
private CacheControl cacheControl
private ResourceHttpMessageWriter resourceHttpMessageWriter
private CompositeContentTypeResolver contentTypeResolver
private PathExtensionContentTypeResolver pathExtensionResolver
public void setLocations(java.util.List<Resource> locations)
List
of Resource
paths to use as sources
for serving static resources.public java.util.List<Resource> getLocations()
List
of Resource
paths to use as sources
for serving static resources.public void setResourceResolvers(java.util.List<ResourceResolver> resourceResolvers)
ResourceResolver
s to use.
By default PathResourceResolver
is configured. If using this property,
it is recommended to add PathResourceResolver
as the last resolver.
public java.util.List<ResourceResolver> getResourceResolvers()
public void setResourceTransformers(java.util.List<ResourceTransformer> resourceTransformers)
ResourceTransformer
s to use.
By default no transformers are configured for use.
public java.util.List<ResourceTransformer> getResourceTransformers()
public void setCacheControl(CacheControl cacheControl)
CacheControl
instance to build
the Cache-Control HTTP response header.public CacheControl getCacheControl()
public void setResourceHttpMessageWriter(ResourceHttpMessageWriter httpMessageWriter)
ResourceHttpMessageWriter
to use.
By default a ResourceHttpMessageWriter
will be configured.
public ResourceHttpMessageWriter getResourceHttpMessageWriter()
public void setContentTypeResolver(CompositeContentTypeResolver contentTypeResolver)
CompositeContentTypeResolver
to help determine the
media types for resources being served. If the manager contains a path
extension resolver it will be checked for registered file extension.contentTypeResolver
- the resolver in usepublic CompositeContentTypeResolver getContentTypeResolver()
CompositeContentTypeResolver
.public void afterPropertiesSet() throws java.lang.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
java.lang.Exception
- in the event of misconfiguration (such
as failure to set an essential property) or if initialization fails.protected void initAllowedLocations()
PathResourceResolver
among the configured resource
resolvers and set its allowedLocations
property (if empty) to
match the locations
configured on this class.public void afterSingletonsInstantiated()
SmartInitializingSingleton
ListableBeanFactory.getBeansOfType(java.lang.Class<T>)
calls within
this method won't trigger accidental side effects during bootstrap.
NOTE: This callback won't be triggered for singleton beans
lazily initialized on demand after BeanFactory
bootstrap,
and not for any other bean scope either. Carefully use it for beans
with the intended bootstrap semantics only.
afterSingletonsInstantiated
in interface SmartInitializingSingleton
protected PathExtensionContentTypeResolver initContentNegotiationStrategy()
public <any> 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 <any> getResource(ServerWebExchange exchange)
protected java.lang.String processPath(java.lang.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(java.lang.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
otherwiseprivate ResourceResolverChain createResolverChain()
private ResourceTransformerChain createTransformerChain(ResourceResolverChain resolverChain)
protected MediaType getMediaType(ServerWebExchange exchange, Resource resource)
PathExtensionContentTypeResolver.resolveMediaTypeForResource(Resource)
.exchange
- the current exchangeresource
- the resource to checknull
if none foundprotected void setHeaders(ServerWebExchange exchange, Resource resource, MediaType mediaType) throws java.io.IOException
exchange
- current exchangeresource
- the identified resource (never null
)mediaType
- the resource's media type (never null
)java.io.IOException
public java.lang.String toString()
toString
in class java.lang.Object