Spring Framework

org.springframework.web.util
Class UrlPathHelper

java.lang.Object
  extended by org.springframework.web.util.UrlPathHelper

public class UrlPathHelper
extends java.lang.Object

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

Used by AbstractUrlHandlerMapping, AbstractUrlMethodNameResolver and RequestContext for path matching and/or URI determination.

Since:
14.01.2004
Author:
Juergen Hoeller, Rob Harrop, Rossen Stoyanchev

Constructor Summary
UrlPathHelper()
           
 
Method Summary
 java.util.Map<java.lang.String,java.lang.String> decodePathVariables(javax.servlet.http.HttpServletRequest request, java.util.Map<java.lang.String,java.lang.String> vars)
          Decode the given URI path variables via decodeRequestString(HttpServletRequest, 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).
 java.lang.String decodeRequestString(javax.servlet.http.HttpServletRequest request, java.lang.String source)
          Decode the given source string with a URLDecoder.
protected  java.lang.String determineEncoding(javax.servlet.http.HttpServletRequest request)
          Determine the encoding for the given request.
 java.lang.String getContextPath(javax.servlet.http.HttpServletRequest request)
          Return the context path for the given request, detecting an include request URL if called within a RequestDispatcher include.
protected  java.lang.String getDefaultEncoding()
          Return the default character encoding to use for URL decoding.
 java.lang.String getLookupPathForRequest(javax.servlet.http.HttpServletRequest request)
          Return the mapping lookup path for the given request, within the current servlet mapping if applicable, else within the web application.
 java.lang.String getOriginatingContextPath(javax.servlet.http.HttpServletRequest request)
          Return the context path for the given request, detecting an include request URL if called within a RequestDispatcher include.
 java.lang.String getOriginatingQueryString(javax.servlet.http.HttpServletRequest request)
          Return the query string part of the given request's URL.
 java.lang.String getOriginatingRequestUri(javax.servlet.http.HttpServletRequest request)
          Return the request URI for the given request.
 java.lang.String getOriginatingServletPath(javax.servlet.http.HttpServletRequest request)
          Return the servlet path for the given request, detecting an include request URL if called within a RequestDispatcher include.
 java.lang.String getPathWithinApplication(javax.servlet.http.HttpServletRequest request)
          Return the path within the web application for the given request.
 java.lang.String getPathWithinServletMapping(javax.servlet.http.HttpServletRequest request)
          Return the path within the servlet mapping for the given request, i.e.
 java.lang.String getRequestUri(javax.servlet.http.HttpServletRequest request)
          Return the request URI for the given request, detecting an include request URL if called within a RequestDispatcher include.
 java.lang.String getServletPath(javax.servlet.http.HttpServletRequest request)
          Return the servlet path for the given request, regarding an include request URL if called within a RequestDispatcher include.
 void setAlwaysUseFullPath(boolean alwaysUseFullPath)
          Set if URL lookup should always use full path within current servlet context.
 void setDefaultEncoding(java.lang.String defaultEncoding)
          Set the default character encoding to use for URL decoding.
 void setUrlDecode(boolean urlDecode)
          Set if context path and request URI should be URL-decoded.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UrlPathHelper

public UrlPathHelper()
Method Detail

setAlwaysUseFullPath

public void setAlwaysUseFullPath(boolean alwaysUseFullPath)
Set if URL lookup should always use full path within current servlet context. Else, the path within the current servlet mapping is used if applicable (i.e. in the case of a ".../*" servlet mapping in web.xml). Default is "false".


setUrlDecode

public void setUrlDecode(boolean urlDecode)
Set if context path and request URI should be URL-decoded. Both are returned undecoded by the Servlet API, in contrast to the servlet path.

Uses either the request encoding or the default encoding according to the Servlet spec (ISO-8859-1).

Default is "true", as of Spring 2.5.

See Also:
getServletPath(javax.servlet.http.HttpServletRequest), getContextPath(javax.servlet.http.HttpServletRequest), getRequestUri(javax.servlet.http.HttpServletRequest), WebUtils.DEFAULT_CHARACTER_ENCODING, ServletRequest.getCharacterEncoding(), URLDecoder.decode(String, String)

setDefaultEncoding

public void setDefaultEncoding(java.lang.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 - the character encoding to use
See Also:
determineEncoding(javax.servlet.http.HttpServletRequest), ServletRequest.getCharacterEncoding(), ServletRequest.setCharacterEncoding(String), WebUtils.DEFAULT_CHARACTER_ENCODING

getDefaultEncoding

protected java.lang.String getDefaultEncoding()
Return the default character encoding to use for URL decoding.


getLookupPathForRequest

public java.lang.String getLookupPathForRequest(javax.servlet.http.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 - current HTTP request
Returns:
the lookup path
See Also:
getPathWithinApplication(javax.servlet.http.HttpServletRequest), getPathWithinServletMapping(javax.servlet.http.HttpServletRequest)

getPathWithinServletMapping

public java.lang.String getPathWithinServletMapping(javax.servlet.http.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.

Detects include request URL if called within a RequestDispatcher include.

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 - current HTTP request
Returns:
the path within the servlet mapping, or ""

getPathWithinApplication

public java.lang.String getPathWithinApplication(javax.servlet.http.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 - current HTTP request
Returns:
the path within the web application

getRequestUri

public java.lang.String getRequestUri(javax.servlet.http.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 - current HTTP request
Returns:
the request URI

getContextPath

public java.lang.String getContextPath(javax.servlet.http.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 - current HTTP request
Returns:
the context path

getServletPath

public java.lang.String getServletPath(javax.servlet.http.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 - current HTTP request
Returns:
the servlet path

getOriginatingRequestUri

public java.lang.String getOriginatingRequestUri(javax.servlet.http.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.


getOriginatingContextPath

public java.lang.String getOriginatingContextPath(javax.servlet.http.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 - current HTTP request
Returns:
the context path

getOriginatingServletPath

public java.lang.String getOriginatingServletPath(javax.servlet.http.HttpServletRequest request)
Return the servlet path for the given request, detecting an include request URL if called within a RequestDispatcher include.

Parameters:
request - current HTTP request
Returns:
the servlet path

getOriginatingQueryString

public java.lang.String getOriginatingQueryString(javax.servlet.http.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 - current HTTP request
Returns:
the query string

decodeRequestString

public java.lang.String decodeRequestString(javax.servlet.http.HttpServletRequest request,
                                            java.lang.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 - current HTTP request
source - the String to decode
Returns:
the decoded String
See Also:
WebUtils.DEFAULT_CHARACTER_ENCODING, ServletRequest.getCharacterEncoding(), URLDecoder.decode(String, String), URLDecoder.decode(String)

determineEncoding

protected java.lang.String determineEncoding(javax.servlet.http.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 - current HTTP request
Returns:
the encoding for the request (never null)
See Also:
ServletRequest.getCharacterEncoding(), setDefaultEncoding(java.lang.String)

decodePathVariables

public java.util.Map<java.lang.String,java.lang.String> decodePathVariables(javax.servlet.http.HttpServletRequest request,
                                                                            java.util.Map<java.lang.String,java.lang.String> vars)
Decode the given URI path variables via decodeRequestString(HttpServletRequest, 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).

Parameters:
request - current HTTP request
vars - URI variables extracted from the URL path
Returns:
the same Map or a new Map instance

Spring Framework