Class MediaRange
Accept header value, e.g.
text/html;level=1;q=0.7, as defined by
RFC 9110, Section 12.5.1.
The type and subtype are normalized to lowercase and either
may be the * wildcard (a wildcard type requires a wildcard subtype). The quality
is the q weight parameter, defaulting to 1 when absent and clamped to the range
[0, 1]; per RFC 9110 it is recognized regardless of its position among the parameters.
Parameters are all non-q media-type parameters, whether they
appear before or after q (RFC 9110 removed RFC 7231's accept-ext grammar).
See Request.getMediaRanges() for the ordered media ranges of a request's Accept header value[s].
- Author:
- Mark Allen
-
Method Summary
Modifier and TypeMethodDescriptionbooleanstatic @NonNull Optional<MediaRange> fromHeaderRepresentation(@Nullable String mediaRange) Parses a single media range, e.g.The media-type parameters of this media range (e.g.Theqweight of this media range in the range[0, 1], defaulting to1when unspecified.The subtype portion of this media range (e.g.getType()The type portion of this media range (e.g.inthashCode()Is this media range's subtype the*wildcard (e.g.Is this media range's type the*wildcard (i.e.toString()
-
Method Details
-
fromHeaderRepresentation
Parses a single media range, e.g.text/html;level=1;q=0.7, per RFC 9110, Section 12.5.1.Parsing is lenient: a malformed representation yields
Optional.empty()rather than an exception. A representation is considered malformed if it lacks atype/subtypestructure, uses invalid HTTP tokens for the type, subtype, or parameter names, pairs a wildcard type with a concrete subtype (e.g.*/html), or carries an unparseableqvalue. Quality values outside[0, 1]are clamped. Theqweight parameter is recognized at any position; if multipleqparameters are present, all but the first are ignored. All other parameters are retained as media-type parameters. Quoted parameter values have their surrounding quotes removed and quoted-pair escape sequences (e.g.\") unescaped.- Parameters:
mediaRange- the media range header representation to parse, e.g.text/html;q=0.7- Returns:
- the parsed media range, or
Optional.empty()if the input is missing or malformed
-
equals
-
hashCode
-
toString
-
getType
-
getSubtype
The subtype portion of this media range (e.g.htmlintext/html), lowercase; may be the*wildcard.- Returns:
- the media range's subtype
-
getQuality
Theqweight of this media range in the range[0, 1], defaulting to1when unspecified. A quality of0means "not acceptable".- Returns:
- the media range's quality weight
-
getParameters
The media-type parameters of this media range (e.g.level=1intext/html;level=1;q=0.7), with lowercase names in their original order. Excludes only theqweight parameter; all other parameters are included regardless of their position relative toq.- Returns:
- an unmodifiable view of the media range's parameters, or an empty map if none were specified
-
isWildcardType
Is this media range's type the*wildcard (i.e.*/*)?- Returns:
trueif the type is a wildcard,falseotherwise
-
isWildcardSubtype
Is this media range's subtype the*wildcard (e.g.text/*)?- Returns:
trueif the subtype is a wildcard,falseotherwise
-