Class HttpHeaders
- All Implemented Interfaces:
Iterable<HttpHeader>
-
Constructor Summary
ConstructorDescriptionCreate an empty HttpHeaders instance.HttpHeaders
(int initialCapacity) Create a HttpHeaders instance with an initialsize
empty headersHttpHeaders
(Iterable<HttpHeader> headers) Create a HttpHeaders instance with the provided initial headers.HttpHeaders
(Map<String, String> headers) Create a HttpHeaders instance with the provided initial headers. -
Method Summary
Modifier and TypeMethodDescriptionadd
(HttpHeaderName name, String value) Adds aheader
with the given name and value if a header with that name doesn't already exist, otherwise adds thevalue
to the existing header.Deprecated.get
(HttpHeaderName name) Gets theheader
for the provided header name.Deprecated.Useget(HttpHeaderName)
as it provides better performance.int
getSize()
Gets the number of headers in the collection.getValue
(HttpHeaderName name) Get the value for the provided header name.Deprecated.UsegetValue(HttpHeaderName)
as it provides better performance.String[]
getValues
(HttpHeaderName name) Get the values for the provided header name.String[]
Deprecated.UsegetValue(HttpHeaderName)
as it provides better performance.iterator()
Deprecated.Useset(HttpHeaderName, String)
instead.remove
(HttpHeaderName name) Removes theheader
with the provided header name.Deprecated.Useremove(HttpHeaderName)
as it provides better performance.set
(HttpHeaderName name, String value) Sets aheader
with the given name and value.set
(HttpHeaderName name, List<String> values) Sets aheader
with the given name and the list of values provided, such that the given values will be comma-separated when necessary.Deprecated.Useset(HttpHeaderName, String)
as it provides better performance.Deprecated.Useset(HttpHeaderName, List)
as it provides better performance.Sets all provided header key/values pairs into this HttpHeaders instance.setAllHttpHeaders
(HttpHeaders headers) Sets all headers from the passedheaders
into thisHttpHeaders
.stream()
Get aStream
representation of the HttpHeader values in this instance.toMap()
Returns a copy of the http headers as an unmodifiableMap
representation of the state of the headers at the time of the toMap call.toString()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
HttpHeaders
public HttpHeaders()Create an empty HttpHeaders instance. -
HttpHeaders
Create a HttpHeaders instance with the provided initial headers.- Parameters:
headers
- the map of initial headers
-
HttpHeaders
Create a HttpHeaders instance with the provided initial headers.- Parameters:
headers
- the collection of initial headers
-
HttpHeaders
public HttpHeaders(int initialCapacity) Create a HttpHeaders instance with an initialsize
empty headers- Parameters:
initialCapacity
- the initial capacity of headers map.
-
-
Method Details
-
getSize
public int getSize()Gets the number of headers in the collection.- Returns:
- the number of headers in this collection.
-
add
Deprecated.Useadd(HttpHeaderName, String)
as it provides better performance.Adds aheader
with the given name and value if a header with that name doesn't already exist, otherwise adds thevalue
to the existing header.- Parameters:
name
- The name of the header.value
- The value of the header.- Returns:
- The updated HttpHeaders object.
-
add
Adds aheader
with the given name and value if a header with that name doesn't already exist, otherwise adds thevalue
to the existing header.- Parameters:
name
- The name of the header.value
- The value of the header.- Returns:
- The updated HttpHeaders object.
-
put
Deprecated.Useset(HttpHeaderName, String)
instead.Sets aheader
with the given name and value.If header with same name already exists then the value will be overwritten.
- Parameters:
name
- the namevalue
- the value- Returns:
- The updated HttpHeaders object
-
set
Deprecated.Useset(HttpHeaderName, String)
as it provides better performance.Sets aheader
with the given name and value. If a header with same name already exists then the value will be overwritten. If the given value is null, the header with the given name will be removed.- Parameters:
name
- the name to set in the header. If it is null, this method will return with no changes to the headers.value
- the value- Returns:
- The updated HttpHeaders object
-
set
Sets aheader
with the given name and value. If a header with same name already exists then the value will be overwritten. If the given value is null, the header with the given name will be removed.- Parameters:
name
- the name to set in the header. If it is null, this method will return with no changes to the headers.value
- the value- Returns:
- The updated HttpHeaders object
-
set
Deprecated.Useset(HttpHeaderName, List)
as it provides better performance.Sets aheader
with the given name and the list of values provided, such that the given values will be comma-separated when necessary. If a header with same name already exists then the values will be overwritten. If the given values list is null, the header with the given name will be removed.- Parameters:
name
- the namevalues
- the values that will be comma-separated as appropriate- Returns:
- The updated HttpHeaders object
-
set
Sets aheader
with the given name and the list of values provided, such that the given values will be comma-separated when necessary. If a header with same name already exists then the values will be overwritten. If the given values list is null, the header with the given name will be removed.- Parameters:
name
- the namevalues
- the values that will be comma-separated as appropriate- Returns:
- The updated HttpHeaders object
-
setAll
Sets all provided header key/values pairs into this HttpHeaders instance. This is equivalent to callingheaders.forEach(this::set)
, and therefore the behavior is as specified inset(String, List)
. In other words, this will create a header for each key in the provided map, replacing or removing an existing one, depending on the value. If the given values list is null, the header with the given name will be removed. If the given name is already a header, it will be removed and replaced with the headers provided.Use
setAllHttpHeaders(HttpHeaders)
if you already have an instance ofHttpHeaders
as it provides better performance.- Parameters:
headers
- a map containing keys representing header names, and keys representing the associated values.- Returns:
- The updated HttpHeaders object
- Throws:
NullPointerException
- Ifheaders
is null.
-
setAllHttpHeaders
Sets all headers from the passedheaders
into thisHttpHeaders
.This is the equivalent to calling
headers.forEach(header -> set(header.getName(), header.getValuesList())
and therefore the behavior is as specified inset(String, List)
.If
headers
is null this is a no-op.- Parameters:
headers
- The headers to add into thisHttpHeaders
.- Returns:
- The updated HttpHeaders object.
-
get
Deprecated.Useget(HttpHeaderName)
as it provides better performance.Gets theheader
for the provided header name. null is returned if the header isn't found.- Parameters:
name
- the name of the header to find.- Returns:
- the header if found, null otherwise.
-
get
Gets theheader
for the provided header name. null is returned if the header isn't found.- Parameters:
name
- the name of the header to find.- Returns:
- the header if found, null otherwise.
-
remove
Deprecated.Useremove(HttpHeaderName)
as it provides better performance.Removes theheader
with the provided header name. null is returned if the header isn't found.- Parameters:
name
- the name of the header to remove.- Returns:
- the header if removed, null otherwise.
-
remove
Removes theheader
with the provided header name. null is returned if the header isn't found.- Parameters:
name
- the name of the header to remove.- Returns:
- the header if removed, null otherwise.
-
getValue
Deprecated.UsegetValue(HttpHeaderName)
as it provides better performance.Get the value for the provided header name. null is returned if the header name isn't found.- Parameters:
name
- the name of the header whose value is being retrieved.- Returns:
- the value of the header, or null if the header isn't found
-
getValue
Get the value for the provided header name. null is returned if the header name isn't found.- Parameters:
name
- the name of the header whose value is being retrieved.- Returns:
- the value of the header, or null if the header isn't found
-
getValues
Deprecated.UsegetValue(HttpHeaderName)
as it provides better performance.Get the values for the provided header name. null is returned if the header name isn't found.This returns
getValue
split bycomma
.- Parameters:
name
- the name of the header whose value is being retrieved.- Returns:
- the values of the header, or null if the header isn't found
-
getValues
Get the values for the provided header name. null is returned if the header name isn't found.This returns
getValue
split bycomma
.- Parameters:
name
- the name of the header whose value is being retrieved.- Returns:
- the values of the header, or null if the header isn't found
-
toMap
Returns a copy of the http headers as an unmodifiableMap
representation of the state of the headers at the time of the toMap call. This map will not change as the underlying http headers change, and nor will modifying the key or values contained in the map have any effect on the state of the http headers.Note that there may be performance implications of using Map APIs on the returned Map. It is highly recommended that users prefer to use alternate APIs present on the HttpHeaders class, over using APIs present on the returned Map class. For example, use the
get(String)
API, rather thanhttpHeaders.toMap().get(name)
.- Returns:
- the headers in a copied and unmodifiable form.
-
iterator
- Specified by:
iterator
in interfaceIterable<HttpHeader>
-
stream
Get aStream
representation of the HttpHeader values in this instance.- Returns:
- A
Stream
of all header values in this instance.
-
toString
-
add(HttpHeaderName, String)
as it provides better performance.