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]".
      • 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:
      • 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.
      • 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.
      • 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.
      • handleRequest

        public void handleRequest(HttpServletRequest request,
                                  HttpServletResponse response)
                           throws ServletException,
        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
        request - current HTTP request
        response - current HTTP response
        ServletException - in case of general errors - 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".
      • 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(HttpServletResponse response,
                                  Resource resource,
                                  MediaType mediaType)
        Set headers on the given servlet response. Called for GET requests as well as HEAD requests.
        response - current servlet response
        resource - the identified resource (never null)
        mediaType - the resource's media type (never null)
        Throws: - in case of errors while setting the headers
      • toString

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