Class ServletUtil

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

public class ServletUtil extends Object
This class provides a set of static methods which perform various utility operations for Servlets.
Author:
Olivier Dedieu
  • Field Details

  • Method Details

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

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

      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 "&" if true, "&" if false
      Returns:
      the URL of the request
    • getOriginalUrl

      public static String getOriginalUrl(javax.servlet.http.HttpServletRequest request)
      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 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 "&" 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 HttpRequest
      params - the name of the parameters to be removed
      escapeAmpersand - 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 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 "&" 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 occurrence 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:
    • 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:
    • 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

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

      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

      public static String toAbsoluteUrl(javax.servlet.http.HttpServletRequest request, String path)
      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