Class MediaType
- java.lang.Object
-
- io.helidon.common.http.MediaType
-
- All Implemented Interfaces:
AcceptPredicate<MediaType>
,Predicate<MediaType>
public final class MediaType extends Object implements AcceptPredicate<MediaType>
An abstraction for a media type. Instances are immutable.- See Also:
- HTTP/1.1 section 3.7
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
MediaType.Builder
A fluent API builder to create instances ofMediaType
.
-
Field Summary
Fields Modifier and Type Field Description static MediaType
APPLICATION_ATOM_XML
AMediaType
constant representingapplication/atom+xml
media type.static MediaType
APPLICATION_FORM_URLENCODED
AMediaType
constant representingapplication/x-www-form-urlencoded
media type.static MediaType
APPLICATION_JSON
AMediaType
constant representingapplication/json
media type.static MediaType
APPLICATION_OCTET_STREAM
AMediaType
constant representingapplication/octet-stream
media type.static MediaType
APPLICATION_OPENAPI_JSON
AMediaType
constant representing OpenAPI json.static MediaType
APPLICATION_OPENAPI_YAML
AMediaType
constant representing OpenAPI yaml.static MediaType
APPLICATION_SVG_XML
AMediaType
constant representingapplication/svg+xml
media type.static MediaType
APPLICATION_X_YAML
AMediaType
constant representing "x" YAML as application.static MediaType
APPLICATION_XHTML_XML
AMediaType
constant representingapplication/xhtml+xml
media type.static MediaType
APPLICATION_XML
AMediaType
constant representingapplication/xml
media type.static MediaType
APPLICATION_YAML
AMediaType
constant representing pseudo-registered YAML.static String
CHARSET_PARAMETER
The media type "charset" parameter name.static Predicate<MediaType>
JSON_PREDICATE
static MediaType
MULTIPART_FORM_DATA
AMediaType
constant representingmultipart/form-data
media type.static MediaType
TEXT_HTML
AMediaType
constant representingtext/html
media type.static MediaType
TEXT_PLAIN
AMediaType
constant representingtext/plain
media type.static MediaType
TEXT_X_YAML
AMediaType
constant representing "x" YAML as text.static MediaType
TEXT_XML
AMediaType
constant representingtext/xml
media type.static MediaType
TEXT_YAML
AMediaType
constant representing pseudo-registered YAML as text.static MediaType
WILDCARD
AMediaType
constant representing wildcard media type.static Predicate<MediaType>
XML_PREDICATE
-
Fields inherited from interface io.helidon.common.http.AcceptPredicate
QUALITY_FACTOR_PARAMETER, WILDCARD_VALUE
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static MediaType.Builder
builder()
A fluent API builder for creating customized Media type instances.Optional<String>
charset()
GetsOptional
value of charset parameter.static MediaType
create(String type, String subtype)
Creates a new instance ofMediaType
with the supplied type and subtype.boolean
equals(Object obj)
Comparesobj
to this media type to see if they are the same by comparing type, subtype and parameters.int
hashCode()
Generate a hash code from the type, subtype and parameters.boolean
hasSuffix(String suffix)
Tests if this media type has provided Structured Syntaxsuffix
(RFC 6839).boolean
isWildcardSubtype()
Checks if the subtype is a wildcard.boolean
isWildcardType()
Checks if the primary type is a wildcard.Map<String,String>
parameters()
Getter for a read-only parameter map.static MediaType
parse(String input)
Parses a media type from its string representation.double
qualityFactor()
Gets quality factor parameter ("q") as a double value.String
subtype()
Getter for subtype.boolean
test(MediaType other)
Check if this media type is compatible with another media type.String
toString()
Convert the media type to a string suitable for use as the value of a corresponding HTTP header.String
type()
Getter for primary type.MediaType
withCharset(String charset)
Create a newMediaType
instance with the same type, subtype and parameters copied from the original instance and the supplied "charset" parameter.
-
-
-
Field Detail
-
CHARSET_PARAMETER
public static final String CHARSET_PARAMETER
The media type "charset" parameter name.- See Also:
- Constant Field Values
-
WILDCARD
public static final MediaType WILDCARD
AMediaType
constant representing wildcard media type.
-
APPLICATION_XML
public static final MediaType APPLICATION_XML
AMediaType
constant representingapplication/xml
media type.
-
APPLICATION_ATOM_XML
public static final MediaType APPLICATION_ATOM_XML
AMediaType
constant representingapplication/atom+xml
media type.
-
APPLICATION_XHTML_XML
public static final MediaType APPLICATION_XHTML_XML
AMediaType
constant representingapplication/xhtml+xml
media type.
-
APPLICATION_SVG_XML
public static final MediaType APPLICATION_SVG_XML
AMediaType
constant representingapplication/svg+xml
media type.
-
APPLICATION_JSON
public static final MediaType APPLICATION_JSON
AMediaType
constant representingapplication/json
media type.
-
APPLICATION_FORM_URLENCODED
public static final MediaType APPLICATION_FORM_URLENCODED
AMediaType
constant representingapplication/x-www-form-urlencoded
media type.
-
MULTIPART_FORM_DATA
public static final MediaType MULTIPART_FORM_DATA
AMediaType
constant representingmultipart/form-data
media type.
-
APPLICATION_OCTET_STREAM
public static final MediaType APPLICATION_OCTET_STREAM
AMediaType
constant representingapplication/octet-stream
media type.
-
TEXT_PLAIN
public static final MediaType TEXT_PLAIN
AMediaType
constant representingtext/plain
media type.
-
TEXT_XML
public static final MediaType TEXT_XML
AMediaType
constant representingtext/xml
media type.
-
TEXT_HTML
public static final MediaType TEXT_HTML
AMediaType
constant representingtext/html
media type.
-
APPLICATION_OPENAPI_YAML
public static final MediaType APPLICATION_OPENAPI_YAML
AMediaType
constant representing OpenAPI yaml.See https://github.com/opengeospatial/WFS_FES/issues/117#issuecomment-402188280
-
APPLICATION_OPENAPI_JSON
public static final MediaType APPLICATION_OPENAPI_JSON
AMediaType
constant representing OpenAPI json.
-
APPLICATION_X_YAML
public static final MediaType APPLICATION_X_YAML
AMediaType
constant representing "x" YAML as application.
-
APPLICATION_YAML
public static final MediaType APPLICATION_YAML
AMediaType
constant representing pseudo-registered YAML. (It is not actually registered.)
-
TEXT_X_YAML
public static final MediaType TEXT_X_YAML
AMediaType
constant representing "x" YAML as text.
-
TEXT_YAML
public static final MediaType TEXT_YAML
AMediaType
constant representing pseudo-registered YAML as text.
-
-
Method Detail
-
create
public static MediaType create(String type, String subtype)
Creates a new instance ofMediaType
with the supplied type and subtype.- Parameters:
type
- the primary type,null
is equivalent toAcceptPredicate.WILDCARD_VALUE
subtype
- the subtype,null
is equivalent toAcceptPredicate.WILDCARD_VALUE
- Returns:
- a new media type for the specified type and subtype
-
parse
public static MediaType parse(String input)
Parses a media type from its string representation.- Parameters:
input
- the input string representing a media type- Returns:
- parsed
MediaType
instance - Throws:
IllegalArgumentException
- if the input is not parsableNullPointerException
- if the input isnull
-
builder
public static MediaType.Builder builder()
A fluent API builder for creating customized Media type instances.- Returns:
- a new builder
-
type
public String type()
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.
-
subtype
public String subtype()
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.
-
parameters
public Map<String,String> parameters()
Getter for a read-only parameter map. Keys are case-insensitive.- Returns:
- an immutable map of parameters.
-
withCharset
public MediaType withCharset(String charset)
Create a newMediaType
instance with the same type, subtype and parameters copied from the original instance and the supplied "charset" parameter.
-
charset
public Optional<String> charset()
GetsOptional
value of charset parameter.- Returns:
- Charset parameter.
-
qualityFactor
public double qualityFactor()
Description copied from interface:AcceptPredicate
Gets quality factor parameter ("q") as a double value. If missing, then returns1.0
- Specified by:
qualityFactor
in interfaceAcceptPredicate<MediaType>
- Returns:
- Quality factor parameter.
-
test
public boolean test(MediaType other)
Check if this media type is compatible with another media type. E.g. image/* is compatible with image/jpeg, image/png, etc. Media type parameters are ignored. The function is commutative.
-
equals
public boolean equals(Object obj)
Comparesobj
to 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.Note that the
equals(...)
implementation does not perform a class equality check (this.getClass() == obj.getClass()
). Therefore any class that extends fromMediaType
class 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.
-
hashCode
public int hashCode()
Generate a hash code from the type, subtype and parameters.Note that the
equals(java.lang.Object)
implementation does not perform a class equality check (this.getClass() == obj.getClass()
). Therefore any class that extends fromMediaType
class 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.
-
toString
public String toString()
Convert the media type to a string suitable for use as the value of a corresponding HTTP header.
-
hasSuffix
public boolean hasSuffix(String suffix)
Tests if this media type has provided Structured Syntaxsuffix
(RFC 6839).- Parameters:
suffix
- Suffix with or without '+' prefix. If null or empty then returnstrue
if this media type has ANY suffix.- Returns:
true
if media type has specifiedsuffix
or has any suffix if parameter isnull
or empty.
-
-