Class HttpFields

  • All Implemented Interfaces:
    Iterable<HttpField>

    @Deprecated(since="2021-05-27")
    public class HttpFields
    extends Object
    implements Iterable<HttpField>
    Deprecated.
    The Eclipse Jetty and Apache Felix Http Jetty packages are no longer supported.
    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.

    • Constructor Detail

      • HttpFields

        public HttpFields()
        Deprecated.
        Initialize an empty HttpFields.
      • HttpFields

        public HttpFields​(int capacity)
        Deprecated.
        Initialize an empty HttpFields.
        Parameters:
        capacity - the capacity of the http fields
      • HttpFields

        public HttpFields​(HttpFields fields)
        Deprecated.
        Initialize HttpFields from copy.
        Parameters:
        fields - the fields to copy data from
    • Method Detail

      • computeField

        public void computeField​(HttpHeader header,
                                 BiFunction<HttpHeader,​List<HttpField>,​HttpField> computeFn)
        Deprecated.

        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
      • size

        public int size()
        Deprecated.
      • getFieldNamesCollection

        public Set<String> getFieldNamesCollection()
        Deprecated.
        Get Collection of header names.
        Returns:
        the unique set of field names.
      • getFieldNames

        public Enumeration<String> getFieldNames()
        Deprecated.
        Get enumeration of header _names. Returns an enumeration of strings representing the header _names for this request.
        Returns:
        an enumeration of field names
      • getField

        public HttpField getField​(int index)
        Deprecated.
        Get a Field by index.
        Parameters:
        index - the field index
        Returns:
        A Field value or null if the Field value has not been set
      • contains

        public boolean contains​(HttpField field)
        Deprecated.
      • contains

        public boolean contains​(HttpHeader header,
                                String value)
        Deprecated.
      • contains

        public boolean contains​(String name,
                                String value)
        Deprecated.
      • contains

        public boolean contains​(HttpHeader header)
        Deprecated.
      • containsKey

        public boolean containsKey​(String name)
        Deprecated.
      • getValuesList

        public List<String> getValuesList​(HttpHeader header)
        Deprecated.
        Get multiple header of the same name
        Parameters:
        header - the header
        Returns:
        List the values
      • getValuesList

        public List<String> getValuesList​(String name)
        Deprecated.
        Get multiple header of the same name
        Parameters:
        name - the case-insensitive field name
        Returns:
        List the header values
      • addCSV

        public boolean addCSV​(HttpHeader header,
                              String... values)
        Deprecated.
        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:
        True if headers were modified
      • addCSV

        public boolean addCSV​(String name,
                              String... values)
        Deprecated.
        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:
        True if headers were modified
      • getCSV

        public List<String> getCSV​(HttpHeader header,
                                   boolean keepQuotes)
        Deprecated.
        Get multiple field values of the same name, split as a QuotedCSV
        Parameters:
        header - The header
        keepQuotes - True if the fields are kept quoted
        Returns:
        List the values with OWS stripped
      • getCSV

        public List<String> getCSV​(String name,
                                   boolean keepQuotes)
        Deprecated.
        Get multiple field values of the same name as a QuotedCSV
        Parameters:
        name - the case-insensitive field name
        keepQuotes - True if the fields are kept quoted
        Returns:
        List the values with OWS stripped
      • getQualityCSV

        public List<String> getQualityCSV​(HttpHeader header)
        Deprecated.
        Get multiple field values of the same name, split and sorted as a QuotedQualityCSV
        Parameters:
        header - The header
        Returns:
        List the values in quality order with the q param and OWS stripped
      • getQualityCSV

        public List<String> getQualityCSV​(HttpHeader header,
                                          ToIntFunction<String> secondaryOrdering)
        Deprecated.
        Get multiple field values of the same name, split and sorted as a QuotedQualityCSV
        Parameters:
        header - The header
        secondaryOrdering - Function to apply an ordering other than specified by quality
        Returns:
        List the values in quality order with the q param and OWS stripped
      • getQualityCSV

        public List<String> getQualityCSV​(String name)
        Deprecated.
        Get multiple field values of the same name, split and sorted as a QuotedQualityCSV
        Parameters:
        name - the case-insensitive field name
        Returns:
        List the values in quality order with the q param and OWS stripped
      • getValues

        public Enumeration<String> getValues​(String name)
        Deprecated.
        Get multi headers
        Parameters:
        name - the case-insensitive field name
        Returns:
        Enumeration of the values
      • getValues

        @Deprecated
        public Enumeration<String> getValues​(String name,
                                             String separators)
        Deprecated.
        Get multi field values with separator. The multiple values can be represented as separate headers of the same name, or by a single header using the separator(s), or a combination of both. Separators may be quoted.
        Parameters:
        name - the case-insensitive field name
        separators - String of separators.
        Returns:
        Enumeration of the values, or null if no such header.
      • put

        public void put​(HttpField field)
        Deprecated.
      • put

        public void put​(String name,
                        String value)
        Deprecated.
        Set a field.
        Parameters:
        name - the name of the field
        value - the value of the field. If null the field is cleared.
      • put

        public void put​(HttpHeader header,
                        String value)
        Deprecated.
        Set a field.
        Parameters:
        header - the header name of the field
        value - the value of the field. If null the field is cleared.
      • put

        public void put​(String name,
                        List<String> list)
        Deprecated.
        Set a field.
        Parameters:
        name - the name of the field
        list - the List value of the field. If null the field is cleared.
      • add

        public void add​(String name,
                        String value)
        Deprecated.
        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.
      • add

        public void add​(HttpHeader header,
                        String value)
        Deprecated.
        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.
      • remove

        public HttpField remove​(HttpHeader name)
        Deprecated.
        Remove a field.
        Parameters:
        name - the field to remove
        Returns:
        the header that was removed
      • remove

        public HttpField remove​(String name)
        Deprecated.
        Remove a field.
        Parameters:
        name - the field to remove
        Returns:
        the header that was removed
      • getLongField

        public long getLongField​(String name)
                          throws NumberFormatException
        Deprecated.
        Get a header as an long value. Returns the value of an integer field or -1 if not found. The case of the field name is ignored.
        Parameters:
        name - the case-insensitive field name
        Returns:
        the value of the field as a long
        Throws:
        NumberFormatException - If bad long found
      • getDateField

        public long getDateField​(String name)
        Deprecated.
        Get a header as a date value. Returns the value of a date field, or -1 if not found. The case of the field name is ignored.
        Parameters:
        name - the case-insensitive field name
        Returns:
        the value of the field as a number of milliseconds since unix epoch
      • putLongField

        public void putLongField​(HttpHeader name,
                                 long value)
        Deprecated.
        Sets the value of an long field.
        Parameters:
        name - the field name
        value - the field long value
      • putLongField

        public void putLongField​(String name,
                                 long value)
        Deprecated.
        Sets the value of an long field.
        Parameters:
        name - the field name
        value - the field long value
      • putDateField

        public void putDateField​(HttpHeader name,
                                 long date)
        Deprecated.
        Sets the value of a date field.
        Parameters:
        name - the field name
        date - the field date value
      • putDateField

        public void putDateField​(String name,
                                 long date)
        Deprecated.
        Sets the value of a date field.
        Parameters:
        name - the field name
        date - the field date value
      • addDateField

        public void addDateField​(String name,
                                 long date)
        Deprecated.
        Sets the value of a date field.
        Parameters:
        name - the field name
        date - the field date value
      • hashCode

        public int hashCode()
        Deprecated.
        Overrides:
        hashCode in class Object
      • equals

        public boolean equals​(Object o)
        Deprecated.
        Overrides:
        equals in class Object
      • clear

        public void clear()
        Deprecated.
      • add

        public void add​(HttpField field)
        Deprecated.
      • addAll

        public void addAll​(HttpFields fields)
        Deprecated.
      • add

        @Deprecated
        public void add​(HttpFields fields)
        Deprecated.
        Add fields from another HttpFields instance. Single valued fields are replaced, while all others are added.
        Parameters:
        fields - the fields to add
      • stripParameters

        public static String stripParameters​(String value)
        Deprecated.
        Get field value without parameters. Some field values can have parameters. This method separates the value from the parameters and optionally populates a map with the parameters. For example:
        
         FieldName : Value ; param1=val1 ; param2=val2
        
         
        Parameters:
        value - The Field value, possibly with parameters.
        Returns:
        The value.
      • valueParameters

        public static String valueParameters​(String value,
                                             Map<String,​String> parameters)
        Deprecated.
        Get field value parameters. Some field values can have parameters. This method separates the value from the parameters and optionally populates a map with the parameters. For example:
        
         FieldName : Value ; param1=val1 ; param2=val2
        
         
        Parameters:
        value - The Field value, possibly with parameters.
        parameters - A map to populate with the parameters, or null
        Returns:
        The value.
      • qualityList

        @Deprecated
        public static List<String> qualityList​(Enumeration<String> e)
        Deprecated.
        List values in quality order.
        Parameters:
        e - Enumeration of values with quality parameters
        Returns:
        values in quality order.