org.springframework.web.util
Class UrlPathHelper


public class UrlPathHelper

Helper class for URL path matching. Provides support for URL paths in ServletDispatcher includes, and support for URL decoding.

Used by AbstractUrlHandlerMapping and AbstractMethodNameResolver.

Author:
Juergen Hoeller
Since: 14.01.2004
See Also: AbstractUrlHandlerMapping, AbstractUrlMethodNameResolver

Field Summary
 final static StringDEFAULT_CHARACTER_ENCODING
          Default character encoding to use when request.getCharacterEncoding returns null, according to the Servlet spec.
 final static StringINCLUDE_CONTEXT_PATH_REQUEST_ATTRIBUTE
           
 final static StringINCLUDE_SERVLET_PATH_REQUEST_ATTRIBUTE
           
 final static StringINCLUDE_URI_REQUEST_ATTRIBUTE
          Standard servlet spec request attributes for include URI and paths.

Method Summary
 StringdecodeRequestString(ServletRequest request, String source)
          Decode the given source string with a URLEncoder.
 StringgetContextPath(HttpServletRequest request)
          Return the context path for the given request, regarding an include request URL if called within a RequestDispatcher include.
 StringgetLookupPathForRequest(HttpServletRequest request)
          Return the mapping lookup path for the given request, within the current servlet mapping if applicable, else within the web application.
 StringgetPathWithinApplication(HttpServletRequest request)
          Return the path within the web application for the given request.
 StringgetPathWithinServletMapping(HttpServletRequest request)
          Return the path within the servlet mapping for the given request, i.e.
 StringgetRequestUri(HttpServletRequest request)
          Return the request URI for the given request, regarding an include request URL if called within a RequestDispatcher include.
 StringgetServletPath(HttpServletRequest request)
          Return the servlet path for the given request, regarding an include request URL if called within a RequestDispatcher include.
 voidsetAlwaysUseFullPath(boolean alwaysUseFullPath)
          Set if URL lookup should always use full path within current servlet context.
 voidsetUrlDecode(boolean urlDecode)
          Set if context path and request URI should be URL-decoded.

Field Detail

DEFAULT_CHARACTER_ENCODING

public final static String DEFAULT_CHARACTER_ENCODING
Default character encoding to use when request.getCharacterEncoding returns null, according to the Servlet spec.
See Also:
javax.servlet.ServletRequest#getCharacterEncoding

INCLUDE_CONTEXT_PATH_REQUEST_ATTRIBUTE

public final static String INCLUDE_CONTEXT_PATH_REQUEST_ATTRIBUTE

INCLUDE_SERVLET_PATH_REQUEST_ATTRIBUTE

public final static String INCLUDE_SERVLET_PATH_REQUEST_ATTRIBUTE

INCLUDE_URI_REQUEST_ATTRIBUTE

public final static String INCLUDE_URI_REQUEST_ATTRIBUTE
Standard servlet spec request attributes for include URI and paths.

If included via a RequestDispatcher, the current resource will see the original request. Its own URI and paths are exposed as request attributes.

Method Detail

decodeRequestString

public String decodeRequestString(ServletRequest request, String source)
Decode the given source string with a URLEncoder. The encoding will be taken from the request, falling back to the default "ISO-8859-1".
Parameters:
request - current HTTP request
source - the String to decode
Returns: the decoded String
See Also:
DEFAULT_CHARACTER_ENCODING, javax.servlet.ServletRequest#getCharacterEncoding, URLDecoder

getContextPath

public String getContextPath(HttpServletRequest request)
Return the context path for the given request, regarding 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

getLookupPathForRequest

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.

Regards include request URL if called within a RequestDispatcher include.

Parameters:
request - current HTTP request
Returns: the lookup path
See Also:
getPathWithinApplication(HttpServletRequest), getPathWithinServletMapping(HttpServletRequest)

getPathWithinApplication

public String getPathWithinApplication(HttpServletRequest request)
Return the path within the web application for the given request.

Regards include request URL if called within a RequestDispatcher include.

Parameters:
request - current HTTP request
Returns: the path within the web application

getPathWithinServletMapping

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.

Regards 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 ""

getRequestUri

public String getRequestUri(HttpServletRequest request)
Return the request URI for the given request, regarding 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

getServletPath

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 - current HTTP request
Returns: the servlet path

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).

Note: Setting this to true requires J2SE 1.4, as J2SE 1.3's URLDecoder class does not offer a way to specify the encoding.

See Also:
getServletPath(HttpServletRequest), getContextPath(HttpServletRequest), getRequestUri(HttpServletRequest), DEFAULT_CHARACTER_ENCODING, javax.servlet.ServletRequest#getCharacterEncoding, java.net.URLDecoder.decode(java.lang.String,java.lang.String)

Association Links

to Class java.lang.String

Default character encoding to use when request.getCharacterEncoding returns null, according to the Servlet spec.

see javax.servlet.ServletRequest#getCharacterEncoding

to Class java.lang.String

Standard servlet spec request attributes for include URI and paths.

If included via a RequestDispatcher, the current resource will see the original request. Its own URI and paths are exposed as request attributes.

to Class java.lang.String

to Class java.lang.String