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.commons.collections.*;
023import org.apache.juneau.http.annotation.*;
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   private static final long serialVersionUID = 1L;
153   private static final String NAME = "Accept";
154
155   private static final Cache<String,Accept> CACHE = Cache.of(String.class, Accept.class).build();
156
157   // Constants
158   @SuppressWarnings("javadoc")
159   // @formatter:off
160   public static final Accept
161      APPLICATION_ATOM_XML = of("application/atom+xml"),
162      APPLICATION_FORM_URLENCODED = of("application/x-www-form-urlencoded"),
163      APPLICATION_JSON = of("application/json"),
164      APPLICATION_OCTET_STREAM = of("application/octet-stream"),
165      APPLICATION_SOAP_XML = of("application/soap+xml"),
166      APPLICATION_SVG_XML = of("application/svg+xml"),
167      APPLICATION_XHTML_XML = of("application/xhtml+xml"),
168      APPLICATION_XML = of("application/xml"),
169      IMAGE_BMP = of("image/bmp"),
170      IMAGE_GIF = of("image/gif"),
171      IMAGE_JPEG = of("image/jpeg"),
172      IMAGE_PNG = of("image/png"),
173      IMAGE_SVG = of("image/svg+xml"),
174      IMAGE_TIFF = of("image/tiff"),
175      IMAGE_WEBP = of("image/webp"),
176      MULTIPART_FORM_DATA = of("multipart/form-data"),
177      TEXT_HTML = of("text/html"),
178      TEXT_PLAIN = of("text/plain"),
179      TEXT_XML = of("text/xml"),
180      WILDCARD = of("*/*"),
181      NULL = new Accept((String)null);
182   // @formatter:on
183
184   /**
185    * Static creator.
186    *
187    * @param value
188    *    The header value.
189    *    <br>Can be <jk>null</jk>.
190    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
191    */
192   public static Accept of(MediaRanges value) {
193      return value == null ? null : new Accept(value);
194   }
195
196   /**
197    * Static creator.
198    *
199    * @param value
200    *    The header value.
201    *    <br>Can be <jk>null</jk>.
202    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
203    */
204   public static Accept of(MediaType value) {
205      return value == null ? null : new Accept(value.toString());
206   }
207
208   /**
209    * Static creator.
210    *
211    * @param value
212    *    The header value.
213    *    <br>Must be parsable by {@link MediaRanges#of(String)}.
214    *    <br>Can be <jk>null</jk>.
215    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
216    */
217   public static Accept of(String value) {
218      return value == null ? null : CACHE.get(value, () -> new Accept(value));
219   }
220
221   /**
222    * Static creator with delayed value.
223    *
224    * <p>
225    * Header value is re-evaluated on each call to {@link #getValue()}.
226    *
227    * @param value
228    *    The header value.
229    *    <br>Can be <jk>null</jk>.
230    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
231    */
232   public static Accept of(Supplier<MediaRanges> value) {
233      return value == null ? null : new Accept(value);
234   }
235
236   /**
237    * Constructor.
238    *
239    * @param value
240    *    The header value.
241    *    <br>Can be <jk>null</jk>.
242    */
243   public Accept(MediaRanges value) {
244      super(NAME, value);
245   }
246
247   /**
248    * Constructor.
249    *
250    * @param value
251    *    The header value.
252    *    <br>Must be parsable by {@link MediaRanges#of(String)}.
253    *    <br>Can be <jk>null</jk>.
254    */
255   public Accept(String value) {
256      super(NAME, value);
257   }
258
259   /**
260    * Constructor with delayed value.
261    *
262    * <p>
263    * Header value is re-evaluated on each call to {@link #getValue()}.
264    *
265    * @param value
266    *    The supplier of the header value.
267    *    <br>Can be <jk>null</jk>.
268    */
269   public Accept(Supplier<MediaRanges> value) {
270      super(NAME, value);
271   }
272}