org.springframework.web.servlet.resource

Class ResourceHttpRequestHandler

    • Constructor Detail

      • ResourceHttpRequestHandler

        public ResourceHttpRequestHandler()
    • Method Detail

      • setLocationValues

        public void setLocationValues(java.util.List<java.lang.String> locationValues)
        An alternative to setLocations(List) that accepts a list of String-based location values, with support for UrlResource's (e.g. files or HTTP URLs) with a special prefix to indicate the charset to use when appending relative paths. For example "[charset=Windows-31J]http://example.org/path".
        Since:
        4.3.13
      • setLocations

        public void setLocations(java.util.List<Resource> locations)
        Set the List of Resource locations to use as sources for serving static resources.
        See Also:
        setLocationValues(List)
      • 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.
      • setContentNegotiationManager

        public void setContentNegotiationManager(@Nullable
                                                 ContentNegotiationManager contentNegotiationManager)
        Configure a ContentNegotiationManager to help determine the media types for resources being served. If the manager contains a path extension strategy it will be checked for registered file extension.
        Since:
        4.3
      • setCorsConfiguration

        public void setCorsConfiguration(CorsConfiguration corsConfiguration)
        Specify the CORS configuration for resources served by this handler.

        By default this is not set in which allows cross-origin requests.

      • setUrlPathHelper

        public void setUrlPathHelper(@Nullable
                                     UrlPathHelper urlPathHelper)
        Provide a reference to the UrlPathHelper used to map requests to static resources. This helps to derive information about the lookup path such as whether it is decoded or not.
        Since:
        4.3.13
      • 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
        Throws:
        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.
      • handleRequest

        public void handleRequest(HttpServletRequest request,
                                  HttpServletResponse response)
                           throws ServletException,
                                  java.io.IOException
        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:
        handleRequest in interface HttpRequestHandler
        Parameters:
        request - current HTTP request
        response - current HTTP response
        Throws:
        ServletException - in case of general errors
        java.io.IOException - in case of I/O errors
      • 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".
        Since:
        3.2.12
      • 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.

        Parameters:
        path - the path to validate
        Returns:
        true if the path is invalid, false otherwise
        Since:
        3.0.6
      • setHeaders

        protected void setHeaders(HttpServletResponse response,
                                  Resource resource,
                                  @Nullable
                                  MediaType mediaType)
                           throws java.io.IOException
        Set headers on the given servlet response. Called for GET requests as well as HEAD requests.
        Parameters:
        response - current servlet response
        resource - the identified resource (never null)
        mediaType - the resource's media type (never null)
        Throws:
        java.io.IOException - in case of errors while setting the headers
      • toString

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