com.jalios.util
Class ServletUtil

java.lang.Object
  extended by com.jalios.util.ServletUtil

public class ServletUtil
extends Object

This class provides a set of static methods which perform various utility operations for Servlets.

Version:
$Revision: 22150 $
Author:
Olivier Dedieu

Nested Class Summary
static interface ServletUtil.ServletUtilAlternate
          This interface is used internally by JCMS to provide custom behavior of ServletUtil and should not be used elsewhere.
 
Field Summary
static String DEFAULT_DOMID_PREFIX
           
static String REVISION
           
static String UNIQUE_DOM_ID_COUNTER
           
static int URL_INIT_SIZE
          Buffer size used for when generating url using StrigBuffer (default is 200).
 
Method Summary
static void backupAttribute(javax.servlet.jsp.PageContext pageContext, String attribute)
          Backup request attribute to page scope
static String decodeURL(String str)
          Convenient method to avoid the need to specify "UTF-8" encoding and catching of exception in call to URLDecoder.decode(String, String)
Because URLDecoder.decode is quite slow, this methods caches the decoded url in a LRU containing 500 entries.
static String encodeURL(String str)
          Convenient method to avoid the need to specify "UTF-8" encoding and catching of exception in call to URLEncoder.encode(String, String).
static String escapeAmpersand(String str)
          Replace all occurence of singles "&" by "&" in the given string Use a regular expression to prevent other entity to be changed
static String generateUniqueDOMId(javax.servlet.http.HttpServletRequest request, String prefix)
          Generates and returns an id, unique for one request.
static SortedSet<String> generateUniqueDOMIdCollection(javax.servlet.http.HttpServletRequest request, String prefix, int size)
          Generates and returns a collection of unique id.
static String getAbsUrlWithRemovedParams(javax.servlet.http.HttpServletRequest request, String[] params)
          Returns an absolute URL with the given parameter removed
static String getAbsUrlWithUpdatedParam(javax.servlet.http.HttpServletRequest request, String name, String newValue)
          Returns an absolute URL with the given parameter added or updated
static String getAbsUrlWithUpdatedParams(javax.servlet.http.HttpServletRequest request, String[] names, String[] newValues)
          Returns an absolute URL with the given parameters added or updated
static String getAbsUrlWithUpdatedParams(javax.servlet.http.HttpServletRequest request, String[] names, String[] newValues, String removePattern)
          Returns an absolute URL with the given parameters added or updated
static String getAbsUrlWithUpdatedParams(javax.servlet.http.HttpServletRequest request, String[] names, String[] newValues, String[] removePatterns)
          Returns an absolute URL with the given parameters added or updated
static String getAbsUrlWithUpdatedParams(javax.servlet.http.HttpServletRequest request, String[] names, String[] newValues, String[] removePatterns, boolean escapeAmpersand)
          Returns an absolute URL with the given parameters added or updated
static ServletUtil.ServletUtilAlternate getAlternateImpl()
           
static String getBaseUrl(javax.servlet.http.HttpServletRequest request)
          Returns the base URL of the given request.
static String getContextPath(javax.servlet.http.HttpServletRequest request)
          Returns the portion of the request URI that indicates the context of the request.
static String getJsp(javax.servlet.http.HttpServletRequest request)
          Returns the requested JSP.
static Map<String,String[]> getOrderedParameterMap(javax.servlet.http.HttpServletRequest request)
          Returns a Map of the parameters of the request.
static Enumeration<String> getOrderedParameterNames(javax.servlet.http.HttpServletRequest request)
          Returns an Enumeration of String objects containing the names of the parameters contained in the request.
static String getPathInfo(javax.servlet.http.HttpServletRequest request)
          Returns any extra path information associated with the URL the client sent when it made this request.
static String getQueryString(javax.servlet.http.HttpServletRequest request, boolean escapeAmpersand)
          Return the queryString of the given request.
static String getResourcePath(javax.servlet.http.HttpServletRequest request)
          Returns the path of the requested resource, without leading '/'.
static String getServletPath(javax.servlet.http.HttpServletRequest request)
          Returns the part of the given request's URL that calls the servlet.
static String getUpdatedParams(javax.servlet.http.HttpServletRequest request, String[] names, String[] newValues)
           
