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.header;
014
015import static org.apache.juneau.http.Constants.*;
016
017import java.util.function.*;
018
019import org.apache.juneau.http.annotation.*;
020import org.apache.juneau.internal.*;
021
022/**
023 * Represents a parsed <l>Accept</l> HTTP request header.
024 *
025 * <p>
026 * Content-Types that are acceptable for the response.
027 *
028 * <h5 class='figure'>Example</h5>
029 * <p class='bcode w800'>
030 *    Accept: text/plain
031 * </p>
032 *
033 * <h5 class='topic'>RFC2616 Specification</h5>
034 *
035 * The Accept request-header field can be used to specify certain media types which are acceptable for the response.
036 * Accept headers can be used to indicate that the request is specifically limited to a small set of desired types, as
037 * in the case of a request for an in-line image.
038 *
039 * <p class='bcode w800'>
040 *     Accept         = "Accept" ":
041 *                      #( media-range [ accept-params ] )
042 *
043 *     media-range    = ( "* /*"
044 *                      | ( type "/" "*" )
045 *                      | ( type "/" subtype )
046 *                      ) *( ";" parameter )
047 *     accept-params  = ";" "q" "=" qvalue *( accept-extension )
048 *     accept-extension = ";" token [ "=" ( token | quoted-string ) ]
049 * </p>
050 *
051 * <p>
052 * The asterisk "*" character is used to group media types into ranges, with "* /*" indicating all media types and
053 * "type/*" indicating all subtypes of that type.
054 * The media-range MAY include media type parameters that are applicable to that range.
055 *
056 * <p>
057 * Each media-range MAY be followed by one or more accept-params, beginning with the "q" parameter for indicating a
058 * relative quality factor.
059 * The first "q" parameter (if any) separates the media-range parameter(s) from the accept-params.
060 * Quality factors allow the user or user agent to indicate the relative degree of preference for that media-range,
061 * using the qvalue scale from 0 to 1 (section 3.9).
062 * The default value is q=1.
063 *
064 * <p>
065 * Note: Use of the "q" parameter name to separate media type parameters from Accept extension parameters is due to
066 * historical practice.
067 * Although this prevents any media type parameter named "q" from being used with a media range, such an event is
068 * believed to be unlikely given the lack of any "q" parameters in the IANA
069 * media type registry and the rare usage of any media type parameters in Accept.
070 * Future media types are discouraged from registering any parameter named "q".
071 *
072 * <p>
073 * The example
074 * <p class='bcode w800'>
075 *    Accept: audio/*; q=0.2, audio/basic
076 * </p>
077 * <p>
078 * SHOULD be interpreted as "I prefer audio/basic, but send me any audio type if it is the best available after an 80%
079 * mark-down in quality."
080 *
081 * <p>
082 * If no Accept header field is present, then it is assumed that the client accepts all media types.
083 *
084 * <p>
085 * If an Accept header field is present, and if the server cannot send a response which is acceptable according to the
086 * combined Accept field value, then the server SHOULD send a 406 (not acceptable) response.
087 *
088 * <p>
089 * A more elaborate example is
090 * <p class='bcode w800'>
091 *    Accept: text/plain; q=0.5, text/html,
092 *            text/x-dvi; q=0.8, text/x-c
093 * </p>
094 *
095 * <p>
096 * Verbally, this would be interpreted as "text/html and text/x-c are the preferred media types, but if they do not
097 * exist, then send the
098 * text/x-dvi entity, and if that does not exist, send the text/plain entity."
099 *
100 * <p>
101 * Media ranges can be overridden by more specific media ranges or specific media types.
102 * If more than one media range applies to a given type, the most specific reference has precedence.
103 * For example,
104 * <p class='bcode w800'>
105 *    Accept: text/ *, text/html, text/html;level=1, * /*
106 * </p>
107 * <p>
108 * have the following precedence:
109 * <ol>
110 *    <li>text/html;level=1
111 *    <li>text/html
112 *    <li>text/*
113 *    <li>* /*
114 * </ol>
115 *
116 * <p>
117 * The media type quality factor associated with a given type is determined by finding the media range with the highest
118 * precedence which matches that type.
119 * For example,
120 * <p class='bcode w800'>
121 *    Accept: text/*;q=0.3, text/html;q=0.7, text/html;level=1,
122 *            text/html;level=2;q=0.4, * /*;q=0.5
123 * </p>
124 * <p>
125 * would cause the following values to be associated:
126 * <p class='bcode w800'>
127 *    text/html;level=1         = 1
128 *    text/html                 = 0.7
129 *    text/plain                = 0.3
130 *    image/jpeg                = 0.5
131 *    text/html;level=2         = 0.4
132 *    text/html;level=3         = 0.7
133 * </p>
134 *
135 * <p>
136 * Note: A user agent might be provided with a default set of quality values for certain media ranges.
137 * However, unless the user agent is a closed system which cannot interact with other rendering agents, this default
138 * set ought to be configurable by the user.
139 *
140 * <ul class='seealso'>
141 *    <li class='extlink'>{@doc ExtRFC2616}
142 * </ul>
143 */
144@Header("Accept")
145public class Accept extends BasicMediaRangeArrayHeader {
146
147   private static final long serialVersionUID = 1L;
148
149   private static final Cache<String,Accept> CACHE = new Cache<>(NOCACHE, CACHE_MAX_SIZE);
150
151   /**
152    * Returns a parsed and cached header.
153    *
154    * @param value
155    *    The header value.
156    * @return A cached {@link AcceptCharset} object.
157    */
158   public static Accept of(String value) {
159      if (value == null)
160         return null;
161      Accept x = CACHE.get(value);
162      if (x == null)
163         x = CACHE.put(value, new Accept(value));
164      return x;
165   }
166
167   /**
168    * Convenience creator.
169    *
170    * @param value
171    *    The header value.
172    *    <br>Can be any of the following:
173    *    <ul>
174    *       <li>{@link String}
175    *       <li>Anything else - Converted to <c>String</c> then parsed.
176    *    </ul>
177    * @return A new {@link Accept} object.
178    */
179   public static Accept of(Object value) {
180      if (value == null)
181         return null;
182      return new Accept(value);
183   }
184
185   /**
186    * Convenience creator using supplier.
187    *
188    * <p>
189    * Header value is re-evaluated on each call to {@link #getValue()}.
190    *
191    * @param value
192    *    The header value supplier.
193    *    <br>Can be any of the following:
194    *    <ul>
195    *       <li>{@link String}
196    *       <li>Anything else - Converted to <c>String</c> then parsed.
197    *    </ul>
198    * @return A new {@link Accept} object.
199    */
200   public static Accept of(Supplier<?> value) {
201      if (value == null)
202         return null;
203      return new Accept(value);
204   }
205
206   /**
207    * Constructor
208    *
209    * @param value
210    *    The header value.
211    *    <br>Can be any of the following:
212    *    <ul>
213    *       <li>{@link String}
214    *       <li>Anything else - Converted to <c>String</c> then parsed.
215    *       <li>A {@link Supplier} of anything on this list.
216    *    </ul>
217    */
218   public Accept(Object value) {
219      super("Accept", value);
220   }
221
222   /**
223    * Constructor
224    *
225    * @param value
226    *    The header value.
227    */
228   public Accept(String value) {
229      this((Object)value);
230   }
231}