Class CmsRequestUtil


  • public final class CmsRequestUtil
    extends java.lang.Object
    Provides utility functions for dealing with values a HttpServletRequest.

    Since:
    6.0.0
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String appendParameter​(java.lang.String url, java.lang.String paramName, java.lang.String paramValue)
      Appends a request parameter to the given URL.
      static java.lang.String appendParameters​(java.lang.String url, java.util.Map<java.lang.String,​java.lang.String[]> params, boolean encode)
      Appends a map of request parameters to the given URL.
      static boolean checkBacklink​(java.lang.String backlink, javax.servlet.http.HttpServletRequest optionalRequest)
      Checks that the given link is a valid backlink for editors.
      static java.util.Map<java.lang.String,​java.lang.String[]> createParameterMap​(java.lang.String query)
      Parses the parameters of the given request query part and creates a parameter map out of them.
      static java.util.Map<java.lang.String,​java.lang.String[]> createParameterMap​(java.lang.String query, boolean decodeParameters, java.lang.String encoding)
      Parses the parameters of the given request query part, optionally decodes them, and creates a parameter map out of them.
      static java.util.Map<java.lang.String,​java.lang.String[]> createParameterMap​(java.util.Map<java.lang.String,​?> params)
      Creates a valid request parameter map from the given map, most notably changing the values form String to String[] if required.
      static void disableCrossSiteFrameEmbedding​(javax.servlet.http.HttpServletResponse response)
      Sets HTTP response headers to disable embedding in iframes on different domains.
      static java.lang.String encodeParams​(javax.servlet.http.HttpServletRequest req)
      Returns all parameters of the given request as a request parameter URL String, that is in the form key1=value1&key2=value2 etc.
      static java.lang.String encodeParamsWithUri​(java.lang.String uri, javax.servlet.http.HttpServletRequest req)
      Encodes the given URI, with all parameters from the given request appended.
      static void forwardRequest​(java.lang.String target, java.util.Map<java.lang.String,​java.lang.String[]> params, javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)
      Forwards the response to the given target, with the provided parameter map.
      static void forwardRequest​(java.lang.String target, javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)
      Forwards the response to the given target, which may contain parameters appended like for example ?a=b&c=d.
      static java.util.Map<java.lang.String,​java.lang.Object> getAtrributeMap​(javax.servlet.ServletRequest req)
      Exactly like getAttributeMap, but incorrectly spelled.
      static java.util.Map<java.lang.String,​java.lang.Object> getAttributeMap​(javax.servlet.ServletRequest req)
      Returns a map with all request attributes.
      static java.lang.String getCookieValue​(javax.servlet.http.Cookie[] cookies, java.lang.String name)
      Gets the value of a specific cookie from an array of cookies.
      static java.lang.String getCookieValue​(CmsJspActionElement jsp, java.lang.String name)
      Returns the value of the cookie with the given name.
      static JSONObject getJsonParameterMap​(java.util.Map<java.lang.String,​java.lang.String[]> params)
      Converts the given parameter map into an JSON object.
      static java.lang.String getNotEmptyDecodedParameter​(javax.servlet.http.HttpServletRequest request, java.lang.String paramName)
      Reads value from the request parameters, will return null if the value is not available or only white space.
      static java.lang.String getNotEmptyParameter​(javax.servlet.http.HttpServletRequest request, java.lang.String paramName)
      Reads value from the request parameters, will return null if the value is not available or only white space.
      static java.util.Map<java.lang.String,​java.lang.String[]> getParameterMapFromJSON​(JSONObject params)
      Converts the given JSON object into a valid parameter map.
      static com.google.common.collect.Multimap<java.lang.String,​java.lang.String> getParameters​(java.net.URI uri)
      Parses parameter map from the given URI.
      static com.google.common.collect.Multimap<java.lang.String,​java.lang.String> getParametersFromRawQuery​(java.lang.String rawQuery)
      Parses the parameter map from a raw query string.
      static java.lang.String getRequestLink​(java.lang.String url)
      Returns the link without parameters from a String that is formatted for a GET request.
      static java.lang.Object getSessionValue​(javax.servlet.http.HttpServletRequest request, java.lang.String key)
      Reads an object from the session of the given HTTP request.
      static java.util.List<org.apache.commons.fileupload.FileItem> readMultipartFileItems​(javax.servlet.http.HttpServletRequest request)
      Parses a request of the form multipart/form-data.
      static java.util.List<org.apache.commons.fileupload.FileItem> readMultipartFileItems​(javax.servlet.http.HttpServletRequest request, java.lang.String tempFolderPath)
      Parses a request of the form multipart/form-data.
      static java.util.Map<java.lang.String,​java.lang.String[]> readParameterMapFromMultiPart​(java.lang.String encoding, java.util.List<org.apache.commons.fileupload.FileItem> multiPartFileItems)
      Creates a "standard" request parameter map from the values of a multipart/form-data request.
      static void redirectPermanently​(CmsJspActionElement jsp, java.lang.String target)
      Redirects the response to the target link using a "301 - Moved Permanently" header.
      static void redirectRequestSecure​(CmsJspActionElement jsp, java.lang.String target)
      Redirects the response to the target link.
      static void removeSessionValue​(javax.servlet.http.HttpServletRequest request, java.lang.String key)
      Removes an object from the session of the given http request.
      static void setCookieValue​(CmsJspActionElement jsp, java.lang.String name, java.lang.String value)
      Sets the value of a specific cookie.
      static void setNoCacheHeaders​(javax.servlet.http.HttpServletResponse res)
      Sets headers to the given response to prevent client side caching.
      static void setSessionValue​(javax.servlet.http.HttpServletRequest request, java.lang.String key, java.lang.Object value)
      Adds an object to the session of the given HTTP request.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • appendParameter

        public static java.lang.String appendParameter​(java.lang.String url,
                                                       java.lang.String paramName,
                                                       java.lang.String paramValue)
        Appends a request parameter to the given URL.

        This method takes care about the adding the parameter as an additional parameter (appending ¶m=value) or as the first parameter (appending ?param=value).

        Parameters:
        url - the URL where to append the parameter to
        paramName - the paramter name to append
        paramValue - the parameter value to append
        Returns:
        the URL with the given parameter appended
      • appendParameters

        public static java.lang.String appendParameters​(java.lang.String url,
                                                        java.util.Map<java.lang.String,​java.lang.String[]> params,
                                                        boolean encode)
        Appends a map of request parameters to the given URL.

        The map can contains values of String[] or simple String values.

        This method takes care about the adding the parameter as an additional parameter (appending ¶m=value) or as the first parameter (appending ?param=value).

        Parameters:
        url - the URL where to append the parameter to
        params - the parameters to append
        encode - if true, the parameter values are encoded before they are appended
        Returns:
        the URL with the given parameter appended
      • checkBacklink

        public static boolean checkBacklink​(java.lang.String backlink,
                                            javax.servlet.http.HttpServletRequest optionalRequest)
        Checks that the given link is a valid backlink for editors.

        This means that the link is either just a path, or starts with a scheme/domain/port that is either registered in the site configuration, or is the same as that of the current request.

        Parameters:
        backlink - the link to check
        optionalRequest - the current request - may be null if no check against the current request is desired
        Returns:
        true if the link is a valid backlink
      • createParameterMap

        public static java.util.Map<java.lang.String,​java.lang.String[]> createParameterMap​(java.util.Map<java.lang.String,​?> params)
        Creates a valid request parameter map from the given map, most notably changing the values form String to String[] if required.

        If the given parameter map is null, then null is returned.

        Parameters:
        params - the map of parameters to create a parameter map from
        Returns:
        the created parameter map, all values will be instances of String[]
      • createParameterMap

        public static java.util.Map<java.lang.String,​java.lang.String[]> createParameterMap​(java.lang.String query)
        Parses the parameters of the given request query part and creates a parameter map out of them.

        Please note: This does not parse a full request URI/URL, only the query part that starts after the "?". For example, in the URI /system/index.html?a=b&c=d, the query part is a=b&c=d.

        If the given String is empty, an empty map is returned.

        Parameters:
        query - the query to parse
        Returns:
        the parameter map created from the query
      • createParameterMap

        public static java.util.Map<java.lang.String,​java.lang.String[]> createParameterMap​(java.lang.String query,
                                                                                                  boolean decodeParameters,
                                                                                                  java.lang.String encoding)
        Parses the parameters of the given request query part, optionally decodes them, and creates a parameter map out of them.

        Please note: This does not parse a full request URI/URL, only the query part that starts after the "?". For example, in the URI /system/index.html?a=b&c=d, the query part is a=b&c=d.

        If the given String is empty, an empty map is returned.

        Parameters:
        query - the query to parse
        decodeParameters - a flag, indicating if the parameters should be decoded.
        encoding - the character encoding used while decoding. If null, the default character encoding is used.
        Returns:
        the parameter map created from the query
      • disableCrossSiteFrameEmbedding

        public static void disableCrossSiteFrameEmbedding​(javax.servlet.http.HttpServletResponse response)
        Sets HTTP response headers to disable embedding in iframes on different domains.
        Parameters:
        response - the response on which to set the HTTP headers
      • encodeParams

        public static java.lang.String encodeParams​(javax.servlet.http.HttpServletRequest req)
        Returns all parameters of the given request as a request parameter URL String, that is in the form key1=value1&key2=value2 etc. The result will be encoded using the CmsEncoder.encode(String) function.

        Parameters:
        req - the request to read the parameters from
        Returns:
        all initialized parameters of the given request as request parameter URL String
      • encodeParamsWithUri

        public static java.lang.String encodeParamsWithUri​(java.lang.String uri,
                                                           javax.servlet.http.HttpServletRequest req)
        Encodes the given URI, with all parameters from the given request appended.

        The result will be encoded using the CmsEncoder.encode(String) function.

        Parameters:
        req - the request where to read the parameters from
        uri - the URI to encode
        Returns:
        the encoded URI, with all parameters from the given request appended
      • forwardRequest

        public static void forwardRequest​(java.lang.String target,
                                          javax.servlet.http.HttpServletRequest req,
                                          javax.servlet.http.HttpServletResponse res)
                                   throws java.io.IOException,
                                          javax.servlet.ServletException
        Forwards the response to the given target, which may contain parameters appended like for example ?a=b&c=d.

        Please note: If possible, use forwardRequest(String, Map, HttpServletRequest, HttpServletResponse) where the parameters are passed as a map, since the parsing of the parameters may introduce issues with encoding and is in general much less effective.

        The parsing of parameters will likely fail for "large values" (e.g. full blown web forms with <textarea> elements etc. Use this method only if you know that the target will just contain up to 3 parameters which are relatively short and have no encoding or line break issues.

        Parameters:
        target - the target to forward to (may contain parameters like ?a=b&c=d)
        req - the request to forward
        res - the response to forward
        Throws:
        java.io.IOException - in case the forwarding fails
        javax.servlet.ServletException - in case the forwarding fails
      • forwardRequest

        public static void forwardRequest​(java.lang.String target,
                                          java.util.Map<java.lang.String,​java.lang.String[]> params,
                                          javax.servlet.http.HttpServletRequest req,
                                          javax.servlet.http.HttpServletResponse res)
                                   throws java.io.IOException,
                                          javax.servlet.ServletException
        Forwards the response to the given target, with the provided parameter map.

        The target URI must NOT have parameters appended like for example ?a=b&c=d. The values in the provided map must be of type String[]. If required, use createParameterMap(Map) before calling this method to make sure all values are actually of the required array type.

        Parameters:
        target - the target to forward to (may NOT contain parameters like ?a=b&c=d)
        params - the parameter map (the values must be of type String[]
        req - the request to forward
        res - the response to forward
        Throws:
        java.io.IOException - in case the forwarding fails
        javax.servlet.ServletException - in case the forwarding fails
      • getAtrributeMap

        public static java.util.Map<java.lang.String,​java.lang.Object> getAtrributeMap​(javax.servlet.ServletRequest req)
        Exactly like getAttributeMap, but incorrectly spelled.

        Kept for backward compatibility.

        Parameters:
        req - the request
        Returns:
        the attribute map
      • getAttributeMap

        public static java.util.Map<java.lang.String,​java.lang.Object> getAttributeMap​(javax.servlet.ServletRequest req)
        Returns a map with all request attributes.

        Parameters:
        req - the request
        Returns:
        the attribute map
      • getCookieValue

        public static java.lang.String getCookieValue​(CmsJspActionElement jsp,
                                                      java.lang.String name)
        Returns the value of the cookie with the given name.

        Parameters:
        jsp - the CmsJspActionElement to use
        name - the name of the cookie
        Returns:
        the value of the cookie with the given name or null, if no cookie exists with the name
      • getCookieValue

        public static java.lang.String getCookieValue​(javax.servlet.http.Cookie[] cookies,
                                                      java.lang.String name)
        Gets the value of a specific cookie from an array of cookies.

        Parameters:
        cookies - the cookie array
        name - the name of the cookie we want
        Returns:
        the cookie value, or null if cookie with the given name wasn't found
      • getJsonParameterMap

        public static JSONObject getJsonParameterMap​(java.util.Map<java.lang.String,​java.lang.String[]> params)
        Converts the given parameter map into an JSON object.

        Parameters:
        params - the parameters map to convert
        Returns:
        the JSON representation of the given parameter map
      • getNotEmptyDecodedParameter

        public static java.lang.String getNotEmptyDecodedParameter​(javax.servlet.http.HttpServletRequest request,
                                                                   java.lang.String paramName)
        Reads value from the request parameters, will return null if the value is not available or only white space.

        The value of the request will also be decoded using CmsEncoder.decode(String) and also trimmed using String.trim().

        Parameters:
        request - the request to read the parameter from
        paramName - the parameter name to read
        Returns:
        the request parameter value for the given parameter
      • getNotEmptyParameter

        public static java.lang.String getNotEmptyParameter​(javax.servlet.http.HttpServletRequest request,
                                                            java.lang.String paramName)
        Reads value from the request parameters, will return null if the value is not available or only white space.

        Parameters:
        request - the request to read the parameter from
        paramName - the parameter name to read
        Returns:
        the request parameter value for the given parameter
      • getParameterMapFromJSON

        public static java.util.Map<java.lang.String,​java.lang.String[]> getParameterMapFromJSON​(JSONObject params)
        Converts the given JSON object into a valid parameter map.

        Parameters:
        params - the JSON object to convert
        Returns:
        the parameter map from the given JSON object
      • getParameters

        public static com.google.common.collect.Multimap<java.lang.String,​java.lang.String> getParameters​(java.net.URI uri)
        Parses parameter map from the given URI.

        Parameters:
        uri - the URI
        Returns:
        the parameter map
      • getParametersFromRawQuery

        public static com.google.common.collect.Multimap<java.lang.String,​java.lang.String> getParametersFromRawQuery​(java.lang.String rawQuery)
        Parses the parameter map from a raw query string.

        Parameters:
        rawQuery - the raw query string
        Returns:
        the parameter map
      • getRequestLink

        public static java.lang.String getRequestLink​(java.lang.String url)
        Returns the link without parameters from a String that is formatted for a GET request.

        Parameters:
        url - the URL to remove the parameters from
        Returns:
        the URL without any parameters
      • getSessionValue

        public static java.lang.Object getSessionValue​(javax.servlet.http.HttpServletRequest request,
                                                       java.lang.String key)
        Reads an object from the session of the given HTTP request.

        A session will be initialized if the request does not currently have a session. As a result, the request will always have a session after this method has been called.

        Will return null if no corresponding object is found in the session.

        Parameters:
        request - the request to get the session from
        key - the key of the object to read from the session
        Returns:
        the object received form the session, or null
      • readMultipartFileItems

        public static java.util.List<org.apache.commons.fileupload.FileItem> readMultipartFileItems​(javax.servlet.http.HttpServletRequest request)
        Parses a request of the form multipart/form-data. The result list will contain items of type FileItem. If the request is not of type multipart/form-data, then null is returned.

        Parameters:
        request - the HTTP servlet request to parse
        Returns:
        the list of FileItem extracted from the multipart request, or null if the request was not of type multipart/form-data
      • readMultipartFileItems

        public static java.util.List<org.apache.commons.fileupload.FileItem> readMultipartFileItems​(javax.servlet.http.HttpServletRequest request,
                                                                                                    java.lang.String tempFolderPath)
        Parses a request of the form multipart/form-data. The result list will contain items of type FileItem. If the request is not of type multipart/form-data, then null is returned.

        Parameters:
        request - the HTTP servlet request to parse
        tempFolderPath - the real file system path to the temp file folder
        Returns:
        the list of FileItem extracted from the multipart request, or null if the request was not of type multipart/form-data
      • readParameterMapFromMultiPart

        public static java.util.Map<java.lang.String,​java.lang.String[]> readParameterMapFromMultiPart​(java.lang.String encoding,
                                                                                                             java.util.List<org.apache.commons.fileupload.FileItem> multiPartFileItems)
        Creates a "standard" request parameter map from the values of a multipart/form-data request.

        Parameters:
        encoding - the encoding to use when creating the values
        multiPartFileItems - the list of parsed multi part file items
        Returns:
        a map containing all non-file request parameters
        See Also:
        readMultipartFileItems(HttpServletRequest)
      • redirectPermanently

        public static void redirectPermanently​(CmsJspActionElement jsp,
                                               java.lang.String target)
        Redirects the response to the target link using a "301 - Moved Permanently" header.

        This implementation will work only on JSP pages in OpenCms that use the default JSP loader implementation.

        Parameters:
        jsp - the OpenCms JSP context
        target - the target link
      • redirectRequestSecure

        public static void redirectRequestSecure​(CmsJspActionElement jsp,
                                                 java.lang.String target)
                                          throws java.io.IOException
        Redirects the response to the target link.

        Use this method instead of HttpServletResponse.sendRedirect(java.lang.String) to avoid relative links with secure sites (and issues with apache).

        Parameters:
        jsp - the OpenCms JSP context
        target - the target link
        Throws:
        java.io.IOException - if something goes wrong during redirection
      • removeSessionValue

        public static void removeSessionValue​(javax.servlet.http.HttpServletRequest request,
                                              java.lang.String key)
        Removes an object from the session of the given http request.

        A session will be initialized if the request does not currently have a session. As a result, the request will always have a session after this method has been called.

        Parameters:
        request - the request to get the session from
        key - the key of the object to be removed from the session
      • setCookieValue

        public static void setCookieValue​(CmsJspActionElement jsp,
                                          java.lang.String name,
                                          java.lang.String value)
        Sets the value of a specific cookie.

        If no cookie exists with the value, a new cookie will be created.

        Parameters:
        jsp - the CmsJspActionElement to use
        name - the name of the cookie
        value - the value of the cookie
      • setNoCacheHeaders

        public static void setNoCacheHeaders​(javax.servlet.http.HttpServletResponse res)
        Sets headers to the given response to prevent client side caching.

        The following headers are set:

        Cache-Control: max-age=0
        Cache-Control: must-revalidate
        Pragma: no-cache

        Parameters:
        res - the request where to set the no-cache headers
      • setSessionValue

        public static void setSessionValue​(javax.servlet.http.HttpServletRequest request,
                                           java.lang.String key,
                                           java.lang.Object value)
        Adds an object to the session of the given HTTP request.

        A session will be initialized if the request does not currently have a session. As a result, the request will always have a session after this method has been called.

        Parameters:
        request - the request to get the session from
        key - the key of the object to be stored in the session
        value - the object to be stored in the session