Top Description Fields Constructors Methods
org.springframework.web.servlet.resource

public Class PathResourceResolver

extends AbstractResourceResolver
Class Inheritance
Imports
java.io.IOException, java.net.URLDecoder, java.nio.charset.Charset, .StandardCharsets, java.util.Arrays, .Collections, .HashMap, .List, .Map, .StringTokenizer, jakarta.servlet.http.HttpServletRequest, org.springframework.core.io.ClassPathResource, .Resource, .UrlResource, org.springframework.core.log.LogFormatUtils, org.springframework.http.server.PathContainer, org.springframework.lang.Nullable, org.springframework.util.StringUtils, org.springframework.web.context.support.ServletContextResource, org.springframework.web.util.ServletRequestPathUtils, .UriUtils, .UrlPathHelper

A simple ResourceResolver that tries to find a resource under the given locations matching to the request path.

This resolver does not delegate to the ResourceResolverChain and is expected to be configured at the end in a chain of resolvers.

Authors
Jeremy Grelle, Rossen Stoyanchev, Sam Brannen
Since
4.1

Field Summary

Modifier and TypeField and Description
private Resource[]
private final Map<Resource, Charset>
private UrlPathHelper
Inherited from org.springframework.web.servlet.resource.AbstractResourceResolver:
logger

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected boolean

Returns:

"true" if resource is in a valid location, "false" otherwise
checkResource
(Resource
the resource to check
resource
,
Resource
the location relative to which the resource was found
location
)

Perform additional checks on a resolved resource beyond checking whether the resource exists and is readable.

private String
public Resource[]
public Map<Resource, Charset>
getLocationCharsets()

Return charsets associated with static resource locations.

private Resource
getResource(String resourcePath, HttpServletRequest request, List<? extends Resource> locations)

protected Resource

Returns:

the resource, or null if none found
getResource
(String
the path to the resource
resourcePath
,
Resource
the location to check
location
)

Find the resource under the given location.

public UrlPathHelper
private boolean
private boolean
protected Resource
protected String
public void
setAllowedLocations(Resource...
the list of allowed locations
locations
)

By default, when a Resource is found, the path of the resolved resource is compared to ensure it's under the input location where it was found.

public void
setLocationCharsets(Map<Resource, Charset> locationCharsets)

Configure charsets associated with locations.

public void
setUrlPathHelper(UrlPathHelper urlPathHelper)

Provide a reference to the UrlPathHelper used to map requests to static resources.

private boolean
shouldDecodeRelativePath(Resource location, boolean usesPathPattern)

When the HandlerMapping is set to not decode the URL path, the path needs to be decoded for non-UrlResource locations.

private boolean
shouldEncodeRelativePath(Resource location, boolean usesPathPattern)

When the HandlerMapping is set to decode the URL path, the path needs to be encoded for UrlResource locations.

Inherited from org.springframework.web.servlet.resource.AbstractResourceResolver:
resolveResourceresolveUrlPath

Field Detail

allowedLocationsback to summary
private Resource[] allowedLocations
Annotations
@Nullable
locationCharsetsback to summary
private final Map<Resource, Charset> locationCharsets
urlPathHelperback to summary
private UrlPathHelper urlPathHelper
Annotations
@Nullable

Constructor Detail

PathResourceResolverback to summary
public PathResourceResolver()

Method Detail

checkResourceback to summary
protected boolean checkResource(Resource resource, Resource location) throws IOException

Perform additional checks on a resolved resource beyond checking whether the resource exists and is readable. The default implementation also verifies the resource is either under the location relative to which it was found or is under one of the allowed locations.

Parameters
resource:Resource

the resource to check

location:Resource

the location relative to which the resource was found

Returns:boolean

"true" if resource is in a valid location, "false" otherwise

Since
4.1.2
encodeOrDecodeIfNecessaryback to summary
private String encodeOrDecodeIfNecessary(String path, HttpServletRequest request, Resource location)
getAllowedLocationsback to summary
public Resource[] getAllowedLocations()
Annotations
@Nullable
getLocationCharsetsback to summary
public Map<Resource, Charset> getLocationCharsets()

Return charsets associated with static resource locations.

Since
4.3.13
getResourceback to summary
private Resource getResource(String resourcePath, HttpServletRequest request, List<? extends Resource> locations)
Annotations
@Nullable
getResourceback to summary
protected Resource getResource(String resourcePath, Resource location) throws IOException

Find the resource under the given location.

The default implementation checks if there is a readable Resource for the given path relative to the location.

Parameters
resourcePath:String

the path to the resource

location:Resource

the location to check

Returns:Resource

the resource, or null if none found

Annotations
@Nullable
getUrlPathHelperback to summary
public UrlPathHelper getUrlPathHelper()

The configured UrlPathHelper.

Annotations
@Nullable
Since
4.3.13
isInvalidEncodedPathback to summary
private boolean isInvalidEncodedPath(String resourcePath)
isResourceUnderLocationback to summary
private boolean isResourceUnderLocation(Resource resource, Resource location) throws IOException
resolveResourceInternalback to summary
protected Resource resolveResourceInternal(HttpServletRequest request, String requestPath, List<? extends Resource> locations, ResourceResolverChain chain)

Implements abstract org.springframework.web.servlet.resource.AbstractResourceResolver.resolveResourceInternal.

Annotations
@Override
@Nullable
resolveUrlPathInternalback to summary
protected String resolveUrlPathInternal(String resourcePath, List<? extends Resource> locations, ResourceResolverChain chain)

Implements abstract org.springframework.web.servlet.resource.AbstractResourceResolver.resolveUrlPathInternal.

Annotations
@Override
@Nullable
setAllowedLocationsback to summary
public void setAllowedLocations(Resource... locations)

By default, when a Resource is found, the path of the resolved resource is compared to ensure it's under the input location where it was found. However sometimes that may not be the case, e.g. when org.springframework.web.servlet.resource.CssLinkResourceTransformer resolves public URLs of links it contains, the CSS file is the location and the resources being resolved are css files, images, fonts and others located in adjacent or parent directories.

This property allows configuring a complete list of locations under which resources must be so that if a resource is not under the location relative to which it was found, this list may be checked as well.

By default ResourceHttpRequestHandler initializes this property to match its list of locations.

Parameters
locations:Resource[]

the list of allowed locations

Since
4.1.2
See Also
ResourceHttpRequestHandler#initAllowedLocations()
setLocationCharsetsback to summary
public void setLocationCharsets(Map<Resource, Charset> locationCharsets)

Configure charsets associated with locations. If a static resource is found under a URL resource location the charset is used to encode the relative path

Note

the charset is used only if the urlPathHelper property is also configured and its urlDecode property is set to true.

Since
4.3.13
setUrlPathHelperback to summary
public void setUrlPathHelper(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
shouldDecodeRelativePathback to summary
private boolean shouldDecodeRelativePath(Resource location, boolean usesPathPattern)

When the HandlerMapping is set to not decode the URL path, the path needs to be decoded for non-UrlResource locations.

shouldEncodeRelativePathback to summary
private boolean shouldEncodeRelativePath(Resource location, boolean usesPathPattern)

When the HandlerMapping is set to decode the URL path, the path needs to be encoded for UrlResource locations.