javax.ws.rs.core

## Class MediaType

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

- Direct Known Subclasses:
    WeightedMediaType

      public class MediaType
      extends Object

  An abstraction for a media type. Instances are immutable.
  Since:
    1.0
  Author:
    Paul Sandoz, Marc Hadley
  See Also:
    HTTP/1.1 section 3.7

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static String` | `APPLICATION_ATOM_XML` A `String` constant representing ""application/atom+xml"" media type.
    `static MediaType` | `APPLICATION_ATOM_XML_TYPE` A `MediaType` constant representing ""application/atom+xml"" media type.
    `static String` | `APPLICATION_FORM_URLENCODED` A `String` constant representing ""application/x-www-form-urlencoded"" media type.
    `static MediaType` | `APPLICATION_FORM_URLENCODED_TYPE` A `MediaType` constant representing ""application/x-www-form-urlencoded"" media type.
    `static String` | `APPLICATION_JSON` A `String` constant representing ""application/json"" media type.
    `static MediaType` | `APPLICATION_JSON_TYPE` A `MediaType` constant representing ""application/json"" media type.
    `static String` | `APPLICATION_OCTET_STREAM` A `String` constant representing ""application/octet-stream"" media type.
    `static MediaType` | `APPLICATION_OCTET_STREAM_TYPE` A `MediaType` constant representing ""application/octet-stream"" media type.
    `static String` | `APPLICATION_SVG_XML` A `String` constant representing ""application/svg+xml"" media type.
    `static MediaType` | `APPLICATION_SVG_XML_TYPE` A `MediaType` constant representing ""application/svg+xml"" media type.
    `static String` | `APPLICATION_XHTML_XML` A `String` constant representing ""application/xhtml+xml"" media type.
    `static MediaType` | `APPLICATION_XHTML_XML_TYPE` A `MediaType` constant representing ""application/xhtml+xml"" media type.
    `static String` | `APPLICATION_XML` A `String` constant representing ""application/xml"" media type.
    `static MediaType` | `APPLICATION_XML_TYPE` A `MediaType` constant representing ""application/xml"" media type.
    `static String` | `CHARSET_PARAMETER` The media type `charset` parameter name.
    `static String` | `MEDIA_TYPE_WILDCARD` The value of a type or subtype wildcard ""*"".
    `static String` | `MULTIPART_FORM_DATA` A `String` constant representing ""multipart/form-data"" media type.
    `static MediaType` | `MULTIPART_FORM_DATA_TYPE` A `MediaType` constant representing ""multipart/form-data"" media type.
    `static String` | `TEXT_HTML` A `String` constant representing ""text/html"" media type.
    `static MediaType` | `TEXT_HTML_TYPE` A `MediaType` constant representing ""text/html"" media type.
    `static String` | `TEXT_PLAIN` A `String` constant representing ""text/plain"" media type.
    `static MediaType` | `TEXT_PLAIN_TYPE` A `MediaType` constant representing ""text/plain"" media type.
    `static String` | `TEXT_XML` A `String` constant representing ""text/xml"" media type.
    `static MediaType` | `TEXT_XML_TYPE` A `MediaType` constant representing ""text/xml"" media type.
    `static String` | `WILDCARD` A `String` constant representing wildcard ""*/*"" media type .
    `static MediaType` | `WILDCARD_TYPE` A `MediaType` constant representing wildcard ""*/*"" media type.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `MediaType()` Creates a new instance of `MediaType`, both type and subtype are wildcards.
    `MediaType(String type, String subtype)` Creates a new instance of `MediaType` with the supplied type and subtype.
    `MediaType(String type, String subtype, Map<String,String> parameters)` Creates a new instance of `MediaType` with the supplied type, subtype and parameters.
    `MediaType(String type, String subtype, String charset)` Creates a new instance of `MediaType` with the supplied type, subtype and ""charset"" parameter.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `boolean` | `equals(Object obj)` Compares `obj` to this media type to see if they are the same by comparing type, subtype and parameters.
    `Map<String,String>` | `getParameters()` Getter for a read-only parameter map.
    `String` | `getSubtype()` Getter for subtype.
    `String` | `getType()` Getter for primary type.
    `int` | `hashCode()` Generate a hash code from the type, subtype and parameters.
    `boolean` | `isCompatible(MediaType other)` Check if this media type is compatible with another media type.
    `boolean` | `isWildcardSubtype()` Checks if the subtype is a wildcard.
    `boolean` | `isWildcardType()` Checks if the primary type is a wildcard.
    `String` | `toString()` Convert the media type to a string suitable for use as the value of a corresponding HTTP header.
    `static MediaType` | `valueOf(String type)` Creates a new instance of `MediaType` by parsing the supplied string.
    `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.

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### CHARSET_PARAMETER

          public static final String CHARSET_PARAMETER

      The media type `charset` parameter name.
      See Also:
        Constant Field Values

    - #### MEDIA_TYPE_WILDCARD

          public static final String MEDIA_TYPE_WILDCARD

      The value of a type or subtype wildcard ""*"".
      See Also:
        Constant Field Values

    - #### WILDCARD

          public static final String WILDCARD

      A `String` constant representing wildcard ""*/*"" media type .
      See Also:
        Constant Field Values

    - #### WILDCARD_TYPE

          public static final MediaType WILDCARD_TYPE

      A `MediaType` constant representing wildcard ""*/*"" media type.

    - #### APPLICATION_XML

          public static final String APPLICATION_XML

      A `String` constant representing ""application/xml"" media type.
      See Also:
        Constant Field Values

    - #### APPLICATION_XML_TYPE

          public static final MediaType APPLICATION_XML_TYPE

      A `MediaType` constant representing ""application/xml"" media type.

    - #### APPLICATION_ATOM_XML

          public static final String APPLICATION_ATOM_XML

      A `String` constant representing ""application/atom+xml"" media type.
      See Also:
        Constant Field Values

    - #### APPLICATION_ATOM_XML_TYPE

          public static final MediaType APPLICATION_ATOM_XML_TYPE

      A `MediaType` constant representing ""application/atom+xml"" media type.

    - #### APPLICATION_XHTML_XML

          public static final String APPLICATION_XHTML_XML

      A `String` constant representing ""application/xhtml+xml"" media type.
      See Also:
        Constant Field Values

    - #### APPLICATION_XHTML_XML_TYPE

          public static final MediaType APPLICATION_XHTML_XML_TYPE

      A `MediaType` constant representing ""application/xhtml+xml"" media type.

    - #### APPLICATION_SVG_XML

          public static final String APPLICATION_SVG_XML

      A `String` constant representing ""application/svg+xml"" media type.
      See Also:
        Constant Field Values

    - #### APPLICATION_SVG_XML_TYPE

          public static final MediaType APPLICATION_SVG_XML_TYPE

      A `MediaType` constant representing ""application/svg+xml"" media type.

    - #### APPLICATION_JSON

          public static final String APPLICATION_JSON

      A `String` constant representing ""application/json"" media type.
      See Also:
        Constant Field Values

    - #### APPLICATION_JSON_TYPE

          public static final MediaType APPLICATION_JSON_TYPE

      A `MediaType` constant representing ""application/json"" media type.

    - #### APPLICATION_FORM_URLENCODED

          public static final String APPLICATION_FORM_URLENCODED

      A `String` constant representing ""application/x-www-form-urlencoded"" media type.
      See Also:
        Constant Field Values

    - #### APPLICATION_FORM_URLENCODED_TYPE

          public static final MediaType APPLICATION_FORM_URLENCODED_TYPE

      A `MediaType` constant representing ""application/x-www-form-urlencoded"" media type.

    - #### MULTIPART_FORM_DATA

          public static final String MULTIPART_FORM_DATA

      A `String` constant representing ""multipart/form-data"" media type.
      See Also:
        Constant Field Values

    - #### MULTIPART_FORM_DATA_TYPE

          public static final MediaType MULTIPART_FORM_DATA_TYPE

      A `MediaType` constant representing ""multipart/form-data"" media type.

    - #### APPLICATION_OCTET_STREAM

          public static final String APPLICATION_OCTET_STREAM

      A `String` constant representing ""application/octet-stream"" media type.
      See Also:
        Constant Field Values

    - #### APPLICATION_OCTET_STREAM_TYPE

          public static final MediaType APPLICATION_OCTET_STREAM_TYPE

      A `MediaType` constant representing ""application/octet-stream"" media type.

    - #### TEXT_PLAIN

          public static final String TEXT_PLAIN

      A `String` constant representing ""text/plain"" media type.
      See Also:
        Constant Field Values

    - #### TEXT_PLAIN_TYPE

          public static final MediaType TEXT_PLAIN_TYPE

      A `MediaType` constant representing ""text/plain"" media type.

    - #### TEXT_XML

          public static final String TEXT_XML

      A `String` constant representing ""text/xml"" media type.
      See Also:
        Constant Field Values

    - #### TEXT_XML_TYPE

          public static final MediaType TEXT_XML_TYPE

      A `MediaType` constant representing ""text/xml"" media type.

    - #### TEXT_HTML

          public static final String TEXT_HTML

      A `String` constant representing ""text/html"" media type.
      See Also:
        Constant Field Values

    - #### TEXT_HTML_TYPE

          public static final MediaType TEXT_HTML_TYPE

      A `MediaType` constant representing ""text/html"" media type.

  - ### Constructor Detail

    - #### 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(String type,
                   String subtype,
                   String charset)

      Creates a new instance of `MediaType` with the supplied type, subtype and ""charset"" parameter.
      Parameters:
        `type` - the primary type, `null` is equivalent to `MEDIA_TYPE_WILDCARD`
        `subtype` - the subtype, `null` is equivalent to `MEDIA_TYPE_WILDCARD`
        `charset` - the ""charset"" parameter value. If `null` or empty the ""charset"" parameter will not be set.

    - #### MediaType

          public MediaType()

      Creates a new instance of `MediaType`, both type and subtype are wildcards. Consider using the constant `WILDCARD_TYPE` instead.

  - ### Method Detail

    - #### valueOf

          public static MediaType valueOf(String type)

      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.

    - #### 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

    - #### 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 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.

Copyright © 2014. All Rights Reserved.