Package io.micronaut.http
Class MediaType
- java.lang.Object
-
- io.micronaut.http.MediaType
-
- All Implemented Interfaces:
java.lang.CharSequence
public class MediaType extends java.lang.Object implements java.lang.CharSequence
Represents a media type. See https://www.iana.org/assignments/media-types/media-types.xhtml and https://tools.ietf.org/html/rfc2046- Since:
- 1.0
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
ALL
A wildcard media type representing all types.static MediaType
ALL_TYPE
A wildcard media type representing all types.static java.lang.String
APPLICATION_ATOM_XML
Atom: application/atom+xml.static MediaType
APPLICATION_ATOM_XML_TYPE
Atom: application/atom+xml.static java.lang.String
APPLICATION_FORM_URLENCODED
Form encoded data: application/x-www-form-urlencoded.static MediaType
APPLICATION_FORM_URLENCODED_TYPE
Form encoded data: application/x-www-form-urlencoded.static java.lang.String
APPLICATION_GRAPHQL
GraphQL: application/graphql.static MediaType
APPLICATION_GRAPHQL_TYPE
GraphQL: application/graphql.static java.lang.String
APPLICATION_HAL_JSON
HAL JSON: application/hal+json.static MediaType
APPLICATION_HAL_JSON_TYPE
HAL JSON: application/hal+json.static java.lang.String
APPLICATION_HAL_XML
HAL XML: application/hal+xml.static MediaType
APPLICATION_HAL_XML_TYPE
HAL XML: application/hal+xml.static java.lang.String
APPLICATION_JSON
JSON: application/json.static java.lang.String
APPLICATION_JSON_STREAM
JSON Stream: application/x-json-stream.static MediaType
APPLICATION_JSON_STREAM_TYPE
JSON Stream: application/x-json-stream.static MediaType
APPLICATION_JSON_TYPE
JSON: application/json.static java.lang.String
APPLICATION_OCTET_STREAM
BINARY: application/octet-stream.static MediaType
APPLICATION_OCTET_STREAM_TYPE
BINARY: application/octet-stream.static java.lang.String
APPLICATION_PDF
PDF: application/pdf.static MediaType
APPLICATION_PDF_TYPE
PDF: application/pdf.static java.lang.String
APPLICATION_VND_ERROR
VND Error: application/vnd.error+json.static MediaType
APPLICATION_VND_ERROR_TYPE
VND Error: application/vnd.error+json.static java.lang.String
APPLICATION_XHTML
XHTML: application/xhtml+xml.static MediaType
APPLICATION_XHTML_TYPE
XHTML: application/xhtml+xml.static java.lang.String
APPLICATION_XML
XML: application/xml.static MediaType
APPLICATION_XML_TYPE
XML: application/xml.static java.lang.String
APPLICATION_YAML
YAML: application/x-yaml.static MediaType
APPLICATION_YAML_TYPE
YAML: application/x-yaml.static java.lang.String
CHARSET_PARAMETER
Parameter"charset"
.static MediaType[]
EMPTY_ARRAY
Default empty media type array.protected java.lang.String
extension
static java.lang.String
EXTENSION_JSON
Default file extension used for JSON.static java.lang.String
EXTENSION_PDF
Default file extension used for PDF.static java.lang.String
EXTENSION_XLS
File extension for Microsoft Excel's workbook files in use between 97-2003.static java.lang.String
EXTENSION_XLSX
File extension used for Microsoft Excel Open XML Spreadsheet (XLSX).static java.lang.String
EXTENSION_XML
Default file extension used for XML.static MediaType
FORM
Short cut forAPPLICATION_FORM_URLENCODED_TYPE
.static java.lang.String
IMAGE_GIF
Gif Image: image/gif.static MediaType
IMAGE_GIF_TYPE
Gif Image: image/gif.static java.lang.String
IMAGE_JPEG
Jpeg Image: image/jpeg.static MediaType
IMAGE_JPEG_TYPE
Jpeg Image: image/jpeg.static java.lang.String
IMAGE_PNG
Png Image: image/png.static MediaType
IMAGE_PNG_TYPE
Png Image: image/png.static java.lang.String
IMAGE_WEBP
Webp Image: image/webp.static MediaType
IMAGE_WEBP_TYPE
Webp Image: image/webp.static java.lang.String
MICROSOFT_EXCEL
Microsoft Excel's workbook files in use between 97-2003.static java.lang.String
MICROSOFT_EXCEL_OPEN_XML
XML: Microsoft Excel Open XML Spreadsheet (XLSX).static MediaType
MICROSOFT_EXCEL_OPEN_XML_TYPE
XML: Microsoft Excel Open XML Spreadsheet (XLSX).static MediaType
MICROSOFT_EXCEL_TYPE
Microsoft Excel's workbook files in use between 97-2003.static java.lang.String
MULTIPART_FORM_DATA
Multi part form data: multipart/form-data.static MediaType
MULTIPART_FORM_DATA_TYPE
Multi part form data: multipart/form-data.protected java.lang.String
name
protected java.util.Map<java.lang.CharSequence,java.lang.String>
parameters
static java.lang.String
Q_PARAMETER
Parameter"q"
.protected java.lang.String
subtype
static java.lang.String
TEXT_CSV
CSV: text/csv.static MediaType
TEXT_CSV_TYPE
CSV: text/csv.static java.lang.String
TEXT_EVENT_STREAM
Server Sent Event: text/event-stream.static MediaType
TEXT_EVENT_STREAM_TYPE
Server Sent Event: text/event-stream.static java.lang.String
TEXT_HTML
HTML: text/html.static MediaType
TEXT_HTML_TYPE
HTML: text/html.static java.lang.String
TEXT_JSON
JSON: text/json.static MediaType
TEXT_JSON_TYPE
JSON: text/json.static java.lang.String
TEXT_PLAIN
Plain Text: text/plain.static MediaType
TEXT_PLAIN_TYPE
Plain Text: text/plain.static java.lang.String
TEXT_XML
XML: text/xml.static MediaType
TEXT_XML_TYPE
XML: text/xml.protected java.lang.String
type
static java.lang.String
V_PARAMETER
Parameter"v"
.
-
Constructor Summary
Constructors Constructor Description MediaType(java.lang.String name)
Constructs a new media type for the given string.MediaType(java.lang.String name, java.lang.String extension)
Constructs a new media type for the given string and extension.MediaType(java.lang.String name, java.lang.String extension, java.util.Map<java.lang.String,java.lang.String> params)
Constructs a new media type for the given string and extension.MediaType(java.lang.String name, java.util.Map<java.lang.String,java.lang.String> params)
Constructs a new media type for the given string and parameters.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description char
charAt(int index)
boolean
equals(java.lang.Object o)
static java.util.Optional<MediaType>
forExtension(java.lang.String extension)
Resolve theMediaType
for the given file extension.static MediaType
forFilename(java.lang.String filename)
Resolve theMediaType
for the given file name.static java.util.Optional<MediaType>
fromType(java.lang.Class<?> type)
java.util.Optional<java.nio.charset.Charset>
getCharset()
java.lang.String
getExtension()
java.lang.String
getName()
io.micronaut.core.value.OptionalValues<java.lang.String>
getParameters()
java.lang.String
getQuality()
java.math.BigDecimal
getQualityAsNumber()
java.lang.String
getSubtype()
java.lang.String
getType()
java.lang.String
getVersion()
int
hashCode()
boolean
isTextBased()
static boolean
isTextBased(java.lang.String contentType)
int
length()
boolean
matches(MediaType expectedContentType)
Determine if this requested content type can be satisfied by a given content type.static MediaType
of(java.lang.CharSequence mediaType)
Create a newMediaType
from the given text.static MediaType[]
of(java.lang.CharSequence... mediaType)
Create a newMediaType
from the given text.static MediaType
of(java.lang.String mediaType)
Create a new or get aMediaType
from the given text.static java.util.List<MediaType>
orderedOf(java.lang.CharSequence... values)
Returns the ordered media types for the given values.static java.util.List<MediaType>
orderedOf(java.util.List<? extends java.lang.CharSequence> values)
Returns the ordered media types for the given values.java.lang.CharSequence
subSequence(int start, int end)
java.lang.String
toString()
-
-
-
Field Detail
-
EXTENSION_JSON
public static final java.lang.String EXTENSION_JSON
Default file extension used for JSON.- See Also:
- Constant Field Values
-
EXTENSION_XML
public static final java.lang.String EXTENSION_XML
Default file extension used for XML.- See Also:
- Constant Field Values
-
EXTENSION_PDF
public static final java.lang.String EXTENSION_PDF
Default file extension used for PDF.- See Also:
- Constant Field Values
-
EXTENSION_XLSX
public static final java.lang.String EXTENSION_XLSX
File extension used for Microsoft Excel Open XML Spreadsheet (XLSX).- See Also:
- Constant Field Values
-
EXTENSION_XLS
public static final java.lang.String EXTENSION_XLS
File extension for Microsoft Excel's workbook files in use between 97-2003.- See Also:
- Constant Field Values
-
EMPTY_ARRAY
public static final MediaType[] EMPTY_ARRAY
Default empty media type array.
-
ALL
public static final java.lang.String ALL
A wildcard media type representing all types.- See Also:
- Constant Field Values
-
ALL_TYPE
public static final MediaType ALL_TYPE
A wildcard media type representing all types.
-
APPLICATION_FORM_URLENCODED
public static final java.lang.String APPLICATION_FORM_URLENCODED
Form encoded data: application/x-www-form-urlencoded.- See Also:
- Constant Field Values
-
APPLICATION_FORM_URLENCODED_TYPE
public static final MediaType APPLICATION_FORM_URLENCODED_TYPE
Form encoded data: application/x-www-form-urlencoded.
-
FORM
public static final MediaType FORM
Short cut forAPPLICATION_FORM_URLENCODED_TYPE
.
-
MULTIPART_FORM_DATA
public static final java.lang.String MULTIPART_FORM_DATA
Multi part form data: multipart/form-data.- See Also:
- Constant Field Values
-
MULTIPART_FORM_DATA_TYPE
public static final MediaType MULTIPART_FORM_DATA_TYPE
Multi part form data: multipart/form-data.
-
TEXT_HTML
public static final java.lang.String TEXT_HTML
HTML: text/html.- See Also:
- Constant Field Values
-
TEXT_HTML_TYPE
public static final MediaType TEXT_HTML_TYPE
HTML: text/html.
-
TEXT_CSV
public static final java.lang.String TEXT_CSV
CSV: text/csv.- See Also:
- Constant Field Values
-
TEXT_CSV_TYPE
public static final MediaType TEXT_CSV_TYPE
CSV: text/csv.
-
APPLICATION_XHTML
public static final java.lang.String APPLICATION_XHTML
XHTML: application/xhtml+xml.- See Also:
- Constant Field Values
-
APPLICATION_XHTML_TYPE
public static final MediaType APPLICATION_XHTML_TYPE
XHTML: application/xhtml+xml.
-
APPLICATION_XML
public static final java.lang.String APPLICATION_XML
XML: application/xml.- See Also:
- Constant Field Values
-
APPLICATION_XML_TYPE
public static final MediaType APPLICATION_XML_TYPE
XML: application/xml.
-
APPLICATION_JSON
public static final java.lang.String APPLICATION_JSON
JSON: application/json.- See Also:
- Constant Field Values
-
APPLICATION_JSON_TYPE
public static final MediaType APPLICATION_JSON_TYPE
JSON: application/json.
-
APPLICATION_YAML
public static final java.lang.String APPLICATION_YAML
YAML: application/x-yaml.- See Also:
- Constant Field Values
-
APPLICATION_YAML_TYPE
public static final MediaType APPLICATION_YAML_TYPE
YAML: application/x-yaml.
-
MICROSOFT_EXCEL_OPEN_XML
public static final java.lang.String MICROSOFT_EXCEL_OPEN_XML
XML: Microsoft Excel Open XML Spreadsheet (XLSX).- See Also:
- Constant Field Values
-
MICROSOFT_EXCEL_OPEN_XML_TYPE
public static final MediaType MICROSOFT_EXCEL_OPEN_XML_TYPE
XML: Microsoft Excel Open XML Spreadsheet (XLSX).
-
MICROSOFT_EXCEL
public static final java.lang.String MICROSOFT_EXCEL
Microsoft Excel's workbook files in use between 97-2003.- See Also:
- Constant Field Values
-
MICROSOFT_EXCEL_TYPE
public static final MediaType MICROSOFT_EXCEL_TYPE
Microsoft Excel's workbook files in use between 97-2003.
-
TEXT_XML
public static final java.lang.String TEXT_XML
XML: text/xml.- See Also:
- Constant Field Values
-
TEXT_XML_TYPE
public static final MediaType TEXT_XML_TYPE
XML: text/xml.
-
TEXT_JSON
public static final java.lang.String TEXT_JSON
JSON: text/json.- See Also:
- Constant Field Values
-
TEXT_JSON_TYPE
public static final MediaType TEXT_JSON_TYPE
JSON: text/json.
-
TEXT_PLAIN
public static final java.lang.String TEXT_PLAIN
Plain Text: text/plain.- See Also:
- Constant Field Values
-
TEXT_PLAIN_TYPE
public static final MediaType TEXT_PLAIN_TYPE
Plain Text: text/plain.
-
APPLICATION_HAL_JSON
public static final java.lang.String APPLICATION_HAL_JSON
HAL JSON: application/hal+json.- See Also:
- Constant Field Values
-
APPLICATION_HAL_JSON_TYPE
public static final MediaType APPLICATION_HAL_JSON_TYPE
HAL JSON: application/hal+json.
-
APPLICATION_HAL_XML
public static final java.lang.String APPLICATION_HAL_XML
HAL XML: application/hal+xml.- See Also:
- Constant Field Values
-
APPLICATION_HAL_XML_TYPE
public static final MediaType APPLICATION_HAL_XML_TYPE
HAL XML: application/hal+xml.
-
APPLICATION_ATOM_XML
public static final java.lang.String APPLICATION_ATOM_XML
Atom: application/atom+xml.- See Also:
- Constant Field Values
-
APPLICATION_ATOM_XML_TYPE
public static final MediaType APPLICATION_ATOM_XML_TYPE
Atom: application/atom+xml.
-
APPLICATION_VND_ERROR
public static final java.lang.String APPLICATION_VND_ERROR
VND Error: application/vnd.error+json.- See Also:
- Constant Field Values
-
APPLICATION_VND_ERROR_TYPE
public static final MediaType APPLICATION_VND_ERROR_TYPE
VND Error: application/vnd.error+json.
-
TEXT_EVENT_STREAM
public static final java.lang.String TEXT_EVENT_STREAM
Server Sent Event: text/event-stream.- See Also:
- Constant Field Values
-
TEXT_EVENT_STREAM_TYPE
public static final MediaType TEXT_EVENT_STREAM_TYPE
Server Sent Event: text/event-stream.
-
APPLICATION_JSON_STREAM
public static final java.lang.String APPLICATION_JSON_STREAM
JSON Stream: application/x-json-stream.- See Also:
- Constant Field Values
-
APPLICATION_JSON_STREAM_TYPE
public static final MediaType APPLICATION_JSON_STREAM_TYPE
JSON Stream: application/x-json-stream.
-
APPLICATION_OCTET_STREAM
public static final java.lang.String APPLICATION_OCTET_STREAM
BINARY: application/octet-stream.- See Also:
- Constant Field Values
-
APPLICATION_OCTET_STREAM_TYPE
public static final MediaType APPLICATION_OCTET_STREAM_TYPE
BINARY: application/octet-stream.
-
APPLICATION_GRAPHQL
public static final java.lang.String APPLICATION_GRAPHQL
GraphQL: application/graphql.- See Also:
- Constant Field Values
-
APPLICATION_GRAPHQL_TYPE
public static final MediaType APPLICATION_GRAPHQL_TYPE
GraphQL: application/graphql.
-
APPLICATION_PDF
public static final java.lang.String APPLICATION_PDF
PDF: application/pdf.- See Also:
- Constant Field Values
-
APPLICATION_PDF_TYPE
public static final MediaType APPLICATION_PDF_TYPE
PDF: application/pdf.
-
IMAGE_PNG
public static final java.lang.String IMAGE_PNG
Png Image: image/png.- See Also:
- Constant Field Values
-
IMAGE_PNG_TYPE
public static final MediaType IMAGE_PNG_TYPE
Png Image: image/png.
-
IMAGE_JPEG
public static final java.lang.String IMAGE_JPEG
Jpeg Image: image/jpeg.- See Also:
- Constant Field Values
-
IMAGE_JPEG_TYPE
public static final MediaType IMAGE_JPEG_TYPE
Jpeg Image: image/jpeg.
-
IMAGE_GIF
public static final java.lang.String IMAGE_GIF
Gif Image: image/gif.- See Also:
- Constant Field Values
-
IMAGE_GIF_TYPE
public static final MediaType IMAGE_GIF_TYPE
Gif Image: image/gif.
-
IMAGE_WEBP
public static final java.lang.String IMAGE_WEBP
Webp Image: image/webp.- See Also:
- Constant Field Values
-
IMAGE_WEBP_TYPE
public static final MediaType IMAGE_WEBP_TYPE
Webp Image: image/webp.
-
CHARSET_PARAMETER
public static final java.lang.String CHARSET_PARAMETER
Parameter"charset"
.- See Also:
- Constant Field Values
-
Q_PARAMETER
public static final java.lang.String Q_PARAMETER
Parameter"q"
.- See Also:
- Constant Field Values
-
V_PARAMETER
public static final java.lang.String V_PARAMETER
Parameter"v"
.- See Also:
- Constant Field Values
-
name
protected final java.lang.String name
-
subtype
protected final java.lang.String subtype
-
type
protected final java.lang.String type
-
extension
protected final java.lang.String extension
-
parameters
protected final java.util.Map<java.lang.CharSequence,java.lang.String> parameters
-
-
Constructor Detail
-
MediaType
public MediaType(java.lang.String name)
Constructs a new media type for the given string.- Parameters:
name
- The name of the media type. For example application/json
-
MediaType
public MediaType(java.lang.String name, java.util.Map<java.lang.String,java.lang.String> params)
Constructs a new media type for the given string and parameters.- Parameters:
name
- The name of the media type. For example application/jsonparams
- The parameters
-
MediaType
public MediaType(java.lang.String name, java.lang.String extension)
Constructs a new media type for the given string and extension.- Parameters:
name
- The name of the media type. For example application/jsonextension
- The extension of the file using this media type if it differs from the subtype
-
MediaType
public MediaType(java.lang.String name, java.lang.String extension, java.util.Map<java.lang.String,java.lang.String> params)
Constructs a new media type for the given string and extension.- Parameters:
name
- The name of the media type. For example application/jsonextension
- The extension of the file using this media type if it differs from the subtypeparams
- The parameters
-
-
Method Detail
-
of
public static MediaType of(java.lang.String mediaType)
Create a new or get aMediaType
from the given text.- Parameters:
mediaType
- The text- Returns:
- The
MediaType
-
matches
public boolean matches(@NonNull MediaType expectedContentType)
Determine if this requested content type can be satisfied by a given content type. e.g. text/* will be satisfied by test/html.- Parameters:
expectedContentType
- Content type to match against- Returns:
- if successful match
-
getName
public java.lang.String getName()
- Returns:
- The name of the mime type without any parameters
-
getType
public java.lang.String getType()
- Returns:
- The type of the media type. For example for application/hal+json this would return "application"
-
getSubtype
public java.lang.String getSubtype()
- Returns:
- The subtype. For example for application/hal+json this would return "hal+json"
-
getExtension
public java.lang.String getExtension()
- Returns:
- The extension. For example for application/hal+json this would return "json"
-
getParameters
public io.micronaut.core.value.OptionalValues<java.lang.String> getParameters()
- Returns:
- The parameters to the media type
-
getQuality
public java.lang.String getQuality()
- Returns:
- The quality of the Mime type
-
getQualityAsNumber
public java.math.BigDecimal getQualityAsNumber()
- Returns:
- The quality in BigDecimal form
-
getVersion
public java.lang.String getVersion()
- Returns:
- The version of the Mime type
-
getCharset
public java.util.Optional<java.nio.charset.Charset> getCharset()
- Returns:
- The charset of the media type if specified
-
length
public int length()
- Specified by:
length
in interfacejava.lang.CharSequence
-
charAt
public char charAt(int index)
- Specified by:
charAt
in interfacejava.lang.CharSequence
-
subSequence
public java.lang.CharSequence subSequence(int start, int end)
- Specified by:
subSequence
in interfacejava.lang.CharSequence
-
isTextBased
public boolean isTextBased()
- Returns:
- Whether the media type is text based
-
isTextBased
public static boolean isTextBased(java.lang.String contentType)
- Parameters:
contentType
- The content type- Returns:
- Whether the content type is text based
-
toString
public java.lang.String toString()
- Specified by:
toString
in interfacejava.lang.CharSequence
- Overrides:
toString
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object o)
Only the name is matched. Parameters are not included.
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
orderedOf
public static java.util.List<MediaType> orderedOf(java.lang.CharSequence... values)
Returns the ordered media types for the given values.- Parameters:
values
- The values- Returns:
- The media types.
- Since:
- 1.3.3
-
orderedOf
public static java.util.List<MediaType> orderedOf(java.util.List<? extends java.lang.CharSequence> values)
Returns the ordered media types for the given values.- Parameters:
values
- The values- Returns:
- The media types.
- Since:
- 1.3.3
-
of
public static MediaType of(java.lang.CharSequence mediaType)
Create a newMediaType
from the given text.- Parameters:
mediaType
- The text- Returns:
- The
MediaType
-
of
public static MediaType[] of(java.lang.CharSequence... mediaType)
Create a newMediaType
from the given text.- Parameters:
mediaType
- The text- Returns:
- The
MediaType
-
fromType
public static java.util.Optional<MediaType> fromType(java.lang.Class<?> type)
- Parameters:
type
- The type- Returns:
- An
Optional
MediaType
-
forExtension
public static java.util.Optional<MediaType> forExtension(java.lang.String extension)
Resolve theMediaType
for the given file extension.- Parameters:
extension
- The file extension- Returns:
- The
MediaType
-
-