Interface HttpFields.Mutable
- All Superinterfaces:
HttpFields
,Iterable<HttpField>
,Supplier<HttpFields>
- All Known Implementing Classes:
HttpFields.Mutable.Wrapper
,HttpFields.MutableHttpFields
,HttpTester.Message
,HttpTester.Request
,HttpTester.Response
,ResponseHttpFields
,WebSocketHttpFieldsWrapper
- Enclosing interface:
- HttpFields
A mutable version of HttpFields
.
Name and value pairs representing HTTP headers or HTTP trailers can be added to or removed from this instance.
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.eclipse.jetty.http.HttpFields
HttpFields.ImmutableHttpFields, HttpFields.Mutable, HttpFields.MutableHttpFields
-
Field Summary
Fields inherited from interface org.eclipse.jetty.http.HttpFields
CONNECTION_CLOSE, CONNECTION_KEEPALIVE, CONTENT_LENGTH_0, EMPTY, EXPIRES_01JAN1970
-
Method Summary
Modifier and TypeMethodDescriptiondefault HttpFields.Mutable
Adds a newHttpField
with the given name andlong
value.default HttpFields.Mutable
Adds a newHttpField
with the given name and string value.default HttpFields.Mutable
Adds a field associated with a list of values.default HttpFields.Mutable
Adds the givenHttpField
to this instance.default HttpFields.Mutable
add
(HttpFields fields) Adds all theHttpField
s of the givenHttpFields
to this instance.default HttpFields.Mutable
add
(HttpHeader header, long value) Adds a newHttpField
with the given name andlong
value.default HttpFields.Mutable
add
(HttpHeader header, String value) Adds a newHttpField
with the given name and string value.default HttpFields.Mutable
add
(HttpHeader header, HttpHeaderValue value) Adds a newHttpField
with the given name and value.default HttpFields.Mutable
Adds the given value(s) to theHttpField
with the given name, encoding them as comma-separated if necessary, unless they are already present in existing fields with the same name.default HttpFields.Mutable
addCSV
(HttpHeader header, String... values) Adds the given value(s) to theHttpField
with the given name, encoding them as comma-separated if necessary, unless they are already present in existing fields with the same name.default HttpFields.Mutable
addDateField
(String name, long date) Adds a new dateHttpField
with the given name anddate
value.default HttpFields.Mutable
clear()
Removes all the fields from this instance.default HttpFields.Mutable
computeField
(String name, BiFunction<String, List<HttpField>, HttpField> computeFn) Computes a single field for the given HTTP field name and for existing fields with the same name.default HttpFields.Mutable
computeField
(HttpHeader header, BiFunction<HttpHeader, List<HttpField>, HttpField> computeFn) Computes a single field for the givenHttpHeader
and for existing fields with the same header.default void
ensureField
(HttpField field) Ensures that the givenHttpField
is present when the field may not exist or may exist and be multi-valued.default HttpFields.Mutable
default HttpFields.Mutable
This method behaves likeremove(String)
when the givenvalue
isnull
, otherwise behaves likeput(HttpField)
.default HttpFields.Mutable
Puts a field associated with a list of values.default HttpFields.Mutable
Puts the givenHttpField
into this instance.default HttpFields.Mutable
put
(HttpHeader header, long value) default HttpFields.Mutable
put
(HttpHeader header, String value) This method behaves likeremove(HttpHeader)
when the givenvalue
isnull
, otherwise behaves likeput(HttpField)
.default HttpFields.Mutable
put
(HttpHeader header, HttpHeaderValue value) This method behaves likeremove(HttpHeader)
when the givenvalue
isnull
, otherwise behaves likeput(HttpField)
.default HttpFields.Mutable
default HttpFields.Mutable
putDate
(HttpHeader name, long date) default HttpFields.Mutable
Removes all the fields with the given name.default HttpFields.Mutable
remove
(EnumSet<HttpHeader> headers) Removes all the fields with the given names.default HttpFields.Mutable
remove
(HttpHeader header) Removes all the fields with the given name.Methods inherited from interface org.eclipse.jetty.http.HttpFields
asImmutable, asString, contains, contains, contains, contains, contains, contains, containsLast, get, get, get, getCSV, getCSV, getDateField, getDateField, getField, getField, getField, getFieldNames, getFieldNamesCollection, getFields, getFields, getLast, getLongField, getLongField, getQualityCSV, getQualityCSV, getQualityCSV, getValues, getValuesList, getValuesList, isEqualTo, iterator, listIterator, listIterator, size, stream
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Method Details
-
add
Adds a new
HttpField
with the given name and string value.The new
HttpField
is added even if a field with the same name is already present.- Parameters:
name
- the non-null
name of the fieldvalue
- the non-null
value of the field- Returns:
- this instance
-
add
Adds a new
HttpField
with the given name andlong
value.The new
HttpField
is added even if a field with the same name is already present.- Parameters:
name
- the non-null
name of the fieldvalue
- the value of the field- Returns:
- this instance
-
add
Adds a new
HttpField
with the given name and value.The new
HttpField
is added even if a field with the same name is already present.- Parameters:
header
- the non-null
name of the fieldvalue
- the non-null
value of the field- Returns:
- this instance
-
add
Adds a new
HttpField
with the given name and string value.The new
HttpField
is added even if a field with the same name is already present.- Parameters:
header
- the non-null
name of the fieldvalue
- the non-null
value of the field- Returns:
- this instance
-
add
Adds a new
HttpField
with the given name andlong
value.The new
HttpField
is added even if a field with the same name is already present.- Parameters:
header
- the non-null
name of the fieldvalue
- the value of the field- Returns:
- this instance
-
add
Adds the given
HttpField
to this instance.- Parameters:
field
- theHttpField
to add- Returns:
- this instance
-
add
Adds all the
HttpField
s of the givenHttpFields
to this instance.- Parameters:
fields
- the fields to add- Returns:
- this instance
-
add
Adds a field associated with a list of values.
- Parameters:
name
- the name of the fieldlist
- the List value of the field.- Returns:
- this builder
-
addCSV
Adds the given value(s) to the
HttpField
with the given name, encoding them as comma-separated if necessary, unless they are already present in existing fields with the same name.- Parameters:
header
- the field name of the field to add the value(s) tovalues
- the value(s) to add- Returns:
- this instance
-
addCSV
Adds the given value(s) to the
HttpField
with the given name, encoding them as comma-separated if necessary, unless they are already present in existing fields with the same name.- Parameters:
name
- the field name of the field to add the value(s) tovalues
- the value(s) to add- Returns:
- this instance
-
addDateField
Adds a new date
TheHttpField
with the given name anddate
value.date
parameter is the number of milliseconds from the Unix Epoch, and it is formatted into a string viaDateGenerator.formatDate(long)
.- Parameters:
name
- the non-null
name of the fielddate
- the field date value- Returns:
- this instance
-
clear
Removes all the fields from this instance.
- Returns:
- this instance
-
ensureField
Ensures that the given
HttpField
is present when the field may not exist or may exist and be multi-valued.Multiple existing
HttpField
s are merged into a single field.- Parameters:
field
- the field to ensure to be present
-
put
Puts the given
HttpField
into this instance.If a fields with the same name is present, the given field replaces it, and other existing fields with the same name are removed, so that only the given field will be present.
If a field with the same name is not present, the given field will be added.
- Parameters:
field
- the field to put- Returns:
- this instance
-
put
This method behaves like
remove(String)
when the givenvalue
isnull
, otherwise behaves likeput(HttpField)
.- Parameters:
name
- the name of the fieldvalue
- the value of the field; ifnull
the field is removed- Returns:
- this instance
-
put
This method behaves like
remove(HttpHeader)
when the givenvalue
isnull
, otherwise behaves likeput(HttpField)
.- Parameters:
header
- the name of the fieldvalue
- the value of the field; ifnull
the field is removed- Returns:
- this instance
-
put
This method behaves like
remove(HttpHeader)
when the givenvalue
isnull
, otherwise behaves likeput(HttpField)
.- Parameters:
header
- the name of the fieldvalue
- the value of the field; ifnull
the field is removed- Returns:
- this instance
-
put
Puts a field associated with a list of values.
- Parameters:
name
- the name of the fieldlist
- the List value of the field. If null the field is cleared.- Returns:
- this builder
-
putDate
Puts a new date
TheHttpField
with the given name anddate
value, with the semantic ofput(HttpField)
.date
parameter is the number of milliseconds from the Unix Epoch, and it is formatted into a string viaDateGenerator.formatDate(long)
.- Parameters:
name
- the non-null
name of the fielddate
- the field date value- Returns:
- this instance
-
putDate
Puts a new date
TheHttpField
with the given name anddate
value, with the semantic ofput(HttpField)
.date
parameter is the number of milliseconds from the Unix Epoch, and it is formatted into a string viaDateGenerator.formatDate(long)
.- Parameters:
name
- the non-null
name of the fielddate
- the field date value- Returns:
- this instance
-
put
Puts a new
HttpField
with the given name andlong
value, with the semantic ofput(HttpField)
.- Parameters:
header
- the non-null
name of the fieldvalue
- the value of the field- Returns:
- this instance
-
put
Puts a new
HttpField
with the given name andlong
value, with the semantic ofput(HttpField)
.- Parameters:
name
- the non-null
name of the fieldvalue
- the value of the field- Returns:
- this instance
-
computeField
default HttpFields.Mutable computeField(HttpHeader header, BiFunction<HttpHeader, List<HttpField>, HttpField> computeFn) Computes a single field for the given
HttpHeader
and for existing fields with the same header.The compute function receives the field name and a list of fields with the same name so that their values can be used to compute the value of the field that is returned by the compute function parameter. If the compute function returns
null
, the fields with the given name are removed.This method comes handy when you want to add an HTTP header if it does not exist, or add a value if the HTTP header already exists, similarly to
Map.compute(Object, BiFunction)
.This method can be used to
put
a new field (or blindly replace its value):httpFields.computeField("X-New-Header", (name, fields) -> new HttpField(name, "NewValue"));
This method can be used to coalesce many fields into one:
// Input: GET / HTTP/1.1 Host: localhost Cookie: foo=1 Cookie: bar=2,baz=3 User-Agent: Jetty // Computation: httpFields.computeField("Cookie", (name, fields) -> { // No cookies, nothing to do. if (fields == null) return null; // Coalesces all cookies. String coalesced = fields.stream() .flatMap(field -> Stream.of(field.getValues())) .collect(Collectors.joining(", ")); // Returns a single Cookie header with all cookies. return new HttpField(name, coalesced); } // Output: GET / HTTP/1.1 Host: localhost Cookie: foo=1, bar=2, baz=3 User-Agent: Jetty
This method can be used to replace a field:
httpFields.computeField("X-Length", (name, fields) -> { if (fields == null) return null; // Get any value among the X-Length headers. String length = fields.stream() .map(HttpField::getValue) .findAny() .orElse("0"); // Replace X-Length headers with X-Capacity header. return new HttpField("X-Capacity", length); });
This method can be used to remove a field:
httpFields.computeField("Connection", (name, fields) -> null);
- Parameters:
header
- the field namecomputeFn
- the compute function- Returns:
- this instance
-
computeField
default HttpFields.Mutable computeField(String name, BiFunction<String, List<HttpField>, HttpField> computeFn) Computes a single field for the given HTTP field name and for existing fields with the same name.
- Parameters:
name
- the field namecomputeFn
- the compute function- Returns:
- this instance
- See Also:
-
remove
Removes all the fields with the given name.
- Parameters:
header
- the name of the fields to remove- Returns:
- this instance
-
remove
Removes all the fields with the given names.
- Parameters:
headers
- the names of the fields to remove- Returns:
- this instance
-
remove
Removes all the fields with the given name.
- Parameters:
name
- the name of the fields to remove- Returns:
- this instance
-