static String getUpdatedParams(javax.servlet.http.HttpServletRequest request, String[] names, String[] newValues, String removePattern)
           
static String getUpdatedParams(javax.servlet.http.HttpServletRequest request, String[] names, String[] newValues, String[] removePatterns)
           
static String getUpdatedParams(javax.servlet.http.HttpServletRequest request, String[] names, String[] newValues, String[] removePatterns, boolean escapeAmpersand)
           
static String getUrl(javax.servlet.http.HttpServletRequest request)
          Returns the absolute URL of the given request.
static String getUrl(javax.servlet.http.HttpServletRequest request, boolean escapeAmpersand)
          Returns the absolute URL of the given request.
static String getUrlWithAddedParam(javax.servlet.http.HttpServletRequest request, String name, String value)
          Returns a relative URL with the given parameter added even if already present (in case of an array of parameters having the same name).
static String getUrlWithAddedParam(javax.servlet.http.HttpServletRequest request, String name, String value, boolean escapeAmpersand)
          Returns a relative URL with the given parameter added even if already present (in case of an array of parameters having the same name).
static String getUrlWithRemovedParams(javax.servlet.http.HttpServletRequest request, String[] params, boolean escapeAmpersand)
          Returns a relative URL with the given parameter removed
static String getUrlWithUpdatedParam(javax.servlet.http.HttpServletRequest request, String name, String newValue)
          Returns a relative URL with the given parameter added or updated
static String getUrlWithUpdatedParams(javax.servlet.http.HttpServletRequest request, String[] names, String[] newValues)
          Returns a relative URL with the given parameters added or updated
static String getUrlWithUpdatedParams(javax.servlet.http.HttpServletRequest request, String[] names, String[] newValues, String removePattern)
          Returns a relative URL with the given parameters added or updated
static String getUrlWithUpdatedParams(javax.servlet.http.HttpServletRequest request, String[] names, String[] newValues, String[] removePatterns)
          Returns a relative URL with the given parameters added or updated
static String getUrlWithUpdatedParams(javax.servlet.http.HttpServletRequest request, String[] names, String[] newValues, String[] removePatterns, boolean escapeAmpersand)
          Returns a relative URL with the given parameters added or updated
static String getUrlWithUpdatedParams(String resourcePath, Map<String,String[]> parameterMap, String[] names, String[] newValues, String[] removePatterns, boolean escapeAmpersand)
          Returns a relative URL with the given parameters added or updated
static String removeTrailingChars(String url)
          Return a String without the trailing chars.
static void restoreAttribute(javax.servlet.jsp.PageContext pageContext, String attribute)
          Restore request attribute from page scope
static void setAlternateImpl(ServletUtil.ServletUtilAlternate alternate)
          Define the ServletUtil.ServletUtilAlternate class to use to provide custom behavior for ServletUtil methods.
static String unescapeAmpersand(String str)
          Replace all occurence of "&amp;" by "&" in the given string
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REVISION

public static final String REVISION
See Also:
Constant Field Values

URL_INIT_SIZE

public static final int URL_INIT_SIZE
Buffer size used for when generating url using StrigBuffer (default is 200).

See Also:
Constant Field Values

DEFAULT_DOMID_PREFIX

public static final String DEFAULT_DOMID_PREFIX
See Also:
Constant Field Values

UNIQUE_DOM_ID_COUNTER

public static final String UNIQUE_DOM_ID_COUNTER
See Also:
Constant Field Values
Method Detail

setAlternateImpl

public static void setAlternateImpl(ServletUtil.ServletUtilAlternate alternate)
Define the ServletUtil.ServletUtilAlternate class to use to provide custom behavior for ServletUtil methods. Warning: calling this override any previous alternate class as only one alternate class can be use at the same time.

Parameters:
alternate - the class to use

getAlternateImpl

public static ServletUtil.ServletUtilAlternate getAlternateImpl()
Returns:
the current alternate class being registered in ServletUtil to provide a custom behavior.

backupAttribute

public static void backupAttribute(javax.servlet.jsp.PageContext pageContext,
                                   String attribute)
Backup request attribute to page scope


restoreAttribute

public static void restoreAttribute(javax.servlet.jsp.PageContext pageContext,
                                    String attribute)
Restore request attribute from page scope

