Top Description Fields Constructors Methods
org.springframework.web.util

public Class UrlPathHelper

extends Object
Class Inheritance
Imports
java.net.URLDecoder, java.nio.charset.UnsupportedCharsetException, java.util.Map, .Properties, jakarta.servlet.RequestDispatcher, .ServletRequest, jakarta.servlet.http.HttpServletMapping, .HttpServletRequest, .MappingMatch, org.apache.commons.logging.Log, .LogFactory, org.springframework.lang.Nullable, org.springframework.util.Assert, .CollectionUtils, .LinkedMultiValueMap, .MultiValueMap, .StringUtils

Helper class for URL path matching. Provides support for URL paths in RequestDispatcher includes and support for consistent URL decoding.

Used by org.springframework.web.servlet.handler.AbstractUrlHandlerMapping and org.springframework.web.servlet.support.RequestContext for path matching and/or URI determination.

Authors
Juergen Hoeller, Rob Harrop, Rossen Stoyanchev
Since
14.01.2004
See Also
getLookupPathForRequest, jakarta.servlet.RequestDispatcher

Field Summary

Modifier and TypeField and Description
private boolean
private String
public static final UrlPathHelper
defaultInstance

Shared, read-only instance with defaults.

private static final Log
public static final String
PATH_ATTRIBUTE

Name of Servlet request attribute that holds a resolved lookupPath.

public static final UrlPathHelper
rawPathInstance

Shared, read-only instance for the full, encoded path.

private boolean
private boolean
private boolean
private static final String
WEBSPHERE_URI_ATTRIBUTE

Special WebSphere request attribute, indicating the original request URI.

pack-priv static volatile Boolean

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private void
private String
decodeAndCleanUriString(HttpServletRequest request, String uri)

Decode the supplied URI string and strips any extraneous portion after a ';'.

private String
public MultiValueMap<String, String>

Returns:

the same Map or a new Map instance
decodeMatrixVariables
(HttpServletRequest
current HTTP request
request
,
MultiValueMap<String, String>
the URI variables extracted from the URL path
vars
)

Decode the given matrix variables via decodeRequestString unless setUrlDecode is set to true in which case it is assumed the URL path from which the variables were extracted is already decoded through a call to getLookupPathForRequest(HttpServletRequest).

public Map<String, String>

Returns:

the same Map or a new Map instance
decodePathVariables
(HttpServletRequest
current HTTP request
request
,
Map<String, String>
the URI variables extracted from the URL path
vars
)

Decode the given URI path variables via decodeRequestString unless setUrlDecode is set to true in which case it is assumed the URL path from which the variables were extracted is already decoded through a call to getLookupPathForRequest(HttpServletRequest).

public String

Returns:

the decoded String
decodeRequestString
(HttpServletRequest
current HTTP request
request
,
String
the String to decode
source
)

Decode the given source string with a URLDecoder.

protected String

Returns:

the encoding for the request (never null)
determineEncoding
(HttpServletRequest
current HTTP request
request
)

Determine the encoding for the given request.

public String

Returns:

the context path
getContextPath
(HttpServletRequest
current HTTP request
request
)

Return the context path for the given request, detecting an include request URL if called within a RequestDispatcher include.

protected String
getDefaultEncoding()

Return the default character encoding to use for URL decoding.

public String

Returns:

the lookup path
getLookupPathForRequest
(HttpServletRequest
current HTTP request
request
,
String
the request attribute that holds the lookupPath
name
)

Variant of getLookupPathForRequest(HttpServletRequest) that automates checking for a previously computed lookupPath saved as a request attribute.
public String

Returns:

the lookup path
getLookupPathForRequest
(HttpServletRequest
current HTTP request
request
)

Return the mapping lookup path for the given request, within the current servlet mapping if applicable, else within the web application.

public String

Returns:

the context path
getOriginatingContextPath
(HttpServletRequest
current HTTP request
request
)

Return the context path for the given request, detecting an include request URL if called within a RequestDispatcher include.

public String

Returns:

the query string
getOriginatingQueryString
(HttpServletRequest
current HTTP request
request
)

Return the query string part of the given request's URL.

public String
getOriginatingRequestUri(HttpServletRequest request)

