Package org.springframework.web.servlet.resource

# Class PathResourceResolver

java.lang.Object
org.springframework.web.servlet.resource.AbstractResourceResolver
org.springframework.web.servlet.resource.PathResourceResolver

All Implemented Interfaces:
  `ResourceResolver`

public class PathResourceResolver extends AbstractResourceResolver
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.

Since:
  4.1
Author:
  Jeremy Grelle, Rossen Stoyanchev, Sam Brannen

## Field Summary

### Fields inherited from class org.springframework.web.servlet.resource.AbstractResourceResolver

`logger`

## Constructor Summary

Constructors
Constructor
Description
`PathResourceResolver()`

## Method Summary

Modifier and Type
Method
Description
`protected boolean`
`checkResource(org.springframework.core.io.Resource resource, org.springframework.core.io.Resource location)`
Perform additional checks on a resolved resource beyond checking whether the resource exists and is readable.
`org.springframework.core.io.Resource[]`
`getAllowedLocations()`
`Map<org.springframework.core.io.Resource,Charset>`
`getLocationCharsets()`
Return charsets associated with static resource locations.
`protected org.springframework.core.io.Resource`
`getResource(String resourcePath, org.springframework.core.io.Resource location)`
Find the resource under the given location.
`org.springframework.web.util.UrlPathHelper`
`getUrlPathHelper()`
The configured `UrlPathHelper`.
`protected org.springframework.core.io.Resource`
`resolveResourceInternal(HttpServletRequest request, String requestPath, List<? extends org.springframework.core.io.Resource> locations, ResourceResolverChain chain)`
`protected String`
`resolveUrlPathInternal(String resourcePath, List<? extends org.springframework.core.io.Resource> locations, ResourceResolverChain chain)`
`void`
`setAllowedLocations(org.springframework.core.io.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.
`void`
`setLocationCharsets(Map<org.springframework.core.io.Resource,Charset> locationCharsets)`
Configure charsets associated with locations.
`void`
`setUrlPathHelper(org.springframework.web.util.UrlPathHelper urlPathHelper)`
Provide a reference to the `UrlPathHelper` used to map requests to static resources.

### Methods inherited from class org.springframework.web.servlet.resource.AbstractResourceResolver

`resolveResource, resolveUrlPath`

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

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

## Constructor Details

### PathResourceResolver

public PathResourceResolver()

## Method Details

### setAllowedLocations

public void setAllowedLocations(@Nullable org.springframework.core.io.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, for example, when `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` - the list of allowed locations
Since:
  4.1.2
See Also:
  - `ResourceHttpRequestHandler.initAllowedLocations()`

### getAllowedLocations

@Nullable public org.springframework.core.io.Resource[] getAllowedLocations()

### setLocationCharsets

public void setLocationCharsets(Map<org.springframework.core.io.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

### getLocationCharsets

public Map<org.springframework.core.io.Resource,Charset> getLocationCharsets()
Return charsets associated with static resource locations.

Since:
  4.3.13

### setUrlPathHelper

public void setUrlPathHelper(@Nullable org.springframework.web.util.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

### getUrlPathHelper

@Nullable public org.springframework.web.util.UrlPathHelper getUrlPathHelper()
The configured `UrlPathHelper`.

Since:
  4.3.13

### resolveResourceInternal

@Nullable protected org.springframework.core.io.Resource resolveResourceInternal(@Nullable HttpServletRequest request, String requestPath, List<? extends org.springframework.core.io.Resource> locations, ResourceResolverChain chain)

Specified by:
  `resolveResourceInternal` in class `AbstractResourceResolver`

### resolveUrlPathInternal

@Nullable protected String resolveUrlPathInternal(String resourcePath, List<? extends org.springframework.core.io.Resource> locations, ResourceResolverChain chain)

Specified by:
  `resolveUrlPathInternal` in class `AbstractResourceResolver`

### getResource

@Nullable protected org.springframework.core.io.Resource getResource(String resourcePath, org.springframework.core.io.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` - the path to the resource
  `location` - the location to check
Returns:
  the resource, or `null` if none found
Throws:
  `IOException`

### checkResource

protected boolean checkResource(org.springframework.core.io.Resource resource, org.springframework.core.io.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` - the resource to check
  `location` - the location relative to which the resource was found
Returns:
  "true" if resource is in a valid location, "false" otherwise
Throws:
  `IOException`
Since:
  4.1.2