- java.lang.Object
-
- jakarta.ws.rs.core.MediaType
-
public class MediaType extends java.lang.ObjectAn abstraction for a media type. Instances are immutable.- Since:
- 1.0
- See Also:
- HTTP/1.1 section 3.7
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringAPPLICATION_ATOM_XMLAStringconstant representing "application/atom+xml" media type.static MediaTypeAPPLICATION_ATOM_XML_TYPEAMediaTypeconstant representing "application/atom+xml" media type.static java.lang.StringAPPLICATION_FORM_URLENCODEDAStringconstant representing "application/x-www-form-urlencoded" media type.static MediaTypeAPPLICATION_FORM_URLENCODED_TYPEAMediaTypeconstant representing "application/x-www-form-urlencoded" media type.static java.lang.StringAPPLICATION_JSONAStringconstant representing "application/json" media type.static java.lang.StringAPPLICATION_JSON_PATCH_JSONStringrepresentation of "application/json-patch+json" media type..static MediaTypeAPPLICATION_JSON_PATCH_JSON_TYPEAMediaTypeconstant representing "application/json-patch+json" media type.static MediaTypeAPPLICATION_JSON_TYPEAMediaTypeconstant representing "application/json" media type.static java.lang.StringAPPLICATION_MERGE_PATCH_JSONStringrepresentation of "application/merge-patch+json" media type..static MediaTypeAPPLICATION_MERGE_PATCH_JSON_TYPEAMediaTypeconstant representing "application/merge-patch+json" media type.static java.lang.StringAPPLICATION_OCTET_STREAMAStringconstant representing "application/octet-stream" media type.static MediaTypeAPPLICATION_OCTET_STREAM_TYPEAMediaTypeconstant representing "application/octet-stream" media type.static java.lang.StringAPPLICATION_SVG_XMLDeprecated, for removal: This API element is subject to removal in a future version.since 4.0, use a custom string instead.static MediaTypeAPPLICATION_SVG_XML_TYPEDeprecated, for removal: This API element is subject to removal in a future version.since 4.0, use a customMediaTypeinstead.static java.lang.StringAPPLICATION_XHTML_XMLAStringconstant representing "application/xhtml+xml" media type.static MediaTypeAPPLICATION_XHTML_XML_TYPEAMediaTypeconstant representing "application/xhtml+xml" media type.static java.lang.StringAPPLICATION_XMLAStringconstant representing "application/xml" media type.static MediaTypeAPPLICATION_XML_TYPEAMediaTypeconstant representing "application/xml" media type.static java.lang.StringCHARSET_PARAMETERThe media typecharsetparameter name.private inthashstatic java.lang.StringMEDIA_TYPE_WILDCARDThe value of a type or subtype wildcard "*".static java.lang.StringMULTIPART_FORM_DATAAStringconstant representing "multipart/form-data" media type.static MediaTypeMULTIPART_FORM_DATA_TYPEAMediaTypeconstant representing "multipart/form-data" media type.private java.util.Map<java.lang.String,java.lang.String>parametersstatic java.lang.StringSERVER_SENT_EVENTSStringrepresentation of Server sent events media type.static MediaTypeSERVER_SENT_EVENTS_TYPEServer sent events media type.private java.lang.Stringsubtypestatic java.lang.StringTEXT_HTMLAStringconstant representing "text/html" media type.static MediaTypeTEXT_HTML_TYPEAMediaTypeconstant representing "text/html" media type.static java.lang.StringTEXT_PLAINAStringconstant representing "text/plain" media type.static MediaTypeTEXT_PLAIN_TYPEAMediaTypeconstant representing "text/plain" media type.static java.lang.StringTEXT_XMLAStringconstant representing "text/xml" media type.static MediaTypeTEXT_XML_TYPEAMediaTypeconstant representing "text/xml" media type.private java.lang.Stringtypestatic java.lang.StringWILDCARDAStringconstant representing wildcard "*/*" media type .static MediaTypeWILDCARD_TYPE
-
Constructor Summary
Constructors Modifier Constructor Description MediaType()Creates a new instance ofMediaType, both type and subtype are wildcards.MediaType(java.lang.String type, java.lang.String subtype)Creates a new instance ofMediaTypewith the supplied type and subtype.MediaType(java.lang.String type, java.lang.String subtype, java.lang.String charset)Creates a new instance ofMediaTypewith the supplied type, subtype and "charset" parameter.privateMediaType(java.lang.String type, java.lang.String subtype, java.lang.String charset, java.util.Map<java.lang.String,java.lang.String> parameterMap)MediaType(java.lang.String type, java.lang.String subtype, java.util.Map<java.lang.String,java.lang.String> parameters)Creates a new instance ofMediaTypewith the supplied type, subtype and parameters.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static java.util.TreeMap<java.lang.String,java.lang.String>createParametersMap(java.util.Map<java.lang.String,java.lang.String> initialValues)booleanequals(java.lang.Object obj)Comparesobjto this media type to see if they are the same by comparing type, subtype and parameters.java.util.Map<java.lang.String,java.lang.String>getParameters()Getter for a read-only parameter map.java.lang.StringgetSubtype()Getter for subtype.java.lang.StringgetType()Getter for primary type.inthashCode()Generate a hash code from the type, subtype and parameters.booleanisCompatible(MediaType other)Check if this media type is compatible with another media type.booleanisWildcardSubtype()Checks if the subtype is a wildcard.booleanisWildcardType()Checks if the primary type is a wildcard.java.lang.StringtoString()Convert the media type to a string suitable for use as the value of a corresponding HTTP header.static MediaTypevalueOf(java.lang.String type)Creates a new instance ofMediaTypeby parsing the supplied string.MediaTypewithCharset(java.lang.String charset)Create a newMediaTypeinstance with the same type, subtype and parameters copied from the original instance and the supplied "charset" parameter.
-
-
-
Field Detail
-
type
private final java.lang.String type
-
subtype
private final java.lang.String subtype
-
parameters
private final java.util.Map<java.lang.String,java.lang.String> parameters
-
hash
private final int hash
-
CHARSET_PARAMETER
public static final java.lang.String CHARSET_PARAMETER
The media typecharsetparameter name.- See Also:
- Constant Field Values
-
MEDIA_TYPE_WILDCARD
public static final java.lang.String MEDIA_TYPE_WILDCARD
The value of a type or subtype wildcard "*".- See Also:
- Constant Field Values
-
WILDCARD
public static final java.lang.String WILDCARD
AStringconstant representing wildcard "*/*" media type .- See Also:
- Constant Field Values
-
WILDCARD_TYPE
public static final MediaType WILDCARD_TYPE
-
APPLICATION_XML
public static final java.lang.String APPLICATION_XML
AStringconstant representing "application/xml" media type.- See Also:
- Constant Field Values
-
APPLICATION_XML_TYPE
public static final MediaType APPLICATION_XML_TYPE
AMediaTypeconstant representing "application/xml" media type.
-
APPLICATION_ATOM_XML
public static final java.lang.String APPLICATION_ATOM_XML
AStringconstant representing "application/atom+xml" media type.- See Also:
- Constant Field Values
-
APPLICATION_ATOM_XML_TYPE
public static final MediaType APPLICATION_ATOM_XML_TYPE
AMediaTypeconstant representing "application/atom+xml" media type.
-
APPLICATION_XHTML_XML
public static final java.lang.String APPLICATION_XHTML_XML
AStringconstant representing "application/xhtml+xml" media type.- See Also:
- Constant Field Values
-
APPLICATION_XHTML_XML_TYPE
public static final MediaType APPLICATION_XHTML_XML_TYPE
AMediaTypeconstant representing "application/xhtml+xml" media type.
-
APPLICATION_SVG_XML
@Deprecated(forRemoval=true) public static final java.lang.String APPLICATION_SVG_XML
Deprecated, for removal: This API element is subject to removal in a future version.since 4.0, use a custom string instead. Will be removed in a future release of this API.AStringconstant representing "application/svg+xml" media type.- See Also:
- Constant Field Values
-
APPLICATION_SVG_XML_TYPE
@Deprecated(forRemoval=true) public static final MediaType APPLICATION_SVG_XML_TYPE
Deprecated, for removal: This API element is subject to removal in a future version.since 4.0, use a customMediaTypeinstead. Will be removed in a future release of this API.AMediaTypeconstant representing "application/svg+xml" media type.
-
APPLICATION_JSON
public static final java.lang.String APPLICATION_JSON
AStringconstant representing "application/json" media type.- See Also:
- Constant Field Values
-
APPLICATION_JSON_TYPE
public static final MediaType APPLICATION_JSON_TYPE
AMediaTypeconstant representing "application/json" media type.
-
APPLICATION_FORM_URLENCODED
public static final java.lang.String APPLICATION_FORM_URLENCODED
AStringconstant representing "application/x-www-form-urlencoded" media type.- See Also:
- Constant Field Values
-
APPLICATION_FORM_URLENCODED_TYPE
public static final MediaType APPLICATION_FORM_URLENCODED_TYPE
AMediaTypeconstant representing "application/x-www-form-urlencoded" media type.
-
MULTIPART_FORM_DATA
public static final java.lang.String MULTIPART_FORM_DATA
AStringconstant representing "multipart/form-data" media type.- See Also:
- Constant Field Values
-
MULTIPART_FORM_DATA_TYPE
public static final MediaType MULTIPART_FORM_DATA_TYPE
AMediaTypeconstant representing "multipart/form-data" media type.
-
APPLICATION_OCTET_STREAM
public static final java.lang.String APPLICATION_OCTET_STREAM
AStringconstant representing "application/octet-stream" media type.- See Also:
- Constant Field Values
-
APPLICATION_OCTET_STREAM_TYPE
public static final MediaType APPLICATION_OCTET_STREAM_TYPE
AMediaTypeconstant representing "application/octet-stream" media type.
-
TEXT_PLAIN
public static final java.lang.String TEXT_PLAIN
AStringconstant representing "text/plain" media type.- See Also:
- Constant Field Values
-
TEXT_PLAIN_TYPE
public static final MediaType TEXT_PLAIN_TYPE
AMediaTypeconstant representing "text/plain" media type.
-
TEXT_XML
public static final java.lang.String TEXT_XML
AStringconstant representing "text/xml" media type.- See Also:
- Constant Field Values
-
TEXT_XML_TYPE
public static final MediaType TEXT_XML_TYPE
AMediaTypeconstant representing "text/xml" media type.
-
TEXT_HTML
public static final java.lang.String TEXT_HTML
AStringconstant representing "text/html" media type.- See Also:
- Constant Field Values
-
TEXT_HTML_TYPE
public static final MediaType TEXT_HTML_TYPE
AMediaTypeconstant representing "text/html" media type.
-
SERVER_SENT_EVENTS
public static final java.lang.String SERVER_SENT_EVENTS
Stringrepresentation of Server sent events media type. (""text/event-stream"").- See Also:
- Constant Field Values
-
SERVER_SENT_EVENTS_TYPE
public static final MediaType SERVER_SENT_EVENTS_TYPE
Server sent events media type.
-
APPLICATION_JSON_PATCH_JSON
public static final java.lang.String APPLICATION_JSON_PATCH_JSON
Stringrepresentation of "application/json-patch+json" media type..- See Also:
- Constant Field Values
-
APPLICATION_JSON_PATCH_JSON_TYPE
public static final MediaType APPLICATION_JSON_PATCH_JSON_TYPE
AMediaTypeconstant representing "application/json-patch+json" media type.
-
APPLICATION_MERGE_PATCH_JSON
public static final java.lang.String APPLICATION_MERGE_PATCH_JSON
Stringrepresentation of "application/merge-patch+json" media type..- See Also:
- Constant Field Values
-
APPLICATION_MERGE_PATCH_JSON_TYPE
public static final MediaType APPLICATION_MERGE_PATCH_JSON_TYPE
AMediaTypeconstant representing "application/merge-patch+json" media type.
-
-
Constructor Detail
-
MediaType
public MediaType(java.lang.String type, java.lang.String subtype, java.util.Map<java.lang.String,java.lang.String> parameters)Creates a new instance ofMediaTypewith the supplied type, subtype and parameters.- Parameters:
type- the primary type,nullis equivalent toMEDIA_TYPE_WILDCARD.subtype- the subtype,nullis equivalent toMEDIA_TYPE_WILDCARD.parameters- a map of media type parameters,nullis the same as an empty map.
-
MediaType
public MediaType(java.lang.String type, java.lang.String subtype)Creates a new instance ofMediaTypewith the supplied type and subtype.- Parameters:
type- the primary type,nullis equivalent toMEDIA_TYPE_WILDCARDsubtype- the subtype,nullis equivalent toMEDIA_TYPE_WILDCARD
-
MediaType
public MediaType(java.lang.String type, java.lang.String subtype, java.lang.String charset)Creates a new instance ofMediaTypewith the supplied type, subtype and "charset" parameter.- Parameters:
type- the primary type,nullis equivalent toMEDIA_TYPE_WILDCARDsubtype- the subtype,nullis equivalent toMEDIA_TYPE_WILDCARDcharset- the "charset" parameter value. Ifnullor empty the "charset" parameter will not be set.
-
MediaType
public MediaType()
Creates a new instance ofMediaType, both type and subtype are wildcards. Consider using the constantWILDCARD_TYPEinstead.
-
MediaType
private MediaType(java.lang.String type, java.lang.String subtype, java.lang.String charset, java.util.Map<java.lang.String,java.lang.String> parameterMap)
-
-
Method Detail
-
valueOf
public static MediaType valueOf(java.lang.String type)
Creates a new instance ofMediaTypeby parsing the supplied string.- Parameters:
type- the media type string.- Returns:
- the newly created MediaType.
- Throws:
java.lang.IllegalArgumentException- if the supplied string cannot be parsed or isnull.
-
createParametersMap
private static java.util.TreeMap<java.lang.String,java.lang.String> createParametersMap(java.util.Map<java.lang.String,java.lang.String> initialValues)
-
getType
public java.lang.String getType()
Getter for primary type.- Returns:
- value of primary type.
-
isWildcardType
public boolean isWildcardType()
Checks if the primary type is a wildcard.- Returns:
- true if the primary type is a wildcard.
-
getSubtype
public java.lang.String getSubtype()
Getter for subtype.- Returns:
- value of subtype.
-
isWildcardSubtype
public boolean isWildcardSubtype()
Checks if the subtype is a wildcard.- Returns:
- true if the subtype is a wildcard.
-
getParameters
public java.util.Map<java.lang.String,java.lang.String> getParameters()
Getter for a read-only parameter map. Keys are case-insensitive.- Returns:
- an immutable map of parameters.
-
withCharset
public MediaType withCharset(java.lang.String charset)
Create a newMediaTypeinstance with the same type, subtype and parameters copied from the original instance and the supplied "charset" parameter.
-
isCompatible
public boolean isCompatible(MediaType other)
Check if this media type is compatible with another media type. Two media types are considered to be compatible if and only if their types are equal, or one of them has a wildcard type, and their subtypes are equal or one of them has a wildcard subtype. Media type parameters are ignored. The function is commutative.- Parameters:
other- the media type to compare with.- Returns:
- true if the types are compatible, false otherwise.
-
equals
public boolean equals(java.lang.Object obj)
Compares
Note that theobjto this media type to see if they are the same by comparing type, subtype and parameters. Note that the case-sensitivity of parameter values is dependent on the semantics of the parameter name, see HTTP/1.1. This method assumes that values are case-sensitive.equals(...)implementation does not perform a class equality check (this.getClass() == obj.getClass()). Therefore any class that extends fromMediaTypeclass and needs to override one of theequals(...)andhashCode()methods must always override both methods to ensure the contract betweenObject.equals(java.lang.Object)andObject.hashCode()does not break.- Overrides:
equalsin classjava.lang.Object- Parameters:
obj- the object to compare to.- Returns:
- true if the two media types are the same, false otherwise.
-
hashCode
public int hashCode()
Generate a hash code from the type, subtype and parameters.
Note that theequals(java.lang.Object)implementation does not perform a class equality check (this.getClass() == obj.getClass()). Therefore any class that extends fromMediaTypeclass and needs to override one of theequals(Object)andhashCode()methods must always override both methods to ensure the contract betweenObject.equals(java.lang.Object)andObject.hashCode()does not break.- Overrides:
hashCodein classjava.lang.Object- Returns:
- a generated hash code.
-
toString
public java.lang.String toString()
Convert the media type to a string suitable for use as the value of a corresponding HTTP header.- Overrides:
toStringin classjava.lang.Object- Returns:
- a string version of the media type.
-
-