Class HttpMessageHeader

java.lang.Object
org.jdrupes.httpcodec.protocols.http.HttpMessageHeader
All Implemented Interfaces:
MessageHeader
Direct Known Subclasses:
HttpRequest, HttpResponse

public abstract class HttpMessageHeader extends Object implements MessageHeader
Represents an HTTP message header (either request or response).

The main addition to the base classe MessageHeader is the handling of header fields.

Classes

  • Constructor Details

    • HttpMessageHeader

      public HttpMessageHeader(HttpConstants.HttpProtocol httpProtocol, boolean hasPayload)
      Creates a new message header.
      Parameters:
      httpProtocol - the HTTP protocol
      hasPayload - indicates that a body is expected after the header
  • Method Details

    • protocol

      Return the protocol.
      Returns:
      the HTTP protocol
    • fields

      public Map<String,HttpField<?>> fields()
      Returns all header fields as unmodifiable map.
      Returns:
      the headers
    • setField

      Sets a header field for the message.
      Parameters:
      value - the header field’s value
      Returns:
      the message header for easy chaining
    • setField

      public <T> HttpMessageHeader setField(String name, T value)
      Sets a header field for the message.

      The converter for the field is looked up using HttpField.lookupConverter(String).

      Type Parameters:
      T - the type of the value
      Parameters:
      name - the field name
      value - the header field’s value
      Returns:
      the message header for easy chaining
    • clearHeaders

      Clear all headers.
      Returns:
      the message header for easy chaining
    • removeField

      Removes a header field from the message.
      Parameters:
      name - the header field’s name
      Returns:
      the message header for easy chaining
    • findField

      public <T> Optional<HttpField<T>> findField(String name, Converter<T> converter)
      Returns the header field with the given type if it exists.

      Header fields are provisionally parsed as HttpFields with value type String. When an attempt is made to retrieve such a string field with this method, it is automatically converted to the type indicated by the converter. The conversion is permanent, i.e. the field instance is replaced by a properly typed instance.

      If the conversion fails, the field is considered ill-formatted and handled as if it didn’t exist.

      Note that field type conversion may already occur while doing internal checks. This implies that not all fields can initially be accessed as HttpFields with a String value.

      Type Parameters:
      T - the type of the value in the header field
      Parameters:
      name - the field name
      converter - the converter for the value type
      Returns:
      the header field if it exists
    • findValue

      public <T> Optional<T> findValue(String name, Converter<T> converter)
      Convenience method for getting the value of a header field.
      Type Parameters:
      T - the type of the value in the header field
      Parameters:
      name - the field name
      converter - the converter for the value type
      Returns:
      the value if the header field exists
    • findStringValue

      Convenience method for getting the value of a string field.
      Parameters:
      name - the field name
      Returns:
      the value if the header field exists
      See Also:
    • computeIfAbsent

      public <T> HttpField<T> computeIfAbsent(String name, Converter<T> converter, Supplier<T> supplier)
      Returns the header field with the given name, computing and adding it if it doesn’t exist.
      Type Parameters:
      T - the type of the header field’s value
      Parameters:
      name - the field name
      converter - the converter for the value type
      supplier - the function that computes a value for a new field.
      Returns:
      the header field
    • computeIfAbsent

      public <T> HttpField<T> computeIfAbsent(String name, Supplier<T> supplier)
      Returns the header field with the given name, computing and adding it if it doesn’t exist.

      The converter for the field is looked up using HttpField.lookupConverter(String).

      Type Parameters:
      T - the type of the header field’s value
      Parameters:
      name - the field name
      supplier - the function that computes a value for a new field.
      Returns:
      the header field
    • setHasPayload

      public MessageHeader setHasPayload(boolean hasPayload)
      Set the flag that indicates whether this header is followed by a body.
      Parameters:
      hasPayload - new value
      Returns:
      the message for easy chaining
    • hasPayload

      public boolean hasPayload()
      Returns true if the header is followed by a payload body.
      Specified by:
      hasPayload in interface MessageHeader
      Returns:
      true if payload body data follows
    • isFinal

      public boolean isFinal()
      Returns true if this is a final message.

      A message is final if the value of the Connection header field includes the value “close”.

      Specified by:
      isFinal in interface MessageHeader
      Returns:
      the result