Interface HttpMediaType

All Superinterfaces:
Comparable<HttpMediaType>, MediaType, Predicate<HttpMediaType>

public sealed interface HttpMediaType extends Predicate<HttpMediaType>, Comparable<HttpMediaType>, MediaType
Media type used in HTTP headers, in addition to the media type definition, these may contain additional parameters, such as QUALITY_FACTOR_PARAMETER and CHARSET_PARAMETER.
  • Field Details

    • CHARSET_PARAMETER

      static final String CHARSET_PARAMETER
      The media type "charset" parameter name.
      See Also:
    • QUALITY_FACTOR_PARAMETER

      static final String QUALITY_FACTOR_PARAMETER
      The media type quality factor "q" parameter name.
      See Also:
    • APPLICATION_JSON

      static final HttpMediaType APPLICATION_JSON
      application/json media type without parameters.
    • JSON_UTF_8

      static final HttpMediaType JSON_UTF_8
      application/json media type with UTF-8 charset.
    • TEXT_PLAIN

      static final HttpMediaType TEXT_PLAIN
      text/plain media type without parameters.
    • PLAINTEXT_UTF_8

      static final HttpMediaType PLAINTEXT_UTF_8
      text/plain media type with UTF-8 charset.
    • APPLICATION_OCTET_STREAM

      static final HttpMediaType APPLICATION_OCTET_STREAM
      application/octet-stream media type without parameters.
    • JSON_PREDICATE

      static final Predicate<HttpMediaType> JSON_PREDICATE
      Predicate to test if MediaType is application/json or has json suffix.
    • JSON_EVENT_STREAM_PREDICATE

      static final Predicate<HttpMediaType> JSON_EVENT_STREAM_PREDICATE
      Predicate to test if MediaType is text/event-stream without any parameter or with parameter "element-type". This "element-type" has to be equal to "application/json".
  • Method Details

    • builder

      static HttpMediaType.Builder builder()
      A fluent API builder for creating customized Media type instances.
      Returns:
      a new builder
    • create

      static HttpMediaType create(MediaType mediaType)
      Create a new HTTP media type from media type.
      Parameters:
      mediaType - media type
      Returns:
      a new HTTP media type without any parameters
    • create

      static HttpMediaType create(String mediaTypeString)
      Parse media type from the provided string.
      Parameters:
      mediaTypeString - media type string
      Returns:
      HTTP media type parsed from the string
    • mediaType

      MediaType mediaType()
      The underlying media type.
      Returns:
      media type
    • qualityFactor

      double qualityFactor()
      Quality factor, if not defined, defaults to 1.
      Returns:
      quality factor
    • parameters

      Map<String,String> parameters()
      Read-only parameter map. Keys are case-insensitive.
      Returns:
      an immutable map of parameters.
    • charset

      default Optional<String> charset()
      Gets Optional value of charset parameter.
      Returns:
      Charset parameter.
    • test

      boolean test(HttpMediaType other)
      Check if this media type is compatible with another media type. E.g. image/* is compatible with image/jpeg, image/png, etc. Media type parameters are ignored. The function is commutative.
      Specified by:
      test in interface Predicate<HttpMediaType>
      Parameters:
      other - the media type to compare with.
      Returns:
      true if the types are compatible, false otherwise.
    • test

      boolean test(MediaType mediaType)
      Check if this media type is compatible with another media type. E.g. image/* is compatible with image/jpeg, image/png, etc. Media type parameters are ignored. The function is commutative.
      Parameters:
      mediaType - the media type to compare with.
      Returns:
      true if the types are compatible, false otherwise.
    • type

      default String type()
      Specified by:
      type in interface MediaType
    • subtype

      default String subtype()
      Specified by:
      subtype in interface MediaType
    • withCharset

      default HttpMediaType withCharset(String charset)
      Create a new HttpMediaType instance with the same type, subtype and parameters copied from the original instance and the supplied "charset" parameter.
      Parameters:
      charset - the "charset" parameter value. If null or empty the "charset" parameter will not be set or updated.
      Returns:
      copy of the current MediaType instance with the "charset" parameter set to the supplied value.
    • text

      String text()
      Text of this media type, to be used on the wire.
      Specified by:
      text in interface MediaType
      Returns:
      text including all parameters
    • withParameter

      default HttpMediaType withParameter(String name, String value)
      Create a new HttpMediaType instance with the same type, subtype and parameters copied from the original instance and the supplied custom parameter.
      Parameters:
      name - name of the parameter
      value - value of the parameter
      Returns:
      copy of the current MediaType instance with the "charset" parameter set to the supplied value.