Return the request URI for the given request.

public String

Returns:

the servlet path
getOriginatingServletPath
(HttpServletRequest
current HTTP request
request
)

Return the servlet path for the given request, detecting an include request URL if called within a RequestDispatcher include.

public String

Returns:

the path within the web application
getPathWithinApplication
(HttpServletRequest
current HTTP request
request
)

Return the path within the web application for the given request.

public String

Returns:

the path within the servlet mapping, or ""
getPathWithinServletMapping
(HttpServletRequest
current HTTP request
request
)

Return the path within the servlet mapping for the given request, i.e. the part of the request's URL beyond the part that called the servlet, or "" if the whole URL has been used to identify the servlet.

protected String

Returns:

the path within the servlet mapping, or ""
getPathWithinServletMapping
(HttpServletRequest
current HTTP request
request
,
String
a precomputed path within the application
pathWithinApp
)

Return the path within the servlet mapping for the given request, i.e. the part of the request's URL beyond the part that called the servlet, or "" if the whole URL has been used to identify the servlet.

private String
getRemainingPath(String requestUri, String mapping, boolean ignoreCase)

Match the given "mapping" to the start of the "requestUri" and if there is a match return the extra part.

public String

Returns:

the request URI
getRequestUri
(HttpServletRequest
current HTTP request
request
)

Return the request URI for the given request, detecting an include request URL if called within a RequestDispatcher include.

public static String

Returns:

the previously resolved lookupPath
getResolvedLookupPath
(ServletRequest
the current request
request
)

Return a previously resolved lookupPath.

private static String
getSanitizedPath(final String path)

Sanitize the given path.

public String

Returns:

the servlet path
getServletPath
(HttpServletRequest
current HTTP request
request
)

Return the servlet path for the given request, regarding an include request URL if called within a RequestDispatcher include.

private boolean
ignoreServletPath(HttpServletRequest request)

Whether we can ignore the servletPath and pathInfo for mapping purposes, which is the case when we can establish that the Servlet is not mapped by servletPath prefix.

public boolean
isUrlDecode()

Whether to decode the request URI when determining the lookup path.

private String
public String

Returns:

the updated URI string
removeSemicolonContent
(String
the request URI string to remove ";" content from
requestUri
)

Remove ";" (semicolon) content from the given request URI if the removeSemicolonContent property is set to "true".

private static String
public String

Returns:

the resolved path
resolveAndCacheLookupPath
(HttpServletRequest
the current request
request
)

Resolve the lookupPath and cache it in a request attribute with the key PATH_ATTRIBUTE for subsequent access via getResolvedLookupPath(ServletRequest).

public void
setAlwaysUseFullPath(boolean alwaysUseFullPath)

Whether URL lookups should always use the full path within the current web application context, i.e. within jakarta.servlet.ServletContext#getContextPath().

public void
setDefaultEncoding(String
the character encoding to use
defaultEncoding
)

Set the default character encoding to use for URL decoding.

private void
setReadOnly()

Switch to read-only mode where further configuration changes are not allowed.

public void
setRemoveSemicolonContent(boolean removeSemicolonContent)

Set if ";" (semicolon) content should be stripped from the request URI.

public void
setUrlDecode(boolean urlDecode)

Whether the context path and request URI should be decoded -- both of which are returned undecoded by the Servlet API, in contrast to the servlet path.

public boolean
shouldRemoveSemicolonContent()

Whether configured to remove ";" (semicolon) content from the request URI.

private boolean
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

alwaysUseFullPathback to summary
private boolean alwaysUseFullPath
defaultEncodingback to summary
private String defaultEncoding
defaultInstanceback to summary
public static final UrlPathHelper defaultInstance

Shared, read-only instance with defaults. The following apply:

loggerback to summary
private static final Log logger
PATH_ATTRIBUTEback to summary
public static final String PATH_ATTRIBUTE

Name of Servlet request attribute that holds a resolved lookupPath.

Since
5.3
rawPathInstanceback to summary
public static final UrlPathHelper rawPathInstance

Shared, read-only instance for the full, encoded path. The following apply:

