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 java.util.function.*;
016
017import org.apache.juneau.*;
018import org.apache.juneau.http.annotation.*;
019import org.apache.juneau.internal.*;
020
021/**
022 * Represents a parsed <l>Accept-Encoding</l> HTTP request header.
023 *
024 * <p>
025 * List of acceptable encodings.
026 *
027 * <h5 class='figure'>Example</h5>
028 * <p class='bcode'>
029 *    Accept-Encoding: gzip, deflate
030 * </p>
031 *
032 * <h5 class='topic'>RFC2616 Specification</h5>
033 *
034 * The Accept-Encoding request-header field is similar to Accept, but restricts the content-codings (section 3.5) that
035 * are acceptable in the response.
036 *
037 * <p class='bcode'>
038 *    Accept-Encoding  = "Accept-Encoding" ":"
039 *                       1#( codings [ ";" "q" "=" qvalue ] )
040 *    codings          = ( content-coding | "*" )
041 * </p>
042 *
043 * <p>
044 * Examples of its use are:
045 * <p class='bcode'>
046 *    Accept-Encoding: compress, gzip
047 *    Accept-Encoding:
048 *    Accept-Encoding: *
049 *    Accept-Encoding: compress;q=0.5, gzip;q=1.0
050 *    Accept-Encoding: gzip;q=1.0, identity; q=0.5, *;q=0
051 * </p>
052 *
053 * <p>
054 * A server tests whether a content-coding is acceptable, according to an Accept-Encoding field, using these rules:
055 * <ol>
056 *    <li>If the content-coding is one of the content-codings listed in the Accept-Encoding field, then it is
057 *       acceptable, unless it is accompanied by a qvalue of 0.
058 *       (As defined in section 3.9, a qvalue of 0 means "not acceptable.")
059 *    <li>The special "*" symbol in an Accept-Encoding field matches any available content-coding not explicitly listed
060 *       in the header field.
061 *    <li>If multiple content-codings are acceptable, then the acceptable content-coding with the highest non-zero
062 *       qvalue is preferred.
063 *    <li>The "identity" content-coding is always acceptable, unless specifically refused because the Accept-Encoding
064 *       field includes "identity;q=0", or because the field includes "*;q=0" and does not explicitly include the
065 *       "identity" content-coding.
066 *       If the Accept-Encoding field-value is empty, then only the "identity" encoding is acceptable.
067 * </ol>
068 *
069 * <p>
070 * If an Accept-Encoding field is present in a request, and if the server cannot send a response which is acceptable
071 * according to the Accept-Encoding header, then the server SHOULD send an error response with the 406 (Not Acceptable)
072 * status code.
073 *
074 * <p>
075 * If no Accept-Encoding field is present in a request, the server MAY assume that the client will accept any content
076 * coding.
077 * In this case, if "identity" is one of the available content-codings, then the server SHOULD use the "identity"
078 * content-coding, unless it has additional information that a different content-coding is meaningful to the client.
079 *
080 * <p>
081 * Note: If the request does not include an Accept-Encoding field, and if the "identity" content-coding is unavailable,
082 * then content-codings commonly understood by HTTP/1.0 clients (i.e.,"gzip" and "compress") are preferred; some older
083 * clients improperly display messages sent with other content-codings.
084 * The server might also make this decision based on information about the particular user-agent or client.
085 *
086 * <p>
087 * Note: Most HTTP/1.0 applications do not recognize or obey qvalues associated with content-codings.
088 * This means that qvalues will not work and are not permitted with x-gzip or x-compress.
089 *
090 * <h5 class='section'>See Also:</h5><ul>
091 *    <li class='link'><a class="doclink" href="../../../../../index.html#juneau-rest-common">juneau-rest-common</a>
092 *    <li class='extlink'><a class="doclink" href="https://www.w3.org/Protocols/rfc2616/rfc2616.html">Hypertext Transfer Protocol -- HTTP/1.1</a>
093 * </ul>
094 *
095 * @serial exclude
096 */
097@Header("Accept-Encoding")
098public class AcceptEncoding extends BasicStringRangesHeader {
099
100   //-----------------------------------------------------------------------------------------------------------------
101   // Static
102   //-----------------------------------------------------------------------------------------------------------------
103
104   private static final long serialVersionUID = 1L;
105   private static final String NAME = "Accept-Encoding";
106
107   private static final Cache<String,AcceptEncoding> CACHE = Cache.of(String.class, AcceptEncoding.class).build();
108
109   /**
110    * Static creator.
111    *
112    * @param value
113    *    The header value.
114    *    <br>Must be parsable by {@link StringRanges#of(String)}.
115    *    <br>Can be <jk>null</jk>.
116    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
117    */
118   public static AcceptEncoding of(String value) {
119      return value == null ? null : CACHE.get(value, ()->new AcceptEncoding(value));
120   }
121
122   /**
123    * Static creator.
124    *
125    * @param value
126    *    The header value.
127    *    <br>Can be <jk>null</jk>.
128    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
129    */
130   public static AcceptEncoding of(StringRanges value) {
131      return value == null ? null : new AcceptEncoding(value);
132   }
133
134   /**
135    * Static creator with delayed value.
136    *
137    * <p>
138    * Header value is re-evaluated on each call to {@link #getValue()}.
139    *
140    * @param value
141    *    The supplier of the header value.
142    *    <br>Can be <jk>null</jk>.
143    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
144    */
145   public static AcceptEncoding of(Supplier<StringRanges> value) {
146      return value == null ? null : new AcceptEncoding(value);
147   }
148
149   //-----------------------------------------------------------------------------------------------------------------
150   // Instance
151   //-----------------------------------------------------------------------------------------------------------------
152
153   /**
154    * Constructor.
155    *
156    * @param value
157    *    The header value.
158    *    <br>Must be parsable by {@link StringRanges#of(String)}.
159    *    <br>Can be <jk>null</jk>.
160    */
161   public AcceptEncoding(String value) {
162      super(NAME, value);
163   }
164
165   /**
166    * Constructor.
167    *
168    * @param value
169    *    The header value.
170    *    <br>Can be <jk>null</jk>.
171    */
172   public AcceptEncoding(StringRanges value) {
173      super(NAME, value);
174   }
175
176   /**
177    * Constructor with delayed value.
178    *
179    * <p>
180    * Header value is re-evaluated on each call to {@link #getValue()}.
181    *
182    * @param value
183    *    The supplier of the header value.
184    *    <br>Can be <jk>null</jk>.
185    */
186   public AcceptEncoding(Supplier<StringRanges> value) {
187      super(NAME, value);
188   }
189}