Package org.apache.juneau
Class MediaType
java.lang.Object
org.apache.juneau.MediaType
- All Implemented Interfaces:
Comparable<MediaType>
- Direct Known Subclasses:
MediaRange
Describes a single media type used in content negotiation between an HTTP client and server, as described in
Section 14.1 and 14.7 of RFC2616 (the HTTP/1.1 specification).
See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final MediaType
Reusable predefined media typestatic final MediaType
Represents an empty media type object.static final MediaType
Reusable predefined media typestatic final MediaType
Reusable predefined media typestatic final MediaType
Reusable predefined media typestatic final MediaType
Reusable predefined media typestatic final MediaType
Reusable predefined media typestatic final MediaType
Reusable predefined media typestatic final MediaType
Reusable predefined media typestatic final MediaType
Reusable predefined media typestatic final MediaType
Reusable predefined media typestatic final MediaType
Reusable predefined media typestatic final MediaType
Reusable predefined media typestatic final MediaType
Reusable predefined media typestatic final MediaType
Reusable predefined media type -
Constructor Summary
ConstructorDescriptionConstructor.MediaType
(String mt, NameValuePair[] parameters) Constructor.Constructor.MediaType
(HeaderElement e, NameValuePair[] parameters) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionfinal int
boolean
forEachParameter
(Consumer<NameValuePair> action) Performs an action on the additional parameters on this media type.final MediaType
forEachSubType
(Consumer<String> action) Performs an action on the subtypes broken down by fragments delimited by"'" .getParameter
(String name) Returns the additional parameter on this media type.Returns the additional parameters on this media type.final String
Returns the'subType' fragment of the'type/subType' string.Returns the subtypes broken down by fragments delimited by"'" .final String
getType()
Returns the'type' fragment of the'type/subType' string.int
hashCode()
final boolean
hasSubType
(String st) Returnstrue if the subtype contains the specified'+' delimited subtype value.final boolean
Returnstrue if this media type subtype contains the'*' meta character.int
Given a list of media types, returns the best match for thisContent-Type header.final int
Returns a match metric against the specified media type where a larger number represents a better match.static MediaType
Returns the media type for the specified string.static MediaType
of
(String value, NameValuePair... parameters) Same asof(String)
but allows you to specify the parameters.static MediaType[]
Same asof(String)
but allows you to construct an array ofMediaTypes from an array of strings.toString()
-
Field Details
-
EMPTY
Represents an empty media type object. -
CSV
Reusable predefined media type -
HTML
Reusable predefined media type -
JSON
Reusable predefined media type -
MSGPACK
Reusable predefined media type -
PLAIN
Reusable predefined media type -
UON
Reusable predefined media type -
URLENCODING
Reusable predefined media type -
XML
Reusable predefined media type -
XMLSOAP
Reusable predefined media type -
RDF
Reusable predefined media type -
RDFABBREV
Reusable predefined media type -
NTRIPLE
Reusable predefined media type -
TURTLE
Reusable predefined media type -
N3
Reusable predefined media type
-
-
Constructor Details
-
MediaType
Constructor.- Parameters:
mt
- The media type string.
-
MediaType
Constructor.- Parameters:
mt
- The media type string.parameters
- The media type parameters. Ifnull , they're pulled from the media type string.
-
MediaType
Constructor.- Parameters:
e
- The parsed media type string.
-
MediaType
Constructor.- Parameters:
e
- The parsed media type string.parameters
- Optional parameters.
-
-
Method Details
-
of
Returns the media type for the specified string. The same media type strings always return the same objects so that these objects can be compared for equality using '=='.Notes:
-
Spaces are replaced with
'+' characters. This gets around the issue where passing media type strings with'+' as HTTP GET parameters get replaced with spaces by your browser. Since spaces aren't supported by the spec, this is doesn't break anything. -
Anything including and following the
';' character is ignored (e.g.";charset=X" ).
- Parameters:
value
- The media type string. Will be lowercased. Returnsnull if input is null or empty.- Returns:
- A cached media type object.
-
Spaces are replaced with
-
of
Same asof(String)
but allows you to specify the parameters.- Parameters:
value
- The media type string. Will be lowercased. Returnsnull if input is null or empty.parameters
- The media type parameters. Ifnull , they're pulled from the media type string.- Returns:
- A new media type object, cached if parameters were not specified.
-
ofAll
Same asof(String)
but allows you to construct an array ofMediaTypes from an array of strings.- Parameters:
values
- The media type strings.- Returns:
- An array of
MediaType objects.
Always the same length as the input string array.
-
getType
Returns the'type' fragment of the'type/subType' string.- Returns:
- The media type.
-
getSubType
Returns the'subType' fragment of the'type/subType' string.- Returns:
- The media subtype.
-
hasSubType
Returnstrue if the subtype contains the specified'+' delimited subtype value.- Parameters:
st
- The subtype string. Case is ignored.- Returns:
true if the subtype contains the specified subtype string.
-
getSubTypes
Returns the subtypes broken down by fragments delimited by"'" .For example, the media type
"text/foo+bar" will return a list of[
'foo' ,'bar' ]- Returns:
- An unmodifiable list of subtype fragments. Never
null .
-
forEachSubType
Performs an action on the subtypes broken down by fragments delimited by"'" .- Parameters:
action
- The action to perform.- Returns:
- This object.
-
isMetaSubtype
Returnstrue if this media type subtype contains the'*' meta character.- Returns:
true if this media type subtype contains the'*' meta character.
-
match
Returns a match metric against the specified media type where a larger number represents a better match.This media type can contain
'*' metacharacters.
The comparison media type must not.- Exact matches (e.g.
"text/json" /"text/json") should match better than meta-character matches (e.g."text/*" /"text/json") - The comparison media type can have additional subtype tokens (e.g.
"text/json+foo" ) that will not prevent a match if theallowExtraSubTypes flag is set. The reverse is not true, e.g. the comparison media type must contain all subtype tokens found in the comparing media type.- We want the
JsonSerializer
("text/json" ) class to be able to handle requests for"text/json+foo" . - We want to make sure
Json5Serializer
("text/json5" ) does not handle requests for"text/json" .
- We want the
type/subTypes :- An exact match is
100,000 . - Add the following for type (assuming subtype match is <0):
10,000 for an exact match (e.g."text" =="text" ).5,000 for a meta match (e.g."*" =="text" ).
- Add the following for subtype (assuming type match is <0):
7,500 for an exact match (e.g."json+foo" =="json+foo" or"json+foo" =="foo+json" )100 for every subtype entry match (e.g."json" /"json+foo" )
- Parameters:
o
- The media type to compare with.allowExtraSubTypes
- Iftrue ,- Returns:
true if the media types match.
- Exact matches (e.g.
-
getParameters
Returns the additional parameters on this media type.For example, given the media type string
"text/html;level=1" , will return a map with the single entry{level:[
.'1' ]}- Returns:
- The map of additional parameters, or an empty map if there are no parameters.
-
forEachParameter
Performs an action on the additional parameters on this media type.- Parameters:
action
- The action to perform.- Returns:
- This object.
-
getParameter
Returns the additional parameter on this media type.- Parameters:
name
- The additional parameter name.- Returns:
- The parameter value, or
null if not found.
-
match
Given a list of media types, returns the best match for thisContent-Type header.Note that fuzzy matching is allowed on the media types where the
Content-Types header may contain additional subtype parts.
For example, given aContent-Type value of"text/json+activity" , the media type"text/json" will match if"text/json+activity" or"text/activity+json" isn't found.
The purpose for this is to allow parsers to match when artifacts such asid properties are present in the header.- Parameters:
mediaTypes
- The media types to match against.- Returns:
- The index into the array of the best match, or
-1 if no suitable matches could be found.
-
toString
-
hashCode
-
equals
-
compareTo
- Specified by:
compareTo
in interfaceComparable<MediaType>
-