Class ServletUtil
- Author:
- Olivier Dedieu
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceThis interface is used internally by JCMS to provide custom behavior of ServletUtil and should not be used elsewhere. -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic voidbackupAttribute(javax.servlet.jsp.PageContext pageContext, String attribute) Backup request attribute to page scopestatic StringConvenient method to avoid the need to specify "UTF-8" encoding and catching of exception in call toURLDecoder.decode(String, String)
Because URLDecoder.decode is quite slow, this methods caches the decoded url in a LRU containing 500 entries.static StringConvenient method to avoid the need to specify "UTF-8" encoding and catching of exception in call toURLEncoder.encode(String, String).static StringescapeAmpersand(String str) Replace all occurrence of singles "&" by "&" in the given string Use a regular expression to prevent other entity to be changedstatic StringgenerateUniqueDOMId(javax.servlet.http.HttpServletRequest request, String prefix) Generates and returns an id, unique for one request.generateUniqueDOMIdCollection(javax.servlet.http.HttpServletRequest request, String prefix, int size) Generates and returns a collection of unique id.static StringgetAbsUrlWithRemovedParams(javax.servlet.http.HttpServletRequest request, String[] params) Returns an absolute URL with the given parameter removedstatic StringgetAbsUrlWithUpdatedParam(javax.servlet.http.HttpServletRequest request, String name, String newValue) Returns an absolute URL with the given parameter added or updatedstatic StringgetAbsUrlWithUpdatedParams(javax.servlet.http.HttpServletRequest request, String[] names, String[] newValues) Returns an absolute URL with the given parameters added or updatedstatic StringgetAbsUrlWithUpdatedParams(javax.servlet.http.HttpServletRequest request, String[] names, String[] newValues, String removePattern) Returns an absolute URL with the given parameters added or updatedstatic StringgetAbsUrlWithUpdatedParams(javax.servlet.http.HttpServletRequest request, String[] names, String[] newValues, String[] removePatterns) Returns an absolute URL with the given parameters added or updatedstatic StringgetAbsUrlWithUpdatedParams(javax.servlet.http.HttpServletRequest request, String[] names, String[] newValues, String[] removePatterns, boolean escapeAmpersand) Returns an absolute URL with the given parameters added or updatedstatic StringgetBaseUrl(javax.servlet.http.HttpServletRequest request) Returns the base URL of the given request.static StringgetContextPath(javax.servlet.http.HttpServletRequest request) Returns the portion of the request URI that indicates the context of the request.static StringgetJsp(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.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 StringgetOriginalUrl(javax.servlet.http.HttpServletRequest request) When invoked during an ajax request, this method returns the absolute URL of the original request.static StringgetPathInfo(javax.servlet.http.HttpServletRequest request) Returns any extra path information associated with the URL the client sent when it made this request.static StringgetQueryString(javax.servlet.http.HttpServletRequest request, boolean escapeAmpersand) Return the queryString of the given request.
The query String does not contain the "?".static StringgetRequestId(javax.servlet.http.HttpServletRequest request) Return unique identifier for the specified request, constant during all the duration of this request and unique accross application lifecyle.static StringgetResourcePath(javax.servlet.http.HttpServletRequest request) Returns the path of the requested resource, without leading '/'.static StringgetServletPath(javax.servlet.http.HttpServletRequest request) Returns the part of the given request's URL that calls the servlet.static StringgetUpdatedParams(javax.servlet.http.HttpServletRequest request, String[] names, String[] newValues) static StringgetUpdatedParams(javax.servlet.http.HttpServletRequest request, String[] names, String[] newValues, String removePattern) static StringgetUpdatedParams(javax.servlet.http.HttpServletRequest request, String[] names, String[] newValues, String[] removePatterns) static StringgetUpdatedParams(javax.servlet.http.HttpServletRequest request, String[] names, String[] newValues, String[] removePatterns, boolean escapeAmpersand) static StringgetUrl(javax.servlet.http.HttpServletRequest request) Returns the absolute URL of the given request.static StringgetUrl(javax.servlet.http.HttpServletRequest request, boolean escapeAmpersand) Returns the absolute URL of the given request.static StringgetUrlWithAddedParam(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 StringgetUrlWithAddedParam(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 StringgetUrlWithRemovedParams(javax.servlet.http.HttpServletRequest request, String[] params, boolean escapeAmpersand) Returns a relative URL with the given parameter removedstatic StringgetUrlWithUpdatedParam(javax.servlet.http.HttpServletRequest request, String name, String newValue) Returns a relative URL with the given parameter added or updatedstatic StringgetUrlWithUpdatedParams(String resourcePath, Map<String, String[]> parameterMap, String[] names, String[] newValues, String[] removePatterns, boolean escapeAmpersand) Returns a relative URL with the given parameters added or updatedstatic StringgetUrlWithUpdatedParams(javax.servlet.http.HttpServletRequest request, String[] names, String[] newValues) Returns a relative URL with the given parameters added or updatedstatic StringgetUrlWithUpdatedParams(javax.servlet.http.HttpServletRequest request, String[] names, String[] newValues, String removePattern) Returns a relative URL with the given parameters added or updatedstatic StringgetUrlWithUpdatedParams(javax.servlet.http.HttpServletRequest request, String[] names, String[] newValues, String[] removePatterns) Returns a relative URL with the given parameters added or updatedstatic StringgetUrlWithUpdatedParams(javax.servlet.http.HttpServletRequest request, String[] names, String[] newValues, String[] removePatterns, boolean escapeAmpersand) Returns a relative URL with the given parameters added or updatedstatic booleanisErrorPage(javax.servlet.http.HttpServletRequest request) Check if the given request is an invocation of the error page.static StringReturn a String without the trailing chars. I.e. '&' or '?'.static voidrestoreAttribute(javax.servlet.jsp.PageContext pageContext, String attribute) Restore request attribute from page scopestatic voidsetAlternateImpl(ServletUtil.ServletUtilAlternate alternate) Define theServletUtil.ServletUtilAlternateclass to use to provide custom behavior for ServletUtil methods.static StringtoAbsoluteUrl(javax.servlet.http.HttpServletRequest request, String path) Convert a relative URL into a full URL.static StringunescapeAmpersand(String str) Replace all occurence of "&" by "&" in the given string
-
Field Details
-
URL_INIT_SIZE
public static final int URL_INIT_SIZEBuffer size used for when generating url using StrigBuffer (default is 200).- See Also:
-
DEFAULT_DOMID_PREFIX
- See Also:
-
UNIQUE_DOM_ID_COUNTER
- See Also:
-
-
Method Details
-
setAlternateImpl
Define theServletUtil.ServletUtilAlternateclass 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
- Returns:
- the current alternate class being registered in ServletUtil to provide a custom behavior.
-
backupAttribute
Backup request attribute to page scope -
restoreAttribute
Restore request attribute from page scope- Parameters:
pageContext-attribute-- Since:
- jcms-4.0
-
getBaseUrl
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
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).
This method will check for attribute "javax.servlet.forward.context_path" that may have been added when calling a servlet using the forward method of a RequestDispatcher.- Parameters:
request- the request- Returns:
- the contextPath of the request
-
getJsp
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 bygetServletPath(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
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 bygetServletPath(HttpServletRequest)andgetPathInfo(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
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
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
-
isErrorPage
public static boolean isErrorPage(javax.servlet.http.HttpServletRequest request) Check if the given request is an invocation of the error page.- Parameters:
request-- Returns:
- true if the request is an error page, false otherwise.
- Since:
- JCMS-10574
-
getUrl
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
Returns the absolute URL of the given request. Rebuild this URL from the request attributes (baseUrl + servletPath + queryString).- Parameters:
request- the requestescapeAmpersand- will use "&" if true, "&" if false- Returns:
- the URL of the request
-
getOriginalUrl
When invoked during an ajax request, this method returns the absolute URL of the original request.Implementation detail : it uses the referrer sent by the browser, and default to
getUrl(HttpServletRequest, boolean).- Parameters:
request- the current Ajax request- Returns:
- the URL of the request (in which ampersand are not encoded for HTML)
- Since:
- JCMS-6567
-
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 methodHttpServletRequest.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 HttpRequestescapeAmpersand- will use "&" 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 "&" 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 HttpRequestparams- the name of the parameters to be removedescapeAmpersand- will use "&" 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 HttpRequestname- the name of the parameternewValue- 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 HttpRequestparams- 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 HttpRequestname- the name of the parameternewValue- 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 HttpRequestnames- an array of String which contains the name of the parametersnewValues- 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 HttpRequestnames- an array of String which contains the name of the parametersnewValues- 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 HttpRequestnames- an array of String which contains the name of the parametersnewValues- 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 HttpRequestnames- an array of String which contains the name of the parametersnewValues- 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 HttpRequestnames- an array of String which contains the name of the parametersnewValues- 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' listescapeAmpersand- will use "&" 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 pathparameterMap- the HttpRequest parameter mapnames- an array of String which contains the name of the parametersnewValues- 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' listescapeAmpersand- will use "&" 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 HttpRequestnames- an array of String which contains the name of the parametersnewValues- 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 HttpRequestnames- an array of String which contains the name of the parametersnewValues- 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 HttpRequestnames- an array of String which contains the name of the parametersnewValues- 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' listescapeAmpersand- will use "& true, "&" if false- Returns:
- the new URL
-
getUpdatedParams
-
getUpdatedParams
-
getUpdatedParams
-
getUpdatedParams
-
removeTrailingChars
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
Replace all occurence of "&" by "&" in the given string- Parameters:
str- the string in which to find &- Returns:
- a new string with all replacement being done.
-
escapeAmpersand
Replace all occurrence of singles "&" by "&" 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
Convenient method to avoid the need to specify "UTF-8" encoding and catching of exception in call toURLDecoder.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:
-
encodeURL
Convenient method to avoid the need to specify "UTF-8" encoding and catching of exception in call toURLEncoder.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:
-
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
-
getRequestId
Return unique identifier for the specified request, constant during all the duration of this request and unique accross application lifecyle.This identifier can be used as a logging facility or as any other way to uniquely identify a request.
- Parameters:
request- an HttpServletRequest, must NOT be null- Returns:
- a non null identifier
-
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).
-
toAbsoluteUrl
Convert a relative URL into a full URL.If the specified path is already an absolute URL the path is returned unmodified.
If the specified request is null the path is returned unmodified.- Parameters:
path- a path relative to the webapp root directory (without leading slash), eg "css/custom/myfile.css"- Returns:
- an absolute URL using current request base URL.
- Since:
- JCMS-6621
-