- All Implemented Interfaces:
Iterable<HttpField>,HttpFields,HttpFields.Mutable
- Direct Known Subclasses:
HttpTester.Message
- Enclosing interface:
- HttpFields
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.ImmutableHttpFields, HttpFields.Mutable, HttpFields.MutableHttpFields -
Field Summary
Fields inherited from interface org.eclipse.jetty.http.HttpFields
CONNECTION_CLOSE, CONNECTION_KEEPALIVE, EMPTY, EXPIRES_01JAN1970 -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedInitialize an empty HttpFields.protectedMutableHttpFields(int capacity) Initialize an empty HttpFields.protectedMutableHttpFields(HttpFields fields) Initialize HttpFields from another.protectedMutableHttpFields(HttpFields fields, EnumSet<HttpHeader> removeFields) Initialize HttpFields from another and remove fieldsprotectedMutableHttpFields(HttpFields fields, HttpField replaceField) Initialize HttpFields from another and replace a field -
Method Summary
Modifier and TypeMethodDescriptionadd(HttpFields fields) clear()voidcomputeField(String name, BiFunction<String, List<HttpField>, HttpField> computeFn) Computes a single field for the given HTTP header name and for existing fields with the same name.voidcomputeField(HttpHeader header, BiFunction<HttpHeader, List<HttpField>, HttpField> computeFn) Computes a single field for the given HttpHeader and for existing fields with the same header.<T> voidcomputeField(T header, BiFunction<T, List<HttpField>, HttpField> computeFn, BiPredicate<HttpField, T> matcher) booleangetField(int index) Get a Field by index.inthashCode()iterator()Set a field.Set a field.put(HttpHeader header, String value) Set a field.put(HttpHeader header, HttpHeaderValue value) Remove a field.remove(EnumSet<HttpHeader> fields) remove(HttpHeader name) Remove a field.intsize()stream()Efficiently take the fields as an Immutable that cannot be changed by any further mutations to this instance.toString()Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.eclipse.jetty.http.HttpFields
asString, contains, contains, contains, contains, contains, contains, get, get, getCSV, getCSV, getDateField, getDateField, getField, getField, getFieldNames, getFieldNamesCollection, getFields, getFields, getLongField, getLongField, getQualityCSV, getQualityCSV, getQualityCSV, getValues, getValuesList, getValuesList, isEqualToMethods inherited from interface org.eclipse.jetty.http.HttpFields.Mutable
add, add, add, addCSV, addCSV, addDateField, ensureField, putDateField, putDateField, putLongField, putLongFieldMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
MutableHttpFields
protected MutableHttpFields()Initialize an empty HttpFields. -
MutableHttpFields
protected MutableHttpFields(int capacity) Initialize an empty HttpFields.- Parameters:
capacity- the capacity of the http fields
-
MutableHttpFields
Initialize HttpFields from another.- Parameters:
fields- the fields to copy data from
-
MutableHttpFields
Initialize HttpFields from another and replace a field- Parameters:
fields- the fields to copy data fromreplaceField- the replacement field
-
MutableHttpFields
Initialize HttpFields from another and remove fields- Parameters:
fields- the fields to copy data fromremoveFields- the the fields to remove
-
-
Method Details
-
add
- Specified by:
addin interfaceHttpFields.Mutable
-
add
- Specified by:
addin interfaceHttpFields.Mutable
-
asImmutable
- Specified by:
asImmutablein interfaceHttpFields
-
takeAsImmutable
Description copied from interface:HttpFieldsEfficiently take the fields as an Immutable that cannot be changed by any further mutations to this instance.- Specified by:
takeAsImmutablein interfaceHttpFields- Returns:
- An immutable version of the fields.
-
clear
- Specified by:
clearin interfaceHttpFields.Mutable
-
equals
-
getField
Get a Field by index.- Specified by:
getFieldin interfaceHttpFields- Parameters:
index- the field index- Returns:
- A Field value or null if the Field value has not been set
-
hashCode
public int hashCode() -
iterator
- Specified by:
iteratorin interfaceHttpFields.Mutable- Specified by:
iteratorin interfaceIterable<HttpField>
-
listIterator
- Specified by:
listIteratorin interfaceHttpFields.Mutable
-
put
- Specified by:
putin interfaceHttpFields.Mutable
-
put
Description copied from interface:HttpFields.MutableSet a field.- Specified by:
putin interfaceHttpFields.Mutable- Parameters:
name- the name of the fieldvalue- the value of the field. If null the field is cleared.- Returns:
- this builder
-
put
- Specified by:
putin interfaceHttpFields.Mutable
-
put
Description copied from interface:HttpFields.MutableSet a field.- Specified by:
putin interfaceHttpFields.Mutable- Parameters:
header- the header name of the fieldvalue- the value of the field. If null the field is cleared.- Returns:
- this builder
-
put
Description copied from interface:HttpFields.MutableSet a field.- Specified by:
putin interfaceHttpFields.Mutable- Parameters:
name- the name of the fieldlist- the List value of the field. If null the field is cleared.- Returns:
- this builder
-
computeField
public void computeField(HttpHeader header, BiFunction<HttpHeader, List<HttpField>, HttpField> computeFn) Description copied from interface:HttpFields.MutableComputes 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
puta 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: JettyThis 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);- Specified by:
computeFieldin interfaceHttpFields.Mutable- Parameters:
header- the HTTP headercomputeFn- the compute function
-
computeField
Description copied from interface:HttpFields.MutableComputes a single field for the given HTTP header name and for existing fields with the same name.
- Specified by:
computeFieldin interfaceHttpFields.Mutable- Parameters:
name- the HTTP header namecomputeFn- the compute function- See Also:
-
computeField
public <T> void computeField(T header, BiFunction<T, List<HttpField>, HttpField> computeFn, BiPredicate<HttpField, T> matcher) -
remove
Description copied from interface:HttpFields.MutableRemove a field.- Specified by:
removein interfaceHttpFields.Mutable- Parameters:
name- the field to remove- Returns:
- this builder
-
remove
- Specified by:
removein interfaceHttpFields.Mutable
-
remove
Description copied from interface:HttpFields.MutableRemove a field.- Specified by:
removein interfaceHttpFields.Mutable- Parameters:
name- the field to remove- Returns:
- this builder
-
size
public int size()- Specified by:
sizein interfaceHttpFields
-
stream
- Specified by:
streamin interfaceHttpFields
-
toString
-