Class RequestUtil


  • public final class RequestUtil
    extends Object
    General purpose request parsing and encoding utility methods.
    Version:
    $Revision: 1.4 $ $Date: 2006/12/12 20:43:07 $
    Author:
    Craig R. McClanahan, Tim Tye
    • Constructor Detail

      • RequestUtil

        public RequestUtil()
    • Method Detail

      • encodeCookie

        public static String encodeCookie​(jakarta.servlet.http.Cookie cookie)
        Encode a cookie as per RFC 2109. The resulting string can be used as the value for a Set-Cookie header.
        Parameters:
        cookie - The cookie to encode.
        Returns:
        A string following RFC 2109.
      • normalize

        public static String normalize​(String path)
        Normalize a relative URI path that may have relative values ("/./", "/../", and so on ) it it. WARNING - This method is useful only for normalizing application-generated paths. It does not try to perform security checks for malicious input.
        Parameters:
        path - Relative path to be normalized
      • normalize

        public static String normalize​(String path,
                                       boolean replaceBackSlash)
        Normalize a relative URI path that may have relative values ("/./", "/../", and so on ) it it. WARNING - This method is useful only for normalizing application-generated paths. It does not try to perform security checks for malicious input.
        Parameters:
        path - Relative path to be normalized
        replaceBackSlash - Should '\\' be replaced with '/'
      • parseCharacterEncoding

        public static String parseCharacterEncoding​(String contentType)
        Parse the character encoding from the specified content type header. If the content type is null, or there is no explicit character encoding, null is returned.
        Parameters:
        contentType - a content type header
      • parseCookieHeader

        public static jakarta.servlet.http.Cookie[] parseCookieHeader​(String header)
        Parse a cookie header into an array of cookies according to RFC 2109.
        Parameters:
        header - Value of an HTTP "Cookie" header
      • parseParameters

        public static void parseParameters​(Map<String,​String[]> map,
                                           String data,
                                           String encoding)
                                    throws UnsupportedEncodingException
        Append request parameters from the specified String to the specified Map. It is presumed that the specified Map is not accessed from any other thread, so no synchronization is performed.

        IMPLEMENTATION NOTE: URL decoding is performed individually on the parsed name and value elements, rather than on the entire query string ahead of time, to properly deal with the case where the name or value includes an encoded "=" or "&" character that would otherwise be interpreted as a delimiter.

        Parameters:
        map - Map that accumulates the resulting parameters
        data - Input string containing request parameters
        encoding - The name of a supported charset used to encode
        Throws:
        IllegalArgumentException - if the data is malformed
        UnsupportedEncodingException
      • urlDecode

        public static String urlDecode​(String str)
        Decode and return the specified URL-encoded String. When the byte array is converted to a string, the system default character encoding is used... This may be different than some other servers.
        Parameters:
        str - The url-encoded string
        Throws:
        IllegalArgumentException - if a '%' character is not followed by a valid 2-digit hexadecimal number
      • urlDecode

        public static String urlDecode​(String str,
                                       String enc)
        Decode and return the specified URL-encoded String.
        Parameters:
        str - The url-encoded string
        enc - The encoding to use; if null, the default encoding is used
        Throws:
        IllegalArgumentException - if a '%' character is not followed by a valid 2-digit hexadecimal number
      • urlDecode

        public static String urlDecode​(byte[] bytes)
        Decode and return the specified URL-encoded byte array.
        Parameters:
        bytes - The url-encoded byte array
        Throws:
        IllegalArgumentException - if a '%' character is not followed by a valid 2-digit hexadecimal number
      • urlDecode

        public static String urlDecode​(byte[] bytes,
                                       String enc)
        Decode and return the specified URL-encoded byte array.
        Parameters:
        bytes - The url-encoded byte array
        enc - The encoding to use; if null, the default encoding is used
        Throws:
        IllegalArgumentException - if a '%' character is not followed by a valid 2-digit hexadecimal number
      • urlDecode

        public static String urlDecode​(org.glassfish.grizzly.http.util.ByteChunk bc,
                                       boolean toString)
        Decode (in place) the specified URL-encoded byte chunk, and optionally return the decoded result as a String
        Parameters:
        bc - The URL-encoded byte chunk to be decoded in place
        toString - true if the decoded result is to be returned as a String, false otherwise
        Returns:
        The decoded result in String form, if toString is true, or null otherwise
        Throws:
        IllegalArgumentException - if a '%' character is not followed by a valid 2-digit hexadecimal number
      • parseParameters

        public static void parseParameters​(Map<String,​String[]> map,
                                           byte[] data,
                                           String encoding)
                                    throws UnsupportedEncodingException
        Append request parameters from the specified String to the specified Map. It is presumed that the specified Map is not accessed from any other thread, so no synchronization is performed.

        IMPLEMENTATION NOTE: URL decoding is performed individually on the parsed name and value elements, rather than on the entire query string ahead of time, to properly deal with the case where the name or value includes an encoded "=" or "&" character that would otherwise be interpreted as a delimiter. NOTE: byte array data is modified by this method. Caller beware.

        Parameters:
        map - Map that accumulates the resulting parameters
        data - Input string containing request parameters
        encoding - Encoding to use for converting hex
        Throws:
        UnsupportedEncodingException - if the data is malformed
      • parseSessionVersionString

        public static final HashMap<String,​String> parseSessionVersionString​(String sessionVersion)
        Parses the given session version string into its components.
        Parameters:
        sessionVersion - The session version string to parse
        Returns:
        The mappings from context paths to session version numbers that were parsed from the given session version string
      • createSessionVersionString

        public static String createSessionVersionString​(Map<String,​String> sessionVersions)
        Creates the string representation for the given context path to session version mappings.

        The returned string will be used as the value of a JSESSIONIDVERSION cookie or jsessionidversion URI parameter, depending on the configured session tracking mode.

        Parameters:
        sessionVersions - Context path to session version mappings
        Returns:
        The resulting string representation, to be used as the value of a JSESSIONIDVERSION cookie or jsessionidversion URI parameter
      • lookupCharset

        public static Charset lookupCharset​(String enc)
                                     throws UnsupportedEncodingException
        This is a convenient API which wraps around the one in Grizzly and throws checked java.io.UnsupportedEncodingException instead of unchecked java.nio.charset.UnsupportedCharsetException. cf. String.getBytes(String charset) throws UnsupportedEncodingException
        Throws:
        UnsupportedEncodingException