- java.lang.Object
-
- java.net.http.HttpHeaders
-
public final class HttpHeaders extends Object
A read-only view of a set of HTTP headers.An
HttpHeadersis not typically created directly, but rather returned from anHttpRequestor anHttpResponse. Specific HTTP headers can be set for a request through one of the request builder'sheadersmethods.The methods of this class ( that accept a String header name ), and the
Mapreturned by themapmethod, operate without regard to case when retrieving the header value(s).An HTTP header name may appear more than once in the HTTP protocol. As such, headers are represented as a name and a list of values. Each occurrence of a header value is added verbatim, to the appropriate header name list, without interpreting its value. In particular,
HttpHeadersdoes not perform any splitting or joining of comma separated header value strings. The order of elements in a header value list is preserved whenbuildinga request. For responses, the order of elements in a header value list is the order in which they were received. TheMapreturned by themapmethod, however, does not provide any guarantee with regard to the ordering of its entries.HttpHeadersinstances are immutable.- Since:
- 11
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description List<String>allValues(String name)Returns an unmodifiable List of all of the header string values of the given named header.booleanequals(Object obj)Tests this HTTP headers instance for equality with the given object.Optional<String>firstValue(String name)Returns anOptionalcontaining the first header string value of the given named (and possibly multi-valued) header.OptionalLongfirstValueAsLong(String name)Returns anOptionalLongcontaining the first header string value of the named header field.inthashCode()Computes a hash code for this HTTP headers instance.Map<String,List<String>>map()Returns an unmodifiable multi Map view of this HttpHeaders.static HttpHeadersof(Map<String,List<String>> headerMap, BiPredicate<String,String> filter)Returns an HTTP headers from the given map.StringtoString()Returns this HTTP headers as a string.
-
-
-
Method Detail
-
firstValue
public Optional<String> firstValue(String name)
Returns anOptionalcontaining the first header string value of the given named (and possibly multi-valued) header. If the header is not present, then the returnedOptionalis empty.- Parameters:
name- the header name- Returns:
- an
Optional<String>containing the first named header string value, if present
-
firstValueAsLong
public OptionalLong firstValueAsLong(String name)
Returns anOptionalLongcontaining the first header string value of the named header field. If the header is not present, then the Optional is empty. If the header is present but contains a value that does not parse as aLongvalue, then an exception is thrown.- Parameters:
name- the header name- Returns:
- an
OptionalLong - Throws:
NumberFormatException- if a value is found, but does not parse as a Long
-
allValues
public List<String> allValues(String name)
Returns an unmodifiable List of all of the header string values of the given named header. Always returns a List, which may be empty if the header is not present.- Parameters:
name- the header name- Returns:
- a List of headers string values
-
map
public Map<String,List<String>> map()
Returns an unmodifiable multi Map view of this HttpHeaders.- Returns:
- the Map
-
equals
public final boolean equals(Object obj)
Tests this HTTP headers instance for equality with the given object.If the given object is not an
HttpHeadersthen this method returnsfalse. Two HTTP headers are equal if each of their corresponding maps are equal.This method satisfies the general contract of the
Object.equalsmethod.- Overrides:
equalsin classObject- Parameters:
obj- the object to which this object is to be compared- Returns:
trueif, and only if, the given object is anHttpHeadersthat is equal to this HTTP headers- See Also:
Object.hashCode()
-
hashCode
public final int hashCode()
Computes a hash code for this HTTP headers instance.The hash code is based upon the components of the HTTP headers
map, and satisfies the general contract of theObject.hashCodemethod.- Overrides:
hashCodein classObject- Returns:
- the hash-code value for this HTTP headers
- See Also:
Object.equals(java.lang.Object)
-
toString
public String toString()
Returns this HTTP headers as a string.
-
of
public static HttpHeaders of(Map<String,List<String>> headerMap, BiPredicate<String,String> filter)
Returns an HTTP headers from the given map. The given map's key represents the header name, and its value the list of string header values for that header name.An HTTP header name may appear more than once in the HTTP protocol. Such, multi-valued, headers must be represented by a single entry in the given map, whose entry value is a list that represents the multiple header string values. Leading and trailing whitespaces are removed from all string values retrieved from the given map and its lists before processing. Only headers that, after filtering, contain at least one, possibly empty string, value will be added to the HTTP headers.
- API Note:
- The primary purpose of this method is for testing frameworks.
Per-request headers can be set through one of the
HttpRequestheadersmethods. - Parameters:
headerMap- the map containing the header names and valuesfilter- a filter that can be used to inspect each header-name-and-value pair in the given map to determine if it should, or should not, be added to the to the HTTP headers- Returns:
- an HTTP headers instance containing the given headers
- Throws:
NullPointerException- if any of:headerMap, a key or value in the given map, or an entry in the map's value list, orfilter, isnullIllegalArgumentException- if the givenheaderMapcontains any two keys that are equal ( without regard to case ); or if the given map contains any key whose length, after trimming whitespaces, is0
-
-