org.springframework.http
Class MediaType

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

public class MediaType
extends Object
implements 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

Field Summary
static MediaType ALL
           
 
Constructor Summary
MediaType(String type)
          Create a new MediaType for the given primary type.
MediaType(String type, String subtype)
          Create a new MediaType for the given primary type and subtype.
MediaType(String type, String subtype, Charset charSet)
          Create a new MediaType for the given type, subtype, and character set.
MediaType(String type, String subtype, Map<String,String> parameters)
          Create a new MediaType for the given type, subtype, and parameters.
 
Method Summary
 int compareTo(MediaType other)
          Compare this MediaType to another.
 boolean equals(Object other)
           
 Charset getCharSet()
          Return the character set, as indicated by a charset parameter, if any.
 String getParameter(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.
 String getSubtype()
          Return the subtype.
 String getType()
          Return the primary type.
 int hashCode()
           
 boolean includes(MediaType other)
          Indicate whether this MediaType includes the given media type.
 boolean isWildcardSubtype()
          Indicate whether the subtype is the wildcard character * or not.
 boolean isWildcardType()
          Indicate whether the type is the wildcard character * or not.
static MediaType parseMediaType(String mediaType)
          Parse the given String into a single MediaType.
static List<MediaType> parseMediaTypes(String mediaTypes)
          Parse the given, comma-seperated string into a list of MediaType objects.
 String toString()
           
static String toString(Collection<MediaType> mediaTypes)
          Return a string representation of the given list of MediaType objects.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

ALL

public static final MediaType ALL
Constructor Detail

MediaType

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

The subtype is set to *, parameters empty.

Parameters:
type - the primary type

MediaType

public MediaType(String type,
                 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

MediaType

public MediaType(String type,
                 String subtype,
                 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

MediaType

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

Parameters:
type - the primary type
subtype - the subtype
parameters - the parameters, mat be null
Method Detail

getType

public String getType()
Return the primary type.


isWildcardType

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


getSubtype

public String getSubtype()
Return the subtype.


isWildcardSubtype

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

Returns:
whether the subtype is *

getCharSet

public 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 String getParameter(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, text/html, etc.

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

compareTo

public int compareTo(MediaType other)
Compare this MediaType to another. Sorting with this comparator follows the general rule:
audio/basic < audio/* < */*
. That is, an explicit media type is sorted before an unspecific media type. Quality parameters are also considered, so that
audio/* < audio/*;q=0.7; audio/*;q=0.3
.

Specified by:
compareTo in interface Comparable<MediaType>
Parameters:
other - the media type to compare to
Returns:
a negative integer, zero, or a positive integer as this media type is less than, equal to, or greater than the specified media type

equals

public boolean equals(Object other)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

toString

public String toString()
Overrides:
toString in class Object

parseMediaType

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

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

parseMediaTypes

public static List<MediaType> parseMediaTypes(String mediaTypes)
Parse the given, comma-seperated 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:
IllegalArgumentException - if the string cannot be parsed

toString

public static String toString(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:
IllegalArgumentException - if the String cannot be parsed