public class UrlPathHelper extends Object
RequestDispatcher
includes and support for consistent URL decoding.
Used by AbstractUrlHandlerMapping
and RequestContext
for path matching
and/or URI determination.
getLookupPathForRequest(javax.servlet.http.HttpServletRequest, java.lang.String)
,
RequestDispatcher
Modifier and Type | Field and Description |
---|---|
static UrlPathHelper |
defaultInstance
Shared, read-only instance with defaults.
|
static String |
PATH_ATTRIBUTE
Name of Servlet request attribute that holds a
resolved lookupPath. |
static UrlPathHelper |
rawPathInstance
Shared, read-only instance for the full, encoded path.
|
Constructor and Description |
---|
UrlPathHelper() |
Modifier and Type | Method and Description |
---|---|
MultiValueMap<String,String> |
decodeMatrixVariables(HttpServletRequest request,
MultiValueMap<String,String> vars)
Decode the given matrix variables via
decodeRequestString(javax.servlet.http.HttpServletRequest, java.lang.String) unless
setUrlDecode(boolean) 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) . |
Map<String,String> |
decodePathVariables(HttpServletRequest request,
Map<String,String> vars)
Decode the given URI path variables via
decodeRequestString(javax.servlet.http.HttpServletRequest, java.lang.String) unless
setUrlDecode(boolean) 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) . |
String |
decodeRequestString(HttpServletRequest request,
String source)
Decode the given source string with a URLDecoder.
|
protected String |
determineEncoding(HttpServletRequest request)
Determine the encoding for the given request.
|
String |
getContextPath(HttpServletRequest 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.
|
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.
|
String |
getLookupPathForRequest(HttpServletRequest request,
String name)
Deprecated.
as of 5.3 in favor of using
resolveAndCacheLookupPath(HttpServletRequest) and
getResolvedLookupPath(ServletRequest) . |
String |
getOriginatingContextPath(HttpServletRequest request)
Return the context path for the given request, detecting an include request
URL if called within a RequestDispatcher include.
|
String |
getOriginatingQueryString(HttpServletRequest request)
Return the query string part of the given request's URL.
|
String |
getOriginatingRequestUri(HttpServletRequest request)
Return the request URI for the given request.
|
String |
getOriginatingServletPath(HttpServletRequest request)
Return the servlet path for the given request, detecting an include request
URL if called within a RequestDispatcher include.
|
String |
getPathWithinApplication(HttpServletRequest request)
Return the path within the web application for the given request.
|
String |
getPathWithinServletMapping(HttpServletRequest request)
Return the path within the servlet mapping for the given request,
i.e.
|
protected String |
getPathWithinServletMapping(HttpServletRequest request,
String pathWithinApp)
Return the path within the servlet mapping for the given request,
i.e.
|
String |
getRequestUri(HttpServletRequest request)
Return the request URI for the given request, detecting an include request
URL if called within a RequestDispatcher include.
|
static String |
getResolvedLookupPath(ServletRequest request)
Return a previously
resolved lookupPath. |
String |
getServletPath(HttpServletRequest request)
Return the servlet path for the given request, regarding an include request
URL if called within a RequestDispatcher include.
|
boolean |
isUrlDecode()
Whether to decode the request URI when determining the lookup path.
|
String |
removeSemicolonContent(String requestUri)
Remove ";" (semicolon) content from the given request URI if the
removeSemicolonContent
property is set to "true".
|
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) . |
void |
setAlwaysUseFullPath(boolean alwaysUseFullPath)
Whether URL lookups should always use the full path within the current
web application context, i.e.
|
void |
setDefaultEncoding(String defaultEncoding)
Set the default character encoding to use for URL decoding.
|
void |
setRemoveSemicolonContent(boolean removeSemicolonContent)
Set if ";" (semicolon) content should be stripped from the request URI.
|
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.
|
boolean |
shouldRemoveSemicolonContent()
Whether configured to remove ";" (semicolon) content from the request URI.
|
public static final String PATH_ATTRIBUTE
resolved
lookupPath.public static final UrlPathHelper defaultInstance
alwaysUseFullPath=false
urlDecode=true
removeSemicolon=true
defaultEncoding=
WebUtils.DEFAULT_CHARACTER_ENCODING
public static final UrlPathHelper rawPathInstance
alwaysUseFullPath=true
urlDecode=false
removeSemicolon=false
defaultEncoding=
WebUtils.DEFAULT_CHARACTER_ENCODING
public void setAlwaysUseFullPath(boolean alwaysUseFullPath)
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".
public void setUrlDecode(boolean urlDecode)
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
.
public boolean isUrlDecode()
public void setRemoveSemicolonContent(boolean removeSemicolonContent)
Default is "true".
public boolean shouldRemoveSemicolonContent()
public void setDefaultEncoding(String defaultEncoding)
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.
defaultEncoding
- the character encoding to usedetermineEncoding(javax.servlet.http.HttpServletRequest)
,
ServletRequest.getCharacterEncoding()
,
ServletRequest.setCharacterEncoding(String)
,
WebUtils.DEFAULT_CHARACTER_ENCODING
protected String getDefaultEncoding()
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)
.request
- the current requestpublic static String getResolvedLookupPath(ServletRequest request)
resolved
lookupPath.request
- the current requestIllegalArgumentException
- if the not found@Deprecated public String getLookupPathForRequest(HttpServletRequest request, @Nullable String name)
resolveAndCacheLookupPath(HttpServletRequest)
and
getResolvedLookupPath(ServletRequest)
.getLookupPathForRequest(HttpServletRequest)
that
automates checking for a previously computed lookupPath saved as a
request attribute. The attribute is only used for lookup purposes.request
- current HTTP requestname
- the request attribute that holds the lookupPathpublic String getLookupPathForRequest(HttpServletRequest request)
Detects include request URL if called within a RequestDispatcher include.
request
- current HTTP requestgetPathWithinServletMapping(javax.servlet.http.HttpServletRequest)
,
getPathWithinApplication(javax.servlet.http.HttpServletRequest)
public String getPathWithinServletMapping(HttpServletRequest request)
request
- current HTTP requestgetPathWithinServletMapping(HttpServletRequest, String)
protected String getPathWithinServletMapping(HttpServletRequest request, String pathWithinApp)
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" → "".
request
- current HTTP requestpathWithinApp
- a precomputed path within the applicationgetLookupPathForRequest(javax.servlet.http.HttpServletRequest, java.lang.String)
public String getPathWithinApplication(HttpServletRequest request)
Detects include request URL if called within a RequestDispatcher include.
request
- current HTTP requestgetLookupPathForRequest(javax.servlet.http.HttpServletRequest, java.lang.String)
public String getRequestUri(HttpServletRequest request)
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.
request
- current HTTP requestpublic String getContextPath(HttpServletRequest request)
As the value returned by request.getContextPath()
is not
decoded by the servlet container, this method will decode it.
request
- current HTTP requestpublic String getServletPath(HttpServletRequest request)
As the value returned by request.getServletPath()
is already
decoded by the servlet container, this method will not attempt to decode it.
request
- current HTTP requestpublic String getOriginatingRequestUri(HttpServletRequest request)
public String getOriginatingContextPath(HttpServletRequest request)
As the value returned by request.getContextPath()
is not
decoded by the servlet container, this method will decode it.
request
- current HTTP requestpublic String getOriginatingServletPath(HttpServletRequest request)
request
- current HTTP requestpublic String getOriginatingQueryString(HttpServletRequest request)
request
- current HTTP requestpublic String decodeRequestString(HttpServletRequest request, String source)
The default implementation uses URLDecoder.decode(input, enc)
.
request
- current HTTP requestsource
- the String to decodeWebUtils.DEFAULT_CHARACTER_ENCODING
,
ServletRequest.getCharacterEncoding()
,
URLDecoder.decode(String, String)
,
URLDecoder.decode(String)
protected String determineEncoding(HttpServletRequest request)
The default implementation checks the request encoding, falling back to the default encoding specified for this resolver.
request
- current HTTP requestnull
)ServletRequest.getCharacterEncoding()
,
setDefaultEncoding(java.lang.String)
public String removeSemicolonContent(String requestUri)
requestUri
- the request URI string to remove ";" content frompublic Map<String,String> decodePathVariables(HttpServletRequest request, Map<String,String> vars)
decodeRequestString(javax.servlet.http.HttpServletRequest, java.lang.String)
unless
setUrlDecode(boolean)
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)
.request
- current HTTP requestvars
- the URI variables extracted from the URL pathpublic MultiValueMap<String,String> decodeMatrixVariables(HttpServletRequest request, MultiValueMap<String,String> vars)
decodeRequestString(javax.servlet.http.HttpServletRequest, java.lang.String)
unless
setUrlDecode(boolean)
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)
.request
- current HTTP requestvars
- the URI variables extracted from the URL path