Class JakartaMediaRangeList
java.lang.Object
java.util.AbstractCollection<JakartaMediaRangeList.MediaRange>
java.util.AbstractSet<JakartaMediaRangeList.MediaRange>
java.util.TreeSet<JakartaMediaRangeList.MediaRange>
org.apache.sling.api.request.header.JakartaMediaRangeList
- All Implemented Interfaces:
Serializable,Cloneable,Iterable<JakartaMediaRangeList.MediaRange>,Collection<JakartaMediaRangeList.MediaRange>,NavigableSet<JakartaMediaRangeList.MediaRange>,Set<JakartaMediaRangeList.MediaRange>,SortedSet<JakartaMediaRangeList.MediaRange>
Facilitates parsing of the Accept HTTP request header.
See RFC 2616 section 14.1
- Since:
- 1.1.0
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassA codeMediaRangerepresents an entry in aMediaRangeList. -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionJakartaMediaRangeList(jakarta.servlet.http.HttpServletRequest request) Deprecated.UsefromRequest(HttpServletRequest)to read only the Accept header without allowing request parameters to override it.JakartaMediaRangeList(String listStr) Constructs aMediaRangeListusing a list of media ranges specified in ajava.lang.String. -
Method Summary
Modifier and TypeMethodDescriptionbooleanDetermines if this MediaRangeList contains a given media type.static @NotNull JakartaMediaRangeListfromRequest(@NotNull jakarta.servlet.http.HttpServletRequest request) Creates aMediaRangeListfrom theHEADER_ACCEPTheader of the supplied request.booleanWhether thisMediaRangeListmatches all media types.@Nullable StringDetermines which of themediaRangesspecifications is preferred by thisMediaRangeList.@Nullable JakartaMediaRangeList.MediaRangeprefer(@NotNull Set<JakartaMediaRangeList.MediaRange> mediaRanges) Given a list of media types, returns the one is preferred by thisMediaRangeList.Methods inherited from class java.util.TreeSet
add, addAll, ceiling, clear, clone, comparator, contains, descendingIterator, descendingSet, first, floor, headSet, headSet, higher, isEmpty, iterator, last, lower, pollFirst, pollLast, remove, size, spliterator, subSet, subSet, tailSet, tailSetMethods inherited from class java.util.AbstractSet
equals, hashCode, removeAllMethods inherited from class java.util.AbstractCollection
containsAll, retainAll, toArray, toArray, toStringMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
-
Field Details
-
HEADER_ACCEPT
The HTTP header name for the Accept header.- See Also:
-
PARAM_ACCEPT
Deprecated.UseHEADER_ACCEPTinstead.The request parameter name for the Accept header override.- See Also:
-
WILDCARD
The wildcard character for media ranges.- See Also:
-
CONTENT_TYPE_HTML
The media type for HTML content.- Since:
- 1.2.0
- See Also:
-
-
Constructor Details
-
JakartaMediaRangeList
Deprecated.UsefromRequest(HttpServletRequest)to read only the Accept header without allowing request parameters to override it.Constructs aMediaRangeListusing information from the suppliedHttpServletRequest. if the request contains aPARAM_ACCEPTquery parameter, the query parameter value overrides anyHEADER_ACCEPTheader value. If the request contains noPARAM_ACCEPTparameter, or the parameter value is empty, the value of theHEADER_ACCEPTis used. If both values are missing, it is assumed that the client accepts all media types, as per the RFC. See alsoJakartaMediaRangeList(java.lang.String)- Parameters:
request- TheHttpServletRequestto extract aMediaRangeListfrom
-
JakartaMediaRangeList
Constructs aMediaRangeListusing a list of media ranges specified in ajava.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.5text/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 aMediaRangeListfrom theHEADER_ACCEPTheader of the supplied request. Request parameters, includingPARAM_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 thisMediaRangeListmatches all media types.- Returns:
trueif thisMediaRangeListmatches all media types,falseotherwise- Since:
- 1.2.0
-
contains
Determines if this MediaRangeList contains a given media type.- Parameters:
mediaType- A string on the formtype/subtype. Neithertypeorsubtypeshould be wildcard (*).- Returns:
trueif thisMediaRangeListcontains a media type that matchesmediaType,falseotherwise- Throws:
IllegalArgumentException- ifmediaTypeis not on an accepted formNullPointerException- ifmediaTypeisnull
-
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 thisMediaRangeList. Wildcards are matched against the offered types. The most specific matching range, including media parameters beforeq, determines each type's quality. Types with quality zero are not acceptable. Accept extensions afterqdo 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 possibleJakartaMediaRangeList.MediaRanges- Returns:
- One of the
mediaRangesthat thisMediaRangeListprefers; ornullif thisMediaRangeListdoes not contain any of themediaRanges - Throws:
NullPointerException- ifmediaRangesisnullor contains anullvalue
-
prefer
Determines which of themediaRangesspecifications is preferred by thisMediaRangeList. Offered order breaks ties when multiple types match the same range.- Parameters:
mediaRanges- String representations ofMediaRanges. The strings must be on the form required byMediaRange(String)- Returns:
- the
toString()representation of the preferredMediaRange, ornullif thisMediaRangeListdoes not contain any of themediaRanges - See Also:
-
HEADER_ACCEPTinstead.