Class Headers

java.lang.Object
com.netflix.zuul.message.Headers

public final class Headers extends Object
An abstraction over a collection of http headers. Allows multiple headers with same name, and header names are compared case insensitively. There are methods for getting and setting headers by String AND by HeaderName. When possible, use the HeaderName variants and cache the HeaderName instances somewhere, to avoid case-insensitive String comparisons.
  • Constructor Details

    • Headers

      public Headers()
    • Headers

      public Headers(int initialSize)
  • Method Details

    • copyOf

      public static Headers copyOf(Headers original)
    • getFirst

      @Nullable public String getFirst(String headerName)
      Get the first value found for this key even if there are multiple. If none, then return null.
    • getFirst

      @Nullable public String getFirst(HeaderName headerName)
      Get the first value found for this key even if there are multiple. If none, then return null.
    • getFirst

      public String getFirst(String headerName, String defaultValue)
      Get the first value found for this key even if there are multiple. If none, then return the specified defaultValue.
    • getFirst

      public String getFirst(HeaderName headerName, String defaultValue)
      Get the first value found for this key even if there are multiple. If none, then return the specified defaultValue.
    • getAll

      public List<String> getAll(String headerName)
      Returns all header values associated with the name.
    • getAll

      public List<String> getAll(HeaderName headerName)
      Returns all header values associated with the name.
    • forEachNormalised

      public void forEachNormalised(BiConsumer<? super String,? super String> entryConsumer)
      Iterates over the header entries with the given consumer. The first argument will be the normalised header name as returned by HeaderName.getNormalised(). The second argument will be the value. Do not modify the headers during iteration.
    • set

      public void set(String headerName, @Nullable String value)
      Replace any/all entries with this key, with this single entry. If value is null, then not added, but any existing header of same name is removed.
    • set

      public void set(HeaderName headerName, String value)
      Replace any/all entries with this key, with this single entry. If value is null, then not added, but any existing header of same name is removed.
    • setAndValidate

      public void setAndValidate(String headerName, @Nullable String value)
      Replace any/all entries with this key, with this single entry and validate. If value is null, then not added, but any existing header of same name is removed.
      Throws:
      ZuulException - on invalid name or value
    • setIfValid

      public void setIfValid(HeaderName headerName, String value)
      Replace any/all entries with this key, with this single entry if the key and entry are valid. If value is null, then not added, but any existing header of same name is removed.
    • setIfValid

      public void setIfValid(String headerName, @Nullable String value)
      Replace any/all entries with this key, with this single entry if the key and entry are valid. If value is null, then not added, but any existing header of same name is removed.
    • setAndValidate

      public void setAndValidate(HeaderName headerName, String value)
      Replace any/all entries with this key, with this single entry and validate. If value is null, then not added, but any existing header of same name is removed.
      Throws:
      ZuulException - on invalid name or value
    • setIfAbsent

      public boolean setIfAbsent(String headerName, String value)
      Adds the name and value to the headers, except if the name is already present. Unlike set(String, String), this method does not accept a null value.
      Returns:
      if the value was successfully added.
    • setIfAbsent

      public boolean setIfAbsent(HeaderName headerName, String value)
      Adds the name and value to the headers, except if the name is already present. Unlike set(HeaderName, String), this method does not accept a null value.
      Returns:
      if the value was successfully added.
    • setIfAbsentAndValid

      public boolean setIfAbsentAndValid(String headerName, String value)
      Validates and adds the name and value to the headers, except if the name is already present. Unlike set(String, String), this method does not accept a null value.
      Returns:
      if the value was successfully added.
    • setIfAbsentAndValid

      public boolean setIfAbsentAndValid(HeaderName headerName, String value)
      Validates and adds the name and value to the headers, except if the name is already present. Unlike set(HeaderName, String), this method does not accept a null value.
      Returns:
      if the value was successfully added.
    • add

      public void add(String headerName, String value)
      Adds the name and value to the headers.
    • add

      public void add(HeaderName headerName, String value)
      Adds the name and value to the headers.
    • addAndValidate

      public void addAndValidate(String headerName, String value)
      Adds the name and value to the headers and validate.
      Throws:
      ZuulException - on invalid name or value
    • addAndValidate

      public void addAndValidate(HeaderName headerName, String value)
      Adds the name and value to the headers and validate
      Throws:
      ZuulException - on invalid name or value
    • addIfValid

      public void addIfValid(String headerName, String value)
      Adds the name and value to the headers if valid
    • addIfValid

      public void addIfValid(HeaderName headerName, String value)
      Adds the name and value to the headers if valid
    • putAll

      public void putAll(Headers headers)
      Adds all the headers into this headers object.
    • remove

      public List<String> remove(String headerName)
      Removes the header entries that match the given header name, and returns them as a list.
    • remove

      public List<String> remove(HeaderName headerName)
      Removes the header entries that match the given header name, and returns them as a list.
    • removeIf

      public boolean removeIf(Predicate<? super Map.Entry<HeaderName,String>> filter)
      Removes all header entries that match the given predicate. Do not access the header list from inside the Predicate.test(T) body.
      Returns:
      if any elements were removed.
    • entries

      public Collection<Header> entries()
      Returns the collection of headers.
    • keySet

      public Set<HeaderName> keySet()
      Returns a set of header names found in this headers object. If there are duplicate header names, the first one present takes precedence.
    • contains

      public boolean contains(String headerName)
      Returns if there is a header entry that matches the given name.
    • contains

      public boolean contains(HeaderName headerName)
      Returns if there is a header entry that matches the given name.
    • contains

      public boolean contains(String headerName, String value)
      Returns if there is a header entry that matches the given name and value.
    • contains

      public boolean contains(HeaderName headerName, String value)
      Returns if there is a header entry that matches the given name and value.
    • size

      public int size()
      Returns the number of header entries.
    • hashCode

      public int hashCode()
      This method should only be used for testing, as it is expensive to call.
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Equality on headers is not clearly defined, but this method makes an attempt to do so. This method should only be used for testing, as it is expensive to call. Two headers object are considered equal if they have the same, normalized header names, and have the corresponding header values in the same order.
      Overrides:
      equals in class Object
    • toString

      public String toString()
      This is used for debugging. It is fairly expensive to construct, so don't call it on a hot path.
      Overrides:
      toString in class Object