org.riotfamily.common.web.util
Class ServletUtils

java.lang.Object
  extended by org.riotfamily.common.web.util.ServletUtils

public final class ServletUtils
extends Object


Field Summary
static String INCLUDE_URI_REQUEST_ATTRIBUTE
           
static String REFERER_HEADER
           
static String REQUESTED_WITH_HEADER
           
static String SCHEME_HTTP
           
static String SCHEME_HTTPS
           
static String USER_AGENT_HEADER
           
static String VALID_SCHEME_CHARS
          Valid characters in a scheme.
static String XML_HTTP_REQUEST
           
 
Method Summary
static String addParameter(String url, String name, String value)
          Returns an URL with the given parameter added to the given URL's query string.
static String addRequestParameters(String url, HttpServletRequest request)
          Returns an URL with all of the given request's parameters added to the given URL's query string.
static String addServletMapping(String path, HttpServletRequest request)
          Adds the mapping of the servlet that is mapped to the given request to the path.
static void appendParameter(StringBuffer url, String name, String value)
          Appends the given parameter to the given URL's query string.
static void appendRequestParameters(StringBuffer url, HttpServletRequest request)
          Appends all of the given request's parameters to the given URL's query string.
static StringBuffer getAbsoluteUrlPrefix(HttpServletRequest request)
          Returns a StringBuffer containing an URL with the protocol, hostname and port (unless it's the protocol's default port) of the given request.
static String getHost(String uri)
          Returns the host of the given URI.
static String getOriginatingContextPath(HttpServletRequest request)
          Return the context path for the given request, detecting an include request URL if called within a RequestDispatcher include.
static String getOriginatingPathWithinApplication(HttpServletRequest request)
          Return the path within the web application for the given request.
