Class HttpHeaderList

All Implemented Interfaces:
Serializable, Cloneable, Iterable<HttpHeader>, Collection<HttpHeader>, List<HttpHeader>, RandomAccess
Direct Known Subclasses:
HttpHeaderList.Unmodifiable, HttpHeaderList.Void

public class HttpHeaderList extends ArrayList<HttpHeader>
A mutable, ordered list of HttpHeader instances.

Mirrors the semantics of HeaderList from juneau-rest-common-classic without the Apache HttpCore dependency. Header-name comparisons are case-insensitive by default.

Example

HttpHeaderList headers = HttpHeaderList.create() .append(HttpHeaderBean.of("Accept", "text/xml")) .append("Content-Type", "text/xml");

See Also:
Since:
10.0.0
See Also:
  • Constructor Details

  • Method Details

    • create

      public static HttpHeaderList create()
      Creates a new empty HttpHeaderList.
      Returns:
      A new instance. Never null.
    • of

      public static HttpHeaderList of(HttpHeader... headers)
      Creates a new HttpHeaderList initialized with the given headers.
      Parameters:
      headers - The headers to include. null entries are ignored.
      Returns:
      A new instance. Never null.
    • of

      public static HttpHeaderList of(List<HttpHeader> headers)
      Creates a new HttpHeaderList initialized with the given headers.
      Parameters:
      headers - The headers to include. null entries are ignored.
      Returns:
      A new instance. Never null.
    • ofPairs

      public static HttpHeaderList ofPairs(String... pairs)
      Creates a new HttpHeaderList initialized with the given alternating name/value pairs.
      Parameters:
      pairs - Alternating name/value strings. Can be null (treated as empty). Length must be even.
      Returns:
      A new instance. Never null.
      Throws:
      IllegalArgumentException - If the number of arguments is odd.
    • caseSensitive

      public HttpHeaderList caseSensitive(boolean value)
      Sets whether header-name comparisons are case-sensitive. Defaults to false.
      Parameters:
      value - New value.
      Returns:
      This object.
    • append

      Appends the given header.
      Parameters:
      value - The header to add. null is ignored.
      Returns:
      This object.
    • append

      public HttpHeaderList append(HttpHeader... values)
      Appends the given headers in order.
      Parameters:
      values - The headers to add. null entries are ignored.
      Returns:
      This object.
    • append

      Appends the given headers in order.
      Parameters:
      values - The headers to add. null entries are ignored.
      Returns:
      This object.
    • append

      public HttpHeaderList append(String name, String value)
      Appends a header constructed from the given name and string value.
      Parameters:
      name - The header name. Must not be null.
      value - The header value. Can be null to omit the header from the request.
      Returns:
      This object.
    • append

      public HttpHeaderList append(String name, Supplier<String> value)
      Appends a header constructed from the given name and lazy value supplier.
      Parameters:
      name - The header name. Must not be null.
      value - Supplier of the header value. Must not be null.
      Returns:
      This object.
    • setDefault

      Sets the given header as a default. If a header with the same name already exists, this is a no-op (first-in-chain wins per name).
      Parameters:
      value - The header to set. null is ignored.
      Returns:
      This object.
    • setDefault

      public HttpHeaderList setDefault(HttpHeader... values)
      Sets the given headers as defaults, in order. For each, if a header with the same name already exists, the entry is skipped (first-in-chain wins per name).
      Parameters:
      values - The headers to set. null entries are ignored.
      Returns:
      This object.
    • setDefault

      public HttpHeaderList setDefault(String name, String value)
      Sets a default header with the given name and value (first-in-chain wins per name).
      Parameters:
      name - The header name. Must not be null.
      value - The header value. Can be null to omit the header from the request.
      Returns:
      This object.
    • set

      public HttpHeaderList set(HttpHeader value)
      Sets the given header, replacing any prior entry with the same name.
      Parameters:
      value - The header to set. null is ignored.
      Returns:
      This object.
    • set

      public HttpHeaderList set(HttpHeader... values)
      Sets the given headers, each replacing any prior entry with the same name.
      Parameters:
      values - The headers to set. null entries are ignored.
      Returns:
      This object.
    • set

      public HttpHeaderList set(String name, String value)
      Sets a header with the given name and value, replacing any prior entry with the same name.
      Parameters:
      name - The header name. Must not be null.
      value - The header value. Can be null to omit the header from the request.
      Returns:
      This object.
    • removeAll

      Removes all headers with the given name.
      Parameters:
      name - The header name. Must not be null.
      Returns:
      This object.
    • contains

      public boolean contains(String name)
      Returns true if a header with the given name is present.
      Parameters:
      name - The header name.
      Returns:
      true if present.
    • getFirst

      public HttpHeader getFirst(String name)
      Returns the first header with the given name, or null if absent.
      Parameters:
      name - The header name.
      Returns:
      The first matching header, or null.
    • getLast

      public HttpHeader getLast(String name)
      Returns the last header with the given name, or null if absent.
      Parameters:
      name - The header name.
      Returns:
      The last matching header, or null.
    • getAll

      public List<HttpHeader> getAll(String name)
      Returns all headers with the given name in insertion order.
      Parameters:
      name - The header name.
      Returns:
      A new list of matching headers. Never null.
    • getAll

      public HttpHeader[] getAll()
      Returns all headers in this list as a typed array.

      Mirrors HeaderList.getAll() from juneau-rest-common-classic.

      Returns:
      A new array. Never null.
    • forEach

      public HttpHeaderList forEach(String name, Consumer<HttpHeader> action)
      Performs an action on each header whose name matches.
      Parameters:
      name - The header name.
      action - The action.
      Returns:
      This object.
    • copy

      public HttpHeaderList copy()
      Returns a copy of this list.
      Returns:
      A new copy. Never null.
    • unmodifiable

      Returns an unmodifiable snapshot of this list.

      Idempotency: if this list is already an HttpHeaderList.Unmodifiable snapshot, it is returned as-is.

      Returns:
      An unmodifiable snapshot of this list, or this list if it is already unmodifiable.
    • isUnmodifiable

      public boolean isUnmodifiable()
      Returns true if this list is an unmodifiable snapshot.
      Returns:
      true if this list is an UnmodifiableBean snapshot.
    • toHeaderArray

      Returns the headers in this list as a typed array.
      Returns:
      A new array. Never null.