Parameters:
pageContext -
attribute -
Since:
jcms-4.0

getBaseUrl

public static String getBaseUrl(javax.servlet.http.HttpServletRequest request)
Returns the base URL of the given request. Rebuild this URL from the request attributes (serverName, serverPort and contextPath). The base URL contains a trailing '/'. The return value may be forced by request's attribute "baseUrl". For example, if the requested url is http://mysite.com:8080/dir/webapp/subdir/my.jsp?param=value returns http://mysite.com:8080/dir/webapp/

Parameters:
request - the request
Returns:
the base URL of the request

getContextPath

public static String getContextPath(javax.servlet.http.HttpServletRequest request)
Returns the portion of the request URI that indicates the context of the request. The context path always comes first in a request URI. The path starts with a "/" character but does not end with a "/" character. For servlets in the default (root) context, this method returns "". May be forced by request's attribute "contextPath" (usefull when using reverseProxy).

Parameters:
request - the request
Returns:
the contextPath of the request

getJsp

public static String getJsp(javax.servlet.http.HttpServletRequest request)
Returns the requested JSP.
e.g: For the URL http://host/webapp/subdir/my.jsp?x=y, it returns my.jsp.
Computed from the value returned by getServletPath(HttpServletRequest), thus if the jsp is accessed through a RequestDispatcher forward, this method will returned the first servlet accessed.

Parameters:
request - the request
Returns:
the requested JSP
Since:
jcms-4.0

getResourcePath

public static String getResourcePath(javax.servlet.http.HttpServletRequest request)
Returns the path of the requested resource, without leading '/'.
Examples:
"http://host/mywebapp/subdir/my.jsp?x=y" ==> "subdir/my.jsp"
"http://host/mywebapp/subdir/images/test.gif" ==> "subdir/images/test.gif"
"http://host/mywebapp/MyServlet/action?x=y" ==> "MyServlet/action"

Equivalent to the requestURI minus the contextPath. From section 4.4 of the Java Servlet Specification 2.3: The following equation is always true: requestURI = contextPath + servletPath + pathInfo
The returned value is computed from the value returned by getServletPath(HttpServletRequest) and getPathInfo(HttpServletRequest), thus if the method is called from a servlet accessed through a RequestDispatcher forward, this method will returned the value for the first servlet accessed.

Parameters:
request - the request
Returns:
a String with the requested resource (jsp, static file, servlet)
Since:
jcms-4.0

getServletPath

public static String getServletPath(javax.servlet.http.HttpServletRequest request)
Returns the part of the given request's URL that calls the servlet. This path starts with a "/" character and includes either the servlet name or a path to the servlet, but does not include any extra path.

This method will check for attribute "javax.servlet.forward.servlet_path" that may have been added when calling a servlet using the forward method of a RequestDispatcher.

Parameters:
request - the request of which to retrieve the servlet path
Returns:
a String containing the name or path of the servlet being called, as specified in the request URL, decoded, or an empty string if the servlet used to process the request is matched using the "/*" pattern.

getPathInfo

public static String getPathInfo(javax.servlet.http.HttpServletRequest request)
Returns any extra path information associated with the URL the client sent when it made this request. The extra path information follows the servlet path but precedes the query string and will start with a "/" character.

This method returns null if there was no extra path information.
Same as the value of the CGI variable PATH_INFO.

This method will check for attribute "javax.servlet.forward.path_info" that may have been added when calling a servlet using the forward method of a RequestDispatcher.

Parameters:
request - the request of which to retrieve the servlet path
Returns:
a String, decoded by the web container, specifying extra path information that comes after the servlet path but before the query string in the request URL; or null if the URL does not have any extra path information

getUrl

public static String getUrl(javax.servlet.http.HttpServletRequest request)
Returns the absolute URL of the given request. Rebuild this URL from the request attributes (baseUrl + servletPath + queryString).

Parameters:
request - the request
Returns:
the URL of the request

getUrl

public static String getUrl(javax.servlet.http.HttpServletRequest request,
                            boolean escapeAmpersand)
Returns the absolute URL of the given request. Rebuild this URL from the request attributes (baseUrl + servletPath + queryString).

Parameters:
request - the request
escapeAmpersand - will use "&amp;" if true, "&" if false
Returns:
the URL of the request

