Package org.springframework.web.servlet.resource

# Class ResourceHandlerUtils

java.lang.Object
org.springframework.web.servlet.resource.ResourceHandlerUtils

public abstract class ResourceHandlerUtils extends Object
Resource handling utility methods to share common logic between `ResourceHttpRequestHandler` and `org.springframework.web.servlet.function`.

Since:
  6.2
Author:
  Rossen Stoyanchev

## Constructor Summary

Constructors
Constructor
Description
`ResourceHandlerUtils()`

## Method Summary

Modifier and Type
Method
Description
`static void`
`assertResourceLocation(org.springframework.core.io.Resource location)`
Assert the given location is not null, and its path ends on slash.
`static String`
`initLocationPath(String path)`
Check if the given static resource location path ends with a trailing slash, and append it if necessary.
`static boolean`
`isInvalidPath(String path)`
Checks for invalid resource input paths rejecting the following: Paths that contain "WEB-INF" or "META-INF" Paths that contain "../" after a call to `StringUtils.cleanPath(java.lang.String)`.
`static boolean`
`isResourceUnderLocation(org.springframework.core.io.Resource location, org.springframework.core.io.Resource resource)`
Check whether the resource is under the given location.
`static String`
`normalizeInputPath(String path)`
Normalize the given resource path replacing the following: Backslash with forward slash.
`static boolean`
`shouldIgnoreInputPath(String path)`
Whether the given input path is invalid as determined by `isInvalidPath(String)`.

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

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

## Constructor Details

### ResourceHandlerUtils

public ResourceHandlerUtils()

## Method Details

### assertResourceLocation

public static void assertResourceLocation(@Nullable org.springframework.core.io.Resource location)
Assert the given location is not null, and its path ends on slash.

### initLocationPath

public static String initLocationPath(String path)
Check if the given static resource location path ends with a trailing slash, and append it if necessary.

Parameters:
  `path` - the location path
Returns:
  the resulting path to use

### normalizeInputPath

public static String normalizeInputPath(String path)
Normalize the given resource path replacing the following:

- 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"`.

### shouldIgnoreInputPath

public static boolean shouldIgnoreInputPath(String path)
Whether the given input path is invalid as determined by `isInvalidPath(String)`. The path is also decoded and the same checks are performed again.

### isInvalidPath

public static boolean isInvalidPath(String path)
Checks for invalid resource input paths rejecting the following:

- Paths that contain "WEB-INF" or "META-INF"
- Paths that contain "../" after a call to `StringUtils.cleanPath(java.lang.String)`.
- Paths that represent a `valid URL` or would represent one after the leading slash is removed.

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

### isResourceUnderLocation

public static boolean isResourceUnderLocation(org.springframework.core.io.Resource location, org.springframework.core.io.Resource resource) throws IOException
Check whether the resource is under the given location.

Throws:
  `IOException`