Class MediaType

java.lang.Object
org.apache.juneau.MediaType
All Implemented Interfaces:
Comparable<MediaType>
Direct Known Subclasses:
MediaRange

@BeanIgnore public class MediaType extends Object implements Comparable<MediaType>
Describes a single media type used in content negotiation between an HTTP client and server, as described in Section 14.1 and 14.7 of RFC2616 (the HTTP/1.1 specification).
See Also:
  • Field Details

    • EMPTY

      public static final MediaType EMPTY
      Represents an empty media type object.
    • CSV

      public static final MediaType CSV
      Reusable predefined media type
    • HTML

      public static final MediaType HTML
      Reusable predefined media type
    • JSON

      public static final MediaType JSON
      Reusable predefined media type
    • MSGPACK

      public static final MediaType MSGPACK
      Reusable predefined media type
    • PLAIN

      public static final MediaType PLAIN
      Reusable predefined media type
    • UON

      public static final MediaType UON
      Reusable predefined media type
    • URLENCODING

      public static final MediaType URLENCODING
      Reusable predefined media type
    • XML

      public static final MediaType XML
      Reusable predefined media type
    • XMLSOAP

      public static final MediaType XMLSOAP
      Reusable predefined media type
    • RDF

      public static final MediaType RDF
      Reusable predefined media type
    • RDFABBREV

      public static final MediaType RDFABBREV
      Reusable predefined media type
    • NTRIPLE

      public static final MediaType NTRIPLE
      Reusable predefined media type
    • TURTLE

      public static final MediaType TURTLE
      Reusable predefined media type
    • N3

      public static final MediaType N3
      Reusable predefined media type
  • Constructor Details

    • MediaType

      public MediaType(String mt)
      Constructor.
      Parameters:
      mt - The media type string.
    • MediaType

      public MediaType(String mt, NameValuePair[] parameters)
      Constructor.
      Parameters:
      mt - The media type string.
      parameters - The media type parameters. If null, they're pulled from the media type string.
    • MediaType

      Constructor.
      Parameters:
      e - The parsed media type string.
    • MediaType

      public MediaType(HeaderElement e, NameValuePair[] parameters)
      Constructor.
      Parameters:
      e - The parsed media type string.
      parameters - Optional parameters.
  • Method Details

    • of

      public static MediaType of(String value)
      Returns the media type for the specified string. The same media type strings always return the same objects so that these objects can be compared for equality using '=='.
      Notes:
      • Spaces are replaced with '+' characters. This gets around the issue where passing media type strings with '+' as HTTP GET parameters get replaced with spaces by your browser. Since spaces aren't supported by the spec, this is doesn't break anything.
      • Anything including and following the ';' character is ignored (e.g. ";charset=X").
      Parameters:
      value - The media type string. Will be lowercased. Returns null if input is null or empty.
      Returns:
      A cached media type object.
    • of

      public static MediaType of(String value, NameValuePair... parameters)
      Same as of(String) but allows you to specify the parameters.
      Parameters:
      value - The media type string. Will be lowercased. Returns null if input is null or empty.
      parameters - The media type parameters. If null, they're pulled from the media type string.
      Returns:
      A new media type object, cached if parameters were not specified.
    • ofAll

      public static MediaType[] ofAll(String... values)
      Same as of(String) but allows you to construct an array of MediaTypes from an array of strings.
      Parameters:
      values - The media type strings.
      Returns:
      An array of MediaType objects.
      Always the same length as the input string array.
    • getType

      public final String getType()
      Returns the 'type' fragment of the 'type/subType' string.
      Returns:
      The media type.
    • getSubType

      public final String getSubType()
      Returns the 'subType' fragment of the 'type/subType' string.
      Returns:
      The media subtype.
    • hasSubType

      public final boolean hasSubType(String st)
      Returns true if the subtype contains the specified '+' delimited subtype value.
      Parameters:
      st - The subtype string. Case is ignored.
      Returns:
      true if the subtype contains the specified subtype string.
    • getSubTypes

      public final List<String> getSubTypes()
      Returns the subtypes broken down by fragments delimited by "'".

      For example, the media type "text/foo+bar" will return a list of ['foo','bar']

      Returns:
      An unmodifiable list of subtype fragments. Never null.
    • forEachSubType

      public final MediaType forEachSubType(Consumer<String> action)
      Performs an action on the subtypes broken down by fragments delimited by "'".
      Parameters:
      action - The action to perform.
      Returns:
      This object.
    • isMetaSubtype

      public final boolean isMetaSubtype()
      Returns true if this media type subtype contains the '*' meta character.
      Returns:
      true if this media type subtype contains the '*' meta character.
    • match

      public final int match(MediaType o, boolean allowExtraSubTypes)
      Returns a match metric against the specified media type where a larger number represents a better match.

      This media type can contain '*' metacharacters.
      The comparison media type must not.

      • Exact matches (e.g. "text/json"/"text/json") should match better than meta-character matches (e.g. "text/*"/"text/json")
      • The comparison media type can have additional subtype tokens (e.g. "text/json+foo") that will not prevent a match if the allowExtraSubTypes flag is set. The reverse is not true, e.g. the comparison media type must contain all subtype tokens found in the comparing media type.
        • We want the JsonSerializer ("text/json") class to be able to handle requests for "text/json+foo".
        • We want to make sure Json5Serializer ("text/json5") does not handle requests for "text/json".
        More token matches should result in a higher match number.
      The formula is as follows for type/subTypes:
      • An exact match is 100,000.
      • Add the following for type (assuming subtype match is <0):
        • 10,000 for an exact match (e.g. "text"=="text").
        • 5,000 for a meta match (e.g. "*"=="text").
      • Add the following for subtype (assuming type match is <0):
        • 7,500 for an exact match (e.g. "json+foo"=="json+foo" or "json+foo"=="foo+json")
        • 100 for every subtype entry match (e.g. "json"/"json+foo")
      Parameters:
      o - The media type to compare with.
      allowExtraSubTypes - If true,
      Returns:
      true if the media types match.
    • getParameters

      Returns the additional parameters on this media type.

      For example, given the media type string "text/html;level=1", will return a map with the single entry {level:['1']}.

      Returns:
      The map of additional parameters, or an empty map if there are no parameters.
    • forEachParameter

      Performs an action on the additional parameters on this media type.
      Parameters:
      action - The action to perform.
      Returns:
      This object.
    • getParameter

      public String getParameter(String name)
      Returns the additional parameter on this media type.
      Parameters:
      name - The additional parameter name.
      Returns:
      The parameter value, or null if not found.
    • match

      public int match(List<MediaType> mediaTypes)
      Given a list of media types, returns the best match for this Content-Type header.

      Note that fuzzy matching is allowed on the media types where the Content-Types header may contain additional subtype parts.
      For example, given a Content-Type value of "text/json+activity", the media type "text/json" will match if "text/json+activity" or "text/activity+json" isn't found.
      The purpose for this is to allow parsers to match when artifacts such as id properties are present in the header.

      Parameters:
      mediaTypes - The media types to match against.
      Returns:
      The index into the array of the best match, or -1 if no suitable matches could be found.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • compareTo

      public final int compareTo(MediaType o)
      Specified by:
      compareTo in interface Comparable<MediaType>