Class Headers

java.lang.Object
nl.colorize.util.http.Headers
All Implemented Interfaces:
Iterable<Tuple<String,String>>

public class Headers extends Object implements Iterable<Tuple<String,String>>
Immutable representation of the request/response headers sent as part of an HTTP request. Used in conjunction with URLLoader, which supports a variety of HTTP clients depending on the platform.

HTTP headers are sometimes represented using a Map<String, String>, but this does not allow for the following:

  • Headers have an explicitly defined order.
  • The same header can occur multiple times.
  • Header names are case-insensitive.
  • Constructor Details

  • Method Details

    • getHeaderNames

      public List<String> getHeaderNames()
      Iterates over header names. Note that header names can appear multiple times in the list, if there are multiple occurrences of that header.
    • get

      public Optional<String> get(String name)
      Returns the value of the header with the specified name. If the header occurs multiple times, the first occurrence is returned.
    • getAll

      public List<String> getAll(String name)
      Returns all values for headers with the specified name. If the header is not present, this will return an empty list.
    • iterator

      public Iterator<Tuple<String,String>> iterator()
      Specified by:
      iterator in interface Iterable<Tuple<String,String>>
    • forEach

      public void forEach(BiConsumer<String,String> callback)
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • of

      public static Headers of(String name, String value, String... rest)
      Creates a Headers instance from the specified HTTP header names and values. The rest parameter expects values in the order name1, value1, name2, value2, ....
      Throws:
      IllegalArgumentException - When an odd number of arguments is passed to the method.
    • fromMap

      public static Headers fromMap(Map<String,String> entries)
      Creates a Headers instance from a map containing the name/value pairs of the HTTP headers. The order of the headers is based on the map's iteration order.
    • none

      public static Headers none()
      Returns an empty Headers instance that indicates the HTTP request or response does not contain any headers.
    • validateHeader

      public static void validateHeader(String name, String value)
      Helper method that validates if the specified name and value should be allowed as a HTTP headers.
      Throws:
      IllegalArgumentException - if the validation fails for the header name and/or value.