Class RequestHeaders

All Implemented Interfaces:
Serializable, Cloneable, Iterable<RequestHeader>, Collection<RequestHeader>, List<RequestHeader>, RandomAccess

public class RequestHeaders extends ArrayList<RequestHeader>
Represents the headers in an HTTP request.

The RequestHeaders object is the API for accessing the headers of an HTTP request. It can be accessed by passing it as a parameter on your REST Java method:

@RestPost(...) public Object myMethod(RequestHeaders headers) {...}

Example:

@RestPost(...) public Object myMethod(RequestHeaders headers) { // Add a default value. headers.addDefault("ETag", DEFAULT_UUID); // Get a header value as a POJO. UUID etag = headers.get("ETag").as(UUID.class).get(); // Get a header as a standard HTTP part. ContentType contentType = headers.get(ContentType.class).orElse(ContentType.TEXT_XML); }

Some important methods on this class are:

Entries are stored in a case-insensitive map unless overridden via the constructor.

See Also:
See Also:
  • Constructor Details

    • RequestHeaders

      public RequestHeaders(RestRequest req, RequestQueryParams query, boolean caseSensitive)
      Constructor.
      Parameters:
      req - The request creating this bean.
      query - The query parameters on the request (used for overloaded header values).
      caseSensitive - Whether case-sensitive name matching is enabled.
  • Method Details

    • parser

      Sets the parser to use for part values.
      Parameters:
      value - The new value for this setting.
      Returns:
      This object.
    • caseSensitive

      public RequestHeaders caseSensitive(boolean value)
      Sets case sensitivity for names in this list.
      Parameters:
      value - The new value for this setting.
      Returns:
      This object (for method chaining).
    • addDefault

      Adds default entries to these headers.

      Similar to set(String, Object) but doesn't override existing values.

      Parameters:
      pairs - The default entries. Must not be null.
      Returns:
      This object.
    • addDefault

      public RequestHeaders addDefault(Header... pairs)
      Adds default entries to these headers.

      Similar to set(String, Object) but doesn't override existing values.

      Parameters:
      pairs - The default entries. Must not be null.
      Returns:
      This object.
    • addDefault

      public RequestHeaders addDefault(String name, String value)
      Adds a default entry to the request headers.
      Parameters:
      name - The name.
      value - The value.
      Returns:
      This object.
    • add

      public RequestHeaders add(String name, Object value)
      Adds a request header value.

      Header is added to the end.
      Existing headers with the same name are not changed.

      Parameters:
      name - The header name. Must not be null.
      value - The header value. Can be null.
      Returns:
      This object.
    • add

      public RequestHeaders add(Header... headers)
      Adds request header values.

      Headers are added to the end.
      Existing headers with the same name are not changed.

      Parameters:
      headers - The header objects. Must not be null.
      Returns:
      This object.
    • set

      public RequestHeaders set(String name, Object value)
      Sets a request header value.

      Header is added to the end.
      Any previous headers with the same name are removed.

      Parameters:
      name - The header name. Must not be null.
      value - The header value.
      Converted to a string using Object.toString().
      Can be null.
      Returns:
      This object.
    • set

      public RequestHeaders set(Header... headers)
      Sets request header values.

      Headers are added to the end.
      Any previous headers with the same name are removed.

      Parameters:
      headers - The header to set. Must not be null or contain null.
      Returns:
      This object.
    • remove

      public RequestHeaders remove(String name)
      Remove header by name.
      Parameters:
      name - The header names. Must not be null.
      Returns:
      This object.
    • subset

      public RequestHeaders subset(String... names)
      Returns a copy of this object but only with the specified header names copied.
      Parameters:
      names - The list to include in the copy.
      Returns:
      A new list object.
    • contains

      public boolean contains(String name)
      Returns true if the header with the specified name is present.
      Parameters:
      name - The header name. Must not be null.
      Returns:
      true if the header with the specified name is present.
    • containsAny

      public boolean containsAny(String... names)
      Returns true if the header with any of the specified names are present.
      Parameters:
      names - The header names. Must not be null.
      Returns:
      true if the header with any of the specified names are present.
    • getAll

      public List<RequestHeader> getAll(String name)
      Returns all headers with the specified name.
      Parameters:
      name - The header name.
      Returns:
      The list of all headers with matching names. Never null.
    • stream

      Returns all headers with the specified name.
      Parameters:
      name - The header name.
      Returns:
      The stream of all headers with matching names. Never null.
    • getSorted

      Returns all headers in sorted order.
      Returns:
      The stream of all headers in sorted order.
    • getNames

      public List<String> getNames()
      Returns all the unique header names in this list.
      Returns:
      The list of all unique header names in this list.
    • getFirst

      public RequestHeader getFirst(String name)
      Returns the first header with the specified name.

      Note that this method never returns null and that RequestHttpPart.isPresent() can be used to test for the existence of the header.

      Parameters:
      name - The header name. Must not be null.
      Returns:
      The header. Never null.
    • getLast

      public RequestHeader getLast(String name)
      Returns the last header with the specified name.

      Note that this method never returns null and that RequestHttpPart.isPresent() can be used to test for the existence of the header.

      Parameters:
      name - The header name. Must not be null.
      Returns:
      The header. Never null.
    • get

      public RequestHeader get(String name)
      Returns the condensed header with the specified name.

      If multiple headers are present, they will be combined into a single comma-delimited list.

      Parameters:
      name - The header name.
      Returns:
      The header, never null.
    • get

      public <T> Optional<T> get(Class<T> type)
      Returns the header as the specified bean type.

      Type must have a name specified via the Header annotation and a public constructor that takes in either value or name,value as strings.

      Type Parameters:
      T - The bean type to create.
      Parameters:
      type - The bean type to create.
      Returns:
      The bean, never null.
    • copy

      public RequestHeaders copy()
      Makes a copy of these parameters.
      Returns:
      A new parameters object.
    • toString

      public String toString()
      Overrides:
      toString in class AbstractCollection<RequestHeader>