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
- 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 ClassesNested classes/interfaces inherited from interface HttpFields
HttpFields.ImmutableHttpFields, HttpFields.Mutable, HttpFields.MutableHttpFields -
Field Summary
Fields inherited from interface HttpFields
CONNECTION_CLOSE, CONNECTION_KEEPALIVE, CONTENT_LENGTH_0, EMPTY, EXPIRES_01JAN1970 -
Method Summary
Modifier and TypeMethodDescriptiondefault HttpFields.MutableAdds a newHttpFieldwith the given name andlongvalue.default HttpFields.MutableAdds a newHttpFieldwith the given name and string value.default HttpFields.MutableAdds a field associated with a list of values.default HttpFields.MutableAdds the givenHttpFieldto this instance.default HttpFields.Mutableadd(HttpFields fields) Adds all theHttpFields of the givenHttpFieldsto this instance.default HttpFields.Mutableadd(HttpHeader header, long value) Adds a newHttpFieldwith the given name andlongvalue.default HttpFields.Mutableadd(HttpHeader header, String value) Adds a newHttpFieldwith the given name and string value.default HttpFields.Mutableadd(HttpHeader header, HttpHeaderValue value) Adds a newHttpFieldwith the given name and value.default HttpFields.MutableAdds the given value(s) to theHttpFieldwith the given name, encoding them as comma-separated if necessary, unless they are already present in existing fields with the same name.default HttpFields.MutableaddCSV(HttpHeader header, String... values) Adds the given value(s) to theHttpFieldwith the given name, encoding them as comma-separated if necessary, unless they are already present in existing fields with the same name.default HttpFields.MutableaddDateField(String name, long date) Adds a new dateHttpFieldwith the given name anddatevalue.default HttpFields.Mutableclear()Removes all the fields from this instance.default HttpFields.MutablecomputeField(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.MutablecomputeField(HttpHeader header, BiFunction<HttpHeader, List<HttpField>, HttpField> computeFn) Computes a single field for the givenHttpHeaderand for existing fields with the same header.default voidensureField(HttpField field) Ensures that the givenHttpFieldis present when the field may not exist or may exist and be multi-valued.default HttpFields.Mutabledefault HttpFields.MutableThis method behaves likeremove(String)when the givenvalueisnull, otherwise behaves likeput(HttpField).default HttpFields.MutablePuts a field associated with a list of values.default HttpFields.MutablePuts the givenHttpFieldinto this instance.default HttpFields.Mutableput(HttpHeader header, long value) default HttpFields.Mutableput(HttpHeader header, String value) This method behaves likeremove(HttpHeader)when the givenvalueisnull, otherwise behaves likeput(HttpField).default HttpFields.Mutableput(HttpHeader header, HttpHeaderValue value) This method behaves likeremove(HttpHeader)when the givenvalueisnull, otherwise behaves likeput(HttpField).default HttpFields.Mutabledefault HttpFields.MutableputDate(HttpHeader name, long date) default HttpFields.MutableRemoves all the fields with the given name.default HttpFields.Mutableremove(EnumSet<HttpHeader> headers) Removes all the fields with the given names.default HttpFields.Mutableremove(HttpHeader header) Removes all the fields with the given name.Methods inherited from interface 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, streamMethods inherited from interface Iterable
forEach, spliterator
-
Method Details
-
add
Adds a new
HttpFieldwith the given name and string value.The new
HttpFieldis added even if a field with the same name is already present.This method has no effect if null is passed for either the name or value parameters.
- Parameters:
name- the name of the fieldvalue- the value of the field- Returns:
- this instance
-
add
-
add
-
add
-
add
-
add
-
add
Adds all the
HttpFields of the givenHttpFieldsto 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
HttpFieldwith 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
HttpFieldwith 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
TheHttpFieldwith the given name anddatevalue.dateparameter is the number of milliseconds from the Unix Epoch, and it is formatted into a string viaDateGenerator.formatDate(long).- Parameters:
name- the non-nullname of the fielddate- the field date value- Returns:
- this instance
-
clear
Removes all the fields from this instance.
- Returns:
- this instance
-
ensureField
-
put
Puts the given
HttpFieldinto 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 givenvalueisnull, otherwise behaves likeput(HttpField).- Parameters:
name- the name of the fieldvalue- the value of the field; ifnullthe field is removed- Returns:
- this instance
-
put
This method behaves like
remove(HttpHeader)when the givenvalueisnull, otherwise behaves likeput(HttpField).- Parameters:
header- the name of the fieldvalue- the value of the field; ifnullthe field is removed- Returns:
- this instance
-
put
This method behaves like
remove(HttpHeader)when the givenvalueisnull, otherwise behaves likeput(HttpField).- Parameters:
header- the name of the fieldvalue- the value of the field; ifnullthe 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
TheHttpFieldwith the given name anddatevalue, with the semantic ofput(HttpField).dateparameter is the number of milliseconds from the Unix Epoch, and it is formatted into a string viaDateGenerator.formatDate(long).- Parameters:
name- the non-nullname of the fielddate- the field date value- Returns:
- this instance
-
putDate
Puts a new date
TheHttpFieldwith the given name anddatevalue, with the semantic ofput(HttpField).dateparameter is the number of milliseconds from the Unix Epoch, and it is formatted into a string viaDateGenerator.formatDate(long).- Parameters:
name- the non-nullname of the fielddate- the field date value- Returns:
- this instance
-
put
Puts a new
HttpFieldwith the given name andlongvalue, with the semantic ofput(HttpField).- Parameters:
header- the non-nullname of the fieldvalue- the value of the field- Returns:
- this instance
-
put
Puts a new
HttpFieldwith the given name andlongvalue, with the semantic ofput(HttpField).- Parameters:
name- the non-nullname 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
HttpHeaderand 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
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);- 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
-