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
FieldsModifier and TypeFieldDescriptionstatic final MediaTypeReusable predefined media typestatic final MediaTypeRepresents an empty media type object.static final MediaTypeReusable predefined media typestatic final MediaTypeReusable predefined media typestatic final MediaTypeReusable predefined media typestatic final MediaTypeReusable predefined media typestatic final MediaTypeReusable predefined media typestatic final MediaTypeReusable predefined media typestatic final MediaTypeReusable predefined media typestatic final MediaTypeReusable predefined media typestatic final MediaTypeReusable predefined media typestatic final MediaTypeReusable predefined media typestatic final MediaTypeReusable predefined media typestatic final MediaTypeReusable predefined media typestatic final MediaTypeReusable predefined media type -
Constructor Summary
ConstructorsConstructorDescriptionConstructor.MediaType(String mt, NameValuePair[] parameters) Constructor.Constructor.MediaType(HeaderElement e, NameValuePair[] parameters) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionfinal intbooleanforEachParameter(Consumer<NameValuePair> action) Performs an action on the additional parameters on this media type.final MediaTypeforEachSubType(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 StringReturns the'subType' fragment of the'type/subType' string.Returns the subtypes broken down by fragments delimited by"'" .final StringgetType()Returns the'type' fragment of the'type/subType' string.inthashCode()final booleanhasSubType(String st) Returnstrue if the subtype contains the specified'+' delimited subtype value.final booleanReturnstrue if this media type subtype contains the'*' meta character.intGiven a list of media types, returns the best match for thisContent-Type header.final intReturns a match metric against the specified media type where a larger number represents a better match.static MediaTypeReturns the media type for the specified string.static MediaTypeof(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:
e- The parsed media type string.
-
MediaType
Constructor.- Parameters:
e- The parsed media type string.parameters- Optional parameters.
-
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.
-
-
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.
-
compareTo
- Specified by:
compareToin interfaceComparable<MediaType>
-
equals
-
forEachParameter
Performs an action on the additional parameters on this media type.- Parameters:
action- The action to perform.- Returns:
- This object.
-
forEachSubType
Performs an action on the subtypes broken down by fragments delimited by"'" .- 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.
-
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.
-
getSubType
Returns the'subType' fragment of the'type/subType' string.- Returns:
- The media subtype.
-
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 .
-
getType
Returns the'type' fragment of the'type/subType' string.- Returns:
- The media type.
-
hashCode
-
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.
-
isMetaSubtype
Returnstrue if this media type subtype contains the'*' meta character.- Returns:
true if this media type subtype contains the'*' meta character.
-
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.
-
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.
-
toString
-