1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.juneau;
18
19 import static org.apache.juneau.commons.utils.CollectionUtils.*;
20 import static org.apache.juneau.commons.utils.StringUtils.*;
21 import static org.apache.juneau.commons.utils.Utils.*;
22
23 import java.util.*;
24 import java.util.function.*;
25
26 import org.apache.http.*;
27 import org.apache.http.message.*;
28 import org.apache.juneau.annotation.*;
29 import org.apache.juneau.commons.collections.*;
30
31 /**
32 * A parsed <c>Accept</c> or similar header value.
33 *
34 * <p>
35 * The returned media ranges are sorted such that the most acceptable media is available at ordinal position
36 * <js>'0'</js>, and the least acceptable at position n-1.
37 *
38 * <p>
39 * The syntax expected to be found in the referenced <c>value</c> complies with the syntax described in
40 * RFC2616, Section 14.1, as described below:
41 * <p class='bcode'>
42 * Accept = "Accept" ":"
43 * #( media-range [ accept-params ] )
44 *
45 * media-range = ( "*\/*"
46 * | ( type "/" "*" )
47 * | ( type "/" subtype )
48 * ) *( ";" parameter )
49 * accept-params = ";" "q" "=" qvalue *( accept-extension )
50 * accept-extension = ";" token [ "=" ( token | quoted-string ) ]
51 * </p>
52 *
53 * <h5 class='section'>See Also:</h5><ul>
54 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestCommonBasics">juneau-rest-common Basics</a>
55 * <li class='extlink'><a class="doclink" href="https://www.w3.org/Protocols/rfc2616/rfc2616.html">Hypertext Transfer Protocol -- HTTP/1.1</a>
56 * </ul>
57 */
58 @BeanIgnore
59 public class MediaRanges {
60 /** Represents an empty media ranges object. */
61 public static final MediaRanges EMPTY = new MediaRanges("");
62
63 private static final Cache<String,MediaRanges> CACHE = Cache.of(String.class, MediaRanges.class).build();
64
65 /**
66 * Compares two MediaRanges for equality.
67 *
68 * <p>
69 * The values are first compared according to <c>qValue</c> values.
70 * Should those values be equal, the <c>type</c> is then lexicographically compared (case-insensitive) in
71 * ascending order, with the <js>"*"</js> type demoted last in that order.
72 * <c>MediaRanges</c> with the same type but different sub-types are compared - a more specific subtype is
73 * promoted over the 'wildcard' subtype.
74 * <c>MediaRanges</c> with the same types but with extensions are promoted over those same types with no
75 * extensions.
76 */
77 private static final Comparator<MediaRange> RANGE_COMPARATOR = (o1, o2) -> {
78 // Compare q-values.
79 var qCompare = Float.compare(o2.getQValue(), o1.getQValue());
80 if (qCompare != 0)
81 return qCompare;
82
83 // Compare media-types.
84 // Note that '*' comes alphabetically before letters, so just do a reverse-alphabetical comparison.
85 return o2.toString().compareTo(o1.toString());
86 };
87
88 /**
89 * Returns a parsed <c>Accept</c> header value.
90 *
91 * @param value The raw <c>Accept</c> header value.
92 * @return A parsed <c>Accept</c> header value.
93 */
94 public static MediaRanges of(String value) {
95 return isEmpty(value) ? EMPTY : CACHE.get(value, () -> new MediaRanges(value));
96 }
97
98 private static HeaderElement[] parse(String value) {
99 return BasicHeaderValueParser.parseElements(emptyIfNull(trim(value)), null);
100 }
101
102 private final MediaRange[] ranges;
103
104 private final String string;
105
106 /**
107 * Constructor.
108 *
109 * @param e The parsed header value.
110 */
111 public MediaRanges(HeaderElement[] e) {
112
113 ranges = new MediaRange[e.length];
114 for (var i = 0; i < e.length; i++)
115 ranges[i] = new MediaRange(e[i]);
116 Arrays.sort(ranges, RANGE_COMPARATOR);
117
118 this.string = ranges.length == 1 ? ranges[0].toString() : join(ranges, ',');
119 }
120
121 /**
122 * Constructor.
123 *
124 * @param value The <c>Accept</c> header value.
125 */
126 public MediaRanges(String value) {
127 this(parse(value));
128 }
129
130 /**
131 * Performs an action on the media ranges that make up this object.
132 *
133 * @param action The action to perform.
134 * @return This object.
135 */
136 public MediaRanges forEachRange(Consumer<MediaRange> action) {
137 for (var r : ranges)
138 action.accept(r);
139 return this;
140 }
141
142 /**
143 * Returns the {@link MediaRange} at the specified index.
144 *
145 * @param index The index position of the media range.
146 * @return The {@link MediaRange} at the specified index or <jk>null</jk> if the index is out of range.
147 */
148 public MediaRange getRange(int index) {
149 if (index < 0 || index >= ranges.length)
150 return null;
151 return ranges[index];
152 }
153
154 /**
155 * Convenience method for searching through all of the subtypes of all the media ranges in this header for the
156 * presence of a subtype fragment.
157 *
158 * <p>
159 * For example, given the header <js>"text/json+activity"</js>, calling
160 * <code>hasSubtypePart(<js>"activity"</js>)</code> returns <jk>true</jk>.
161 *
162 * @param part The media type subtype fragment.
163 * @return <jk>true</jk> if subtype fragment exists.
164 */
165 public boolean hasSubtypePart(String part) {
166
167 for (var mr : ranges)
168 if (mr.getQValue() > 0 && mr.getSubTypes().indexOf(part) >= 0)
169 return true;
170
171 return false;
172 }
173
174 /**
175 * Given a list of media types, returns the best match for this <c>Accept</c> header.
176 *
177 * <p>
178 * Note that fuzzy matching is allowed on the media types where the <c>Accept</c> header may
179 * contain additional subtype parts.
180 * <br>For example, given identical q-values and an <c>Accept</c> value of <js>"text/json+activity"</js>,
181 * the media type <js>"text/json"</js> will match if <js>"text/json+activity"</js> or <js>"text/activity+json"</js>
182 * isn't found.
183 * <br>The purpose for this is to allow serializers to match when artifacts such as <c>id</c> properties are
184 * present in the header.
185 *
186 * <p>
187 * See <a class="doclink" href="https://www.w3.org/TR/activitypub/#retrieving-objects">ActivityPub / Retrieving Objects</a>
188 *
189 * @param mediaTypes The media types to match against.
190 * @return The index into the array of the best match, or <c>-1</c> if no suitable matches could be found.
191 */
192 public int match(List<? extends MediaType> mediaTypes) {
193 if (string.isEmpty() || mediaTypes == null)
194 return -1;
195
196 int matchQuant = 0;
197 int matchIndex = -1;
198 var q = 0f;
199
200 // Media ranges are ordered by 'q'.
201 // So we only need to search until we've found a match.
202 for (var mr : ranges) {
203 var q2 = mr.getQValue();
204
205 if (q2 < q || q2 == 0)
206 break;
207
208 for (var i = 0; i < mediaTypes.size(); i++) {
209 var mt = mediaTypes.get(i);
210 var matchQuant2 = mr.match(mt, false);
211
212 if (matchQuant2 > matchQuant) {
213 matchIndex = i;
214 matchQuant = matchQuant2;
215 q = q2;
216 }
217 }
218 }
219
220 return matchIndex;
221 }
222
223 /**
224 * Returns the media ranges that make up this object.
225 *
226 * @return The media ranges that make up this object.
227 */
228 public List<MediaRange> toList() {
229 return l(ranges);
230 }
231
232 @Override /* Overridden from Object */
233 public String toString() {
234 return string;
235 }
236 }