Package 

Class Headers


  • 
    public final class Headers
    
                        

    The header fields of a single HTTP message. Values are uninterpreted strings; use {@code MobonRequest} and {@code MobonResponse} for interpreted headers. This class maintains the order of the header fields within the HTTP message.

    This class tracks header values line-by-line. A field with multiple comma- separated values on the same line will be treated as a field with a single value by this class. It is the caller's responsibility to detect and split on commas if their field permits multiple values. This simplifies use of single-valued fields whose values routinely contain commas, such as cookies or dates.

    This class trims whitespace from values. It never returns values with leading or trailing whitespace.

    Instances of this class are immutable. Use Builder to create instances.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      public final class Headers.Builder
    • Method Summary

      Modifier and Type Method Description
      String get(String name) Returns the last value corresponding to the specified field, or null.
      Date getDate(String name) Returns the last value corresponding to the specified field parsed as an HTTP date, or null ifeither the field is absent or cannot be parsed as a date.
      int size() Returns the number of field values.
      String name(int index) Returns the field at {@code position}.
      String value(int index) Returns the value at {@code index}.
      Set<String> names() Returns an immutable case-insensitive set of header names.
      List<String> values(String name) Returns an immutable list of the header values for {@code name}.
      long byteCount() Returns the number of bytes required to encode these headers using HTTP/1.1.
      Headers.Builder newBuilder()
      boolean equals(@Nullable() Object other) Returns true if {@code other} is a {@code Headers} object with the same headers, with the samecasing, in the same order.
      int hashCode()
      String toString()
      Map<String, List<String>> toMultimap()
      static Headers of(Array<String> namesAndValues) Returns headers for the alternating header names and values.
      static Headers of(Map<String, String> headers) Returns headers for the header names and values in the Map.
      • Methods inherited from class java.lang.Object

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

      • getDate

        @Nullable() Date getDate(String name)

        Returns the last value corresponding to the specified field parsed as an HTTP date, or null ifeither the field is absent or cannot be parsed as a date.

      • size

         int size()

        Returns the number of field values.

      • name

         String name(int index)

        Returns the field at {@code position}.

      • value

         String value(int index)

        Returns the value at {@code index}.

      • names

         Set<String> names()

        Returns an immutable case-insensitive set of header names.

      • byteCount

         long byteCount()

        Returns the number of bytes required to encode these headers using HTTP/1.1. This is also theapproximate size of HTTP/2 headers before they are compressed with HPACK. This value isintended to be used as a metric: smaller headers are more efficient to encode and transmit.

      • equals

         boolean equals(@Nullable() Object other)

        Returns true if {@code other} is a {@code Headers} object with the same headers, with the samecasing, in the same order. Note that two headers instances may be semantically equalbut not equal according to this method. In particular, none of the following sets of headersare equal according to this method:

           {@code * * 1. Original * Content-Type: text/html * Content-Length: 50 * * 2. Different order * Content-Length: 50 * Content-Type: text/html * * 3. Different case * content-type: text/html * content-length: 50 * * 4. Different values * Content-Type: text/html * Content-Length: 050 * }
        Applications that require semantically equal headers should convert them into a canonical formbefore comparing them for equality.
      • of

         static Headers of(Array<String> namesAndValues)

        Returns headers for the alternating header names and values. There must be an even number ofarguments, and they must alternate between header names and values.