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.internal.*;
022
023/**
024 * Represents a parsed <l>Accept</l> HTTP request header.
025 * 
026 * <p>
027 * Content-Types that are acceptable for the response.
028 * 
029 * <h5 class='figure'>Example</h5>
030 * <p class='bcode'>
031 *    Accept: text/plain
032 * </p>
033 * 
034 * <h5 class='topic'>RFC2616 Specification</h5>
035 * 
036 * The Accept request-header field can be used to specify certain media types which are acceptable for the response.
037 * Accept headers can be used to indicate that the request is specifically limited to a small set of desired types, as
038 * in the case of a request for an in-line image.
039 * 
040 * <p class='bcode'>
041 *     Accept         = "Accept" ":
042 *                      #( media-range [ accept-params ] )
043 * 
044 *     media-range    = ( "* /*"
045 *                      | ( type "/" "*" )
046 *                      | ( type "/" subtype )
047 *                      ) *( ";" parameter )
048 *     accept-params  = ";" "q" "=" qvalue *( accept-extension )
049 *     accept-extension = ";" token [ "=" ( token | quoted-string ) ]
050 * </p>
051 * 
052 * <p>
053 * The asterisk "*" character is used to group media types into ranges, with "* /*" indicating all media types and
054 * "type/*" indicating all subtypes of that type.
055 * The media-range MAY include media type parameters that are applicable to that range.
056 * 
057 * <p>
058 * Each media-range MAY be followed by one or more accept-params, beginning with the "q" parameter for indicating a
059 * relative quality factor.
060 * The first "q" parameter (if any) separates the media-range parameter(s) from the accept-params.
061 * Quality factors allow the user or user agent to indicate the relative degree of preference for that media-range,
062 * using the qvalue scale from 0 to 1 (section 3.9).
063 * The default value is q=1.
064 * 
065 * <p>
066 * Note: Use of the "q" parameter name to separate media type parameters from Accept extension parameters is due to
067 * historical practice.
068 * Although this prevents any media type parameter named "q" from being used with a media range, such an event is
069 * believed to be unlikely given the lack of any "q" parameters in the IANA
070 * media type registry and the rare usage of any media type parameters in Accept.
071 * Future media types are discouraged from registering any parameter named "q".
072 * 
073 * <p>
074 * The example
075 * <p class='bcode'>
076 *    Accept: audio/*; q=0.2, audio/basic
077 * </p>
078 * <p>
079 * SHOULD be interpreted as "I prefer audio/basic, but send me any audio type if it is the best available after an 80%
080 * mark-down in quality."
081 * 
082 * <p>
083 * If no Accept header field is present, then it is assumed that the client accepts all media types.
084 * 
085 * <p>
086 * If an Accept header field is present, and if the server cannot send a response which is acceptable according to the
087 * combined Accept field value, then the server SHOULD send a 406 (not acceptable) response.
088 * 
089 * <p>
090 * A more elaborate example is
091 * <p class='bcode'>
092 *    Accept: text/plain; q=0.5, text/html,
093 *            text/x-dvi; q=0.8, text/x-c
094 * </p>
095 * 
096 * <p>
097 * Verbally, this would be interpreted as "text/html and text/x-c are the preferred media types, but if they do not
098 * exist, then send the
099 * text/x-dvi entity, and if that does not exist, send the text/plain entity."
100 * 
101 * <p>
102 * Media ranges can be overridden by more specific media ranges or specific media types.
103 * If more than one media range applies to a given type, the most specific reference has precedence.
104 * For example,
105 * <p class='bcode'>
106 *    Accept: text/ *, text/html, text/html;level=1, * /*
107 * </p>
108 * <p>
109 * have the following precedence:
110 * <ol>
111 *    <li>text/html;level=1
112 *    <li>text/html
113 *    <li>text/*
114 *    <li>* /*
115 * </ol>
116 * 
117 * <p>
118 * The media type quality factor associated with a given type is determined by finding the media range with the highest
119 * precedence which matches that type.
120 * For example,
121 * <p class='bcode'>
122 *    Accept: text/*;q=0.3, text/html;q=0.7, text/html;level=1,
123 *            text/html;level=2;q=0.4, * /*;q=0.5
124 * </p>
125 * <p>
126 * would cause the following values to be associated:
127 * <p class='bcode'>
128 *    text/html;level=1         = 1
129 *    text/html                 = 0.7
130 *    text/plain                = 0.3
131 *    image/jpeg                = 0.5
132 *    text/html;level=2         = 0.4
133 *    text/html;level=3         = 0.7
134 * </p>
135 * 
136 * <p>
137 * Note: A user agent might be provided with a default set of quality values for certain media ranges.
138 * However, unless the user agent is a closed system which cannot interact with other rendering agents, this default
139 * set ought to be configurable by the user.
140 * 
141 * <h5 class='section'>See Also:</h5>
142 * <ul class='doctree'>
143 *    <li class='extlink'><a class='doclink' href='https://www.w3.org/Protocols/rfc2616/rfc2616.html'>Hypertext Transfer Protocol -- HTTP/1.1</a>
144 * </ul>
145 */
146public final class Accept {
147
148   private static final Cache<String,Accept> cache = new Cache<>(NOCACHE, CACHE_MAX_SIZE);
149
150   /**
151    * Returns a parsed <code>Accept</code> header.
152    * 
153    * @param value The <code>Accept</code> header string.
154    * @return The parsed <code>Accept</code> header, or <jk>null</jk> if the string was null.
155    */
156   public static Accept forString(String value) {
157      if (value == null)
158         return null;
159      Accept a = cache.get(value);
160      if (a == null)
161         a = cache.put(value, new Accept(value));
162      return a;
163   }
164
165
166   private final MediaTypeRange[] mediaRanges;
167   private final List<MediaTypeRange> mediaRangesList;
168
169   private Accept(String value) {
170      this.mediaRanges = MediaTypeRange.parse(value);
171      this.mediaRangesList = immutableList(mediaRanges);
172   }
173
174   /**
175    * Returns the list of the media ranges that make up this header.
176    * 
177    * <p>
178    * The media ranges in the list are sorted by their q-value in descending order.
179    * 
180    * @return An unmodifiable list of media ranges.
181    */
182   public List<MediaTypeRange> asRanges() {
183      return mediaRangesList;
184   }
185
186   /**
187    * Given a list of media types, returns the best match for this <code>Accept</code> header.
188    * 
189    * <p>
190    * Note that fuzzy matching is allowed on the media types where the <code>Accept</code> header may
191    * contain additional subtype parts.
192    * <br>For example, given identical q-values and an <code>Accept</code> value of <js>"text/json+activity"</js>,
193    * the media type <js>"text/json"</js> will match if <js>"text/json+activity"</js> or <js>"text/activity+json"</js>
194    * isn't found.
195    * <br>The purpose for this is to allow serializers to match when artifacts such as <code>id</code> properties are
196    * present in the header.
197    * 
198    * <p>
199    * See <a class='doclink' href='https://www.w3.org/TR/activitypub/#retrieving-objects'>
200    * ActivityPub / Retrieving Objects</a>
201    * 
202    * @param mediaTypes The media types to match against.
203    * @return The index into the array of the best match, or <code>-1</code> 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    * Convenience method for searching through all of the subtypes of all the media ranges in this header for the
234    * presence of a subtype fragment.
235    * 
236    * <p>
237    * For example, given the header <js>"text/json+activity"</js>, calling
238    * <code>hasSubtypePart(<js>"activity"</js>)</code> returns <jk>true</jk>.
239    * 
240    * @param part The media type subtype fragment.
241    * @return <jk>true</jk> if subtype fragment exists.
242    */
243   public boolean hasSubtypePart(String part) {
244
245      for (MediaTypeRange mr : this.mediaRanges)
246         if (mr.getQValue() > 0 && mr.getMediaType().getSubTypes().indexOf(part) >= 0)
247            return true;
248
249      return false;
250   }
251
252   @Override /* Object */
253   public String toString() {
254      return join(mediaRanges, ',');
255   }
256}