readOnlyback to summary
private boolean readOnly
removeSemicolonContentback to summary
private boolean removeSemicolonContent
urlDecodeback to summary
private boolean urlDecode
WEBSPHERE_URI_ATTRIBUTEback to summary
private static final String WEBSPHERE_URI_ATTRIBUTE

Special WebSphere request attribute, indicating the original request URI. Preferable over the standard Servlet 2.4 forward attribute on WebSphere, simply because we need the very first URI in the request forwarding chain.

websphereComplianceFlagback to summary
pack-priv static volatile Boolean websphereComplianceFlag
Annotations
@Nullable

Constructor Detail

UrlPathHelperback to summary
public UrlPathHelper()

Method Detail

checkReadOnlyback to summary
private void checkReadOnly()
decodeAndCleanUriStringback to summary
private String decodeAndCleanUriString(HttpServletRequest request, String uri)

Decode the supplied URI string and strips any extraneous portion after a ';'.

decodeInternalback to summary
private String decodeInternal(HttpServletRequest request, String source)
Annotations
@SuppressWarnings:deprecation
decodeMatrixVariablesback to summary
public MultiValueMap<String, String> decodeMatrixVariables(HttpServletRequest request, MultiValueMap<String, String> vars)

Decode the given matrix variables via decodeRequestString unless setUrlDecode is set to true in which case it is assumed the URL path from which the variables were extracted is already decoded through a call to getLookupPathForRequest(HttpServletRequest).

Parameters
request:HttpServletRequest

current HTTP request

vars:MultiValueMap<String, String>

the URI variables extracted from the URL path

Returns:MultiValueMap<String, String>

the same Map or a new Map instance

decodePathVariablesback to summary
public Map<String, String> decodePathVariables(HttpServletRequest request, Map<String, String> vars)

Decode the given URI path variables via decodeRequestString unless setUrlDecode is set to true in which case it is assumed the URL path from which the variables were extracted is already decoded through a call to getLookupPathForRequest(HttpServletRequest).

Parameters
request:HttpServletRequest

current HTTP request

vars:Map<String, String>

the URI variables extracted from the URL path

Returns:Map<String, String>

the same Map or a new Map instance

decodeRequestStringback to summary
public String decodeRequestString(HttpServletRequest request, String source)

Decode the given source string with a URLDecoder. The encoding will be taken from the request, falling back to the default "ISO-8859-1".

The default implementation uses URLDecoder.decode(input, enc).

Parameters
request:HttpServletRequest

current HTTP request

source:String

the String to decode

Returns:String

the decoded String

See Also
WebUtils#DEFAULT_CHARACTER_ENCODING, jakarta.servlet.ServletRequest#getCharacterEncoding, java.net.URLDecoder#decode(String, String), java.net.URLDecoder#decode(String)
determineEncodingback to summary
protected String determineEncoding(HttpServletRequest request)

Determine the encoding for the given request. Can be overridden in subclasses.

The default implementation checks the request encoding, falling back to the default encoding specified for this resolver.

Parameters
request:HttpServletRequest

current HTTP request

Returns:String

the encoding for the request (never null)

See Also
jakarta.servlet.ServletRequest#getCharacterEncoding(), setDefaultEncoding
getContextPathback to summary
public String getContextPath(HttpServletRequest request)

Return the context path for the given request, detecting an include request URL if called within a RequestDispatcher include.

As the value returned by request.getContextPath() is not decoded by the servlet container, this method will decode it.

Parameters
request:HttpServletRequest

current HTTP request

Returns:String

the context path

getDefaultEncodingback to summary
protected String getDefaultEncoding()

Return the default character encoding to use for URL decoding.

getLookupPathForRequestback to summary
public String getLookupPathForRequest(HttpServletRequest request, String name)

Variant of getLookupPathForRequest(HttpServletRequest) that automates checking for a previously computed lookupPath saved as a request attribute. The attribute is only used for lookup purposes.

Parameters
request:HttpServletRequest

current HTTP request

name:String

the request attribute that holds the lookupPath

Returns:String

the lookup path

Annotations
@Deprecated
Since
5.2
getLookupPathForRequestback to summary
public String getLookupPathForRequest(HttpServletRequest request)

Return the mapping lookup path for the given request, within the current servlet mapping if applicable, else within the web application.

