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,CharSequenceandMediaType- Converted via
toString() - e.g.
"42","string","text/plain; charset=utf-8"
- Converted via
CacheControlandContentDisposition- Converted via
asHeaderValue() - e.g.
"no-cache, no-store, max-age=0, must-revalidate","form-data; name=\"fieldName\""
- Converted via
Instant,TemporalAccessor,DateandCalendar- 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 HttpHeadersBuilderbuilder()Returns a new empty builder.booleancontains(CharSequence name) Returnstrueif a header with thenameexists,falseotherwise.booleancontains(CharSequence name, String value) Returnstrueif a header with thenameandvalueexists.booleancontainsBoolean(CharSequence name, boolean value) Returnstrueif a header with thenameandvalueexists.booleancontainsDouble(CharSequence name, double value) Returnstrueif a header with thenameandvalueexists.booleancontainsFloat(CharSequence name, float value) Returnstrueif a header with thenameandvalueexists.booleancontainsInt(CharSequence name, int value) Returnstrueif a header with thenameandvalueexists.booleancontainsLong(CharSequence name, long value) Returnstrueif a header with thenameandvalueexists.booleancontainsObject(CharSequence name, Object value) Returnstrueif a header with thenameandvalueexists.booleancontainsTimeMillis(CharSequence name, long value) Returnstrueif a header with thenameandvalueexists.Returns the parsed"content-disposition"header.longReturns the value of the content-length header, or-1if this value is not known.Returns the parsed"content-type"header.voidforEach(BiConsumer<io.netty.util.AsciiString, String> action) Invokes the specifiedactionfor all header entries.voidforEachValue(CharSequence name, Consumer<String> action) Invokes the specifiedactionfor 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 thebooleanvalue of a header with the specifiedname.booleangetBoolean(CharSequence name, boolean defaultValue) Returns thebooleanvalue of a header with the specifiedname.getDouble(CharSequence name) Returns thedoublevalue of a header with the specifiedname.doublegetDouble(CharSequence name, double defaultValue) Returns thedoublevalue of a header with the specifiedname.getFloat(CharSequence name) Returns thefloatvalue of a header with the specifiedname.floatgetFloat(CharSequence name, float defaultValue) Returns thefloatvalue of a header with the specifiedname.getInt(CharSequence name) Returns theintvalue of a header with the specifiedname.intgetInt(CharSequence name, int defaultValue) Returns theintvalue 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 thebooleanvalue of a header with the specifiedname.booleangetLastBoolean(CharSequence name, boolean defaultValue) Returns thebooleanvalue of a header with the specifiedname.getLastDouble(CharSequence name) Returns thedoublevalue of a header with the specifiedname.doublegetLastDouble(CharSequence name, double defaultValue) Returns thedoublevalue of a header with the specifiedname.getLastFloat(CharSequence name) Returns thefloatvalue of a header with the specifiedname.floatgetLastFloat(CharSequence name, float defaultValue) Returns thefloatvalue of a header with the specifiedname.getLastInt(CharSequence name) Returns theintvalue of a header with the specifiedname.intgetLastInt(CharSequence name, int defaultValue) Returns theintvalue of a header with the specifiedname.getLastLong(CharSequence name) Returns thelongvalue of a header with the specifiedname.longgetLastLong(CharSequence name, long defaultValue) Returns thelongvalue of a header with the specifiedname.Returns the value of a header with the specifiednamein milliseconds.longgetLastTimeMillis(CharSequence name, long defaultValue) Returns the value of a header with the specifiednamein milliseconds.getLong(CharSequence name) Returns thelongvalue of a header with the specifiedname.longgetLong(CharSequence name, long defaultValue) Returns thelongvalue of a header with the specifiedname.getTimeMillis(CharSequence name) Returns the value of a header with the specifiednamein milliseconds.longgetTimeMillis(CharSequence name, long defaultValue) Returns the value of a header with the specifiednamein milliseconds.booleanReturns whether the content length is unknown.booleanisEmpty()Returnstrueif this headers does not contain any entries.booleanTells whether the headers correspond to the last frame in an HTTP/2 stream.iterator()Returns anIteratorthat yields all header entries.Set<io.netty.util.AsciiString>names()Returns aSetof all header names.static HttpHeadersof()Returns an emptyHttpHeaders.static HttpHeadersof(CharSequence name, Object value) Returns a newHttpHeaderswith the specified header.static HttpHeadersof(CharSequence name1, Object value1, CharSequence name2, Object value2) Returns a newHttpHeaderswith the specified headers.static HttpHeadersof(CharSequence name1, Object value1, CharSequence name2, Object value2, CharSequence name3, Object value3) Returns a newHttpHeaderswith the specified headers.static HttpHeadersof(CharSequence name1, Object value1, CharSequence name2, Object value2, CharSequence name3, Object value3, CharSequence name4, Object value4) Returns a newHttpHeaderswith the specified headers.static HttpHeadersof(CharSequence name, String value) Returns a newHttpHeaderswith the specified header.static HttpHeadersof(CharSequence name1, String value1, CharSequence name2, String value2) Returns a newHttpHeaderswith the specified headers.static HttpHeadersof(CharSequence name1, String value1, CharSequence name2, String value2, CharSequence name3, String value3) Returns a newHttpHeaderswith the specified headers.static HttpHeadersof(CharSequence name1, String value1, CharSequence name2, String value2, CharSequence name3, String value3, CharSequence name4, String value4) Returns a newHttpHeaderswith the specified headers.intsize()Returns the number of headers.stream()Returns aStreamthat yields all header entries.Returns a new builder created from the entries of this headers.valueIterator(CharSequence name) Returns anIteratorthat yields all values of the headers with the specifiedname.valueStream(CharSequence name) Returns aStreamthat yields all values of the headers with the specifiedname.default HttpHeaderswithMutations(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
isEndOfStreamMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Method Details
-
builder
Returns a new empty builder. -
of
Returns an emptyHttpHeaders. -
of
Returns a newHttpHeaderswith the specified header. -
of
Returns a newHttpHeaderswith the specified header. The value is converted into aStringas explained in Specifying a non-String header value. -
of
Returns a newHttpHeaderswith the specified headers. -
of
Returns a newHttpHeaderswith the specified headers. The values are converted intoStrings 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 newHttpHeaderswith the specified headers. -
of
static HttpHeaders of(CharSequence name1, Object value1, CharSequence name2, Object value2, CharSequence name3, Object value3) Returns a newHttpHeaderswith the specified headers. The values are converted intoStrings 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 newHttpHeaderswith 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 newHttpHeaderswith the specified headers. The values are converted intoStrings 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-1if this value is not known. -
isContentLengthUnknown
Returns whether the content length is unknown. Iftrue,content-lengthheader is not automatically updated. -
contentType
Returns the parsed"content-type"header.- Returns:
- the parsed
MediaTypeif present and valid, ornullotherwise.
-
contentDisposition
Returns the parsed"content-disposition"header.- Returns:
- the parsed
MediaTypeif present and valid.nullif 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
nullif 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
defaultValueif 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
nullif 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
defaultValueif there is no such header
-
getAll
Returns all values for the header with the specified name. The returnedListcan't be modified. -
getBoolean
Returns thebooleanvalue 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
booleanvalue of the first value in insertion order ornullif there is no such header or it can't be converted toboolean.
-
getBoolean
Returns thebooleanvalue 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
booleanvalue of the first value in insertion order ordefaultValueif there is no such header or it can't be converted toboolean.
-
getLastBoolean
Returns thebooleanvalue 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
booleanvalue of the last value in insertion order ornullif there is no such header or it can't be converted toboolean.
-
getLastBoolean
Returns thebooleanvalue 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
booleanvalue of the last value in insertion order ordefaultValueif there is no such header or it can't be converted toboolean.
-
getLastInt
Returns theintvalue 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
intvalue of the last value in insertion order ornullif there is no such header or it can't be converted toint.
-
getLastInt
Returns theintvalue 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
intvalue of the last value in insertion order ordefaultValueif there is no such header or it can't be converted toint.
-
getInt
Returns theintvalue 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
intvalue of the first value in insertion order ornullif there is no such header or it can't be converted toint.
-
getInt
Returns theintvalue 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
intvalue of the first value in insertion order ordefaultValueif there is no such header or it can't be converted toint.
-
getLong
Returns thelongvalue 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
longvalue of the first value in insertion order ornullif there is no such header or it can't be converted tolong.
-
getLong
Returns thelongvalue 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
longvalue of the first value in insertion order ordefaultValueif there is no such header or it can't be converted tolong.
-
getLastLong
Returns thelongvalue 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
longvalue of the last value in insertion order ornullif there is no such header or it can't be converted tolong.
-
getLastLong
Returns thelongvalue 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
longvalue of the last value in insertion order ordefaultValueif there is no such header or it can't be converted tolong.
-
getFloat
Returns thefloatvalue 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
floatvalue of the first value in insertion order ornullif there is no such header or it can't be converted tofloat.
-
getFloat
Returns thefloatvalue 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
floatvalue of the first value in insertion order ordefaultValueif there is no such header or it can't be converted tofloat.
-
getLastFloat
Returns thefloatvalue 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
floatvalue of the last value in insertion order ornullif there is no such header or it can't be converted tofloat.
-
getLastFloat
Returns thefloatvalue 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
floatvalue of the last value in insertion order ordefaultValueif there is no such header or it can't be converted tofloat.
-
getDouble
Returns thedoublevalue 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
doublevalue of the first value in insertion order ornullif there is no such header or it can't be converted todouble.
-
getDouble
Returns thedoublevalue 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
doublevalue of the first value in insertion order ordefaultValueif there is no such header or it can't be converted todouble.
-
getLastDouble
Returns thedoublevalue 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
doublevalue of the last value in insertion order ornullif there is no such header or it can't be converted todouble.
-
getLastDouble
Returns thedoublevalue 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
doublevalue of the last value in insertion order ordefaultValueif there is no such header or it can't be converted todouble.
-
getTimeMillis
Returns the value of a header with the specifiednamein 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
nullif there is no such header or it can't be converted to milliseconds.
-
getTimeMillis
Returns the value of a header with the specifiednamein 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
defaultValueif there is no such header or it can't be converted to milliseconds.
-
getLastTimeMillis
Returns the value of a header with the specifiednamein 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
nullif there is no such header or it can't be converted to milliseconds.
-
getLastTimeMillis
Returns the value of a header with the specifiednamein 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
defaultValueif there is no such header or it can't be converted to milliseconds.
-
contains
Returnstrueif a header with thenameexists,falseotherwise.- Parameters:
name- the header name
-
contains
Returnstrueif a header with thenameandvalueexists.- Parameters:
name- the header namevalue- the header value of the header to find
-
containsObject
Returnstrueif a header with thenameandvalueexists.- Parameters:
name- the header namevalue- the header value- Returns:
trueif the header exists.falseotherwise
-
containsBoolean
Returnstrueif a header with thenameandvalueexists.- Parameters:
name- the header namevalue- the header value- Returns:
trueif the header exists.falseotherwise
-
containsInt
Returnstrueif a header with thenameandvalueexists.- Parameters:
name- the header namevalue- the header value- Returns:
trueif the header exists.falseotherwise
-
containsLong
Returnstrueif a header with thenameandvalueexists.- Parameters:
name- the header namevalue- the header value- Returns:
trueif the header exists.falseotherwise
-
containsFloat
Returnstrueif a header with thenameandvalueexists.- Parameters:
name- the header namevalue- the header value- Returns:
trueif the header exists.falseotherwise
-
containsDouble
Returnstrueif a header with thenameandvalueexists.- Parameters:
name- the header namevalue- the header value- Returns:
trueif the header exists.falseotherwise
-
containsTimeMillis
Returnstrueif a header with thenameandvalueexists.- Parameters:
name- the header namevalue- the header value- Returns:
trueif the header exists.falseotherwise
-
size
int size()Returns the number of headers. -
isEmpty
boolean isEmpty()Returnstrueif this headers does not contain any entries. -
names
Set<io.netty.util.AsciiString> names() -
iterator
Returns anIteratorthat 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 anIteratorthat yields all values of the headers with the specifiedname. -
forEach
Invokes the specifiedactionfor all header entries. -
forEachValue
Invokes the specifiedactionfor all values of the headers with the specifiedname. -
stream
Returns aStreamthat yields all header entries. -
valueStream
Returns aStreamthat yields all values of the headers with the specifiedname.
-