001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.http;
014
015import static org.apache.juneau.http.Constants.*;
016import static org.apache.juneau.internal.CollectionUtils.*;
017import static org.apache.juneau.internal.StringUtils.*;
018
019import java.util.*;
020
021import org.apache.juneau.http.annotation.*;
022import org.apache.juneau.internal.*;
023
024/**
025 * Represents a parsed <l>Accept</l> HTTP request header.
026 *
027 * <p>
028 * Content-Types that are acceptable for the response.
029 *
030 * <h5 class='figure'>Example</h5>
031 * <p class='bcode w800'>
032 *    Accept: text/plain
033 * </p>
034 *
035 * <h5 class='topic'>RFC2616 Specification</h5>
036 *
037 * The Accept request-header field can be used to specify certain media types which are acceptable for the response.
038 * Accept headers can be used to indicate that the request is specifically limited to a small set of desired types, as
039 * in the case of a request for an in-line image.
040 *
041 * <p class='bcode w800'>
042 *     Accept         = "Accept" ":
043 *                      #( media-range [ accept-params ] )
044 *
045 *     media-range    = ( "* /*"
046 *                      | ( type "/" "*" )
047 *                      | ( type "/" subtype )
048 *                      ) *( ";" parameter )
049 *     accept-params  = ";" "q" "=" qvalue *( accept-extension )
050 *     accept-extension = ";" token [ "=" ( token | quoted-string ) ]
051 * </p>
052 *
053 * <p>
054 * The asterisk "*" character is used to group media types into ranges, with "* /*" indicating all media types and
055 * "type/*" indicating all subtypes of that type.
056 * The media-range MAY include media type parameters that are applicable to that range.
057 *
058 * <p>
059 * Each media-range MAY be followed by one or more accept-params, beginning with the "q" parameter for indicating a
060 * relative quality factor.
061 * The first "q" parameter (if any) separates the media-range parameter(s) from the accept-params.
062 * Quality factors allow the user or user agent to indicate the relative degree of preference for that media-range,
063 * using the qvalue scale from 0 to 1 (section 3.9).
064 * The default value is q=1.
065 *
066 * <p>
067 * Note: Use of the "q" parameter name to separate media type parameters from Accept extension parameters is due to
068 * historical practice.
069 * Although this prevents any media type parameter named "q" from being used with a media range, such an event is
070 * believed to be unlikely given the lack of any "q" parameters in the IANA
071 * media type registry and the rare usage of any media type parameters in Accept.
072 * Future media types are discouraged from registering any parameter named "q".
073 *
074 * <p>
075 * The example
076 * <p class='bcode w800'>
077 *    Accept: audio/*; q=0.2, audio/basic
078 * </p>
079 * <p>
080 * SHOULD be interpreted as "I prefer audio/basic, but send me any audio type if it is the best available after an 80%
081 * mark-down in quality."
082 *
083 * <p>
084 * If no Accept header field is present, then it is assumed that the client accepts all media types.
085 *
086 * <p>
087 * If an Accept header field is present, and if the server cannot send a response which is acceptable according to the
088 * combined Accept field value, then the server SHOULD send a 406 (not acceptable) response.
089 *
090 * <p>
091 * A more elaborate example is
092 * <p class='bcode w800'>
093 *    Accept: text/plain; q=0.5, text/html,
094 *            text/x-dvi; q=0.8, text/x-c
095 * </p>
096 *
097 * <p>
098 * Verbally, this would be interpreted as "text/html and text/x-c are the preferred media types, but if they do not
099 * exist, then send the
100 * text/x-dvi entity, and if that does not exist, send the text/plain entity."
101 *
102 * <p>
103 * Media ranges can be overridden by more specific media ranges or specific media types.
104 * If more than one media range applies to a given type, the most specific reference has precedence.
105 * For example,
106 * <p class='bcode w800'>
107 *    Accept: text/ *, text/html, text/html;level=1, * /*
108 * </p>
109 * <p>
110 * have the following precedence:
111 * <ol>
112 *    <li>text/html;level=1
113 *    <li>text/html
114 *    <li>text/*
115 *    <li>* /*
116 * </ol>
117 *
118 * <p>
119 * The media type quality factor associated with a given type is determined by finding the media range with the highest
120 * precedence which matches that type.
121 * For example,
122 * <p class='bcode w800'>
123 *    Accept: text/*;q=0.3, text/html;q=0.7, text/html;level=1,
124 *            text/html;level=2;q=0.4, * /*;q=0.5
125 * </p>
126 * <p>
127 * would cause the following values to be associated:
128 * <p class='bcode w800'>
129 *    text/html;level=1         = 1
130 *    text/html                 = 0.7
131 *    text/plain                = 0.3
132 *    image/jpeg                = 0.5
133 *    text/html;level=2         = 0.4
134 *    text/html;level=3         = 0.7
135 * </p>
136 *
137 * <p>
138 * Note: A user agent might be provided with a default set of quality values for certain media ranges.
139 * However, unless the user agent is a closed system which cannot interact with other rendering agents, this default
140 * set ought to be configurable by the user.
141 *
142 * <ul class='seealso'>
143 *    <li class='extlink'>{@doc RFC2616}
144 * </ul>
145 */
146@Header("Accept")
147public final class Accept {
148
149   private static final Cache<String,Accept> cache = new Cache<>(NOCACHE, CACHE_MAX_SIZE);
150
151   /**
152    * Returns a parsed <c>Accept</c> header.
153    *
154    * @param value The <c>Accept</c> header string.
155    * @return The parsed <c>Accept</c> header, or <jk>null</jk> if the string was null.
156    */
157   public static Accept forString(String value) {
158      if (value == null)
159         return null;
160      Accept a = cache.get(value);
161      if (a == null)
162         a = cache.put(value, new Accept(value));
163      return a;
164   }
165
166
167   private final MediaTypeRange[] mediaRanges;
168   private final List<MediaTypeRange> mediaRangesList;
169
170   private Accept(String value) {
171      this.mediaRanges = MediaTypeRange.parse(value);
172      this.mediaRangesList = immutableList(mediaRanges);
173   }
174
175   /**
176    * Returns the list of the media ranges that make up this header.
177    *
178    * <p>
179    * The media ranges in the list are sorted by their q-value in descending order.
180    *
181    * @return An unmodifiable list of media ranges.
182    */
183   public List<MediaTypeRange> asRanges() {
184      return mediaRangesList;
185   }
186
187   /**
188    * Given a list of media types, returns the best match for this <c>Accept</c> header.
189    *
190    * <p>
191    * Note that fuzzy matching is allowed on the media types where the <c>Accept</c> header may
192    * contain additional subtype parts.
193    * <br>For example, given identical q-values and an <c>Accept</c> value of <js>"text/json+activity"</js>,
194    * the media type <js>"text/json"</js> will match if <js>"text/json+activity"</js> or <js>"text/activity+json"</js>
195    * isn't found.
196    * <br>The purpose for this is to allow serializers to match when artifacts such as <c>id</c> properties are
197    * present in the header.
198    *
199    * <p>
200    * See {@doc https://www.w3.org/TR/activitypub/#retrieving-objects ActivityPub / Retrieving Objects}
201    *
202    * @param mediaTypes The media types to match against.
203    * @return The index into the array of the best match, or <c>-1</c> if no suitable matches could be found.
204    */
205   public int findMatch(MediaType[] mediaTypes) {
206      int matchQuant = 0, matchIndex = -1;
207      float q = 0f;
208
209      // Media ranges are ordered by 'q'.
210      // So we only need to search until we've found a match.
211      for (MediaTypeRange mr : mediaRanges) {
212         float q2 = mr.getQValue();
213
214         if (q2 < q || q2 == 0)
215            break;
216
217         for (int i = 0; i < mediaTypes.length; i++) {
218            MediaType mt = mediaTypes[i];
219            int matchQuant2 = mr.getMediaType().match(mt, false);
220
221            if (matchQuant2 > matchQuant) {
222               matchIndex = i;
223               matchQuant = matchQuant2;
224               q = q2;
225            }
226         }
227      }
228
229      return matchIndex;
230   }
231
232   /**
233    * Same as {@link #findMatch(MediaType[])} but matching against media type ranges.
234    *
235    * <p>
236    * Note that the q-types on both the <c>mediaTypeRanges</c> parameter and this header
237    * are taken into account when trying to find the best match.
238    * <br>When both this header and the matching range have q-values, the q-value for the match is the result of multiplying them.
239    * <br>(e.g. Accept=<js>"text/html;q=0.9"</js> and mediaTypeRange=<js>"text/html;q=0.9"</js> ==>, q-value=<c>0.81</c>).
240    *
241    * @param mediaTypeRanges The media type ranges to match against.
242    * @return The index into the array of the best match, or <c>-1</c> if no suitable matches could be found.
243    */
244   public int findMatch(MediaTypeRange[] mediaTypeRanges) {
245      float matchQuant = 0;
246      int matchIndex = -1;
247      float q = 0f;
248
249      // Media ranges are ordered by 'q'.
250      // So we only need to search until we've found a match.
251      for (MediaTypeRange mr : mediaRanges) {
252         float q2 = mr.getQValue();
253
254         if (q2 < q || q2 == 0)
255            break;
256
257         for (int i = 0; i < mediaTypeRanges.length; i++) {
258            MediaTypeRange mt = mediaTypeRanges[i];
259            float matchQuant2 = mr.getMediaType().match(mt.getMediaType(), false) * mt.getQValue();
260
261            if (matchQuant2 > matchQuant) {
262               matchIndex = i;
263               matchQuant = matchQuant2;
264               q = q2;
265            }
266         }
267      }
268
269      return matchIndex;
270   }
271
272
273   /**
274    * Convenience method for searching through all of the subtypes of all the media ranges in this header for the
275    * presence of a subtype fragment.
276    *
277    * <p>
278    * For example, given the header <js>"text/json+activity"</js>, calling
279    * <code>hasSubtypePart(<js>"activity"</js>)</code> returns <jk>true</jk>.
280    *
281    * @param part The media type subtype fragment.
282    * @return <jk>true</jk> if subtype fragment exists.
283    */
284   public boolean hasSubtypePart(String part) {
285
286      for (MediaTypeRange mr : this.mediaRanges)
287         if (mr.getQValue() > 0 && mr.getMediaType().getSubTypes().indexOf(part) >= 0)
288            return true;
289
290      return false;
291   }
292
293   @Override /* Object */
294   public String toString() {
295      return join(mediaRanges, ',');
296   }
297}