Class MediaType

java.lang.Object
io.helidon.common.http.MediaType
All Implemented Interfaces:
AcceptPredicate<MediaType>, Predicate<MediaType>

public final class MediaType extends Object implements AcceptPredicate<MediaType>
An abstraction for a media type. Instances are immutable.
See Also:
  • Field Details

    • CHARSET_PARAMETER

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

      public static final MediaType WILDCARD
      A MediaType constant representing wildcard media type.
    • APPLICATION_XML

      public static final MediaType APPLICATION_XML
      A MediaType constant representing application/xml media type.
    • APPLICATION_ATOM_XML

      public static final MediaType APPLICATION_ATOM_XML
      A MediaType constant representing application/atom+xml media type.
    • APPLICATION_XHTML_XML

      public static final MediaType APPLICATION_XHTML_XML
      A MediaType constant representing application/xhtml+xml media type.
    • APPLICATION_SVG_XML

      public static final MediaType APPLICATION_SVG_XML
      A MediaType constant representing application/svg+xml media type.
    • APPLICATION_JSON

      public static final MediaType APPLICATION_JSON
      A MediaType constant representing application/json media type.
    • APPLICATION_STREAM_JSON

      public static final MediaType APPLICATION_STREAM_JSON
      A MediaType constant representing application/stream+json media type.
    • APPLICATION_FORM_URLENCODED

      public static final MediaType APPLICATION_FORM_URLENCODED
      A MediaType constant representing application/x-www-form-urlencoded media type.
    • MULTIPART_FORM_DATA

      public static final MediaType MULTIPART_FORM_DATA
      A MediaType constant representing multipart/form-data media type.
    • APPLICATION_OCTET_STREAM

      public static final MediaType APPLICATION_OCTET_STREAM
      A MediaType constant representing application/octet-stream media type.
    • TEXT_PLAIN

      public static final MediaType TEXT_PLAIN
      A MediaType constant representing text/plain media type.
    • TEXT_XML

      public static final MediaType TEXT_XML
      A MediaType constant representing text/xml media type.
    • TEXT_HTML

      public static final MediaType TEXT_HTML
      A MediaType constant representing text/html media type.
    • APPLICATION_OPENAPI_YAML

      public static final MediaType APPLICATION_OPENAPI_YAML
      A MediaType constant representing OpenAPI yaml.

      See https://github.com/opengeospatial/WFS_FES/issues/117#issuecomment-402188280

    • APPLICATION_OPENAPI_JSON

      public static final MediaType APPLICATION_OPENAPI_JSON
      A MediaType constant representing OpenAPI json.
    • APPLICATION_X_YAML

      public static final MediaType APPLICATION_X_YAML
      A MediaType constant representing "x" YAML as application.
    • APPLICATION_YAML

      public static final MediaType APPLICATION_YAML
      A MediaType constant representing pseudo-registered YAML. (It is not actually registered.)
    • TEXT_X_YAML

      public static final MediaType TEXT_X_YAML
      A MediaType constant representing "x" YAML as text.
    • TEXT_YAML

      public static final MediaType TEXT_YAML
      A MediaType constant representing pseudo-registered YAML as text.
    • APPLICATION_JAVASCRIPT

      public static final MediaType APPLICATION_JAVASCRIPT
      A MediaType constant representing application/javascript media type.
    • TEXT_EVENT_STREAM

      public static final MediaType TEXT_EVENT_STREAM
      A MediaType constant representing text/event-stream media type.
    • APPLICATION_X_NDJSON

      public static final MediaType APPLICATION_X_NDJSON
      A MediaType constant representing application/x-ndjson media type.
    • APPLICATION_OPENMETRICS

      public static final MediaType APPLICATION_OPENMETRICS
      A MediaType constant representing the application/openmetrics-text media type.
    • XML_PREDICATE

      public static final Predicate<MediaType> XML_PREDICATE
      Predicate to test if MediaType is application/xml or text/xml or has xml suffix.
    • JSON_PREDICATE

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

      public static final Predicate<MediaType> 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

    • create

      public static MediaType create(String type, String subtype)
      Creates a new instance of MediaType with the supplied type and subtype.
      Parameters:
      type - the primary type, null is equivalent to AcceptPredicate.WILDCARD_VALUE
      subtype - the subtype, null is equivalent to AcceptPredicate.WILDCARD_VALUE
      Returns:
      a new media type for the specified type and subtype
    • parse

      public static MediaType parse(String input)
      Parses a media type from its string representation.
      Parameters:
      input - the input string representing a media type
      Returns:
      parsed MediaType instance
      Throws:
      IllegalArgumentException - if the input is not parsable
      NullPointerException - if the input is null
    • builder

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

      public String type()
      Getter for primary type.
      Returns:
      value of primary type.
    • isWildcardType

      public boolean isWildcardType()
      Checks if the primary type is a wildcard.
      Returns:
      true if the primary type is a wildcard.
    • subtype

      public String subtype()
      Getter for subtype.
      Returns:
      value of subtype.
    • isWildcardSubtype

      public boolean isWildcardSubtype()
      Checks if the subtype is a wildcard.
      Returns:
      true if the subtype is a wildcard.
    • parameters

      public Map<String,String> parameters()
      Getter for a read-only parameter map. Keys are case-insensitive.
      Returns:
      an immutable map of parameters.
    • withCharset

      public MediaType withCharset(String charset)
      Create a new MediaType 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.
      Since:
      2.0
    • withoutParameters

      @Deprecated(since="2.5.5", forRemoval=true) public MediaType withoutParameters()
      Deprecated, for removal: This API element is subject to removal in a future version.
      will be removed in 4.x version.
      Create a new MediaType instance with the same type and subtype. All parameters are trimmed. If current MediaType instance does not contain parameters, the same instance is returned.
      Returns:
      MediaType instance without parameters.
    • charset

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

      public double qualityFactor()
      Description copied from interface: AcceptPredicate
      Gets quality factor parameter ("q") as a double value. If missing, then returns 1.0
      Specified by:
      qualityFactor in interface AcceptPredicate<MediaType>
      Returns:
      Quality factor parameter.
    • test

      public boolean test(MediaType 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<MediaType>
      Parameters:
      other - the media type to compare with.
      Returns:
      true if the types are compatible, false otherwise.
    • equals

      public boolean equals(Object obj)
      Compares obj to this media type to see if they are the same by comparing type, subtype and parameters. Note that the case-sensitivity of parameter values is dependent on the semantics of the parameter name, see HTTP/1.1. This method assumes that values are case-sensitive.

      Note that the equals(...) implementation does not perform a class equality check (this.getClass() == obj.getClass()). Therefore any class that extends from MediaType class and needs to override one of the equals(...) and hashCode() methods must always override both methods to ensure the contract between Object.equals(java.lang.Object) and Object.hashCode() does not break.

      Overrides:
      equals in class Object
      Parameters:
      obj - the object to compare to.
      Returns:
      true if the two media types are the same, false otherwise.
    • hashCode

      public int hashCode()
      Generate a hash code from the type, subtype and parameters.

      Note that the equals(java.lang.Object) implementation does not perform a class equality check (this.getClass() == obj.getClass()). Therefore any class that extends from MediaType class and needs to override one of the equals(Object) and hashCode() methods must always override both methods to ensure the contract between Object.equals(java.lang.Object) and Object.hashCode() does not break.

      Overrides:
      hashCode in class Object
      Returns:
      a generated hash code.
    • toString

      public String toString()
      Convert the media type to a string suitable for use as the value of a corresponding HTTP header.
      Overrides:
      toString in class Object
      Returns:
      a string version of the media type.
    • hasSuffix

      public boolean hasSuffix(String suffix)
      Tests if this media type has provided Structured Syntax suffix (RFC 6839).
      Parameters:
      suffix - Suffix with or without '+' prefix. If null or empty then returns true if this media type has ANY suffix.
      Returns:
      true if media type has specified suffix or has any suffix if parameter is null or empty.