Class HeaderUtil


  • public final class HeaderUtil
    extends Object
    Utility class for processing values in HTTP header fields.
    • Method Detail

      • split

        public static List<String> split​(String value,
                                         char separator)
        Parses an HTTP header value, splitting it into multiple values around the specified separator. Quoted strings are not split into multiple values if they contain separator characters. All leading and trailing white space in values is trimmed. All quotations remain intact.

        Note: This method is liberal in its interpretation of malformed header values; namely the incorrect use of string and character quoting mechanisms and unquoted white space. If a null or empty string is supplied as a value, this method yields an empty list.

        Parameters:
        value - the header value to be split.
        separator - the separator character to split headers around.
        Returns:
        A list of string representing the split values of the header.
      • join

        public static String join​(Collection<String> values,
                                  char separator)
        Joins a collection of header values into a single header value, with a specified specified separator. A null or empty collection of header values yeilds a null return value.
        Parameters:
        values - the values to be joined.
        separator - the separator to separate values within the returned value.
        Returns:
        a single header value, with values separated by the separator.
      • parseDirective

        public static Directive parseDirective​(String value)
        Splits a single HTTP directive name and value from an input string value. The input string value is presumed to have been extracted from a collection provided by the split(String, char) method.

        This method returns the directive name-value pair as a Directive.

        A value that is contained within a quoted-string is processed such that the surrounding '"' (quotation mark) characters are removed and single-character quotations hold the character being quoted without the escape '\' (backslash) character. White space is discarded unless it is within the quoted-string or a directive value.

        Note: This method is liberal in its interpretation of a malformed directive value; namely the incorrect use of string and character quoting mechanisms and unquoted white space.

        Parameters:
        value - the string to parse the name-value directive from.
        Returns:
        the name-value pair as a Directive.
      • parseDirectives

        public static Map<String,​String> parseDirectives​(Collection<String> values)
        Parses a set of HTTP directives from a collection of values. The input collection of values is presumed to have been provided from the split(String, char) method.

        A well-formed directives contains an attribute and optional value, separated by an '=' (equals sign) character. If the directives contains no value, it is represented by a null value in the returned map.

        Values that are contained in quoted-strings are processed such that the surrounding '"' (quotation mark) characters are removed and single-character quotations hold the character being quoted without the escape '\' (backslash) character. White space is discarded unless it is within the quoted-string or a directive value.

        Note: This method is liberal in its interpretation of malformed directive values; namely the incorrect use of string and character quoting mechanisms and unquoted white space.

        Parameters:
        values - the HTTP directives.
        Returns:
        a map of directive name-value pairs or an empty map if values was null.
      • parseDirectivesAsDirectiveMap

        public static Map<String,​Directive> parseDirectivesAsDirectiveMap​(Collection<String> values)
        Parses a set of HTTP directives from a collection of values. The input collection of values is presumed to have been provided from the split(String, char) method.
        Parameters:
        values - the HTTP directives.
        Returns:
        a map of $Directive objects based on all the unique directives found or an empty map if values was null.
      • quote

        public static String quote​(String value)
        Encloses a string in quotation marks. Quotation marks and backslash characters are escaped with the single-character quoting mechanism. For more information, see RFC 2616 ยง2.2.
        Parameters:
        value - the value to be enclosed in quotation marks.
        Returns:
        the value enclosed in quotation marks.
      • unquote

        public static String unquote​(String value)
        Unquotes a string following the logic of quote(String).
        Parameters:
        value - Value to unquote
        Returns:
        Unquoted value
      • parseMultiValuedHeader

        public static List<String> parseMultiValuedHeader​(Message<?> message,
                                                          String name)
        Parses the named header from the message as a multi-valued comma separated value. If there are multiple headers present then they are first merged and then split.
        Parameters:
        message - The HTTP request or response.
        name - The name of the header.
        Returns:
        A list of strings representing the split values of the header, which may be empty if the header was not present in the message.
      • parseMultiValuedHeader

        public static List<String> parseMultiValuedHeader​(String header)
        Parses the header content as a multi-valued comma separated value.
        Parameters:
        header - The HTTP header content.
        Returns:
        A list of strings representing the split values of the header, which may be empty if the header was null or empty.
      • parseSingleValuedHeader

        public static String parseSingleValuedHeader​(Message<?> message,
                                                     String name)
        Parses the named single-valued header from the message. If there are multiple headers present then only the first is used.
        Parameters:
        message - The HTTP request or response.
        name - The name of the header.
        Returns:
        The header value, or null if the header was not present in the message.
      • formatDate

        public static String formatDate​(Date date)
        Formats a HTTP-date using RFC 1123 format as specified in RFC 2616.
        Parameters:
        date - Date to format
        Returns:
        Formatted HTTP-date
      • parseDate

        public static Date parseDate​(String s)
        Parses the supported HTTP-date formats as specified in RFC 2616.
        Parameters:
        s - Date String
        Returns:
        Date instance, or null if unable to parse the date or s is null