public class MimeType extends java.lang.Object implements java.lang.Comparable<MimeType>, java.io.Serializable
This class, however, does not contain support for the q-parameters used
in HTTP content negotiation. Those can be found in the subclass
org.springframework.http.MediaType
in the spring-web
module.
Consists of a type and a subtype.
Also has functionality to parse MIME Type values from a String
using
valueOf(String)
. For more parsing options see MimeTypeUtils
.
MimeTypeUtils
,
Serialized FormModifier and Type | Class and Description |
---|---|
static class |
MimeType.SpecificityComparator<T extends MimeType>
Comparator to sort
MimeTypes in order of specificity. |
Modifier and Type | Field and Description |
---|---|
private static java.lang.String |
PARAM_CHARSET |
private java.util.Map<java.lang.String,java.lang.String> |
parameters |
private static long |
serialVersionUID |
private java.lang.String |
subtype |
private static java.util.BitSet |
TOKEN |
private java.lang.String |
toStringValue |
private java.lang.String |
type |
protected static java.lang.String |
WILDCARD_TYPE |
Constructor and Description |
---|
MimeType(MimeType other,
java.nio.charset.Charset charset)
Copy-constructor that copies the type, subtype, parameters of the given
MimeType ,
and allows to set the specified character set. |
MimeType(MimeType other,
java.util.Map<java.lang.String,java.lang.String> parameters)
Copy-constructor that copies the type and subtype of the given
MimeType ,
and allows for different parameter. |
MimeType(java.lang.String type)
Create a new
MimeType for the given primary type. |
MimeType(java.lang.String type,
java.lang.String subtype)
Create a new
MimeType for the given primary type and subtype. |
MimeType(java.lang.String type,
java.lang.String subtype,
java.nio.charset.Charset charset)
Create a new
MimeType for the given type, subtype, and character set. |
MimeType(java.lang.String type,
java.lang.String subtype,
java.util.Map<java.lang.String,java.lang.String> parameters)
Create a new
MimeType for the given type, subtype, and parameters. |
Modifier and Type | Method and Description |
---|---|
private static java.util.Map<java.lang.String,java.lang.String> |
addCharsetParameter(java.nio.charset.Charset charset,
java.util.Map<java.lang.String,java.lang.String> parameters) |
private void |
appendTo(java.util.Map<java.lang.String,java.lang.String> map,
java.lang.StringBuilder builder) |
protected void |
appendTo(java.lang.StringBuilder builder) |
protected void |
checkParameters(java.lang.String attribute,
java.lang.String value) |
private void |
checkToken(java.lang.String token)
Checks the given token string for illegal characters, as defined in RFC 2616,
section 2.2.
|
int |
compareTo(MimeType other)
Compares this MIME Type to another alphabetically.
|
boolean |
equals(java.lang.Object other) |
boolean |
equalsTypeAndSubtype(MimeType other)
Similar to
equals(Object) but based on the type and subtype
only, i.e. |
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.
|
java.util.Map<java.lang.String,java.lang.String> |
getParameters()
Return all generic parameter values.
|
java.lang.String |
getSubtype()
Return the subtype.
|
java.lang.String |
getType()
Return the primary type.
|
int |
hashCode() |
boolean |
includes(MimeType other)
Indicate whether this MIME Type includes the given MIME Type.
|
boolean |
isCompatibleWith(MimeType other)
Indicate whether this MIME Type is compatible with the given MIME Type.
|
boolean |
isConcrete()
Indicates whether this MIME Type is concrete, i.e.
|
boolean |
isPresentIn(java.util.Collection<? extends MimeType> mimeTypes)
Unlike
Collection.contains(Object) which relies on
equals(Object) , this method only checks the type and the
subtype, but otherwise ignores parameters. |
private boolean |
isQuotedString(java.lang.String s) |
boolean |
isWildcardSubtype()
Indicates whether the subtype is the wildcard
character
* or the wildcard character followed by a suffix
(e.g. |
boolean |
isWildcardType()
Indicates whether the type is the wildcard character
* or not. |
private boolean |
parametersAreEqual(MimeType other)
Determine if the parameters in this
MimeType and the supplied
MimeType are equal, performing case-insensitive comparisons
for Charsets . |
java.lang.String |
toString() |
protected java.lang.String |
unquote(java.lang.String s) |
static MimeType |
valueOf(java.lang.String value)
Parse the given String value into a
MimeType object,
with this method name following the 'valueOf' naming convention
(as supported by ConversionService . |
private static final long serialVersionUID
protected static final java.lang.String WILDCARD_TYPE
private static final java.lang.String PARAM_CHARSET
private static final java.util.BitSet TOKEN
private final java.lang.String type
private final java.lang.String subtype
private final java.util.Map<java.lang.String,java.lang.String> parameters
@Nullable private volatile java.lang.String toStringValue
public MimeType(java.lang.String type)
MimeType
for the given primary type.
The subtype is set to "*"
,
and the parameters are empty.
type
- the primary typejava.lang.IllegalArgumentException
- if any of the parameters contains illegal characterspublic MimeType(java.lang.String type, java.lang.String subtype)
MimeType
for the given primary type and subtype.
The parameters are empty.
type
- the primary typesubtype
- the subtypejava.lang.IllegalArgumentException
- if any of the parameters contains illegal characterspublic MimeType(java.lang.String type, java.lang.String subtype, java.nio.charset.Charset charset)
MimeType
for the given type, subtype, and character set.type
- the primary typesubtype
- the subtypecharset
- the character setjava.lang.IllegalArgumentException
- if any of the parameters contains illegal characterspublic MimeType(MimeType other, java.nio.charset.Charset charset)
MimeType
,
and allows to set the specified character set.other
- the other MimeTypecharset
- the character setjava.lang.IllegalArgumentException
- if any of the parameters contains illegal characterspublic MimeType(MimeType other, @Nullable java.util.Map<java.lang.String,java.lang.String> parameters)
MimeType
,
and allows for different parameter.other
- the other MimeTypeparameters
- the parameters (may be null
)java.lang.IllegalArgumentException
- if any of the parameters contains illegal characterspublic MimeType(java.lang.String type, java.lang.String subtype, @Nullable java.util.Map<java.lang.String,java.lang.String> parameters)
MimeType
for the given type, subtype, and parameters.type
- the primary typesubtype
- the subtypeparameters
- the parameters (may be null
)java.lang.IllegalArgumentException
- if any of the parameters contains illegal charactersprivate void checkToken(java.lang.String token)
java.lang.IllegalArgumentException
- in case of illegal charactersprotected void checkParameters(java.lang.String attribute, java.lang.String value)
private boolean isQuotedString(java.lang.String s)
protected java.lang.String unquote(java.lang.String s)
public boolean isWildcardType()
*
or not.public boolean isWildcardSubtype()
*
or the wildcard character followed by a suffix
(e.g. *+xml
).public boolean isConcrete()
*
.public java.lang.String getType()
public java.lang.String getSubtype()
@Nullable public java.nio.charset.Charset getCharset()
charset
parameter, if any.null
if not available@Nullable public java.lang.String getParameter(java.lang.String name)
name
- the parameter namenull
if not presentpublic java.util.Map<java.lang.String,java.lang.String> getParameters()
null
)public boolean includes(@Nullable MimeType other)
For instance, text/*
includes text/plain
and text/html
,
and application/*+xml
includes application/soap+xml
, etc.
This method is not symmetric.
other
- the reference MIME Type with which to comparetrue
if this MIME Type includes the given MIME Type;
false
otherwisepublic boolean isCompatibleWith(@Nullable MimeType other)
For instance, text/*
is compatible with text/plain
,
text/html
, and vice versa. In effect, this method is similar to
includes(org.springframework.util.MimeType)
, except that it is symmetric.
other
- the reference MIME Type with which to comparetrue
if this MIME Type is compatible with the given MIME Type;
false
otherwisepublic boolean equalsTypeAndSubtype(@Nullable MimeType other)
equals(Object)
but based on the type and subtype
only, i.e. ignoring parameters.other
- the other mime type to compare topublic boolean isPresentIn(java.util.Collection<? extends MimeType> mimeTypes)
Collection.contains(Object)
which relies on
equals(Object)
, this method only checks the type and the
subtype, but otherwise ignores parameters.mimeTypes
- the list of mime types to perform the check againstpublic boolean equals(java.lang.Object other)
equals
in class java.lang.Object
private boolean parametersAreEqual(MimeType other)
MimeType
and the supplied
MimeType
are equal, performing case-insensitive comparisons
for Charsets
.public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
protected void appendTo(java.lang.StringBuilder builder)
private void appendTo(java.util.Map<java.lang.String,java.lang.String> map, java.lang.StringBuilder builder)
public int compareTo(MimeType other)
compareTo
in interface java.lang.Comparable<MimeType>
other
- the MIME Type to compare toMimeTypeUtils.sortBySpecificity(List)
public static MimeType valueOf(java.lang.String value)
MimeType
object,
with this method name following the 'valueOf' naming convention
(as supported by ConversionService
.MimeTypeUtils.parseMimeType(String)
private static java.util.Map<java.lang.String,java.lang.String> addCharsetParameter(java.nio.charset.Charset charset, java.util.Map<java.lang.String,java.lang.String> parameters)