Package com.linecorp.armeria.common
Interface HttpHeaders
- All Superinterfaces:
HttpObject
,Iterable<Map.Entry<io.netty.util.AsciiString,
String>>
- All Known Subinterfaces:
RequestHeaders
,ResponseHeaders
Immutable HTTP/2 headers.
Building a new
Building a new
Specifying a non-
Using
Using
Building a new HttpHeaders
You can use the HttpHeaders.of()
factory methods or
the HttpHeadersBuilder
to build a new HttpHeaders
from scratch:
// Using of()
HttpHeaders headersWithOf =
HttpHeaders.of(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=utf-8",
HttpHeaderNames.CONTENT_LENGTH, "42");
// Using builder()
HttpHeaders headersWithBuilder =
HttpHeaders.builder()
.add(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=utf-8")
.add(HttpHeaderNames.CONTENT_LENGTH, "42")
.build();
assert headersWithOf.equals(headersWithBuilder);
Building a new HttpHeaders
from an existing one
You can use toBuilder()
or withMutations(Consumer)
to build
a new HttpHeaders
derived from an existing one:
HttpHeaders headers = HttpHeaders.of("name1", "value0");
// Using toBuilder()
HttpHeaders headersWithToBuilder = headers.toBuilder()
.set("name1", "value1")
.add("name2", "value2")
.build();
// Using withMutations()
HttpHeaders headersWithMutations = headers.withMutations(builder -> {
builder.set("name1", "value1");
builder.add("name2", "value2");
});
assert headersWithToBuilder.equals(headersWithMutations);
// Note that the original headers remain unmodified.
assert !headers.equals(headersWithToBuilder);
assert !headers.equals(headersWithMutations);
Specifying a non-String
header value
Certain header values are better represented as a Java object than as a String
.
For example, it is more convenient to specify "content-length"
, "content-type"
and
"date"
header as Integer
, MediaType
and Instant
(or Date
)
respectively. Armeria's HTTP header API allows you to specify a Java object of well-known type
as a header value by converting it into an HTTP-friendly String
representation:
Number
,CharSequence
andMediaType
- Converted via
toString()
- e.g.
"42"
,"string"
,"text/plain; charset=utf-8"
- Converted via
CacheControl
andContentDisposition
- Converted via
asHeaderValue()
- e.g.
"no-cache, no-store, max-age=0, must-revalidate"
,"form-data; name=\"fieldName\""
- Converted via
Instant
,TemporalAccessor
,Date
andCalendar
- Converted into a time and date string as specified in RFC1123
- e.g.
Sun, 27 Nov 2016 19:37:15 UTC
- All other types
- Converted via
toString()
- Converted via
Using HttpHeaders.of()
factory methods
HttpHeaders headers =
HttpHeaders.of(HttpHeaderNames.CONTENT_LENGTH, 42,
HttpHeaderNames.CONTENT_TYPE, MediaType.JSON_UTF_8,
HttpHeaderNames.DATE, Instant.now());
Using HttpHeadersBuilder
HttpHeaders headers =
HttpHeaders.builder()
.setObject(HttpHeaderNames.CONTENT_LENGTH, 42)
.setObject(HttpHeaderNames.CONTENT_TYPE, MediaType.JSON_UTF_8)
.setObject(HttpHeaderNames.DATE, Instant.now())
.build();
Specifying value type explicitly
You might prefer type-safe setters for more efficiency and less ambiguity:
HttpHeaders headers =
HttpHeaders.builder()
.setInt(HttpHeaderNames.CONTENT_LENGTH, 42)
.set(HttpHeaderNames.CONTENT_TYPE, MediaType.JSON_UTF_8.toString())
.setTimeMillis(HttpHeaderNames.DATE, System.currentTimeMillis())
.build();
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic HttpHeadersBuilder
builder()
Returns a new empty builder.boolean
contains
(CharSequence name) Returnstrue
if a header with thename
exists,false
otherwise.boolean
contains
(CharSequence name, String value) Returnstrue
if a header with thename
andvalue
exists.boolean
containsBoolean
(CharSequence name, boolean value) Returnstrue
if a header with thename
andvalue
exists.boolean
containsDouble
(CharSequence name, double value) Returnstrue
if a header with thename
andvalue
exists.boolean
containsFloat
(CharSequence name, float value) Returnstrue
if a header with thename
andvalue
exists.boolean
containsInt
(CharSequence name, int value) Returnstrue
if a header with thename
andvalue
exists.boolean
containsLong
(CharSequence name, long value) Returnstrue
if a header with thename
andvalue
exists.boolean
containsObject
(CharSequence name, Object value) Returnstrue
if a header with thename
andvalue
exists.boolean
containsTimeMillis
(CharSequence name, long value) Returnstrue
if a header with thename
andvalue
exists.Returns the parsed"content-disposition"
header.long
Returns the value of the content-length header, or-1
if this value is not known.Returns the parsed"content-type"
header.void
forEach
(BiConsumer<io.netty.util.AsciiString, String> action) Invokes the specifiedaction
for all header entries.void
forEachValue
(CharSequence name, Consumer<String> action) Invokes the specifiedaction
for all values of the headers with the specifiedname
.get
(CharSequence name) Returns the value of a header with the specifiedname
.get
(CharSequence name, String defaultValue) Returns the value of a header with the specifiedname
.getAll
(CharSequence name) Returns all values for the header with the specified name.getBoolean
(CharSequence name) Returns theboolean
value of a header with the specifiedname
.boolean
getBoolean
(CharSequence name, boolean defaultValue) Returns theboolean
value of a header with the specifiedname
.getDouble
(CharSequence name) Returns thedouble
value of a header with the specifiedname
.double
getDouble
(CharSequence name, double defaultValue) Returns thedouble
value of a header with the specifiedname
.getFloat
(CharSequence name) Returns thefloat
value of a header with the specifiedname
.float
getFloat
(CharSequence name, float defaultValue) Returns thefloat
value of a header with the specifiedname
.getInt
(CharSequence name) Returns theint
value of a header with the specifiedname
.int
getInt
(CharSequence name, int defaultValue) Returns theint
value of a header with the specifiedname
.getLast
(CharSequence name) Returns the value of a header with the specifiedname
.getLast
(CharSequence name, String defaultValue) Returns the value of a header with the specifiedname
.getLastBoolean
(CharSequence name) Returns theboolean
value of a header with the specifiedname
.boolean
getLastBoolean
(CharSequence name, boolean defaultValue) Returns theboolean
value of a header with the specifiedname
.getLastDouble
(CharSequence name) Returns thedouble
value of a header with the specifiedname
.double
getLastDouble
(CharSequence name, double defaultValue) Returns thedouble
value of a header with the specifiedname
.getLastFloat
(CharSequence name) Returns thefloat
value of a header with the specifiedname
.float
getLastFloat
(CharSequence name, float defaultValue) Returns thefloat
value of a header with the specifiedname
.getLastInt
(CharSequence name) Returns theint
value of a header with the specifiedname
.int
getLastInt
(CharSequence name, int defaultValue) Returns theint
value of a header with the specifiedname
.getLastLong
(CharSequence name) Returns thelong
value of a header with the specifiedname
.long
getLastLong
(CharSequence name, long defaultValue) Returns thelong
value of a header with the specifiedname
.Returns the value of a header with the specifiedname
in milliseconds.long
getLastTimeMillis
(CharSequence name, long defaultValue) Returns the value of a header with the specifiedname
in milliseconds.getLong
(CharSequence name) Returns thelong
value of a header with the specifiedname
.long
getLong
(CharSequence name, long defaultValue) Returns thelong
value of a header with the specifiedname
.getTimeMillis
(CharSequence name) Returns the value of a header with the specifiedname
in milliseconds.long
getTimeMillis
(CharSequence name, long defaultValue) Returns the value of a header with the specifiedname
in milliseconds.boolean
Returns whether the content length is unknown.boolean
isEmpty()
Returnstrue
if this headers does not contain any entries.boolean
Tells whether the headers correspond to the last frame in an HTTP/2 stream.iterator()
Returns anIterator
that yields all header entries.Set<io.netty.util.AsciiString>
names()
Returns aSet
of all header names.static HttpHeaders
of()
Returns an emptyHttpHeaders
.static HttpHeaders
of
(CharSequence name, Object value) Returns a newHttpHeaders
with the specified header.static HttpHeaders
of
(CharSequence name1, Object value1, CharSequence name2, Object value2) Returns a newHttpHeaders
with the specified headers.static HttpHeaders
of
(CharSequence name1, Object value1, CharSequence name2, Object value2, CharSequence name3, Object value3) Returns a newHttpHeaders
with the specified headers.static HttpHeaders
of
(CharSequence name1, Object value1, CharSequence name2, Object value2, CharSequence name3, Object value3, CharSequence name4, Object value4) Returns a newHttpHeaders
with the specified headers.static HttpHeaders
of
(CharSequence name, String value) Returns a newHttpHeaders
with the specified header.static HttpHeaders
of
(CharSequence name1, String value1, CharSequence name2, String value2) Returns a newHttpHeaders
with the specified headers.static HttpHeaders
of
(CharSequence name1, String value1, CharSequence name2, String value2, CharSequence name3, String value3) Returns a newHttpHeaders
with the specified headers.static HttpHeaders
of
(CharSequence name1, String value1, CharSequence name2, String value2, CharSequence name3, String value3, CharSequence name4, String value4) Returns a newHttpHeaders
with the specified headers.int
size()
Returns the number of headers.stream()
Returns aStream
that yields all header entries.Returns a new builder created from the entries of this headers.valueIterator
(CharSequence name) Returns anIterator
that yields all values of the headers with the specifiedname
.valueStream
(CharSequence name) Returns aStream
that yields all values of the headers with the specifiedname
.default HttpHeaders
withMutations
(Consumer<HttpHeadersBuilder> mutator) Returns a new headers which is the result from the mutation by the specifiedConsumer
.Methods inherited from interface com.linecorp.armeria.common.HttpObject
isEndOfStream
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Method Details
-
builder
Returns a new empty builder. -
of
Returns an emptyHttpHeaders
. -
of
Returns a newHttpHeaders
with the specified header. -
of
Returns a newHttpHeaders
with the specified header. The value is converted into aString
as explained in Specifying a non-String header value. -
of
Returns a newHttpHeaders
with the specified headers. -
of
Returns a newHttpHeaders
with the specified headers. The values are converted intoString
s as explained in Specifying a non-String header value. -
of
static HttpHeaders of(CharSequence name1, String value1, CharSequence name2, String value2, CharSequence name3, String value3) Returns a newHttpHeaders
with the specified headers. -
of
static HttpHeaders of(CharSequence name1, Object value1, CharSequence name2, Object value2, CharSequence name3, Object value3) Returns a newHttpHeaders
with the specified headers. The values are converted intoString
s as explained in Specifying a non-String header value. -
of
static HttpHeaders of(CharSequence name1, String value1, CharSequence name2, String value2, CharSequence name3, String value3, CharSequence name4, String value4) Returns a newHttpHeaders
with the specified headers. -
of
static HttpHeaders of(CharSequence name1, Object value1, CharSequence name2, Object value2, CharSequence name3, Object value3, CharSequence name4, Object value4) Returns a newHttpHeaders
with the specified headers. The values are converted intoString
s as explained in Specifying a non-String header value. -
toBuilder
HttpHeadersBuilder toBuilder()Returns a new builder created from the entries of this headers.- See Also:
-
withMutations
Returns a new headers which is the result from the mutation by the specifiedConsumer
. This method is a shortcut for:builder = toBuilder(); mutator.accept(builder); return builder.build();
- See Also:
-
isEndOfStream
boolean isEndOfStream()Tells whether the headers correspond to the last frame in an HTTP/2 stream. -
contentLength
long contentLength()Returns the value of the content-length header, or-1
if this value is not known. -
isContentLengthUnknown
Returns whether the content length is unknown. Iftrue
,content-length
header is not automatically updated. -
contentType
Returns the parsed"content-type"
header.- Returns:
- the parsed
MediaType
if present and valid, ornull
otherwise.
-
contentDisposition
Returns the parsed"content-disposition"
header.- Returns:
- the parsed
MediaType
if present and valid.null
if not present or failed to parse"content-disposition"
header.
-
get
Returns the value of a header with the specifiedname
. If there are more than one value for the specifiedname
, the first value in insertion order is returned.- Parameters:
name
- the name of the header to retrieve- Returns:
- the first header value if the header is found, or
null
if there's no such header
-
get
Returns the value of a header with the specifiedname
. If there are more than one value for the specifiedname
, the first value in insertion order is returned.- Parameters:
name
- the name of the header to retrievedefaultValue
- the default value- Returns:
- the first header value or
defaultValue
if there is no such header
-
getLast
Returns the value of a header with the specifiedname
. If there are more than one value for the specifiedname
, the last value in insertion order is returned.- Parameters:
name
- the name of the header to retrieve- Returns:
- the last header value if the header is found, or
null
if there's no such header
-
getLast
Returns the value of a header with the specifiedname
. If there are more than one value for the specifiedname
, the last value in insertion order is returned.- Parameters:
name
- the name of the header to retrievedefaultValue
- the default value- Returns:
- the last header value or
defaultValue
if there is no such header
-
getAll
Returns all values for the header with the specified name. The returnedList
can't be modified. -
getBoolean
Returns theboolean
value of a header with the specifiedname
. If there are more than one value for the specifiedname
, the first value in insertion order is returned.- Parameters:
name
- the name of the header to retrieve- Returns:
true
if the first value in insertion order is one of"true", "TRUE", "1"
.false
if the first value in insertion order is one of"false", "FALSE", "0"
.null
if there is no such header or it can't be converted toboolean
.
-
getBoolean
Returns theboolean
value of a header with the specifiedname
. If there are more than one value for the specifiedname
, the first value in insertion order is returned.- Parameters:
name
- the name of the header to retrievedefaultValue
- the default value- Returns:
true
if the first value in insertion order is one of"true", "TRUE", "1"
.false
if the first value in insertion order is one of"false", "FALSE", "0"
.defaultValue
if there is no such header or it can't be converted toboolean
.
-
getLastBoolean
Returns theboolean
value of a header with the specifiedname
. If there are more than one value for the specifiedname
, the last value in insertion order is returned.- Parameters:
name
- the name of the header to retrieve- Returns:
true
if the last value in insertion order is one of"true", "TRUE", "1"
.false
if the last value in insertion order is one of"false", "FALSE", "0"
.null
if there is no such header or it can't be converted toboolean
.
-
getLastBoolean
Returns theboolean
value of a header with the specifiedname
. If there are more than one value for the specifiedname
, the last value in insertion order is returned.- Parameters:
name
- the name of the header to retrievedefaultValue
- the default value- Returns:
true
if the last value in insertion order is one of"true", "TRUE", "1"
.false
if the last value in insertion order is one of"false", "FALSE", "0"
.defaultValue
if there is no such header or it can't be converted toboolean
.
-
getLastInt
Returns theint
value of a header with the specifiedname
. If there are more than one value for the specifiedname
, the last value in insertion order is returned.- Parameters:
name
- the name of the header to retrieve- Returns:
- the
int
value of the last value in insertion order ornull
if there is no such header or it can't be converted toint
.
-
getLastInt
Returns theint
value of a header with the specifiedname
. If there are more than one value for the specifiedname
, the last value in insertion order is returned.- Parameters:
name
- the name of the header to retrievedefaultValue
- the default value- Returns:
- the
int
value of the last value in insertion order ordefaultValue
if there is no such header or it can't be converted toint
.
-
getInt
Returns theint
value of a header with the specifiedname
. If there are more than one value for the specifiedname
, the first value in insertion order is returned.- Parameters:
name
- the name of the header to retrieve- Returns:
- the
int
value of the first value in insertion order ornull
if there is no such header or it can't be converted toint
.
-
getInt
Returns theint
value of a header with the specifiedname
. If there are more than one value for the specifiedname
, the first value in insertion order is returned.- Parameters:
name
- the name of the header to retrievedefaultValue
- the default value- Returns:
- the
int
value of the first value in insertion order ordefaultValue
if there is no such header or it can't be converted toint
.
-
getLong
Returns thelong
value of a header with the specifiedname
. If there are more than one value for the specifiedname
, the first value in insertion order is returned.- Parameters:
name
- the name of the header to retrieve- Returns:
- the
long
value of the first value in insertion order ornull
if there is no such header or it can't be converted tolong
.
-
getLong
Returns thelong
value of a header with the specifiedname
. If there are more than one value for the specifiedname
, the first value in insertion order is returned.- Parameters:
name
- the name of the header to retrievedefaultValue
- the default value- Returns:
- the
long
value of the first value in insertion order ordefaultValue
if there is no such header or it can't be converted tolong
.
-
getLastLong
Returns thelong
value of a header with the specifiedname
. If there are more than one value for the specifiedname
, the last value in insertion order is returned.- Parameters:
name
- the name of the header to retrieve- Returns:
- the
long
value of the last value in insertion order ornull
if there is no such header or it can't be converted tolong
.
-
getLastLong
Returns thelong
value of a header with the specifiedname
. If there are more than one value for the specifiedname
, the last value in insertion order is returned.- Parameters:
name
- the name of the header to retrievedefaultValue
- the default value- Returns:
- the
long
value of the last value in insertion order ordefaultValue
if there is no such header or it can't be converted tolong
.
-
getFloat
Returns thefloat
value of a header with the specifiedname
. If there are more than one value for the specifiedname
, the first value in insertion order is returned.- Parameters:
name
- the name of the header to retrieve- Returns:
- the
float
value of the first value in insertion order ornull
if there is no such header or it can't be converted tofloat
.
-
getFloat
Returns thefloat
value of a header with the specifiedname
. If there are more than one value for the specifiedname
, the first value in insertion order is returned.- Parameters:
name
- the name of the header to retrievedefaultValue
- the default value- Returns:
- the
float
value of the first value in insertion order ordefaultValue
if there is no such header or it can't be converted tofloat
.
-
getLastFloat
Returns thefloat
value of a header with the specifiedname
. If there are more than one value for the specifiedname
, the last value in insertion order is returned.- Parameters:
name
- the name of the header to retrieve- Returns:
- the
float
value of the last value in insertion order ornull
if there is no such header or it can't be converted tofloat
.
-
getLastFloat
Returns thefloat
value of a header with the specifiedname
. If there are more than one value for the specifiedname
, the last value in insertion order is returned.- Parameters:
name
- the name of the header to retrievedefaultValue
- the default value- Returns:
- the
float
value of the last value in insertion order ordefaultValue
if there is no such header or it can't be converted tofloat
.
-
getDouble
Returns thedouble
value of a header with the specifiedname
. If there are more than one value for the specifiedname
, the first value in insertion order is returned.- Parameters:
name
- the name of the header to retrieve- Returns:
- the
double
value of the first value in insertion order ornull
if there is no such header or it can't be converted todouble
.
-
getDouble
Returns thedouble
value of a header with the specifiedname
. If there are more than one value for the specifiedname
, the first value in insertion order is returned.- Parameters:
name
- the name of the header to retrievedefaultValue
- the default value- Returns:
- the
double
value of the first value in insertion order ordefaultValue
if there is no such header or it can't be converted todouble
.
-
getLastDouble
Returns thedouble
value of a header with the specifiedname
. If there are more than one value for the specifiedname
, the last value in insertion order is returned.- Parameters:
name
- the name of the header to retrieve- Returns:
- the
double
value of the last value in insertion order ornull
if there is no such header or it can't be converted todouble
.
-
getLastDouble
Returns thedouble
value of a header with the specifiedname
. If there are more than one value for the specifiedname
, the last value in insertion order is returned.- Parameters:
name
- the name of the header to retrievedefaultValue
- the default value- Returns:
- the
double
value of the last value in insertion order ordefaultValue
if there is no such header or it can't be converted todouble
.
-
getTimeMillis
Returns the value of a header with the specifiedname
in milliseconds. If there are more than one value for the specifiedname
, the first value in insertion order is returned.- Parameters:
name
- the name of the header to retrieve- Returns:
- the milliseconds value of the first value in insertion order or
null
if there is no such header or it can't be converted to milliseconds.
-
getTimeMillis
Returns the value of a header with the specifiedname
in milliseconds. If there are more than one value for the specifiedname
, the first value in insertion order is returned.- Parameters:
name
- the name of the header to retrievedefaultValue
- the default value- Returns:
- the milliseconds value of the first value in insertion order or
defaultValue
if there is no such header or it can't be converted to milliseconds.
-
getLastTimeMillis
Returns the value of a header with the specifiedname
in milliseconds. If there are more than one value for the specifiedname
, the last value in insertion order is returned.- Parameters:
name
- the name of the header to retrieve- Returns:
- the milliseconds value of the last value in insertion order or
null
if there is no such header or it can't be converted to milliseconds.
-
getLastTimeMillis
Returns the value of a header with the specifiedname
in milliseconds. If there are more than one value for the specifiedname
, the last value in insertion order is returned.- Parameters:
name
- the name of the header to retrievedefaultValue
- the default value- Returns:
- the milliseconds value of the last value in insertion order or
defaultValue
if there is no such header or it can't be converted to milliseconds.
-
contains
Returnstrue
if a header with thename
exists,false
otherwise.- Parameters:
name
- the header name
-
contains
Returnstrue
if a header with thename
andvalue
exists.- Parameters:
name
- the header namevalue
- the header value of the header to find
-
containsObject
Returnstrue
if a header with thename
andvalue
exists.- Parameters:
name
- the header namevalue
- the header value- Returns:
true
if the header exists.false
otherwise
-
containsBoolean
Returnstrue
if a header with thename
andvalue
exists.- Parameters:
name
- the header namevalue
- the header value- Returns:
true
if the header with thename
exist and value istrue
and header contains value that one of"true", "TRUE", "1"
or value isfalse
and header contains value that one of"false", "FALSE", "0"
.false
otherwise
-
containsInt
Returnstrue
if a header with thename
andvalue
exists.- Parameters:
name
- the header namevalue
- the header value- Returns:
true
if the header exists.false
otherwise
-
containsLong
Returnstrue
if a header with thename
andvalue
exists.- Parameters:
name
- the header namevalue
- the header value- Returns:
true
if the header exists.false
otherwise
-
containsFloat
Returnstrue
if a header with thename
andvalue
exists.- Parameters:
name
- the header namevalue
- the header value- Returns:
true
if the header exists.false
otherwise
-
containsDouble
Returnstrue
if a header with thename
andvalue
exists.- Parameters:
name
- the header namevalue
- the header value- Returns:
true
if the header exists.false
otherwise
-
containsTimeMillis
Returnstrue
if a header with thename
andvalue
exists.- Parameters:
name
- the header namevalue
- the header value- Returns:
true
if the header exists.false
otherwise
-
size
int size()Returns the number of headers. -
isEmpty
boolean isEmpty()Returnstrue
if this headers does not contain any entries. -
names
Set<io.netty.util.AsciiString> names() -
iterator
Returns anIterator
that yields all header entries. The iteration order is as follows:- All pseudo headers (order not specified).
- All non-pseudo headers (in insertion order).
-
valueIterator
Returns anIterator
that yields all values of the headers with the specifiedname
. -
forEach
Invokes the specifiedaction
for all header entries. -
forEachValue
Invokes the specifiedaction
for all values of the headers with the specifiedname
. -
stream
Returns aStream
that yields all header entries. -
valueStream
Returns aStream
that yields all values of the headers with the specifiedname
.
-