org.springframework.http
Class MediaType

java.lang.Object
  extended by org.springframework.http.MediaType
All Implemented Interfaces:
java.lang.Comparable<MediaType>

public class MediaType
extends java.lang.Object
implements java.lang.Comparable<MediaType>

Represents an Internet Media Type, as defined in the HTTP specification.

Consists of a type and a subtype. Also has functionality to parse media types from a string using parseMediaType(String), or multiple comma-separated media types using parseMediaTypes(String).

Since:
3.0
Author:
Arjen Poutsma, Juergen Hoeller
See Also:
HTTP 1.1, section 3.7

Field Summary
static MediaType ALL
          Public constant media type that includes all media ranges (i.e.
static MediaType APPLICATION_ATOM_XML
          Public constant media type for application/atom+xml.
static MediaType APPLICATION_FORM_URLENCODED
          Public constant media type for application/x-www-form-urlencoded.
static MediaType APPLICATION_JSON
          Public constant media type for application/json.
static MediaType APPLICATION_OCTET_STREAM
          Public constant media type for application/octet-stream.
static MediaType APPLICATION_XHTML_XML
          Public constant media type for application/xhtml+xml.
static MediaType APPLICATION_XML
          Public constant media type for application/xml.
static MediaType IMAGE_GIF
          Public constant media type for image/gif.
static MediaType IMAGE_JPEG
          Public constant media type for image/jpeg.
static MediaType IMAGE_PNG
          Public constant media type for image/png.
static MediaType MULTIPART_FORM_DATA
          Public constant media type for multipart/form-data.
private static java.lang.String PARAM_CHARSET
           
private static java.lang.String PARAM_QUALITY_FACTOR
           
private  java.util.Map<java.lang.String,java.lang.String> parameters
           
static java.util.Comparator<MediaType> QUALITY_VALUE_COMPARATOR
          Comparator used by sortByQualityValue(List).
static java.util.Comparator<MediaType> SPECIFICITY_COMPARATOR
          Comparator used by sortBySpecificity(List).
private  java.lang.String subtype
           
static MediaType TEXT_HTML
          Public constant media type for text/html.
static MediaType TEXT_PLAIN
          Public constant media type for text/plain.
static MediaType TEXT_XML
          Public constant media type for text/xml.
private static java.util.BitSet TOKEN
           
private  java.lang.String type
           
private static java.lang.String WILDCARD_TYPE
           
 
Constructor Summary
MediaType(MediaType other, java.util.Map<java.lang.String,java.lang.String> parameters)
          Copy-constructor that copies the type and subtype of the given MediaType, and allows for different parameter.
MediaType(java.lang.String type)
          Create a new MediaType for the given primary type.
MediaType(java.lang.String type, java.lang.String subtype)
          Create a new MediaType for the given primary type and subtype.
MediaType(java.lang.String type, java.lang.String subtype, java.nio.charset.Charset charSet)
          Create a new MediaType for the given type, subtype, and character set.
MediaType(java.lang.String type, java.lang.String subtype, double qualityValue)
          Create a new MediaType for the given type, subtype, and quality value.
MediaType(java.lang.String type, java.lang.String subtype, java.util.Map<java.lang.String,java.lang.String> parameters)
          Create a new MediaType for the given type, subtype, and parameters.
 
Method Summary
private  void appendTo(java.util.Map<java.lang.String,java.lang.String> map, java.lang.StringBuilder builder)
           
private  void appendTo(java.lang.StringBuilder builder)
           
private  void checkParameters(java.lang.String attribute, java.lang.String value)
           
private  void checkToken(java.lang.String s)
          Checks the given token string for illegal characters, as defined in RFC 2616, section 2.2.
 int compareTo(MediaType other)
          Compares this MediaType to another alphabetically.
 boolean equals(java.lang.Object other)
           
 java.nio.charset.Charset getCharSet()
          Return the character set, as indicated by a charset parameter, if any.
 java.lang.String getParameter(java.lang.String name)
          Return a generic parameter value, given a parameter name.
 double getQualityValue()
          Return the quality value, as indicated by a q parameter, if any.
 java.lang.String getSubtype()
          Return the subtype.
 java.lang.String getType()
          Return the primary type.
 int hashCode()
           
 boolean includes(MediaType other)
          Indicate whether this MediaType includes the given media type.
 boolean isCompatibleWith(MediaType other)
          Indicate whether this MediaType is compatible with the given media type.
 boolean isConcrete()
          Indicates whether this media type is concrete, i.e.
private  boolean isQuotedString(java.lang.String s)
           
 boolean isWildcardSubtype()
          Indicates whether the subtype is the wildcard character * or not.
 boolean isWildcardType()
          Indicates whether the type is the wildcard character * or not.
static MediaType parseMediaType(java.lang.String mediaType)
          Parse the given String into a single MediaType.
static java.util.List<MediaType> parseMediaTypes(java.lang.String mediaTypes)
          Parse the given, comma-separated string into a list of MediaType objects.
static void sortByQualityValue(java.util.List<MediaType> mediaTypes)
          Sorts the given list of MediaType objects by quality value.
static void sortBySpecificity(java.util.List<MediaType> mediaTypes)
          Sorts the given list of MediaType objects by specificity.
 java.lang.String toString()
           
static java.lang.String toString(java.util.Collection<MediaType> mediaTypes)
          Return a string representation of the given list of MediaType objects.
private  java.lang.String unquote(java.lang.String s)
           
static MediaType valueOf(java.lang.String value)
          Parse the given String value into a MediaType object, with this method name following the 'valueOf' naming convention (as supported by ConversionService.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

ALL

public static final MediaType ALL
Public constant media type that includes all media ranges (i.e. */*).


APPLICATION_ATOM_XML

public static final MediaType APPLICATION_ATOM_XML
Public constant media type for application/atom+xml.


APPLICATION_FORM_URLENCODED

public static final MediaType APPLICATION_FORM_URLENCODED
Public constant media type for application/x-www-form-urlencoded.


APPLICATION_JSON

public static final MediaType APPLICATION_JSON
Public constant media type for application/json.


APPLICATION_OCTET_STREAM

public static final MediaType APPLICATION_OCTET_STREAM
Public constant media type for application/octet-stream.


APPLICATION_XHTML_XML

public static final MediaType APPLICATION_XHTML_XML
Public constant media type for application/xhtml+xml.


APPLICATION_XML

public static final MediaType APPLICATION_XML
Public constant media type for application/xml.


IMAGE_GIF

public static final MediaType IMAGE_GIF
Public constant media type for image/gif.


IMAGE_JPEG

public static final MediaType IMAGE_JPEG
Public constant media type for image/jpeg.


IMAGE_PNG

public static final MediaType IMAGE_PNG
Public constant media type for image/png.


MULTIPART_FORM_DATA

public static final MediaType MULTIPART_FORM_DATA
Public constant media type for multipart/form-data.


TEXT_HTML

public static final MediaType TEXT_HTML
Public constant media type for text/html.


TEXT_PLAIN

public static final MediaType TEXT_PLAIN
Public constant media type for text/plain.


TEXT_XML

public static final MediaType TEXT_XML
Public constant media type for text/xml.


TOKEN

private static final java.util.BitSet TOKEN

WILDCARD_TYPE

private static final java.lang.String WILDCARD_TYPE
See Also:
Constant Field Values

PARAM_QUALITY_FACTOR

private static final java.lang.String PARAM_QUALITY_FACTOR
See Also:
Constant Field Values

PARAM_CHARSET

private static final java.lang.String PARAM_CHARSET
See Also:
Constant Field Values

type

private final java.lang.String type

subtype

private final java.lang.String subtype

parameters

private final java.util.Map<java.lang.String,java.lang.String> parameters

SPECIFICITY_COMPARATOR

public static final java.util.Comparator<MediaType> SPECIFICITY_COMPARATOR
Comparator used by sortBySpecificity(List).


QUALITY_VALUE_COMPARATOR

public static final java.util.Comparator<MediaType> QUALITY_VALUE_COMPARATOR
Comparator used by sortByQualityValue(List).

Constructor Detail

MediaType

public MediaType(java.lang.String type)
Create a new MediaType for the given primary type.

The subtype is set to *, parameters empty.

Parameters:
type - the primary type
Throws:
java.lang.IllegalArgumentException - if any of the parameters contain illegal characters

MediaType

public MediaType(java.lang.String type,
                 java.lang.String subtype)
Create a new MediaType for the given primary type and subtype.

The parameters are empty.

Parameters:
type - the primary type
subtype - the subtype
Throws:
java.lang.IllegalArgumentException - if any of the parameters contain illegal characters

MediaType

public MediaType(java.lang.String type,
                 java.lang.String subtype,
                 java.nio.charset.Charset charSet)
Create a new MediaType for the given type, subtype, and character set.

Parameters:
type - the primary type
subtype - the subtype
charSet - the character set
Throws:
java.lang.IllegalArgumentException - if any of the parameters contain illegal characters

MediaType

public MediaType(java.lang.String type,
                 java.lang.String subtype,
                 double qualityValue)
Create a new MediaType for the given type, subtype, and quality value.

Parameters:
type - the primary type
subtype - the subtype
qualityValue - the quality value
Throws:
java.lang.IllegalArgumentException - if any of the parameters contain illegal characters

MediaType

public MediaType(MediaType other,
                 java.util.Map<java.lang.String,java.lang.String> parameters)
Copy-constructor that copies the type and subtype of the given MediaType, and allows for different parameter.

Parameters:
other - the other media type
parameters - the parameters, may be null
Throws:
java.lang.IllegalArgumentException - if any of the parameters contain illegal characters

MediaType

public MediaType(java.lang.String type,
                 java.lang.String subtype,
                 java.util.Map<java.lang.String,java.lang.String> parameters)
Create a new MediaType for the given type, subtype, and parameters.

Parameters:
type - the primary type
subtype - the subtype
parameters - the parameters, may be null
Throws:
java.lang.IllegalArgumentException - if any of the parameters contain illegal characters
Method Detail

checkToken

private void checkToken(java.lang.String s)
Checks the given token string for illegal characters, as defined in RFC 2616, section 2.2.

Throws:
java.lang.IllegalArgumentException - in case of illegal characters
See Also:
HTTP 1.1, section 2.2

checkParameters

private void checkParameters(java.lang.String attribute,
                             java.lang.String value)

isQuotedString

private boolean isQuotedString(java.lang.String s)

unquote

private java.lang.String unquote(java.lang.String s)

getType

public java.lang.String getType()
Return the primary type.


isWildcardType

public boolean isWildcardType()
Indicates whether the type is the wildcard character * or not.


getSubtype

public java.lang.String getSubtype()
Return the subtype.


isWildcardSubtype

public boolean isWildcardSubtype()
Indicates whether the subtype is the wildcard character * or not.

Returns:
whether the subtype is *

isConcrete

public boolean isConcrete()
Indicates whether this media type is concrete, i.e. whether neither the type or subtype is a wildcard character *.

Returns:
whether this media type is concrete

getCharSet

public java.nio.charset.Charset getCharSet()
Return the character set, as indicated by a charset parameter, if any.

Returns:
the character set; or null if not available

getQualityValue

public double getQualityValue()
Return the quality value, as indicated by a q parameter, if any. Defaults to 1.0.

Returns:
the quality factory

getParameter

public java.lang.String getParameter(java.lang.String name)
Return a generic parameter value, given a parameter name.

Parameters:
name - the parameter name
Returns:
the parameter value; or null if not present

includes

public boolean includes(MediaType other)
Indicate whether this MediaType includes the given media type.

For instance, text/* includes text/plain and text/html, and application/*+xml includes application/soap+xml, etc. This method is not symmetric.

Parameters:
other - the reference media type with which to compare
Returns:
true if this media type includes the given media type; false otherwise

isCompatibleWith

public boolean isCompatibleWith(MediaType other)
Indicate whether this MediaType is compatible with the given media type.

For instance, text/* is compatible with text/plain, text/html, and vice versa. In effect, this method is similar to includes(MediaType), except that it is symmetric.

Parameters:
other - the reference media type with which to compare
Returns:
true if this media type is compatible with the given media type; false otherwise

compareTo

public int compareTo(MediaType other)
Compares this MediaType to another alphabetically.

Specified by:
compareTo in interface java.lang.Comparable<MediaType>
Parameters:
other - media type to compare to
See Also:
sortBySpecificity(List)

equals

public boolean equals(java.lang.Object other)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

appendTo

private void appendTo(java.lang.StringBuilder builder)

appendTo

private void appendTo(java.util.Map<java.lang.String,java.lang.String> map,
                      java.lang.StringBuilder builder)

valueOf

public static MediaType valueOf(java.lang.String value)
Parse the given String value into a MediaType object, with this method name following the 'valueOf' naming convention (as supported by ConversionService.

See Also:
parseMediaType(String)

parseMediaType

public static MediaType parseMediaType(java.lang.String mediaType)
Parse the given String into a single MediaType.

Parameters:
mediaType - the string to parse
Returns:
the media type
Throws:
java.lang.IllegalArgumentException - if the string cannot be parsed

parseMediaTypes

public static java.util.List<MediaType> parseMediaTypes(java.lang.String mediaTypes)
Parse the given, comma-separated string into a list of MediaType objects.

This method can be used to parse an Accept or Content-Type header.

Parameters:
mediaTypes - the string to parse
Returns:
the list of media types
Throws:
java.lang.IllegalArgumentException - if the string cannot be parsed

toString

public static java.lang.String toString(java.util.Collection<MediaType> mediaTypes)
Return a string representation of the given list of MediaType objects.

This method can be used to for an Accept or Content-Type header.

Parameters:
mediaTypes - the string to parse
Returns:
the list of media types
Throws:
java.lang.IllegalArgumentException - if the String cannot be parsed

sortBySpecificity

public static void sortBySpecificity(java.util.List<MediaType> mediaTypes)
Sorts the given list of MediaType objects by specificity.

Given two media types:

  1. if either media type has a wildcard type, then the media type without the wildcard is ordered before the other.
  2. if the two media types have different types, then they are considered equal and remain their current order.
  3. if either media type has a wildcard subtype, then the media type without the wildcard is sorted before the other.
  4. if the two media types have different subtypes, then they are considered equal and remain their current order.
  5. if the two media types have different quality value, then the media type with the highest quality value is ordered before the other.
  6. if the two media types have a different amount of parameters, then the media type with the most parameters is ordered before the other.

For example:

audio/basic < audio/* < */*
audio/* < audio/*;q=0.7; audio/*;q=0.3
audio/basic;level=1 < audio/basic
audio/basic == text/html
audio/basic == audio/wave

Parameters:
mediaTypes - the list of media types to be sorted
See Also:
HTTP 1.1, section 14.1

sortByQualityValue

public static void sortByQualityValue(java.util.List<MediaType> mediaTypes)
Sorts the given list of MediaType objects by quality value.

Given two media types:

  1. if the two media types have different quality value, then the media type with the highest quality value is ordered before the other.
  2. if either media type has a wildcard type, then the media type without the wildcard is ordered before the other.
  3. if the two media types have different types, then they are considered equal and remain their current order.
  4. if either media type has a wildcard subtype, then the media type without the wildcard is sorted before the other.
  5. if the two media types have different subtypes, then they are considered equal and remain their current order.
  6. if the two media types have a different amount of parameters, then the media type with the most parameters is ordered before the other.

Parameters:
mediaTypes - the list of media types to be sorted
See Also:
getQualityValue()