- java.lang.Object
-
- org.eclipse.jetty.http.HttpFields.Mutable
-
- All Implemented Interfaces:
java.lang.Iterable<HttpField>
,HttpFields
- Direct Known Subclasses:
HttpTester.Message
- Enclosing interface:
- HttpFields
public static class HttpFields.Mutable extends java.lang.Object implements java.lang.Iterable<HttpField>, HttpFields
HTTP Fields. A collection of HTTP header and or Trailer fields.This class is not synchronized as it is expected that modifications will only be performed by a single thread.
The cookie handling provided by this class is guided by the Servlet specification and RFC6265.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.eclipse.jetty.http.HttpFields
HttpFields.Immutable, HttpFields.Mutable
-
-
Field Summary
-
Fields inherited from interface org.eclipse.jetty.http.HttpFields
EMPTY
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
Mutable()
Initialize an empty HttpFields.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description HttpFields.Mutable
add(java.lang.String name, java.lang.String value)
Add to or set a field.HttpFields.Mutable
add(HttpField field)
HttpFields.Mutable
add(HttpFields fields)
HttpFields.Mutable
add(HttpHeader header, java.lang.String value)
Add to or set a field.HttpFields.Mutable
add(HttpHeader header, HttpHeaderValue value)
HttpFields.Mutable
addCSV(java.lang.String name, java.lang.String... values)
Add comma separated values, but only if not already present.HttpFields.Mutable
addCSV(HttpHeader header, java.lang.String... values)
Add comma separated values, but only if not already present.HttpFields.Mutable
addDateField(java.lang.String name, long date)
Sets the value of a date field.HttpFields.Immutable
asImmutable()
HttpFields.Mutable
clear()
void
computeField(java.lang.String name, java.util.function.BiFunction<java.lang.String,java.util.List<HttpField>,HttpField> computeFn)
Computes a single field for the given HTTP header name and for existing fields with the same name.void
computeField(HttpHeader header, java.util.function.BiFunction<HttpHeader,java.util.List<HttpField>,HttpField> computeFn)
Computes a single field for the given HttpHeader and for existing fields with the same header.void
ensureField(HttpField field)
Ensure that specific HttpField exists when the field may not exist or may exist and be multi valued.boolean
equals(java.lang.Object o)
HttpField
getField(int index)
Get a Field by index.int
hashCode()
java.util.Iterator<HttpField>
iterator()
java.util.ListIterator<HttpField>
listIterator()
HttpFields.Mutable
put(java.lang.String name, java.lang.String value)
Set a field.HttpFields.Mutable
put(java.lang.String name, java.util.List<java.lang.String> list)
Set a field.HttpFields.Mutable
put(HttpField field)
HttpFields.Mutable
put(HttpHeader header, java.lang.String value)
Set a field.HttpFields.Mutable
put(HttpHeader header, HttpHeaderValue value)
HttpFields.Mutable
putDateField(java.lang.String name, long date)
Sets the value of a date field.HttpFields.Mutable
putDateField(HttpHeader name, long date)
Sets the value of a date field.HttpFields.Mutable
putLongField(java.lang.String name, long value)
Sets the value of an long field.HttpFields.Mutable
putLongField(HttpHeader name, long value)
Sets the value of an long field.HttpFields.Mutable
remove(java.lang.String name)
Remove a field.HttpFields.Mutable
remove(java.util.EnumSet<HttpHeader> fields)
HttpFields.Mutable
remove(HttpHeader name)
Remove a field.int
size()
java.util.stream.Stream<HttpField>
stream()
java.lang.String
toString()
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.eclipse.jetty.http.HttpFields
asString, contains, contains, contains, contains, contains, contains, get, get, getCSV, getCSV, getDateField, getField, getField, getFieldNames, getFieldNamesCollection, getFields, getFields, getLongField, getLongField, getQualityCSV, getQualityCSV, getQualityCSV, getValues, getValuesList, getValuesList, isEqualTo
-
-
-
-
Method Detail
-
add
public HttpFields.Mutable add(java.lang.String name, java.lang.String value)
Add to or set a field. If the field is allowed to have multiple values, add will add multiple headers of the same name.- Parameters:
name
- the name of the fieldvalue
- the value of the field.- Returns:
- this builder
-
add
public HttpFields.Mutable add(HttpHeader header, HttpHeaderValue value)
-
add
public HttpFields.Mutable add(HttpHeader header, java.lang.String value)
Add to or set a field. If the field is allowed to have multiple values, add will add multiple headers of the same name.- Parameters:
header
- the headervalue
- the value of the field.- Returns:
- this builder
-
add
public HttpFields.Mutable add(HttpField field)
-
add
public HttpFields.Mutable add(HttpFields fields)
-
addCSV
public HttpFields.Mutable addCSV(HttpHeader header, java.lang.String... values)
Add comma separated values, but only if not already present.- Parameters:
header
- The header to add the value(s) tovalues
- The value(s) to add- Returns:
- this builder
-
addCSV
public HttpFields.Mutable addCSV(java.lang.String name, java.lang.String... values)
Add comma separated values, but only if not already present.- Parameters:
name
- The header to add the value(s) tovalues
- The value(s) to add- Returns:
- this builder
-
addDateField
public HttpFields.Mutable addDateField(java.lang.String name, long date)
Sets the value of a date field.- Parameters:
name
- the field namedate
- the field date value- Returns:
- this builder
-
asImmutable
public HttpFields.Immutable asImmutable()
- Specified by:
asImmutable
in interfaceHttpFields
-
clear
public HttpFields.Mutable clear()
-
ensureField
public void ensureField(HttpField field)
Ensure that specific HttpField exists when the field may not exist or may exist and be multi valued. Multiple existing fields are merged into a single field.- Parameters:
field
- The header to ensure is contained. The field is used directly if possible soPreEncodedHttpField
s can be passed. If the value needs to be merged with existing values, then a new field is created.
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
getField
public HttpField getField(int index)
Get a Field by index.- Specified by:
getField
in interfaceHttpFields
- Parameters:
index
- the field index- Returns:
- A Field value or null if the Field value has not been set
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
iterator
public java.util.Iterator<HttpField> iterator()
- Specified by:
iterator
in interfacejava.lang.Iterable<HttpField>
-
listIterator
public java.util.ListIterator<HttpField> listIterator()
-
put
public HttpFields.Mutable put(HttpField field)
-
put
public HttpFields.Mutable put(java.lang.String name, java.lang.String value)
Set a field.- Parameters:
name
- the name of the fieldvalue
- the value of the field. If null the field is cleared.- Returns:
- this builder
-
put
public HttpFields.Mutable put(HttpHeader header, HttpHeaderValue value)
-
put
public HttpFields.Mutable put(HttpHeader header, java.lang.String value)
Set a field.- Parameters:
header
- the header name of the fieldvalue
- the value of the field. If null the field is cleared.- Returns:
- this builder
-
put
public HttpFields.Mutable put(java.lang.String name, java.util.List<java.lang.String> list)
Set a field.- Parameters:
name
- the name of the fieldlist
- the List value of the field. If null the field is cleared.- Returns:
- this builder
-
putDateField
public HttpFields.Mutable putDateField(HttpHeader name, long date)
Sets the value of a date field.- Parameters:
name
- the field namedate
- the field date value- Returns:
- this builder
-
putDateField
public HttpFields.Mutable putDateField(java.lang.String name, long date)
Sets the value of a date field.- Parameters:
name
- the field namedate
- the field date value- Returns:
- this builder
-
putLongField
public HttpFields.Mutable putLongField(HttpHeader name, long value)
Sets the value of an long field.- Parameters:
name
- the field namevalue
- the field long value- Returns:
- this builder
-
putLongField
public HttpFields.Mutable putLongField(java.lang.String name, long value)
Sets the value of an long field.- Parameters:
name
- the field namevalue
- the field long value- Returns:
- this builder
-
computeField
public void computeField(HttpHeader header, java.util.function.BiFunction<HttpHeader,java.util.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. 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 HTTP headercomputeFn
- the compute function
-
computeField
public void computeField(java.lang.String name, java.util.function.BiFunction<java.lang.String,java.util.List<HttpField>,HttpField> computeFn)
Computes a single field for the given HTTP header name and for existing fields with the same name.
- Parameters:
name
- the HTTP header namecomputeFn
- the compute function- See Also:
computeField(HttpHeader, BiFunction)
-
remove
public HttpFields.Mutable remove(HttpHeader name)
Remove a field.- Parameters:
name
- the field to remove- Returns:
- this builder
-
remove
public HttpFields.Mutable remove(java.util.EnumSet<HttpHeader> fields)
-
remove
public HttpFields.Mutable remove(java.lang.String name)
Remove a field.- Parameters:
name
- the field to remove- Returns:
- this builder
-
size
public int size()
- Specified by:
size
in interfaceHttpFields
-
stream
public java.util.stream.Stream<HttpField> stream()
- Specified by:
stream
in interfaceHttpFields
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-