org.springframework.web.reactive.resource

## Class ResourceWebHandler

- java.lang.Object
- - org.springframework.web.reactive.resource.ResourceWebHandler

All Implemented Interfaces:
  org.springframework.beans.factory.InitializingBean, org.springframework.web.server.WebHandler

    public class ResourceWebHandler
    extends Object
    implements org.springframework.web.server.WebHandler, org.springframework.beans.factory.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.

Since:
  5.0
Author:
  Rossen Stoyanchev, Brian Clozel

### Constructor Summary

Constructors
Constructor and Description
`ResourceWebHandler()`

### Method Summary

All Methods Instance Methods Concrete Methods
`void` `afterPropertiesSet()`
`org.springframework.http.CacheControl` `getCacheControl()` Return the `CacheControl` instance to build the Cache-Control HTTP response header.
`List<org.springframework.core.io.Resource>` `getLocations()` Return the `List` of `Resource` paths to use as sources for serving static resources.
`List<String>` `getLocationValues()` Return the configured location values.
`protected reactor.core.publisher.Mono<org.springframework.core.io.Resource>` `getResource(org.springframework.web.server.ServerWebExchange exchange)`
`org.springframework.http.codec.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(org.springframework.web.server.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.
`protected String` `processPath(String path)` Process the given resource path.
`void` `setCacheControl(org.springframework.http.CacheControl cacheControl)` Set the `CacheControl` instance to build the Cache-Control HTTP response header.
`protected void` `setHeaders(org.springframework.web.server.ServerWebExchange exchange, org.springframework.core.io.Resource resource, org.springframework.http.MediaType mediaType)` Set headers on the response.
`void` `setLocations(List<org.springframework.core.io.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` `setResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter httpMessageWriter)` Configure the `ResourceHttpMessageWriter` to use.
`void` `setResourceLoader(org.springframework.core.io.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.
`String` `toString()`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

### Constructor Detail

#### ResourceWebHandler

    public ResourceWebHandler()

### Method Detail

#### setLocationValues

    public void setLocationValues(List<String> locationValues)

Accepts a list of String-based location values to be resolved into `Resource` locations.

Since:
  5.1

#### getLocationValues

    public List<String> getLocationValues()

Return the configured location values.

Since:
  5.1

#### setLocations

    public void setLocations(@Nullable
                             List<org.springframework.core.io.Resource> locations)

Set the `List` of `Resource` paths to use as sources for serving static resources.

#### getLocations

    public List<org.springframework.core.io.Resource> getLocations()

Return the `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()`.

See Also:
  `setLocationValues(java.util.List<java.lang.String>)`, `setLocations(java.util.List<org.springframework.core.io.Resource>)`

#### setResourceResolvers

    public void setResourceResolvers(@Nullable
                                     List<ResourceResolver> resourceResolvers)

Configure the list of `ResourceResolvers` to use.

By default `PathResourceResolver` is configured. If using this property, it is recommended to add `PathResourceResolver` as the last resolver.

#### getResourceResolvers

    public List<ResourceResolver> getResourceResolvers()

Return the list of configured resource resolvers.

#### setResourceTransformers

    public void setResourceTransformers(@Nullable
                                        List<ResourceTransformer> resourceTransformers)

Configure the list of `ResourceTransformers` to use.

By default no transformers are configured for use.

#### getResourceTransformers

    public List<ResourceTransformer> getResourceTransformers()

Return the list of configured resource transformers.

#### setCacheControl

    public void setCacheControl(@Nullable
                                org.springframework.http.CacheControl cacheControl)

Set the `CacheControl` instance to build the Cache-Control HTTP response header.

#### getCacheControl

    @Nullable
    public org.springframework.http.CacheControl getCacheControl()

Return the `CacheControl` instance to build the Cache-Control HTTP response header.

#### setResourceHttpMessageWriter

    public void setResourceHttpMessageWriter(@Nullable
                                             org.springframework.http.codec.ResourceHttpMessageWriter httpMessageWriter)

Configure the `ResourceHttpMessageWriter` to use.

By default a `ResourceHttpMessageWriter` will be configured.

#### getResourceHttpMessageWriter

    @Nullable
    public org.springframework.http.codec.ResourceHttpMessageWriter getResourceHttpMessageWriter()

Return the configured resource message writer.

#### setResourceLoader

    public void setResourceLoader(org.springframework.core.io.ResourceLoader resourceLoader)

Provide the ResourceLoader to load `location values` with.

Since:
  5.1

#### afterPropertiesSet

    public void afterPropertiesSet()
                            throws Exception

Specified by:
  `afterPropertiesSet` in interface `org.springframework.beans.factory.InitializingBean`
Throws:
  `Exception`

#### initAllowedLocations

    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.

#### handle

    public reactor.core.publisher.Mono<Void> handle(org.springframework.web.server.ServerWebExchange exchange)

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 `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.

Specified by:
  `handle` in interface `org.springframework.web.server.WebHandler`

#### getResource

    protected reactor.core.publisher.Mono<org.springframework.core.io.Resource> getResource(org.springframework.web.server.ServerWebExchange exchange)

#### processPath

    protected String processPath(String path)

Process the given resource path.

The default implementation replaces:

- Backslash with forward slash.
- Duplicate occurrences of slash with a single slash.
- Any combination of leading slash and control characters (00-1F and 7F) with a single "/" or "". For example `" / // foo/bar"` becomes `"/foo/bar"`.

Since:
  3.2.12

#### isInvalidPath

    protected boolean isInvalidPath(String path)

Identifies invalid resource paths. By default rejects:

- Paths that contain "WEB-INF" or "META-INF"
- Paths that contain "../" after a call to `StringUtils.cleanPath(java.lang.String)`.
- Paths that represent a `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.

Parameters:
  `path` - the path to validate
Returns:
  `true` if the path is invalid, `false` otherwise

#### setHeaders

    protected void setHeaders(org.springframework.web.server.ServerWebExchange exchange,
                              org.springframework.core.io.Resource resource,
                              @Nullable
                              org.springframework.http.MediaType mediaType)
                       throws IOException

Set headers on the response. Called for both GET and HEAD requests.

Parameters:
  `exchange` - current exchange
  `resource` - the identified resource (never `null`)
  `mediaType` - the resource's media type (never `null`)
Throws:
  `IOException`

#### toString

    public String toString()

Overrides:
  `toString` in class `Object`