Class HttpHeaderMap

java.lang.Object
com.helger.commons.http.HttpHeaderMap
All Implemented Interfaces:
ICommonsIterable<Map.Entry<String,ICommonsList<String>>>, ICloneable<HttpHeaderMap>, IHasSize, IClearable, Iterable<Map.Entry<String,ICommonsList<String>>>

@NotThreadSafe public class HttpHeaderMap extends Object implements IHasSize, ICommonsIterable<Map.Entry<String,ICommonsList<String>>>, ICloneable<HttpHeaderMap>, IClearable
Abstracts HTTP header interface for external usage.
Note: since version 9.1.8 (issue #11) the internal scheme changed and the original case is stored. Older versions always stored the lower case header names. The implications are that the casing of the first header is sustained. So if the first API call uses name "Foo" and the second is "foo" they both refer to the same header name and "Foo" will be the name that is retrieved.
Since:
9.0.0
Author:
Philip Helger
  • Field Details

    • SEPARATOR_KEY_VALUE

      public static final String SEPARATOR_KEY_VALUE
      The separator between key and value
      See Also:
    • DEFAULT_QUOTE_IF_NECESSARY

      public static final boolean DEFAULT_QUOTE_IF_NECESSARY
      Default quote if necessary: false (since v10)
      See Also:
  • Constructor Details

    • HttpHeaderMap

      public HttpHeaderMap()
      Default constructor.
    • HttpHeaderMap

      public HttpHeaderMap(@Nonnull HttpHeaderMap aOther)
      Copy constructor.
      Parameters:
      aOther - Map to copy from. May not be null.
  • Method Details

    • getUnifiedValue

      @Nonnull public static String getUnifiedValue(@Nullable String sValue)
      Avoid having header values spanning multiple lines. This has been deprecated by RFC 7230 and Jetty 9.3 refuses to parse these requests with HTTP 400 by default.
      Since v9.3.6 this method also takes care of quoting header values correctly. If the value does not correspond to a token according to RFC 2616 chapter 2.2, the value is enclosed in double quotes.
      Parameters:
      sValue - The source header value. May be null.
      Returns:
      The unified header value without \r, \n and \t. Never null.
    • getUnifiedValue

      @Nonnull public static String getUnifiedValue(@Nullable String sValue, boolean bQuoteIfNecessary)
      Avoid having header values spanning multiple lines. This has been deprecated by RFC 7230 and Jetty 9.3 refuses to parse these requests with HTTP 400 by default.
      Parameters:
      sValue - The source header value. May be null.
      bQuoteIfNecessary - true if automatic quoting according to RFC 2616, chapter 2.2 should be used if necessary.
      Returns:
      The unified header value without \r, \n and \t. Never null.
      Since:
      9.3.6
    • removeAll

      @Nonnull public EChange removeAll()
      Remove all contained headers.
      Specified by:
      removeAll in interface IClearable
      Returns:
      EChange.
    • setHeader

      public void setHeader(@Nonnull @Nonempty String sName, @Nullable String sValue)
      Set the passed header as is.
      Parameters:
      sName - Header name. May neither be null nor empty.
      sValue - The value to be set. May be null in which case nothing happens.
    • addHeader

      public void addHeader(@Nonnull @Nonempty String sName, @Nullable String sValue)
      Add the passed header as is.
      Parameters:
      sName - Header name. May neither be null nor empty.
      sValue - The value to be set. May be null in which case nothing happens.
    • getDateTimeAsString

      @Nonnull public static String getDateTimeAsString(@Nonnull ZonedDateTime aDT)
    • getDateTimeAsString

      @Nonnull public static String getDateTimeAsString(@Nonnull LocalDateTime aLDT)
    • setDateHeader

      public void setDateHeader(@Nonnull @Nonempty String sName, long nMillis)
      Set the passed header as a date header.
      Parameters:
      sName - Header name. May neither be null nor empty.
      nMillis - The milliseconds to set as a date.
    • setDateHeader

      public void setDateHeader(@Nonnull @Nonempty String sName, @Nonnull LocalDate aLD)
      Set the passed header as a date header.
      Parameters:
      sName - Header name. May neither be null nor empty.
      aLD - The LocalDate to set as a date. The time is set to start of day. May not be null.
    • setDateHeader

      public void setDateHeader(@Nonnull @Nonempty String sName, @Nonnull LocalDateTime aLDT)
      Set the passed header as a date header.
      Parameters:
      sName - Header name. May neither be null nor empty.
      aLDT - The LocalDateTime to set as a date. May not be null.
    • setDateHeader

      public void setDateHeader(@Nonnull @Nonempty String sName, @Nonnull ZonedDateTime aDT)
      Set the passed header as a date header.
      Parameters:
      sName - Header name. May neither be null nor empty.
      aDT - The DateTime to set as a date. May not be null.
    • addDateHeader

      public void addDateHeader(@Nonnull @Nonempty String sName, long nMillis)
      Add the passed header as a date header.
      Parameters:
      sName - Header name. May neither be null nor empty.
      nMillis - The milliseconds to set as a date.
    • addDateHeader

      public void addDateHeader(@Nonnull @Nonempty String sName, @Nonnull LocalDate aLD)
      Add the passed header as a date header.
      Parameters:
      sName - Header name. May neither be null nor empty.
      aLD - The LocalDate to set as a date. The time is set to start of day. May not be null.
    • addDateHeader

      public void addDateHeader(@Nonnull @Nonempty String sName, @Nonnull LocalDateTime aLDT)
      Add the passed header as a date header.
      Parameters:
      sName - Header name. May neither be null nor empty.
      aLDT - The LocalDateTime to set as a date. May not be null.
    • addDateHeader

      public void addDateHeader(@Nonnull @Nonempty String sName, @Nonnull ZonedDateTime aDT)
      Add the passed header as a date header.
      Parameters:
      sName - Header name. May neither be null nor empty.
      aDT - The DateTime to set as a date. May not be null.
    • setIntHeader

      public void setIntHeader(@Nonnull @Nonempty String sName, int nValue)
      Set the passed header as a number.
      Parameters:
      sName - Header name. May neither be null nor empty.
      nValue - The value to be set. May not be null.
    • addIntHeader

      public void addIntHeader(@Nonnull @Nonempty String sName, int nValue)
      Add the passed header as a number.
      Parameters:
      sName - Header name. May neither be null nor empty.
      nValue - The value to be set. May not be null.
    • setLongHeader

      public void setLongHeader(@Nonnull @Nonempty String sName, long nValue)
      Set the passed header as a number.
      Parameters:
      sName - Header name. May neither be null nor empty.
      nValue - The value to be set. May not be null.
    • addLongHeader

      public void addLongHeader(@Nonnull @Nonempty String sName, long nValue)
      Add the passed header as a number.
      Parameters:
      sName - Header name. May neither be null nor empty.
      nValue - The value to be set. May not be null.
    • setAllHeaders

      public void setAllHeaders(@Nonnull HttpHeaderMap aOther)
      Set all headers from the passed map. Existing headers with the same name are overwritten. Existing headers are not changed!
      Parameters:
      aOther - The header map to add. May not be null.
    • addAllHeaders

      public void addAllHeaders(@Nonnull HttpHeaderMap aOther)
      Add all headers from the passed map. Existing headers with the same name are extended.
      Parameters:
      aOther - The header map to add. May not be null.
    • getAllHeaders

    • getAllHeaderNames

      Returns:
      A copy of all contained header names. Never null.
    • getAllHeaderValues

      @Nonnull @ReturnsMutableCopy public ICommonsList<String> getAllHeaderValues(@Nullable String sName)
      Get all header values of a certain header name.
      Parameters:
      sName - The name to be searched.
      Returns:
      The list with all matching values. Never null but maybe empty.
    • getFirstHeaderValue

      @Nullable public String getFirstHeaderValue(@Nullable String sName)
      Get the first header value of a certain header name. The matching of the name happens case insensitive.
      Parameters:
      sName - The name to be searched. May be null.
      Returns:
      The first matching value or null.
    • getHeaderCombined

      @Nullable public String getHeaderCombined(@Nullable String sName, @Nonnull String sDelimiter)
      Get the header value as a combination of all contained values. The matching of the name happens case insensitive.
      Parameters:
      sName - The header name to retrieve. May be null.
      sDelimiter - The delimiter to be used. May not be null.
      Returns:
      null if no such header is contained.
    • containsHeaders

      public boolean containsHeaders(@Nullable String sName)
    • removeHeadersIf

      @Nonnull public EChange removeHeadersIf(@Nonnull Predicate<? super String> aNameFilter)
      Remove all header values where the name matches the provided filter.
      Parameters:
      aNameFilter - The name filter to be applied. May not be null.
      Returns:
      EChange
    • removeHeaders

      @Nonnull public EChange removeHeaders(@Nullable String sName)
      Remove all header values with the provided name
      Parameters:
      sName - The name to be removed. May be null.
      Returns:
      EChange
    • removeHeader

      @Nonnull public EChange removeHeader(@Nullable String sName, @Nullable String sValue)
    • iterator

      Specified by:
      iterator in interface Iterable<Map.Entry<String,ICommonsList<String>>>
    • size

      @Nonnegative public int size()
      Specified by:
      size in interface IHasSize
      Returns:
      The number of contained elements. Always ≥ 0.
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface IHasSize
      Returns:
      true if no items are present, false if at least a single item is present.
      See Also:
    • forEachSingleHeader

      public void forEachSingleHeader(@Nonnull BiConsumer<? super String,? super String> aConsumer, boolean bUnifyValue)
      Invoke the provided consumer for every name/value pair.
      Parameters:
      aConsumer - Consumer with key and unified value to be invoked. May not be null.
      bUnifyValue - true to unify the values, false if not
      Since:
      9.3.6
      See Also:
    • forEachSingleHeader

      public void forEachSingleHeader(@Nonnull BiConsumer<? super String,? super String> aConsumer, boolean bUnifyValue, boolean bQuoteIfNecessary)
      Invoke the provided consumer for every name/value pair.
      Parameters:
      aConsumer - Consumer with key and unified value to be invoked. May not be null.
      bUnifyValue - true to unify the values, false if not
      bQuoteIfNecessary - true to automatically quote values if it is necessary, false to not do it. This is only used, if "unify values" is true.
      Since:
      9.3.7
      See Also:
    • forEachHeaderLine

      public void forEachHeaderLine(@Nonnull Consumer<? super String> aConsumer, boolean bUnifyValue)
      Invoke the provided consumer for every header line.
      Parameters:
      aConsumer - Consumer with the assembled line to be invoked. May not be null.
      bUnifyValue - true to unify the values, false if not
      Since:
      9.3.6
      See Also:
    • forEachHeaderLine

      public void forEachHeaderLine(@Nonnull Consumer<? super String> aConsumer, boolean bUnifyValue, boolean bQuoteIfNecessary)
      Invoke the provided consumer for every header line.
      Parameters:
      aConsumer - Consumer with the assembled line to be invoked. May not be null.
      bUnifyValue - true to unify the values, false if not.
      bQuoteIfNecessary - true to automatically quote values if it is necessary, false to not do it. This is only used, if "unify values" is true.
      Since:
      9.3.7
      See Also:
    • getAllHeaderLines

      @Nonnull @ReturnsMutableCopy public ICommonsList<String> getAllHeaderLines(boolean bUnifyValue)
      Get all header lines as a list of strings.
      Parameters:
      bUnifyValue - true to unify the values, false if not
      Returns:
      Never null but maybe an empty list.
    • getAllHeaderLines

      @Nonnull @ReturnsMutableCopy public ICommonsList<String> getAllHeaderLines(boolean bUnifyValue, boolean bQuoteIfNecessary)
      Get all header lines as a list of strings.
      Parameters:
      bUnifyValue - true to unify the values, false if not
      bQuoteIfNecessary - true to automatically quote values if it is necessary, false to not do it. This is only used, if "unify values" is true.
      Returns:
      Never null but maybe an empty list.
      Since:
      9.3.7
    • setContentLength

      public void setContentLength(long nLength)
    • setContentType

      public void setContentType(@Nonnull String sContentType)
    • getClone

      Specified by:
      getClone in interface ICloneable<HttpHeaderMap>
      Returns:
      A 100% deep-copy of the implementing class.
    • getAsMapStringToListString

      @Nonnull public Map<String,List<String>> getAsMapStringToListString()
      Returns:
      The HTTP header map in a different representation. Never null but maybe empty.
      Since:
      10.0
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object