getQueryString

public static String getQueryString(javax.servlet.http.HttpServletRequest request,
                                    boolean escapeAmpersand)
Return the queryString of the given request.

The query String does not contain the "?". As stated in the rfc 1738, section 3.3. : In an URL with the form: http://{host}:{port}/{path}?{searchpart} {searchpart} is a query string.

This methods differs from the method HttpServletRequest.getQueryString() because it rebuilds the querystring using all parameters of the given request.
Thus, calling this method in a servlet called through RequestDispatcher.forward will gives you all the parameters (from the "original" servlet request and from the forwarded servlet).
For example, if a servlet 1 accessed using "/servlet1/view/id?a=b&c=d" forward to a servlet 2 "/servlet2?view=id". The computed querystring will be "view=id&a=b&c=d".

Parameters:
request - the HttpRequest
escapeAmpersand - will use "&amp;" if true, "&" if false
Returns:
the queryString of the given request.

getUrlWithAddedParam

public static String getUrlWithAddedParam(javax.servlet.http.HttpServletRequest request,
                                          String name,
                                          String value)
Returns a relative URL with the given parameter added even if already present (in case of an array of parameters having the same name).

Parameters:
request -
name -
value -
Returns:
the new URL

getUrlWithAddedParam

public static String getUrlWithAddedParam(javax.servlet.http.HttpServletRequest request,
                                          String name,
                                          String value,
                                          boolean escapeAmpersand)
Returns a relative URL with the given parameter added even if already present (in case of an array of parameters having the same name).

Parameters:
request -
name -
value -
escapeAmpersand - will use "&amp;" if true, "&" if false
Returns:
the new URL

getUrlWithRemovedParams

public static String getUrlWithRemovedParams(javax.servlet.http.HttpServletRequest request,
                                             String[] params,
                                             boolean escapeAmpersand)
Returns a relative URL with the given parameter removed

Parameters:
request - the HttpRequest
params - the name of the parameters to be removed
escapeAmpersand - will use "&amp;" if true, "&" if false
Returns:
the new URL
Since:
jcms-5.5.0

getUrlWithUpdatedParam

public static String getUrlWithUpdatedParam(javax.servlet.http.HttpServletRequest request,
                                            String name,
                                            String newValue)
Returns a relative URL with the given parameter added or updated

Parameters:
request - the HttpRequest
name - the name of the parameter
newValue - the value of the parameter (if newValue is null the param is removed)
Returns:
the new URL

getAbsUrlWithRemovedParams

public static String getAbsUrlWithRemovedParams(javax.servlet.http.HttpServletRequest request,
                                                String[] params)
Returns an absolute URL with the given parameter removed

Parameters:
request - the HttpRequest
params - the name of the parameters to be removed
Returns:
the new URL
Since:
jcms-5.5.0

getAbsUrlWithUpdatedParam

public static String getAbsUrlWithUpdatedParam(javax.servlet.http.HttpServletRequest request,
                                               String name,
                                               String newValue)
Returns an absolute URL with the given parameter added or updated

Parameters:
request - the HttpRequest
name - the name of the parameter
newValue - the value of the parameter (if newValue is null the param is removed)
Returns:
the new URL

getUrlWithUpdatedParams

public static String getUrlWithUpdatedParams(javax.servlet.http.HttpServletRequest request,
                                             String[] names,
                                             String[] newValues)
Returns a relative URL with the given parameters added or updated

Parameters:
request - the HttpRequest
names - an array of String which contains the name of the parameters
newValues - an array of String which contains the values of the parameters (if newValue[i] is null the names[i] parameter is removed)
Returns:
the new URL

getAbsUrlWithUpdatedParams

public static String getAbsUrlWithUpdatedParams(javax.servlet.http.HttpServletRequest request,
                                                String[] names,
                                                String[] newValues)
Returns an absolute URL with the given parameters added or updated

Parameters:
request - the HttpRequest
names - an array of String which contains the name of the parameters
newValues - an array of String which contains the values of the parameters (if newValue[i] is null the names[i] parameter is removed)
Returns:
the new URL

getUrlWithUpdatedParams

public static String getUrlWithUpdatedParams(javax.servlet.http.HttpServletRequest request,
                                             String[] names,
                                             String[] newValues,
                                             String removePattern)
