Class MediaType

java.lang.Object
javax.ws.rs.core.MediaType

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

    • type

      private String type
    • subtype

      private String subtype
    • parameters

      private Map<String,String> parameters
    • emptyMap

      private static final Map<String,String> emptyMap
      Empty immutable map used for all instances without parameters
    • delegate

      private static final RuntimeDelegate.HeaderDelegate<MediaType> delegate
    • MEDIA_TYPE_WILDCARD

      public static final String MEDIA_TYPE_WILDCARD
      The value of a type or subtype wildcard: "*"
      See Also:
    • WILDCARD

      public static final String WILDCARD
      "*/*"
      See Also:
    • WILDCARD_TYPE

      public static final MediaType WILDCARD_TYPE
      "*/*"
    • APPLICATION_XML

      public static final String APPLICATION_XML
      "application/xml"
      See Also:
    • APPLICATION_XML_TYPE

      public static final MediaType APPLICATION_XML_TYPE
      "application/xml"
    • APPLICATION_ATOM_XML

      public static final String APPLICATION_ATOM_XML
      "application/atom+xml"
      See Also:
    • APPLICATION_ATOM_XML_TYPE

      public static final MediaType APPLICATION_ATOM_XML_TYPE
      "application/atom+xml"
    • APPLICATION_XHTML_XML

      public static final String APPLICATION_XHTML_XML
      "application/xhtml+xml"
      See Also:
    • APPLICATION_XHTML_XML_TYPE

      public static final MediaType APPLICATION_XHTML_XML_TYPE
      "application/xhtml+xml"
    • APPLICATION_SVG_XML

      public static final String APPLICATION_SVG_XML
      "application/svg+xml"
      See Also:
    • APPLICATION_SVG_XML_TYPE

      public static final MediaType APPLICATION_SVG_XML_TYPE
      "application/svg+xml"
    • APPLICATION_JSON

      public static final String APPLICATION_JSON
      "application/json"
      See Also:
    • APPLICATION_JSON_TYPE

      public static final MediaType APPLICATION_JSON_TYPE
      "application/json"
    • APPLICATION_FORM_URLENCODED

      public static final String APPLICATION_FORM_URLENCODED
      "application/x-www-form-urlencoded"
      See Also:
    • APPLICATION_FORM_URLENCODED_TYPE

      public static final MediaType APPLICATION_FORM_URLENCODED_TYPE
      "application/x-www-form-urlencoded"
    • MULTIPART_FORM_DATA

      public static final String MULTIPART_FORM_DATA
      "multipart/form-data"
      See Also:
    • MULTIPART_FORM_DATA_TYPE

      public static final MediaType MULTIPART_FORM_DATA_TYPE
      "multipart/form-data"
    • APPLICATION_OCTET_STREAM

      public static final String APPLICATION_OCTET_STREAM
      "application/octet-stream"
      See Also:
    • APPLICATION_OCTET_STREAM_TYPE

      public static final MediaType APPLICATION_OCTET_STREAM_TYPE
      "application/octet-stream"
    • TEXT_PLAIN

      public static final String TEXT_PLAIN
      "text/plain"
      See Also:
    • TEXT_PLAIN_TYPE

      public static final MediaType TEXT_PLAIN_TYPE
      "text/plain"
    • TEXT_XML

      public static final String TEXT_XML
      "text/xml"
      See Also:
    • TEXT_XML_TYPE

      public static final MediaType TEXT_XML_TYPE
      "text/xml"
    • TEXT_HTML

      public static final String TEXT_HTML
      "text/html"
      See Also:
    • TEXT_HTML_TYPE

      public static final MediaType TEXT_HTML_TYPE
      "text/html"
  • Constructor Details

    • MediaType

      public MediaType(String type, String subtype, Map<String,String> parameters)
      Creates a new instance of MediaType with the supplied type, subtype and parameters.
      Parameters:
      type - the primary type, null is equivalent to MEDIA_TYPE_WILDCARD.
      subtype - the subtype, null is equivalent to MEDIA_TYPE_WILDCARD.
      parameters - a map of media type parameters, null is the same as an empty map.
    • MediaType

      public MediaType(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 MEDIA_TYPE_WILDCARD
      subtype - the subtype, null is equivalent to MEDIA_TYPE_WILDCARD
    • MediaType

      public MediaType()
      Creates a new instance of MediaType, both type and subtype are wildcards. Consider using the constant WILDCARD_TYPE instead.
  • Method Details

    • valueOf

      public static MediaType valueOf(String type) throws IllegalArgumentException
      Creates a new instance of MediaType by parsing the supplied string.
      Parameters:
      type - the media type string
      Returns:
      the newly created MediaType
      Throws:
      IllegalArgumentException - if the supplied string cannot be parsed or is null
    • getType

      public String getType()
      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
    • getSubtype

      public String getSubtype()
      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
    • getParameters

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

      public boolean isCompatible(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.
      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
      invalid @link
      {@link <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7">HTTP/1.1</a>
      }. This method assumes that values are case-sensitive.
      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 hashcode from the type, subtype and parameters.
      Overrides:
      hashCode in class Object
      Returns:
      a hashcode
    • 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 stringified media type