Class BasicMediaTypeHeader
- All Implemented Interfaces:
Serializable
,Cloneable
,Header
,NameValuePair
- Direct Known Subclasses:
ContentType
Example
Content-Type: application/json;charset=utf-8
See Also:
-
Constructor Summary
ConstructorDescriptionBasicMediaTypeHeader
(String name, String value) Constructor.BasicMediaTypeHeader
(String name, Supplier<MediaType> value) Constructor with delayed value.BasicMediaTypeHeader
(String name, MediaType value) Constructor. -
Method Summary
Modifier and TypeMethodDescriptiongetParameter
(String name) Returns a parameterized value of the header.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.getValue()
final boolean
hasSubType
(String value) Returnstrue if the subtype contains the specified'+' delimited subtype value.final boolean
Returnstrue if this media type 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 BasicMediaTypeHeader
Static creator.static BasicMediaTypeHeader
Static creator.Return the value if present, otherwise returnother .Returns the header value as aMediaType
.Methods inherited from class org.apache.juneau.http.header.BasicStringHeader
assertString, asString, of, ofPair, orElse
Methods inherited from class org.apache.juneau.http.header.BasicHeader
assertName, assertStringValue, equals, equalsIgnoreCase, get, getElements, getName, hashCode, isNotEmpty, isPresent, of, of, toString
-
Constructor Details
-
BasicMediaTypeHeader
Constructor.- Parameters:
name
- The header name.value
- The header value.
Must be parsable byMediaType.of(String)
.
Can benull .- Throws:
IllegalArgumentException
- If name isnull or empty.
-
BasicMediaTypeHeader
Constructor.- Parameters:
name
- The header name.value
- The header value.
Can benull .- Throws:
IllegalArgumentException
- If name isnull or empty.
-
BasicMediaTypeHeader
Constructor with delayed value.Header value is re-evaluated on each call to
getValue()
.- Parameters:
name
- The header name.value
- The supplier of the header value.
Can benull .- Throws:
IllegalArgumentException
- If name isnull or empty.
-
-
Method Details
-
of
Static creator.- Parameters:
name
- The header name.value
- The header value.
Must be parsable byMediaType.of(String)
.
Can benull .- Returns:
- A new header bean, or
null if the value isnull . - Throws:
IllegalArgumentException
- If name isnull or empty.
-
of
Static creator.- Parameters:
name
- The header name.value
- The header value.
Can benull .- Returns:
- A new header bean, or
null if the value isnull . - Throws:
IllegalArgumentException
- If name isnull or empty.
-
getValue
- Specified by:
getValue
in interfaceNameValuePair
- Overrides:
getValue
in classBasicStringHeader
-
asMediaType
-
toMediaType
Returns the header value as aMediaType
.- Returns:
- The header value as a
MediaType
. Can benull .
-
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.
-
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:
value
- 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. Can be
null .
-
isMetaSubtype
Returnstrue if this media type contains the'*' meta character.- Returns:
true if this media type 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.
-
getParameter
Returns a parameterized value of the header.ContentType
contentType = ContentType.of ("application/json;charset=foo" );assertEquals ("foo" ,contentType .getParameter("charset" );- Parameters:
name
- The header name.- Returns:
- The header value, or
null if the parameter is not present.
-
orElse
Return the value if present, otherwise returnother .This is a shortened form for calling
asMediaType().orElse( .other )- Parameters:
other
- The value to be returned if there is no value present, can benull .- Returns:
- The value, if present, otherwise
other .
-