@Beta @GwtCompatible public final class MediaType extends Object
*
character is treated as a wildcard and is used to represent any acceptable type or subtype value. A media type may not have wildcard type with a declared subtype. The
*
character has no special meaning as part of a parameter. All values for type, subtype, parameter attributes or parameter values must be valid according to RFCs
2045 and
2046.
All portions of the media type that are case-insensitive (type, subtype, parameter attributes) are normalized to lowercase. The value of the charset
parameter is normalized to lowercase, but all others are left as-is.
Note that this specifically does not represent the value of the MIME Content-Type
header and as such has no support for header-specific considerations such as line folding and comments.
For media types that take a charset the predefined constants default to UTF-8 and have a "_UTF_8" suffix. To get a version without a character set, use withoutParameters()
.
Modifier and Type | Method and Description |
---|---|
Optional |
charset()
Returns an optional charset for the value of the charset parameter if it is specified.
|
static MediaType |
create(String
Creates a new media type with the given type and subtype.
|
boolean |
equals(Object
|
int |
hashCode()
|
boolean |
hasWildcard()
Returns true if either the type or subtype is the wildcard.
|
boolean |
is(MediaType
Returns
true if this instance falls within the range (as defined by
the HTTP Accept header) given by the argument according to three criteria: The type of the argument is the wildcard or equal to the type of this instance.
|
ImmutableListMultimap |
parameters()
Returns a multimap containing the parameters of this media type.
|
static MediaType |
parse(String
Parses a media type from its string representation.
|
String |
subtype()
Returns the media subtype.
|
String |
toString()
Returns the string representation of this media type in the format described in
RFC 2045.
|
String |
type()
Returns the top-level media type.
|
MediaType |
withCharset(Charset
Returns a new instance with the same type and subtype as this instance, with the
charset parameter set to the
name of the given charset.
|
MediaType |
withoutParameters()
Returns a new instance with the same type and subtype as this instance, but without any parameters.
|
MediaType |
withParameter(String
Replaces all parameters with the given attribute with a single parameter with the given value.
|
MediaType |
withParameters(Multimap
Replaces all parameters with the given parameters.
|
public static final MediaTypeANY_TYPE
public static final MediaTypeANY_TEXT_TYPE
public static final MediaTypeANY_IMAGE_TYPE
public static final MediaTypeANY_AUDIO_TYPE
public static final MediaTypeANY_VIDEO_TYPE
public static final MediaTypeANY_APPLICATION_TYPE
public static final MediaTypeCACHE_MANIFEST_UTF_8
public static final MediaTypeCSS_UTF_8
public static final MediaTypeCSV_UTF_8
public static final MediaTypeHTML_UTF_8
public static final MediaTypeI_CALENDAR_UTF_8
public static final MediaTypePLAIN_TEXT_UTF_8
public static final MediaTypeTEXT_JAVASCRIPT_UTF_8
application/javascript
to be the correct media type for JavaScript, but this may be necessary in certain situations for compatibility.
public static final MediaTypeTSV_UTF_8
public static final MediaTypeVCARD_UTF_8
public static final MediaTypeWML_UTF_8
public static final MediaTypeXML_UTF_8
text/xml
) is used for XML documents that are "readable by casual users."
APPLICATION_XML_UTF_8
is provided for documents that are intended for applications.
public static final MediaTypeBMP
public static final MediaTypeCRW
crw
files), a widely-used "raw image" format for cameras. It is found in
/etc/mime.types
, e.g. in
public static final MediaTypeGIF
public static final MediaTypeICO
public static final MediaTypeJPEG
public static final MediaTypePNG
public static final MediaTypePSD
psd
files) as defined by
IANA, and found in
/etc/mime.types
, e.g.
of the Apache
HTTPD project; for the specification, see
This is a more recent replacement for the older, experimental type x-photoshop
: RFC-2046.6.
public static final MediaTypeSVG_UTF_8
public static final MediaTypeTIFF
public static final MediaTypeWEBP
public static final MediaTypeMP4_AUDIO
public static final MediaTypeMPEG_AUDIO
public static final MediaTypeOGG_AUDIO
public static final MediaTypeWEBM_AUDIO
public static final MediaTypeMP4_VIDEO
public static final MediaTypeMPEG_VIDEO
public static final MediaTypeOGG_VIDEO
public static final MediaTypeQUICKTIME
public static final MediaTypeWEBM_VIDEO
public static final MediaTypeWMV
public static final MediaTypeAPPLICATION_XML_UTF_8
public static final MediaTypeATOM_UTF_8
public static final MediaTypeBZIP2
public static final MediaTypeDART_UTF_8
public static final MediaTypeAPPLE_PASSBOOK
public static final MediaTypeEOT
public static final MediaTypeEPUB
public static final MediaTypeFORM_DATA
public static final MediaTypeKEY_ARCHIVE
public static final MediaTypeAPPLICATION_BINARY
OCTET_STREAM
for binary data that is not being served to a browser.
public static final MediaTypeGZIP
public static final MediaTypeJAVASCRIPT_UTF_8
text/javascript
may be necessary in certain situations for compatibility.
public static final MediaTypeJSON_UTF_8
public static final MediaTypeMANIFEST_JSON_UTF_8
public static final MediaTypeKML
public static final MediaTypeKMZ
public static final MediaTypeMBOX
public static final MediaTypeAPPLE_MOBILE_CONFIG
public static final MediaTypeMICROSOFT_EXCEL
public static final MediaTypeMICROSOFT_POWERPOINT
public static final MediaTypeMICROSOFT_WORD
public static final MediaTypeOCTET_STREAM
public static final MediaTypeOGG_CONTAINER
public static final MediaTypeOOXML_DOCUMENT
public static final MediaTypeOOXML_PRESENTATION
public static final MediaTypeOOXML_SHEET
public static final MediaTypeOPENDOCUMENT_GRAPHICS
public static final MediaTypeOPENDOCUMENT_PRESENTATION
public static final MediaTypeOPENDOCUMENT_SPREADSHEET
public static final MediaTypeOPENDOCUMENT_TEXT
public static final MediaType
public static final MediaTypePOSTSCRIPT
public static final MediaTypePROTOBUF
public static final MediaTypeRDF_XML_UTF_8
public static final MediaTypeRTF_UTF_8
public static final MediaTypeSFNT
public static final MediaTypeSHOCKWAVE_FLASH
public static final MediaTypeSKETCHUP
public static final MediaTypeTAR
public static final MediaTypeWOFF
public static final MediaTypeXHTML_UTF_8
public static final MediaTypeXRD_UTF_8
public static final MediaTypeZIP
public Stringtype()
"text"
in
"text/plain"
.
public Stringsubtype()
"plain"
in
"text/plain"
.
public ImmutableListMultimap<String ,String > parameters()
public Optional<Charset > charset()
IllegalStateException
- if multiple charset values have been set for this media type
IllegalCharsetNameException
- if a charset value is present, but illegal
UnsupportedCharsetException
- if a charset value is present, but no support is available in this instance of the Java virtual machine
public MediaTypewithoutParameters()
public MediaTypewithParameters(Multimap <String ,String > parameters)
IllegalArgumentException
- if any parameter or value is invalid
public MediaTypewithParameter(String attribute, String value)
withParameters(com.google.common.collect.Multimap<java.lang.String, java.lang.String>)
. Prefer
withCharset(java.nio.charset.Charset)
for setting the
charset
parameter when using a
Charset
object.
IllegalArgumentException
- if either
attribute
or
value
is invalid
public MediaTypewithCharset(Charset charset)
charset
parameter set to the
name
of the given charset. Only one
charset
parameter will be present on the new instance regardless of the number set on this one.
If a charset must be specified that is not supported on this JVM (and thus is not representable as a Charset
instance, use withParameter(java.lang.String, java.lang.String)
.
public boolean hasWildcard()
public boolean is(MediaTypemediaTypeRange)
true
if this instance falls within the range (as defined by
the HTTP Accept header) given by the argument according to three criteria:
For example:
PLAIN_TEXT_UTF_8.is(PLAIN_TEXT_UTF_8) // true PLAIN_TEXT_UTF_8.is(HTML_UTF_8) // false PLAIN_TEXT_UTF_8.is(ANY_TYPE) // true PLAIN_TEXT_UTF_8.is(ANY_TEXT_TYPE) // true PLAIN_TEXT_UTF_8.is(ANY_IMAGE_TYPE) // false PLAIN_TEXT_UTF_8.is(ANY_TEXT_TYPE.withCharset(UTF_8)) // true PLAIN_TEXT_UTF_8.withoutParameters().is(ANY_TEXT_TYPE.withCharset(UTF_8)) // false PLAIN_TEXT_UTF_8.is(ANY_TEXT_TYPE.withCharset(UTF_16)) // false
Note that while it is possible to have the same parameter declared multiple times within a media type this method does not consider the number of occurrences of a parameter. For example, "text/plain; charset=UTF-8"
satisfies "text/plain; charset=UTF-8; charset=UTF-8"
.
public static MediaTypecreate(String type, String subtype)
IllegalArgumentException
- if type or subtype is invalid or if a wildcard is used for the type, but not the subtype.
public static MediaTypeparse(String input)
IllegalArgumentException
- if the input is not parsable
public boolean equals(Objectobj)
public int hashCode()