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

public static interface HttpFields.Mutable extends 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.

  • Method Details

    • add

      default HttpFields.Mutable add(String name, String value)

      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 field
      value - the non-null value of the field
      Returns:
      this instance
    • add

      default HttpFields.Mutable add(String name, long value)

      Adds a new HttpField with the given name and long 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 field
      value - the value of the field
      Returns:
      this instance
    • add

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

      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 field
      value - the non-null value of the field
      Returns:
      this instance
    • add

      default HttpFields.Mutable add(HttpHeader header, String value)

      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 field
      value - the non-null value of the field
      Returns:
      this instance
    • add

      default HttpFields.Mutable add(HttpHeader header, long value)

      Adds a new HttpField with the given name and long 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 field
      value - the value of the field
      Returns:
      this instance
    • add

      default HttpFields.Mutable add(HttpField field)

      Adds the given HttpField to this instance.

      Parameters:
      field - the HttpField to add
      Returns:
      this instance
    • add

      default HttpFields.Mutable add(HttpFields fields)

      Adds all the HttpFields of the given HttpFields to this instance.

      Parameters:
      fields - the fields to add
      Returns:
      this instance
    • add

      default HttpFields.Mutable add(String name, List<String> list)

      Adds a field associated with a list of values.

      Parameters:
      name - the name of the field
      list - the List value of the field.
      Returns:
      this builder
    • addCSV

      default HttpFields.Mutable addCSV(HttpHeader header, String... values)

      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) to
      values - the value(s) to add
      Returns:
      this instance
    • addCSV

      default HttpFields.Mutable addCSV(String name, String... values)

      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) to
      values - the value(s) to add
      Returns:
      this instance
    • addDateField

      default HttpFields.Mutable addDateField(String name, long date)

      Adds a new date HttpField with the given name and date value.

      The date parameter is the number of milliseconds from the Unix Epoch, and it is formatted into a string via DateGenerator.formatDate(long).
      Parameters:
      name - the non-null name of the field
      date - the field date value
      Returns:
      this instance
    • clear

      default HttpFields.Mutable clear()

      Removes all the fields from this instance.

      Returns:
      this instance
    • ensureField

      default void ensureField(HttpField field)

      Ensures that the given HttpField is present when the field may not exist or may exist and be multi-valued.

      Multiple existing HttpFields are merged into a single field.

      Parameters:
      field - the field to ensure to be present
    • put

      default HttpFields.Mutable put(HttpField field)

      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

      default HttpFields.Mutable put(String name, String value)

      This method behaves like remove(String) when the given value is null, otherwise behaves like put(HttpField).

      Parameters:
      name - the name of the field
      value - the value of the field; if null the field is removed
      Returns:
      this instance
    • put

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

      This method behaves like remove(HttpHeader) when the given value is null, otherwise behaves like put(HttpField).

      Parameters:
      header - the name of the field
      value - the value of the field; if null the field is removed
      Returns:
      this instance
    • put

      default HttpFields.Mutable put(HttpHeader header, String value)

      This method behaves like remove(HttpHeader) when the given value is null, otherwise behaves like put(HttpField).

      Parameters:
      header - the name of the field
      value - the value of the field; if null the field is removed
      Returns:
      this instance
    • put

      default HttpFields.Mutable put(String name, List<String> list)

      Puts a field associated with a list of values.

      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)

      Puts a new date HttpField with the given name and date value, with the semantic of put(HttpField).

      The date parameter is the number of milliseconds from the Unix Epoch, and it is formatted into a string via DateGenerator.formatDate(long).
      Parameters:
      name - the non-null name of the field
      date - the field date value
      Returns:
      this instance
    • putDate

      default HttpFields.Mutable putDate(String name, long date)

      Puts a new date HttpField with the given name and date value, with the semantic of put(HttpField).

      The date parameter is the number of milliseconds from the Unix Epoch, and it is formatted into a string via DateGenerator.formatDate(long).
      Parameters:
      name - the non-null name of the field
      date - the field date value
      Returns:
      this instance
    • put

      default HttpFields.Mutable put(HttpHeader header, long value)

      Puts a new HttpField with the given name and long value, with the semantic of put(HttpField).

      Parameters:
      header - the non-null name of the field
      value - the value of the field
      Returns:
      this instance
    • put

      default HttpFields.Mutable put(String name, long value)

      Puts a new HttpField with the given name and long value, with the semantic of put(HttpField).

      Parameters:
      name - the non-null name of the field
      value - 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 name
      computeFn - 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 name
      computeFn - the compute function
      Returns:
      this instance
      See Also:
    • remove

      default HttpFields.Mutable remove(HttpHeader header)

      Removes all the fields with the given name.

      Parameters:
      header - the name of the fields to remove
      Returns:
      this instance
    • remove

      default HttpFields.Mutable remove(EnumSet<HttpHeader> headers)

      Removes all the fields with the given names.

      Parameters:
      headers - the names of the fields to remove
      Returns:
      this instance
    • remove

      default HttpFields.Mutable remove(String name)

      Removes all the fields with the given name.

      Parameters:
      name - the name of the fields to remove
      Returns:
      this instance