Detects include request URL if called within a RequestDispatcher include.

Parameters
request:HttpServletRequest

current HTTP request

Returns:String

the lookup path

See Also
getPathWithinServletMapping, getPathWithinApplication
getOriginatingContextPathback to summary
public String getOriginatingContextPath(HttpServletRequest request)

Return the context path for the given request, detecting an include request URL if called within a RequestDispatcher include.

As the value returned by request.getContextPath() is not decoded by the servlet container, this method will decode it.

Parameters
request:HttpServletRequest

current HTTP request

Returns:String

the context path

getOriginatingQueryStringback to summary
public String getOriginatingQueryString(HttpServletRequest request)

Return the query string part of the given request's URL. If this is a forwarded request, correctly resolves to the query string of the original request.

Parameters
request:HttpServletRequest

current HTTP request

Returns:String

the query string

getOriginatingRequestUriback to summary
public String getOriginatingRequestUri(HttpServletRequest request)

Return the request URI for the given request. If this is a forwarded request, correctly resolves to the request URI of the original request.

getOriginatingServletPathback to summary
public String getOriginatingServletPath(HttpServletRequest request)

Return the servlet path for the given request, detecting an include request URL if called within a RequestDispatcher include.

Parameters
request:HttpServletRequest

current HTTP request

Returns:String

the servlet path

getPathWithinApplicationback to summary
public String getPathWithinApplication(HttpServletRequest request)

Return the path within the web application for the given request.

Detects include request URL if called within a RequestDispatcher include.

Parameters
request:HttpServletRequest

current HTTP request

Returns:String

the path within the web application

See Also
getLookupPathForRequest
getPathWithinServletMappingback to summary
public String getPathWithinServletMapping(HttpServletRequest request)

Return the path within the servlet mapping for the given request, i.e. the part of the request's URL beyond the part that called the servlet, or "" if the whole URL has been used to identify the servlet.

Parameters
request:HttpServletRequest

current HTTP request

Returns:String

the path within the servlet mapping, or ""

See Also
getPathWithinServletMapping(HttpServletRequest, String)
getPathWithinServletMappingback to summary
protected String getPathWithinServletMapping(HttpServletRequest request, String pathWithinApp)

Return the path within the servlet mapping for the given request, i.e. the part of the request's URL beyond the part that called the servlet, or "" if the whole URL has been used to identify the servlet.

Detects include request URL if called within a RequestDispatcher include.

E.g.: servlet mapping = "/*"; request URI = "/test/a" → "/test/a".

E.g.: servlet mapping = "/"; request URI = "/test/a" → "/test/a".

E.g.: servlet mapping = "/test/*"; request URI = "/test/a" → "/a".

E.g.: servlet mapping = "/test"; request URI = "/test" → "".

E.g.: servlet mapping = "/*.test"; request URI = "/a.test" → "".

Parameters
request:HttpServletRequest

current HTTP request

pathWithinApp:String

a precomputed path within the application

Returns:String

the path within the servlet mapping, or ""

Since
5.2.9
See Also
getLookupPathForRequest
getRemainingPathback to summary
private String getRemainingPath(String requestUri, String mapping, boolean ignoreCase)

Match the given "mapping" to the start of the "requestUri" and if there is a match return the extra part. This method is needed because the context path and the servlet path returned by the HttpServletRequest are stripped of semicolon content unlike the requestUri.

Annotations
@Nullable
getRequestUriback to summary
public String getRequestUri(HttpServletRequest request)

Return the request URI for the given request, detecting an include request URL if called within a RequestDispatcher include.

As the value returned by request.getRequestURI() is not decoded by the servlet container, this method will decode it.

The URI that the web container resolves should be correct, but some containers like JBoss/Jetty incorrectly include ";" strings like ";jsessionid" in the URI. This method cuts off such incorrect appendices.

Parameters
request:HttpServletRequest

current HTTP request

Returns:String

the request URI

getResolvedLookupPathback to summary
public static String getResolvedLookupPath(ServletRequest request)

Return a previously resolved lookupPath.

Parameters
request:ServletRequest

the current request

Returns:String

the previously resolved lookupPath

