Class BasicMediaTypeHeader

All Implemented Interfaces:
Serializable, Cloneable, Header, NameValuePair
Direct Known Subclasses:
ContentType

Category of headers that consist of a single parameterized string value.

Example

Content-Type: application/json;charset=utf-8

See Also:
  • Constructor Details

  • Method Details

    • of

      public static BasicMediaTypeHeader of(String name, String value)
      Static creator.
      Parameters:
      name - The header name.
      value - The header value.
      Must be parsable by MediaType.of(String).
      Can be null.
      Returns:
      A new header bean, or null if the value is null.
      Throws:
      IllegalArgumentException - If name is null or empty.
    • of

      public static BasicMediaTypeHeader of(String name, MediaType value)
      Static creator.
      Parameters:
      name - The header name.
      value - The header value.
      Can be null.
      Returns:
      A new header bean, or null if the value is null.
      Throws:
      IllegalArgumentException - If name is null or empty.
    • getValue

      public String getValue()
      Specified by:
      getValue in interface NameValuePair
      Overrides:
      getValue in class BasicStringHeader
    • asMediaType

      Returns the header value as a MediaType wrapped in an Optional.
      Returns:
      The header value as a MediaType wrapped in an Optional. Never null.
    • toMediaType

      Returns the header value as a MediaType.
      Returns:
      The header value as a MediaType. Can be null.
    • match

      public int match(List<MediaType> mediaTypes)
      Given a list of media types, returns the best match for this Content-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 a Content-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 as id 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

      public final String getType()
      Returns the 'type' fragment of the 'type/subType' string.
      Returns:
      The media type.
    • getSubType

      public final String getSubType()
      Returns the 'subType' fragment of the 'type/subType' string.
      Returns:
      The media subtype.
    • hasSubType

      public final boolean hasSubType(String value)
      Returns true 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

      public final List<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

      public final boolean isMetaSubtype()
      Returns true if this media type contains the '*' meta character.
      Returns:
      true if this media type contains the '*' meta character.
    • match

      public final int match(MediaType o, boolean allowExtraSubTypes)
      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 the allowExtraSubTypes 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".
        More token matches should result in a higher match number.
      The formula is as follows for 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 - If true,
      Returns:
      true if the media types match.
    • 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

      public String getParameter(String name)
      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

      public MediaType orElse(MediaType other)
      Return the value if present, otherwise return other.

      This is a shortened form for calling asMediaType().orElse(other).

      Parameters:
      other - The value to be returned if there is no value present, can be null.
      Returns:
      The value, if present, otherwise other.