Class MimeType
- java.lang.Object
-
- com.okta.commons.http.MimeType
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Comparable<MimeType>
- Direct Known Subclasses:
MediaType
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.This class, however, does not contain support for the q-parameters used in HTTP content negotiation. Those can be found in the sub-class
org.springframework.http.MediaTypein thespring-webmodule.Consists of a type and a subtype. Also has functionality to parse media types from a string using
valueOf(String). For more parsing options seeMimeTypeUtils.- Since:
- 4.0
- Author:
- Arjen Poutsma, Juergen Hoeller, Rossen Stoyanchev, Sam Brannen
- See Also:
MimeTypeUtils, Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMimeType.SpecificityComparator<T extends MimeType>
-
Field Summary
Fields Modifier and Type Field Description protected static java.lang.StringWILDCARD_TYPE
-
Constructor Summary
Constructors Constructor Description MimeType(MimeType other, java.nio.charset.Charset charset)Copy-constructor that copies the type, subtype, parameters of the givenMimeType, 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 givenMimeType, and allows for different parameter.MimeType(java.lang.String type)Create a newMimeTypefor the given primary type.MimeType(java.lang.String type, java.lang.String subtype)Create a newMimeTypefor the given primary type and subtype.MimeType(java.lang.String type, java.lang.String subtype, java.nio.charset.Charset charset)Create a newMimeTypefor 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 newMimeTypefor the given type, subtype, and parameters.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidappendTo(java.lang.StringBuilder builder)protected voidcheckParameters(java.lang.String attribute, java.lang.String value)intcompareTo(MimeType other)Compares thisMediaTypeto another alphabetically.booleanequals(java.lang.Object other)java.nio.charset.CharsetgetCharset()Return the character set, as indicated by acharsetparameter, if any.java.lang.StringgetParameter(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.StringgetSubtype()Return the subtype.java.lang.StringgetType()Return the primary type.inthashCode()booleanincludes(MimeType other)Indicate whether thisMediaTypeincludes the given media type.booleanisCompatibleWith(MimeType other)Indicate whether thisMediaTypeis compatible with the given media type.booleanisConcrete()Indicates whether this media type is concrete, i.e.booleanisWildcardSubtype()Indicates whether the subtype is the wildcard character*or the wildcard character followed by a suffix (e.g.booleanisWildcardType()Indicates whether the type is the wildcard character*or not.java.lang.StringtoString()protected java.lang.Stringunquote(java.lang.String s)static MimeTypevalueOf(java.lang.String value)Parse the given String value into aMimeTypeobject, with this method name following the 'valueOf' naming convention (as supported byorg.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 newMimeTypefor 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 newMimeTypefor 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 newMimeTypefor 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.nio.charset.Charset charset)
Copy-constructor that copies the type, subtype, parameters of the givenMimeType, and allows to set the specified character set.- Parameters:
other- the other media typecharset- the character set- Throws:
java.lang.IllegalArgumentException- if any of the parameters contains illegal characters- Since:
- 4.3
-
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 newMimeTypefor 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.- Returns:
- return true if this type is a wildcard
-
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.- Returns:
- return the type
-
getSubtype
public java.lang.String getSubtype()
Return the subtype.- Returns:
- return the subtype
-
getCharset
public java.nio.charset.Charset getCharset()
Return the character set, as indicated by acharsetparameter, if any.- Returns:
- the character set, or
nullif not available - Since:
- 4.3
-
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
nullif 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 thisMediaTypeincludes the given media type.For instance,
text/*includestext/plainandtext/html, andapplication/*+xmlincludesapplication/soap+xml, etc. This method is not symmetric.- Parameters:
other- the reference media type with which to compare- Returns:
trueif this media type includes the given media type;falseotherwise
-
isCompatibleWith
public boolean isCompatibleWith(MimeType other)
Indicate whether thisMediaTypeis 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.okta.commons.http.MimeType), except that it is symmetric.- Parameters:
other- the reference media type with which to compare- Returns:
trueif this media type is compatible with the given media type;falseotherwise
-
equals
public boolean equals(java.lang.Object other)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
appendTo
protected void appendTo(java.lang.StringBuilder builder)
-
compareTo
public int compareTo(MimeType other)
Compares thisMediaTypeto another alphabetically.- Specified by:
compareToin interfacejava.lang.Comparable<MimeType>- Parameters:
other- media type to compare to- See Also:
MimeTypeUtils.sortBySpecificity(List)
-
valueOf
public static MimeType valueOf(java.lang.String value)
Parse the given String value into aMimeTypeobject, with this method name following the 'valueOf' naming convention (as supported byorg.springframework.core.convert.ConversionService.- Parameters:
value- string to parse- Returns:
- MimeType base on
value - See Also:
MimeTypeUtils.parseMimeType(String)
-
-