Exceptions
IllegalArgumentException:
if the lookup path is not found
Since
5.3
getSanitizedPathback to summary
private static String getSanitizedPath(final String path)

Sanitize the given path. Uses the following rules:

  • replace all "//" by "/"
getServletPathback to summary
public String getServletPath(HttpServletRequest request)

Return the servlet path for the given request, regarding an include request URL if called within a RequestDispatcher include.

As the value returned by request.getServletPath() is already decoded by the servlet container, this method will not attempt to decode it.

Parameters
request:HttpServletRequest

current HTTP request

Returns:String

the servlet path

ignoreServletPathback to summary
private boolean ignoreServletPath(HttpServletRequest request)

Whether we can ignore the servletPath and pathInfo for mapping purposes, which is the case when we can establish that the Servlet is not mapped by servletPath prefix.

isUrlDecodeback to summary
public boolean isUrlDecode()

Whether to decode the request URI when determining the lookup path.

Since
4.3.13
removeJsessionidback to summary
private String removeJsessionid(String requestUri)
removeSemicolonContentback to summary
public String removeSemicolonContent(String requestUri)

Remove ";" (semicolon) content from the given request URI if the removeSemicolonContent property is set to "true". Note that "jsessionid" is always removed.

Parameters
requestUri:String

the request URI string to remove ";" content from

Returns:String

the updated URI string

removeSemicolonContentInternalback to summary
private static String removeSemicolonContentInternal(String requestUri)
resolveAndCacheLookupPathback to summary
public String resolveAndCacheLookupPath(HttpServletRequest request)

Resolve the lookupPath and cache it in a request attribute with the key PATH_ATTRIBUTE for subsequent access via getResolvedLookupPath(ServletRequest).

Parameters
request:HttpServletRequest

the current request

Returns:String

the resolved path

Since
5.3
setAlwaysUseFullPathback to summary
public void setAlwaysUseFullPath(boolean alwaysUseFullPath)

Whether URL lookups should always use the full path within the current web application context, i.e. within jakarta.servlet.ServletContext#getContextPath().

If set to false the path within the current servlet mapping is used instead if applicable (i.e. in the case of a prefix based Servlet mapping such as "/myServlet/*").

By default this is set to "false".

setDefaultEncodingback to summary
public void setDefaultEncoding(String defaultEncoding)

Set the default character encoding to use for URL decoding. Default is ISO-8859-1, according to the Servlet spec.

If the request specifies a character encoding itself, the request encoding will override this setting. This also allows for generically overriding the character encoding in a filter that invokes the ServletRequest.setCharacterEncoding method.

Parameters
defaultEncoding:String

the character encoding to use

See Also
determineEncoding, jakarta.servlet.ServletRequest#getCharacterEncoding(), jakarta.servlet.ServletRequest#setCharacterEncoding(String), WebUtils#DEFAULT_CHARACTER_ENCODING
setReadOnlyback to summary
private void setReadOnly()

Switch to read-only mode where further configuration changes are not allowed.

setRemoveSemicolonContentback to summary
public void setRemoveSemicolonContent(boolean removeSemicolonContent)

Set if ";" (semicolon) content should be stripped from the request URI.

Default is "true".

setUrlDecodeback to summary
public void setUrlDecode(boolean urlDecode)

Whether the context path and request URI should be decoded -- both of which are returned undecoded by the Servlet API, in contrast to the servlet path.

Either the request encoding or the default Servlet spec encoding (ISO-8859-1) is used when set to "true".

By default this is set to true.

Note

Be aware the servlet path will not match when compared to encoded paths. Therefore use of urlDecode=false is not compatible with a prefix-based Servlet mapping and likewise implies also setting alwaysUseFullPath=true.

See Also
getServletPath, getContextPath, getRequestUri, WebUtils#DEFAULT_CHARACTER_ENCODING, jakarta.servlet.ServletRequest#getCharacterEncoding(), java.net.URLDecoder#decode(String, String)
shouldRemoveSemicolonContentback to summary
public boolean shouldRemoveSemicolonContent()

Whether configured to remove ";" (semicolon) content from the request URI.

shouldRemoveTrailingServletPathSlashback to summary
private boolean shouldRemoveTrailingServletPathSlash(HttpServletRequest request)