Returns a relative URL with the given parameters added or updated

Parameters:
request - the HttpRequest
names - an array of String which contains the name of the parameters
newValues - an array of String which contains the values of the parameters (if newValue[i] is null the names[i] parameter is removed)
removePattern - any parameter name containing the given String will be removed from the parameters' list
Returns:
the new URL

getUrlWithUpdatedParams

public static String getUrlWithUpdatedParams(javax.servlet.http.HttpServletRequest request,
                                             String[] names,
                                             String[] newValues,
                                             String[] removePatterns)
Returns a relative URL with the given parameters added or updated

Parameters:
request - the HttpRequest
names - an array of String which contains the name of the parameters
newValues - an array of String which contains the values of the parameters (if newValue[i] is null the names[i] parameter is removed)
removePatterns - any parameter name containing one of the given Strings will be removed from the parameters' list
Returns:
the new URL

getUrlWithUpdatedParams

public static String getUrlWithUpdatedParams(javax.servlet.http.HttpServletRequest request,
                                             String[] names,
                                             String[] newValues,
                                             String[] removePatterns,
                                             boolean escapeAmpersand)
Returns a relative URL with the given parameters added or updated

Parameters:
request - the HttpRequest
names - an array of String which contains the name of the parameters
newValues - an array of String which contains the values of the parameters (if newValue[i] is null the names[i] parameter is removed)
removePatterns - any parameter name containing one of the given Strings will be removed from the parameters' list
escapeAmpersand - will use "&amp;" if true, "&" if false
Returns:
the new URL

getUrlWithUpdatedParams

public static String getUrlWithUpdatedParams(String resourcePath,
                                             Map<String,String[]> parameterMap,
                                             String[] names,
                                             String[] newValues,
                                             String[] removePatterns,
                                             boolean escapeAmpersand)
Returns a relative URL with the given parameters added or updated

Parameters:
resourcePath - the HttpRequest resource path
parameterMap - the HttpRequest parameter map
names - an array of String which contains the name of the parameters
newValues - an array of String which contains the values of the parameters (if newValue[i] is null the names[i] parameter is removed)
removePatterns - any parameter name containing one of the given Strings will be removed from the parameters' list
escapeAmpersand - will use "&amp;" if true, "&" if false
Returns:
the new URL

getAbsUrlWithUpdatedParams

public static String getAbsUrlWithUpdatedParams(javax.servlet.http.HttpServletRequest request,
                                                String[] names,
                                                String[] newValues,
                                                String removePattern)
Returns an absolute URL with the given parameters added or updated

Parameters:
request - the HttpRequest
names - an array of String which contains the name of the parameters
newValues - an array of String which contains the values of the parameters (if newValue[i] is null the names[i] parameter is removed)
removePattern - any parameter name containing the given String will be removed from the parameters' list
Returns:
the new URL

getAbsUrlWithUpdatedParams

public static String getAbsUrlWithUpdatedParams(javax.servlet.http.HttpServletRequest request,
                                                String[] names,
                                                String[] newValues,
                                                String[] removePatterns)
Returns an absolute URL with the given parameters added or updated

Parameters:
request - the HttpRequest
names - an array of String which contains the name of the parameters
newValues - an array of String which contains the values of the parameters (if newValue[i] is null the names[i] parameter is removed)
removePatterns - any parameter name containing one of the given Strings will be removed from the parameters' list
Returns:
the new URL

getAbsUrlWithUpdatedParams

public static String getAbsUrlWithUpdatedParams(javax.servlet.http.HttpServletRequest request,
                                                String[] names,
                                                String[] newValues,
                                                String[] removePatterns,
                                                boolean escapeAmpersand)
Returns an absolute URL with the given parameters added or updated

Parameters:
request - the HttpRequest
names - an array of String which contains the name of the parameters
newValues - an array of String which contains the values of the parameters (if newValue[i] is null the names[i] parameter is removed)
removePatterns - any parameter name containing one of the given Strings will be removed from the parameters' list
escapeAmpersand - will use "&amp; true, "&" if false
Returns:
the new URL

getUpdatedParams

public static String getUpdatedParams(javax.servlet.http.HttpServletRequest request,
                                      String[] names,
                                      String[] newValues)

