Class ResourceWebHandler

  • java.lang.Object
    • org.springframework.web.reactive.resource.ResourceWebHandler
  • All Implemented Interfaces:
    InitializingBean, WebHandler

    public class ResourceWebHandler
    extends java.lang.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.

    Rossen Stoyanchev, Brian Clozel
    • Constructor Detail

      • ResourceWebHandler

        public ResourceWebHandler()
    • Method Detail

      • setLocationValues

        public void setLocationValues(java.util.List<java.lang.String> locationValues)
        Accepts a list of String-based location values to be resolved into Resource locations.
      • getLocationValues

        public java.util.List<java.lang.String> getLocationValues()
        Return the configured location values.
      • setLocations

        public void setLocations(@Nullable
                                 java.util.List<Resource> locations)
        Set the List of Resource paths to use as sources for serving static resources.
      • getResourceResolvers

        public java.util.List<ResourceResolver> getResourceResolvers()
        Return the list of configured resource resolvers.
      • getResourceTransformers

        public java.util.List<ResourceTransformer> getResourceTransformers()
        Return the list of configured resource transformers.
      • setResourceLoader

        public void setResourceLoader(ResourceLoader resourceLoader)
        Provide the ResourceLoader to load location values with.
      • afterPropertiesSet

        public void afterPropertiesSet()
                                throws java.lang.Exception
        Description copied from interface: InitializingBean
        Invoked by the containing 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.

        Specified by:
        afterPropertiesSet in interface InitializingBean
        java.lang.Exception - in the event of misconfiguration (such as failure to set an essential property) or if initialization fails for any other reason
      • 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<java.lang.Void> handle(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 WebHandler
        exchange - the current server exchange
        Mono<Void> to indicate when request handling is complete
      • processPath

        protected java.lang.String processPath(java.lang.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".
      • isInvalidPath

        protected boolean isInvalidPath(java.lang.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.

        path - the path to validate
        true if the path is invalid, false otherwise
      • setHeaders

        protected void setHeaders(ServerWebExchange exchange,
                                  Resource resource,
                                  MediaType mediaType)
        Set headers on the response. Called for both GET and HEAD requests.
        exchange - current exchange
        resource - the identified resource (never null)
        mediaType - the resource's media type (never null)
      • toString

        public java.lang.String toString()
        toString in class java.lang.Object