Class JakartaMediaRangeList

All Implemented Interfaces:
Serializable, Cloneable, Iterable<JakartaMediaRangeList.MediaRange>, Collection<JakartaMediaRangeList.MediaRange>, NavigableSet<JakartaMediaRangeList.MediaRange>, Set<JakartaMediaRangeList.MediaRange>, SortedSet<JakartaMediaRangeList.MediaRange>

@ConsumerType public class JakartaMediaRangeList extends TreeSet<JakartaMediaRangeList.MediaRange>
Facilitates parsing of the Accept HTTP request header. See RFC 2616 section 14.1
Since:
1.1.0
See Also:
  • Field Details

  • Constructor Details

    • JakartaMediaRangeList

      @Deprecated public JakartaMediaRangeList(jakarta.servlet.http.HttpServletRequest request)
      Deprecated.
      Use fromRequest(HttpServletRequest) to read only the Accept header without allowing request parameters to override it.
      Constructs a MediaRangeList using information from the supplied HttpServletRequest. if the request contains a PARAM_ACCEPT query parameter, the query parameter value overrides any HEADER_ACCEPT header value. If the request contains no PARAM_ACCEPT parameter, or the parameter value is empty, the value of the HEADER_ACCEPT is used. If both values are missing, it is assumed that the client accepts all media types, as per the RFC. See also JakartaMediaRangeList(java.lang.String)
      Parameters:
      request - The HttpServletRequest to extract a MediaRangeList from
    • JakartaMediaRangeList

      public JakartaMediaRangeList(String listStr)
      Constructs a MediaRangeList using a list of media ranges specified in a java.lang.String. The string is a comma-separated list of media ranges, as specified by the RFC.
      Examples:
      • text/*;q=0.3, text/html;q=0.7, text/html;level=1, text/html;level=2;q=0.4, */*;q=0.5
      • text/html;q=0.8, application/json
      Parameters:
      listStr - The list of media range specifications
  • Method Details

    • fromRequest

      @NotNull public static @NotNull JakartaMediaRangeList fromRequest(@NotNull @NotNull jakarta.servlet.http.HttpServletRequest request)
      Creates a MediaRangeList from the HEADER_ACCEPT header of the supplied request. Request parameters, including PARAM_ACCEPT, are ignored. If the header is missing or empty, the client is assumed to accept all media types.
      Parameters:
      request - The request to read the Accept header from
      Returns:
      The media ranges accepted by the client
      Since:
      1.2.0
    • matchesAll

      public boolean matchesAll()
      Whether this MediaRangeList matches all media types.
      Returns:
      true if this MediaRangeList matches all media types, false otherwise
      Since:
      1.2.0
    • contains

      public boolean contains(String mediaType)
      Determines if this MediaRangeList contains a given media type.
      Parameters:
      mediaType - A string on the form type/subtype. Neither type or subtype should be wildcard (*).
      Returns:
      true if this MediaRangeList contains a media type that matches mediaType, false otherwise
      Throws:
      IllegalArgumentException - if mediaType is not on an accepted form
      NullPointerException - if mediaType is null
    • prefer

      @Nullable public @Nullable JakartaMediaRangeList.MediaRange prefer(@NotNull @NotNull Set<JakartaMediaRangeList.MediaRange> mediaRanges)
      Given a list of media types, returns the one is preferred by this MediaRangeList. Wildcards are matched against the offered types. The most specific matching range, including media parameters before q, determines each type's quality. Types with quality zero are not acceptable. Accept extensions after q do not constrain matching. Equal-quality types follow the ordering of this list; when the same range matches multiple offered types, their iteration order breaks the tie.
      Parameters:
      mediaRanges - An array of possible JakartaMediaRangeList.MediaRanges
      Returns:
      One of the mediaRanges that this MediaRangeList prefers; or null if this MediaRangeList does not contain any of the mediaRanges
      Throws:
      NullPointerException - if mediaRanges is null or contains a null value
    • prefer

      @Nullable public @Nullable String prefer(@NotNull @NotNull String... mediaRanges)
      Determines which of the mediaRanges specifications is preferred by this MediaRangeList. Offered order breaks ties when multiple types match the same range.
      Parameters:
      mediaRanges - String representations of MediaRanges. The strings must be on the form required by MediaRange(String)
      Returns:
      the toString() representation of the preferred MediaRange, or null if this MediaRangeList does not contain any of the mediaRanges
      See Also: