001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.http.header;
018
019import java.util.function.*;
020
021import org.apache.juneau.*;
022import org.apache.juneau.http.annotation.*;
023import org.apache.juneau.internal.*;
024
025/**
026 * Represents a parsed <l>Accept</l> HTTP request header.
027 *
028 * <p>
029 * Content-Types that are acceptable for the response.
030 *
031 * <h5 class='figure'>Example</h5>
032 * <p class='bcode'>
033 *    Accept: text/plain
034 * </p>
035 *
036 * <h5 class='topic'>RFC2616 Specification</h5>
037 *
038 * The Accept request-header field can be used to specify certain media types which are acceptable for the response.
039 * Accept headers can be used to indicate that the request is specifically limited to a small set of desired types, as
040 * in the case of a request for an in-line image.
041 *
042 * <p class='bcode'>
043 *     Accept         = "Accept" ":
044 *                      #( media-range [ accept-params ] )
045 *
046 *     media-range    = ( "* /*"
047 *                      | ( type "/" "*" )
048 *                      | ( type "/" subtype )
049 *                      ) *( ";" parameter )
050 *     accept-params  = ";" "q" "=" qvalue *( accept-extension )
051 *     accept-extension = ";" token [ "=" ( token | quoted-string ) ]
052 * </p>
053 *
054 * <p>
055 * The asterisk "*" character is used to group media types into ranges, with "* /*" indicating all media types and
056 * "type/*" indicating all subtypes of that type.
057 * The media-range MAY include media type parameters that are applicable to that range.
058 *
059 * <p>
060 * Each media-range MAY be followed by one or more accept-params, beginning with the "q" parameter for indicating a
061 * relative quality factor.
062 * The first "q" parameter (if any) separates the media-range parameter(s) from the accept-params.
063 * Quality factors allow the user or user agent to indicate the relative degree of preference for that media-range,
064 * using the qvalue scale from 0 to 1 (section 3.9).
065 * The default value is q=1.
066 *
067 * <p>
068 * Note: Use of the "q" parameter name to separate media type parameters from Accept extension parameters is due to
069 * historical practice.
070 * Although this prevents any media type parameter named "q" from being used with a media range, such an event is
071 * believed to be unlikely given the lack of any "q" parameters in the IANA
072 * media type registry and the rare usage of any media type parameters in Accept.
073 * Future media types are discouraged from registering any parameter named "q".
074 *
075 * <p>
076 * The example
077 * <p class='bcode'>
078 *    Accept: audio/*; q=0.2, audio/basic
079 * </p>
080 * <p>
081 * SHOULD be interpreted as "I prefer audio/basic, but send me any audio type if it is the best available after an 80%
082 * mark-down in quality."
083 *
084 * <p>
085 * If no Accept header field is present, then it is assumed that the client accepts all media types.
086 *
087 * <p>
088 * If an Accept header field is present, and if the server cannot send a response which is acceptable according to the
089 * combined Accept field value, then the server SHOULD send a 406 (not acceptable) response.
090 *
091 * <p>
092 * A more elaborate example is
093 * <p class='bcode'>
094 *    Accept: text/plain; q=0.5, text/html,
095 *            text/x-dvi; q=0.8, text/x-c
096 * </p>
097 *
098 * <p>
099 * Verbally, this would be interpreted as "text/html and text/x-c are the preferred media types, but if they do not
100 * exist, then send the
101 * text/x-dvi entity, and if that does not exist, send the text/plain entity."
102 *
103 * <p>
104 * Media ranges can be overridden by more specific media ranges or specific media types.
105 * If more than one media range applies to a given type, the most specific reference has precedence.
106 * For example,
107 * <p class='bcode'>
108 *    Accept: text/ *, text/html, text/html;level=1, * /*
109 * </p>
110 * <p>
111 * have the following precedence:
112 * <ol>
113 *    <li>text/html;level=1
114 *    <li>text/html
115 *    <li>text/*
116 *    <li>* /*
117 * </ol>
118 *
119 * <p>
120 * The media type quality factor associated with a given type is determined by finding the media range with the highest
121 * precedence which matches that type.
122 * For example,
123 * <p class='bcode'>
124 *    Accept: text/*;q=0.3, text/html;q=0.7, text/html;level=1,
125 *            text/html;level=2;q=0.4, * /*;q=0.5
126 * </p>
127 * <p>
128 * would cause the following values to be associated:
129 * <p class='bcode'>
130 *    text/html;level=1         = 1
131 *    text/html                 = 0.7
132 *    text/plain                = 0.3
133 *    image/jpeg                = 0.5
134 *    text/html;level=2         = 0.4
135 *    text/html;level=3         = 0.7
136 * </p>
137 *
138 * <p>
139 * Note: A user agent might be provided with a default set of quality values for certain media ranges.
140 * However, unless the user agent is a closed system which cannot interact with other rendering agents, this default
141 * set ought to be configurable by the user.
142 *
143 * <h5 class='section'>See Also:</h5><ul>
144 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestCommonBasics">juneau-rest-common Basics</a>
145 *    <li class='extlink'><a class="doclink" href="https://www.w3.org/Protocols/rfc2616/rfc2616.html">Hypertext Transfer Protocol -- HTTP/1.1</a>
146 * </ul>
147 *
148 * @serial exclude
149 */
150@Header("Accept")
151public class Accept extends BasicMediaRangesHeader {
152
153   //-----------------------------------------------------------------------------------------------------------------
154   // Static
155   //-----------------------------------------------------------------------------------------------------------------
156
157   private static final long serialVersionUID = 1L;
158   private static final String NAME = "Accept";
159
160   private static final Cache<String,Accept> CACHE = Cache.of(String.class, Accept.class).build();
161
162   // Constants
163   @SuppressWarnings("javadoc")
164   public static final Accept
165      APPLICATION_ATOM_XML = of("application/atom+xml"),
166      APPLICATION_FORM_URLENCODED = of("application/x-www-form-urlencoded"),
167      APPLICATION_JSON = of("application/json"),
168      APPLICATION_OCTET_STREAM = of("application/octet-stream"),
169      APPLICATION_SOAP_XML = of("application/soap+xml"),
170      APPLICATION_SVG_XML = of("application/svg+xml"),
171      APPLICATION_XHTML_XML = of("application/xhtml+xml"),
172      APPLICATION_XML = of("application/xml"),
173      IMAGE_BMP = of("image/bmp"),
174      IMAGE_GIF = of("image/gif"),
175      IMAGE_JPEG = of("image/jpeg"),
176      IMAGE_PNG = of("image/png"),
177      IMAGE_SVG = of("image/svg+xml"),
178      IMAGE_TIFF = of("image/tiff"),
179      IMAGE_WEBP = of("image/webp"),
180      MULTIPART_FORM_DATA = of("multipart/form-data"),
181      TEXT_HTML = of("text/html"),
182      TEXT_PLAIN = of("text/plain"),
183      TEXT_XML = of("text/xml"),
184      WILDCARD = of("*/*"),
185      NULL = new Accept((String)null);
186
187   /**
188    * Static creator.
189    *
190    * @param value
191    *    The header value.
192    *    <br>Must be parsable by {@link MediaRanges#of(String)}.
193    *    <br>Can be <jk>null</jk>.
194    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
195    */
196   public static Accept of(String value) {
197      return value == null ? null : CACHE.get(value, ()->new Accept(value));
198   }
199
200   /**
201    * Static creator.
202    *
203    * @param value
204    *    The header value.
205    *    <br>Can be <jk>null</jk>.
206    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
207    */
208   public static Accept of(MediaRanges value) {
209      return value == null ? null : new Accept(value);
210   }
211
212   /**
213    * Static creator.
214    *
215    * @param value
216    *    The header value.
217    *    <br>Can be <jk>null</jk>.
218    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
219    */
220   public static Accept of(MediaType value) {
221      return value == null ? null : new Accept(value.toString());
222   }
223
224   /**
225    * Static creator with delayed value.
226    *
227    * <p>
228    * Header value is re-evaluated on each call to {@link #getValue()}.
229    *
230    * @param value
231    *    The header value.
232    *    <br>Can be <jk>null</jk>.
233    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
234    */
235   public static Accept of(Supplier<MediaRanges> value) {
236      return value == null ? null : new Accept(value);
237   }
238
239   //-----------------------------------------------------------------------------------------------------------------
240   // Instance
241   //-----------------------------------------------------------------------------------------------------------------
242
243   /**
244    * Constructor.
245    *
246    * @param value
247    *    The header value.
248    *    <br>Must be parsable by {@link MediaRanges#of(String)}.
249    *    <br>Can be <jk>null</jk>.
250    */
251   public Accept(String value) {
252      super(NAME, value);
253   }
254
255   /**
256    * Constructor.
257    *
258    * @param value
259    *    The header value.
260    *    <br>Can be <jk>null</jk>.
261    */
262   public Accept(MediaRanges value) {
263      super(NAME, value);
264   }
265
266   /**
267    * Constructor with delayed value.
268    *
269    * <p>
270    * Header value is re-evaluated on each call to {@link #getValue()}.
271    *
272    * @param value
273    *    The supplier of the header value.
274    *    <br>Can be <jk>null</jk>.
275    */
276   public Accept(Supplier<MediaRanges> value) {
277      super(NAME, value);
278   }
279}