getUpdatedParams

public static String getUpdatedParams(javax.servlet.http.HttpServletRequest request,
                                      String[] names,
                                      String[] newValues,
                                      String removePattern)

getUpdatedParams

public static String getUpdatedParams(javax.servlet.http.HttpServletRequest request,
                                      String[] names,
                                      String[] newValues,
                                      String[] removePatterns)

getUpdatedParams

public static String getUpdatedParams(javax.servlet.http.HttpServletRequest request,
                                      String[] names,
                                      String[] newValues,
                                      String[] removePatterns,
                                      boolean escapeAmpersand)

removeTrailingChars

public static String removeTrailingChars(String url)
Return a String without the trailing chars. I.e. '&' or '?'.

Parameters:
url - an URL as String
Returns:
the URL stripped of trailing chars '&' or '?' if any

unescapeAmpersand

public static String unescapeAmpersand(String str)
Replace all occurence of "&amp;" by "&" in the given string

Parameters:
str - the string in which to find &amp;
Returns:
a new string with all replacement being done.

escapeAmpersand

public static String escapeAmpersand(String str)
Replace all occurence of singles "&" by "&amp;" in the given string Use a regular expression to prevent other entity to be changed

Parameters:
str - the string in which to find &
Returns:
a new string with all replacement being done.

decodeURL

public static String decodeURL(String str)
Convenient method to avoid the need to specify "UTF-8" encoding and catching of exception in call to URLDecoder.decode(String, String)
Because URLDecoder.decode is quite slow, this methods caches the decoded url in a LRU containing 500 entries.

Parameters:
str - the string to url decode
Returns:
the decoded URL
See Also:
URLDecoder.decode(String, String)

encodeURL

public static String encodeURL(String str)
Convenient method to avoid the need to specify "UTF-8" encoding and catching of exception in call to URLEncoder.encode(String, String).
Because URLEncoder.encode is quite slow, this methods caches the encoded url in a LRU containing 2000 entries.

Parameters:
str - the string to url encode
Returns:
the encoded URL
See Also:
URLEncoder.encode(String, String)

getOrderedParameterMap

public static Map<String,String[]> getOrderedParameterMap(javax.servlet.http.HttpServletRequest request)
Returns a Map of the parameters of the request. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.

Parameters:
request - the request
Returns:
an immutable java.util.Map containing parameter names as keys and parameter values as map values. The keys in the parameter map are of type String. The values in the parameter map are of type String array. Parameter are order in the way they were sent to the server

getOrderedParameterNames

public static Enumeration<String> getOrderedParameterNames(javax.servlet.http.HttpServletRequest request)
Returns an Enumeration of String objects containing the names of the parameters contained in the request. If the request has no parameters, the method returns an empty Enumeration.

Parameters:
request - the request
Returns:
an Enumeration of String objects, each String containing the name of a request parameter; or an empty Enumeration if the request has no parameters. Parameter names are order in the way they were sent to the server

generateUniqueDOMId

public static String generateUniqueDOMId(javax.servlet.http.HttpServletRequest request,
                                         String prefix)
Generates and returns an id, unique for one request.

This id embed the current date, not to be equals to an id, already used in a cache. You may use your own prefix, to set a human readable part in the id.

As of current implementation, the returned id will match the pattern required by the scriptaculous javascript API, "{prefix}_{identifier}" : http://wiki.script.aculo.us/scriptaculous/show/Sortable.serialize

Parameters:
request - the current HttpServletRequest. Required, must be not null.
prefix - a custom prefix, if null the default prefix "generateddomid" will be used. Use only alphanumeric characters.
Returns:
a unique identifier (or null if request is null).

generateUniqueDOMIdCollection

public static SortedSet<String> generateUniqueDOMIdCollection(javax.servlet.http.HttpServletRequest request,
                                                              String prefix,
                                                              int size)
Generates and returns a collection of unique id.

Parameters:
request - the current HttpServletRequest. Required, must be not null.
prefix - a custom prefix, if null the default prefix "generateddomid" will be used. Use only alphanumeric characters.
size - Size of the Set to return.
Returns:
a SortedSet of unique identifiers (or null if request is null).


Copyright © 2001-2007 Jalios SA. All Rights Reserved.