java.lang.Object
com.github.mizosoft.methanol.MediaType
A MIME
type. The text representation of this class can be used as the value
of the
Content-Type
HTTP header.
A MediaType
also defines a media range. A media range has
either both wildcard type and subtype, both concrete type and subtype, or a concrete type and a
wildcard subtype (but not a wildcard type and a concrete subtype), with the character *
denoting a wildcard. Inclusion in media ranges can be tested using any of includes(MediaType)
or isCompatibleWith(MediaType)
, with the latter being symmetric
among operands.
Case insensitive attributes such as the type, subtype, parameter names or the value of the charset parameter are converted into lower-case.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final MediaType
*
/*
static final MediaType
application
/*
static final MediaType
application/x-www-form-urlencoded
static final MediaType
application/json
static final MediaType
application/octet-stream
static final MediaType
application/x-protobuf
static final MediaType
application/xhtml+xml
static final MediaType
application/xml
static final MediaType
image
/*
static final MediaType
image/gif
static final MediaType
image/jpeg
static final MediaType
image/png
static final MediaType
text
/*
static final MediaType
text/html
static final MediaType
text/markdown
static final MediaType
text/plain
static final MediaType
text/xml
-
Method Summary
Modifier and TypeMethodDescriptioncharset()
Returns anOptional
representing the value of the charset parameter.charsetOrDefault
(Charset defaultCharset) Returns either the value of the charset parameter or the given default charset if no such parameter exists or if the charset has no support in this JVM.boolean
Tests the given object for equality with this instance.int
hashCode()
Returns a hashcode for this media type.boolean
Returntrue
if this media type is*
/*
or if it has a wildcard subtype.boolean
Returns whether this media type includes the given one.boolean
isCompatibleWith
(MediaType other) Returns whether this media type is compatible with the given one.static MediaType
Returns a newMediaType
with the given type and subtype.static MediaType
Returns a newMediaType
with the given type, subtype and parameters.Returns an immutable map representing the parameters.static MediaType
Parses the given string into aMediaType
instance.subtype()
Returns the subtype.toString()
Returns a text representation of this media type that is compatible with the value of theContent-Type
header.type()
Returns the general type.withCharset
(Charset charset) Returns a newMediaType
with this instance's type, subtype and parameters but with the name of the given charset as the value of the charset parameter.withParameter
(String name, String value) Returns a newMediaType
with this instance's type, subtype and parameters but with the value of the parameter specified by the given name set to the given value.withParameters
(Map<String, String> parameters) Returns a newMediaType
with this instance's type, subtype and parameters but with each of the given parameters' names set to their corresponding values.
-
Field Details
-
ANY
*
/*
-
APPLICATION_ANY
application
/*
-
IMAGE_ANY
image
/*
-
TEXT_ANY
text
/*
-
APPLICATION_FORM_URLENCODED
application/x-www-form-urlencoded
-
APPLICATION_JSON
application/json
-
APPLICATION_OCTET_STREAM
application/octet-stream
-
APPLICATION_XHTML_XML
application/xhtml+xml
-
APPLICATION_XML
application/xml
-
APPLICATION_X_PROTOBUF
application/x-protobuf
-
IMAGE_GIF
image/gif
-
IMAGE_JPEG
image/jpeg
-
IMAGE_PNG
image/png
-
TEXT_HTML
text/html
-
TEXT_MARKDOWN
text/markdown
-
TEXT_PLAIN
text/plain
-
TEXT_XML
text/xml
-
-
Method Details
-
type
Returns the general type. -
subtype
Returns the subtype. -
parameters
Returns an immutable map representing the parameters. -
charset
Returns anOptional
representing the value of the charset parameter. An emptyOptional
is returned if no such parameter exists.- Throws:
IllegalCharsetNameException
- if a charset parameter exists the value of which is invalidUnsupportedCharsetException
- if a charset parameter exists the value of which is not supported in this JVM
-
charsetOrDefault
Returns either the value of the charset parameter or the given default charset if no such parameter exists or if the charset has no support in this JVM.- Parameters:
defaultCharset
- the charset to fall back to- Throws:
IllegalCharsetNameException
- if a charset parameter exists the value of which is invalid
-
hasWildcard
public boolean hasWildcard()Returntrue
if this media type is*
/*
or if it has a wildcard subtype. -
includes
Returns whether this media type includes the given one. A media type includes the other if the former's parameters is a subset of the latter's and either the former is awildcard type
that includes the latter or both have equal concrete type and subtype.- Parameters:
other
- the other media type
-
isCompatibleWith
Returns whether this media type is compatible with the given one. Two media types are compatible if either of themincludes
the other.- Parameters:
other
- the other media type
-
withCharset
Returns a newMediaType
with this instance's type, subtype and parameters but with the name of the given charset as the value of the charset parameter.- Parameters:
charset
- the new type's charset
-
withParameter
Returns a newMediaType
with this instance's type, subtype and parameters but with the value of the parameter specified by the given name set to the given value.- Parameters:
name
- the parameter's namevalue
- the parameter's value- Throws:
IllegalArgumentException
- if the given name or value is invalid
-
withParameters
Returns a newMediaType
with this instance's type, subtype and parameters but with each of the given parameters' names set to their corresponding values.- Parameters:
parameters
- the parameters to add or replace- Throws:
IllegalArgumentException
- if any of the given parameters is invalid
-
equals
Tests the given object for equality with this instance.true
is returned if the given object is aMediaType
and both instances's type, subtype and parameters are equal. -
hashCode
public int hashCode()Returns a hashcode for this media type. -
toString
Returns a text representation of this media type that is compatible with the value of theContent-Type
header. -
of
Returns a newMediaType
with the given type and subtype.- Parameters:
type
- the general typesubtype
- the subtype- Throws:
IllegalArgumentException
- if the given type or subtype is invalid
-
of
Returns a newMediaType
with the given type, subtype and parameters.- Parameters:
type
- the general typesubtype
- the subtypeparameters
- the parameters- Throws:
IllegalArgumentException
- if the given type, subtype or any of the given parameters is invalid
-
parse
Parses the given string into aMediaType
instance.- Parameters:
value
- the media type string- Throws:
IllegalArgumentException
- if the given string is an invalid media type
-