Package com.jalios.util
Class MimeType
- java.lang.Object
-
- com.jalios.util.MimeType
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Comparable<MimeType>
public class MimeType extends java.lang.Object implements java.lang.Comparable<MimeType>, java.io.Serializable
Represents a MIME Type, as originally defined in RFC 2046 and subsequently used in other Internet protocols including HTTP.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
MimeType.SpecificityComparator<T extends MimeType>
-
Field Summary
Fields Modifier and Type Field Description protected static java.lang.String
WILDCARD_TYPE
-
Constructor Summary
Constructors Constructor Description MimeType(MimeType other, java.util.Map<java.lang.String,java.lang.String> parameters)
Copy-constructor that copies the type and subtype of the givenMimeType
, and allows for different parameter.MimeType(java.lang.String type)
Create a newMimeType
for the given primary type.MimeType(java.lang.String type, java.lang.String subtype)
Create a newMimeType
for the given primary type and subtype.MimeType(java.lang.String type, java.lang.String subtype, java.nio.charset.Charset charSet)
Create a newMimeType
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 newMimeType
for the given type, subtype, and parameters.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
appendTo(java.lang.StringBuilder builder)
protected void
checkParameters(java.lang.String attribute, java.lang.String value)
int
compareTo(MimeType other)
Compares thisMediaType
to another alphabetically.boolean
equals(java.lang.Object other)
java.nio.charset.Charset
getCharSet()
Return the character set, as indicated by acharset
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 thisMediaType
includes the given media type.boolean
isCompatibleWith(MimeType other)
Indicate whether thisMediaType
is compatible with the given media type.boolean
isConcrete()
Indicates whether this media type is concrete, i.e.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.static MimeType
parseMimeType(java.lang.String mimeType)
Parse the given String into a singleMimeType
.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 aMimeType
object, with this method name following the 'valueOf' naming convention (as supported by org.springframework.core.convert.ConversionService}.
-
-
-
Field Detail
-
WILDCARD_TYPE
protected static final java.lang.String WILDCARD_TYPE
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
MimeType
public MimeType(java.lang.String type)
Create a newMimeType
for the given primary type.The subtype is set to
"*"
, and the parameters are empty.- Parameters:
type
- the primary type- Throws:
java.lang.IllegalArgumentException
- if any of the parameters contains illegal characters
-
MimeType
public MimeType(java.lang.String type, java.lang.String subtype)
Create a newMimeType
for the given primary type and subtype.The parameters are empty.
- Parameters:
type
- the primary typesubtype
- the subtype- Throws:
java.lang.IllegalArgumentException
- if any of the parameters contains illegal characters
-
MimeType
public MimeType(java.lang.String type, java.lang.String subtype, java.nio.charset.Charset charSet)
Create a newMimeType
for the given type, subtype, and character set.- Parameters:
type
- the primary typesubtype
- the subtypecharSet
- the character set- Throws:
java.lang.IllegalArgumentException
- if any of the parameters contains illegal characters
-
MimeType
public MimeType(MimeType other, java.util.Map<java.lang.String,java.lang.String> parameters)
Copy-constructor that copies the type and subtype of the givenMimeType
, and allows for different parameter.- Parameters:
other
- the other media typeparameters
- the parameters, may benull
- Throws:
java.lang.IllegalArgumentException
- if any of the parameters contains illegal characters
-
MimeType
public MimeType(java.lang.String type, java.lang.String subtype, java.util.Map<java.lang.String,java.lang.String> parameters)
Create a newMimeType
for the given type, subtype, and parameters.- Parameters:
type
- the primary typesubtype
- the subtypeparameters
- the parameters, may benull
- Throws:
java.lang.IllegalArgumentException
- if any of the parameters contains illegal characters
-
-
Method Detail
-
checkParameters
protected void checkParameters(java.lang.String attribute, java.lang.String value)
-
unquote
protected java.lang.String unquote(java.lang.String s)
-
isWildcardType
public boolean isWildcardType()
Indicates whether the type is the wildcard character*
or not.
-
isWildcardSubtype
public boolean isWildcardSubtype()
Indicates whether the subtype is the wildcard character*
or the wildcard character followed by a suffix (e.g.*+xml
).- Returns:
- whether the subtype is a wildcard
-
isConcrete
public boolean isConcrete()
Indicates whether this media type is concrete, i.e. whether neither the type nor the subtype is a wildcard character*
.- Returns:
- whether this media type is concrete
-
getType
public java.lang.String getType()
Return the primary type.
-
getSubtype
public java.lang.String getSubtype()
Return the subtype.
-
getCharSet
public java.nio.charset.Charset getCharSet()
Return the character set, as indicated by acharset
parameter, if any.- Returns:
- the character set, or
null
if not available
-
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
-
getParameters
public java.util.Map<java.lang.String,java.lang.String> getParameters()
Return all generic parameter values.- Returns:
- a read-only map (possibly empty, never
null
)
-
includes
public boolean includes(MimeType other)
Indicate whether thisMediaType
includes the given media type.For instance,
text/*
includestext/plain
andtext/html
, andapplication/*+xml
includesapplication/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(MimeType other)
Indicate whether thisMediaType
is compatible with the given media type.For instance,
text/*
is compatible withtext/plain
,text/html
, and vice versa. In effect, this method is similar toincludes(com.jalios.util.MimeType)
, 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(MimeType other)
Compares thisMediaType
to another alphabetically.- Specified by:
compareTo
in interfacejava.lang.Comparable<MimeType>
- Parameters:
other
- media type to compare to
-
equals
public boolean equals(java.lang.Object other)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
appendTo
protected void appendTo(java.lang.StringBuilder builder)
-
valueOf
public static MimeType valueOf(java.lang.String value)
Parse the given String value into aMimeType
object, with this method name following the 'valueOf' naming convention (as supported by org.springframework.core.convert.ConversionService}.
-
parseMimeType
public static MimeType parseMimeType(java.lang.String mimeType)
Parse the given String into a singleMimeType
.- Parameters:
mimeType
- the string to parse- Returns:
- the mime type
-
-