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

public static interface HttpFields.Mutable extends Iterable<HttpField>, HttpFields
A mutable HttpFields interface. To implement this interface, only the listIterator() method needs to be implemented, however default implementations may not be efficient.
  • Method Details

    • add

      default HttpFields.Mutable add(String name, 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 field
      value - the value of the field.
      Returns:
      this builder
    • add

      default HttpFields.Mutable add(String name, long 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 field
      value - the value of the field.
      Returns:
      this builder
    • add

      default HttpFields.Mutable add(HttpHeader header, HttpHeaderValue value)
    • add

      default HttpFields.Mutable add(HttpHeader header, 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 header
      value - the value of the field.
      Returns:
      this builder
    • add

      default HttpFields.Mutable add(HttpHeader header, long 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 header
      value - the value of the field.
      Returns:
      this builder
    • add

      default HttpFields.Mutable add(HttpField field)
    • add

      default HttpFields.Mutable add(HttpFields fields)
    • addCSV

      default HttpFields.Mutable addCSV(HttpHeader header, String... values)
      Add comma separated values, but only if not already present.
      Parameters:
      header - The header to add the value(s) to
      values - The value(s) to add
      Returns:
      this builder
    • addCSV

      default HttpFields.Mutable addCSV(String name, String... values)
      Add comma separated values, but only if not already present.
      Parameters:
      name - The header to add the value(s) to
      values - The value(s) to add
      Returns:
      this builder
    • addDateField

      default HttpFields.Mutable addDateField(String name, long date)
      Sets the value of a date field.
      Parameters:
      name - the field name
      date - the field date value
      Returns:
      this builder
    • clear

      default HttpFields.Mutable clear()
    • ensureField

      default 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 so PreEncodedHttpFields can be passed. If the value needs to be merged with existing values, then a new field is created.
    • iterator

      default Iterator<HttpField> iterator()
      Specified by:
      iterator in interface Iterable<HttpField>
    • listIterator

      ListIterator<HttpField> listIterator()
    • put

      default HttpFields.Mutable put(HttpField field)
    • put

      default HttpFields.Mutable put(String name, String value)
      Set a field.
      Parameters:
      name - the name of the field
      value - the value of the field. If null the field is cleared.
      Returns:
      this builder
    • put

      default HttpFields.Mutable put(HttpHeader header, HttpHeaderValue value)
    • put

      default HttpFields.Mutable put(HttpHeader header, String value)
      Set a field.
      Parameters:
      header - the header name of the field
      value - the value of the field. If null the field is cleared.
      Returns:
      this builder
    • put

      default HttpFields.Mutable put(String name, List<String> list)
      Set a field.
      Parameters:
      name - the name of the field
      list - the List value of the field. If null the field is cleared.
      Returns:
      this builder
    • putDate

      default HttpFields.Mutable putDate(HttpHeader name, long date)
      Sets the value of a date field.
      Parameters:
      name - the field name
      date - the field date value
      Returns:
      this builder
    • putDate

      default HttpFields.Mutable putDate(String name, long date)
      Sets the value of a date field.
      Parameters:
      name - the field name
      date - the field date value
      Returns:
      this builder
    • put

      default HttpFields.Mutable put(HttpHeader header, long value)
      Sets the value of a long field.
      Parameters:
      header - the field name
      value - the field long value
      Returns:
      this builder
    • put

      default HttpFields.Mutable put(String name, long value)
      Sets the value of a long field.
      Parameters:
      name - the field name
      value - the field long value
      Returns:
      this builder
    • computeField

      default void 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. 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 header
      computeFn - the compute function
    • computeField

      default void computeField(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.

      Parameters:
      name - the HTTP header name
      computeFn - the compute function
      See Also:
    • remove

      default HttpFields.Mutable remove(HttpHeader header)
      Remove a field.
      Parameters:
      header - the field to remove
      Returns:
      this builder
    • remove

      default HttpFields.Mutable remove(EnumSet<HttpHeader> fields)
    • remove

      default HttpFields.Mutable remove(String name)
      Remove a field.
      Parameters:
      name - the field to remove
      Returns:
      this builder
    • formatCsvExcludingExisting

      static String formatCsvExcludingExisting(QuotedCSV existing, String... values)
    • computeEnsure

      static HttpField computeEnsure(HttpField ensure, List<HttpField> fields)
      Compute ensure field with a single value
      Parameters:
      ensure - The field to ensure exists
      fields - The list of existing fields with the same header
    • computeEnsure

      static HttpField computeEnsure(HttpField ensure, String[] values, List<HttpField> fields)
      Compute ensure field with a multiple values
      Parameters:
      ensure - The field to ensure exists
      values - The QuotedCSV parsed field values.
      fields - The list of existing fields with the same header