static String getOriginatingPathWithinServletMapping(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.
static String getOriginatingPathWithoutServletMapping(HttpServletRequest request)
          Returns the lookup-path for a given request.
static String getOriginatingRequestUri(HttpServletRequest request)
          Return the request URI for root of the given request.
static String getOriginatingServletPath(HttpServletRequest request)
           
static String getPath(String uri)
          Returns the path of the given URI.
static String getPathWithinApplication(HttpServletRequest request)
          Return the path within the web application for the given request.
static String getPathWithoutServletMapping(HttpServletRequest request)
          Returns the lookup-path for a given request.
static String getReferer(HttpServletRequest request)
          Convenience method to get the Referer header.
static 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 getRequestUrlWithQueryString(HttpServletRequest request)
           
static Long getRequiredLongAttribute(HttpServletRequest request, String name)
           
static String getRequiredStringAttribute(HttpServletRequest request, String name)
           
static String getRootPath(HttpServletRequest request)
          Returns a String consisting of the context-path and the servlet-prefix for the given request.
static String getServerNameAndPort(HttpServletRequest request)
          Returns the serverName and port (if applicable) for the given request.
static String getServletMapping(String servletName, ServletContext servletContext)
          Parses the web.xml deployment descriptor and returns the url-pattern for the given servlet-name, or null if no mapping is found.
static String getServletPrefix(HttpServletRequest request)
          Returns the servlet-mapping prefix for the given request or an empty String if the servlet is mapped by a suffix.
static String getServletSuffix(HttpServletRequest request)
          Returns the servlet-mapping suffix for the given request or an empty String if the servlet is mapped by a prefix.
static Map<String,String> getSingularParameterMap(HttpServletRequest request)
          Returns a map of request parameters.
static String getUserAgent(HttpServletRequest request)
          Convenience method to get the USER_AGENT_HEADER header.
static boolean isAbsoluteUrl(String url)
          Returns true if our current URL is absolute, false otherwise.
static boolean isDirectRequest(HttpServletRequest request)
          Returns whether the given request is a direct request, i.e. not dispatched by a RequestDispatcher.
static boolean isHttpUrl(String url)
           
static boolean isMultipartRequest(HttpServletRequest request)
           
static boolean isXmlHttpRequest(HttpServletRequest request)
          Returns whether the X-Requested-With header is set to XMLHttpRequest as done by prototype.js.
static String resolveAndEncodeUrl(String url, HttpServletRequest request, HttpServletResponse response)
           
static String resolveToAbsoluteUrl(String url, HttpServletRequest request)
           
static String resolveUrl(String url, HttpServletRequest request)
           
static void restoreAttributes(HttpServletRequest request, Map<String,Object> attributesSnapshot)
          Restores request attributes from the given map.
static void setCacheHeaders(HttpServletResponse response, String period)
          Sets Expires and Cache-Control headers to allow caching for the given period.
static void setFarFutureExpiresHeader(HttpServletResponse response)
          Sets an far future Expires header.
static void setNoCacheHeaders(HttpServletResponse response)
          Sets Pragma, Expires and Cache-Control headers to prevent caching.
static String setParameter(String url, String name, String value)
          This method tries to replace the given parameter's value in the given URL's query string with the given new value or adds the parameter if it is not yet contained.
static Map<String,Object> takeAttributesSnapshot(HttpServletRequest request)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INCLUDE_URI_REQUEST_ATTRIBUTE

public static final String INCLUDE_URI_REQUEST_ATTRIBUTE
See Also:
Constant Field Values

REFERER_HEADER

public static final String REFERER_HEADER
See Also:
Constant Field Values

USER_AGENT_HEADER

public static final String USER_AGENT_HEADER
See Also:
Constant Field Values

REQUESTED_WITH_HEADER

public static final String REQUESTED_WITH_HEADER
See Also:
Constant Field Values

XML_HTTP_REQUEST

public static final String XML_HTTP_REQUEST
See Also:
Constant Field Values

SCHEME_HTTP

public static final String SCHEME_HTTP
See Also:
Constant Field Values

SCHEME_HTTPS

public static final String SCHEME_HTTPS
See Also:
Constant Field Values

VALID_SCHEME_CHARS

public static final String VALID_SCHEME_CHARS

Valid characters in a scheme.

RFC 1738 says the following:

Scheme names consist of a sequence of characters. The lower case letters "a"--"z", digits, and the characters plus ("+"), period ("."), and hyphen ("-") are allowed. For resiliency, programs interpreting URLs should treat upper case letters as equivalent to lower case in scheme names (e.g., allow "HTTP" as well as "http").

We treat as absolute any URL that begins with such a scheme name, followed by a colon.

See Also:
Constant Field Values
Method Detail

getOriginatingContextPath

public static String getOriginatingContextPath(HttpServletRequest request)
Return the context path for the given request, detecting an include request URL if called within a RequestDispatcher include.

See Also:
UrlPathHelper.getOriginatingContextPath(HttpServletRequest)

getRequestUri

public static String getRequestUri(HttpServletRequest request)
Return the request URI for the given request, detecting an include request URL if called within a RequestDispatcher include.

See Also:
UrlPathHelper.getRequestUri(HttpServletRequest)

getOriginatingRequestUri

public static String getOriginatingRequestUri(HttpServletRequest request)
Return the request URI for root of the given request. If this is a forwarded request, correctly resolves to the request URI of the original request.

See Also:
UrlPathHelper.getOriginatingRequestUri(HttpServletRequest)

getOriginatingServletPath

public static String getOriginatingServletPath(HttpServletRequest request)

getPathWithinApplication

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

See Also:
UrlPathHelper.getPathWithinApplication(HttpServletRequest)

getOriginatingPathWithinApplication

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

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

getOriginatingPathWithinServletMapping

public static String getOriginatingPathWithinServletMapping(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.

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

getPathWithoutServletMapping

public static String getPathWithoutServletMapping(HttpServletRequest request)
Returns the lookup-path for a given request. This is either the path within the servlet-mapping (in case of a prefix mapping) or the path within the application without the trailing suffix (in case of a suffix mapping).


getOriginatingPathWithoutServletMapping

public static String getOriginatingPathWithoutServletMapping(HttpServletRequest request)
Returns the lookup-path for a given request. This is either the path within the servlet-mapping (in case of a prefix mapping) or the path within the application without the trailing suffix (in case of a suffix mapping).


getServletPrefix

public static String getServletPrefix(HttpServletRequest request)
Returns the servlet-mapping prefix for the given request or an empty String if the servlet is mapped by a suffix.


getServletSuffix

public static String getServletSuffix(HttpServletRequest request)
Returns the servlet-mapping suffix for the given request or an empty String if the servlet is mapped by a prefix.


addServletMapping

public static String addServletMapping(String path,
                                       HttpServletRequest request)
Adds the mapping of the servlet that is mapped to the given request to the path.


getRootPath

public static String getRootPath(HttpServletRequest request)
Returns a String consisting of the context-path and the servlet-prefix for the given request. The String will always end with a slash.


isAbsoluteUrl

public static boolean isAbsoluteUrl(String url)
Returns true if our current URL is absolute, false otherwise.


isHttpUrl

public static boolean isHttpUrl(String url)
Since:
6.4

resolveUrl

public static String resolveUrl(String url,
                                HttpServletRequest request)

resolveAndEncodeUrl

public static String resolveAndEncodeUrl(String url,
                                         HttpServletRequest request,
                                         HttpServletResponse response)

resolveToAbsoluteUrl

public static String resolveToAbsoluteUrl(String url,
                                          HttpServletRequest request)

takeAttributesSnapshot

public static Map<String,Object> takeAttributesSnapshot(HttpServletRequest request)

restoreAttributes

public static void restoreAttributes(HttpServletRequest request,
                                     Map<String,Object> attributesSnapshot)
Restores request attributes from the given map.


getSingularParameterMap

public static Map<String,String> getSingularParameterMap(HttpServletRequest request)
Returns a map of request parameters. Unlike ServletRequest.getParameterMap() this method returns Strings instead of String arrays. When more than one parameter with the same name is present, only the first value is put into the map.


getHost

public static String getHost(String uri)
Returns the host of the given URI. Uses URI internally to parse the given String. If the given string violates RFC 2396 null will be returned.


getPath

public static String getPath(String uri)
Returns the path of the given URI. Uses URI internally to parse the given String. If the given string violates RFC 2396 null will be returned.


getAbsoluteUrlPrefix

public static StringBuffer getAbsoluteUrlPrefix(HttpServletRequest request)
Returns a StringBuffer containing an URL with the protocol, hostname and port (unless it's the protocol's default port) of the given request.


getServerNameAndPort

public static String getServerNameAndPort(HttpServletRequest request)
Returns the serverName and port (if applicable) for the given request.


getReferer

public static String getReferer(HttpServletRequest request)
Convenience method to get the Referer header.


getUserAgent

public static String getUserAgent(HttpServletRequest request)
Convenience method to get the USER_AGENT_HEADER header.


isXmlHttpRequest

public static boolean isXmlHttpRequest(HttpServletRequest request)
Returns whether the X-Requested-With header is set to XMLHttpRequest as done by prototype.js.


isMultipartRequest

public static boolean isMultipartRequest(HttpServletRequest request)

isDirectRequest

public static boolean isDirectRequest(HttpServletRequest request)
Returns whether the given request is a direct request, i.e. not dispatched by a RequestDispatcher.


setNoCacheHeaders

public static void setNoCacheHeaders(HttpServletResponse response)
Sets Pragma, Expires and Cache-Control headers to prevent caching.

Since:
6.4

setCacheHeaders

public static void setCacheHeaders(HttpServletResponse response,
                                   String period)
Sets Expires and Cache-Control headers to allow caching for the given period.

Since:
6.5
See Also:
FormatUtils.parseMillis(String)

setFarFutureExpiresHeader

public static void setFarFutureExpiresHeader(HttpServletResponse response)
Sets an far future Expires header.


getServletMapping

public static String getServletMapping(String servletName,
                                       ServletContext servletContext)
Parses the web.xml deployment descriptor and returns the url-pattern for the given servlet-name, or null if no mapping is found.

Since:
6.4

setParameter

public static String setParameter(String url,
                                  String name,
                                  String value)
This method tries to replace the given parameter's value in the given URL's query string with the given new value or adds the parameter if it is not yet contained. The modified URL then is returned.


addParameter

public static String addParameter(String url,
                                  String name,
                                  String value)
Returns an URL with the given parameter added to the given URL's query string.


appendParameter

public static void appendParameter(StringBuffer url,
                                   String name,
                                   String value)
Appends the given parameter to the given URL's query string.


addRequestParameters

public static String addRequestParameters(String url,
                                          HttpServletRequest request)
Returns an URL with all of the given request's parameters added to the given URL's query string.


appendRequestParameters

public static void appendRequestParameters(StringBuffer url,
                                           HttpServletRequest request)
Appends all of the given request's parameters to the given URL's query string.


getRequestUrlWithQueryString

public static String getRequestUrlWithQueryString(HttpServletRequest request)

getRequiredStringAttribute

public static String getRequiredStringAttribute(HttpServletRequest request,
                                                String name)

getRequiredLongAttribute

public static Long getRequiredLongAttribute(HttpServletRequest request,
                                            String name)