Class MediaRange

java.lang.Object
com.soklet.MediaRange

@ThreadSafe public final class MediaRange extends Object
An immutable representation of a single media range from an HTTP 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 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 a type/subtype structure, 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 unparseable q value. Quality values outside [0, 1] are clamped. The q weight parameter is recognized at any position; if multiple q parameters 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

      public boolean equals(@Nullable Object object)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      Overrides:
      toString in class Object
    • getType

      The type portion of this media range (e.g. text in text/html), lowercase; may be the * wildcard.
      Returns:
      the media range's type
    • getSubtype

      The subtype portion of this media range (e.g. html in text/html), lowercase; may be the * wildcard.
      Returns:
      the media range's subtype
    • getQuality

      The q weight of this media range in the range [0, 1], defaulting to 1 when unspecified. A quality of 0 means "not acceptable".
      Returns:
      the media range's quality weight
    • getParameters

      The media-type parameters of this media range (e.g. level=1 in text/html;level=1;q=0.7), with lowercase names in their original order. Excludes only the q weight parameter; all other parameters are included regardless of their position relative to q.
      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:
      true if the type is a wildcard, false otherwise
    • isWildcardSubtype

      Is this media range's subtype the * wildcard (e.g. text/*)?
      Returns:
      true if the subtype is a wildcard, false otherwise