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.*;
016
017import org.apache.juneau.internal.*;
018
019/**
020 * Represents a parsed <l>Accept-Encoding</l> HTTP request header.
021 * 
022 * <p>
023 * List of acceptable encodings.
024 * 
025 * <h5 class='figure'>Example</h5>
026 * <p class='bcode'>
027 *    Accept-Encoding: gzip, deflate
028 * </p>
029 * 
030 * <h5 class='topic'>RFC2616 Specification</h5>
031 * 
032 * The Accept-Encoding request-header field is similar to Accept, but restricts the content-codings (section 3.5) that
033 * are acceptable in the response.
034 * 
035 * <p class='bcode'>
036 *    Accept-Encoding  = "Accept-Encoding" ":"
037 *                       1#( codings [ ";" "q" "=" qvalue ] )
038 *    codings          = ( content-coding | "*" )
039 * </p>
040 * 
041 * <p>
042 * Examples of its use are:
043 * <p class='bcode'>
044 *    Accept-Encoding: compress, gzip
045 *    Accept-Encoding:
046 *    Accept-Encoding: *
047 *    Accept-Encoding: compress;q=0.5, gzip;q=1.0
048 *    Accept-Encoding: gzip;q=1.0, identity; q=0.5, *;q=0
049 * </p>
050 * 
051 * <p>
052 * A server tests whether a content-coding is acceptable, according to an Accept-Encoding field, using these rules:
053 * <ol>
054 *    <li>If the content-coding is one of the content-codings listed in the Accept-Encoding field, then it is
055 *       acceptable, unless it is accompanied by a qvalue of 0.
056 *       (As defined in section 3.9, a qvalue of 0 means "not acceptable.")
057 *    <li>The special "*" symbol in an Accept-Encoding field matches any available content-coding not explicitly listed
058 *       in the header field.
059 *    <li>If multiple content-codings are acceptable, then the acceptable content-coding with the highest non-zero
060 *       qvalue is preferred.
061 *    <li>The "identity" content-coding is always acceptable, unless specifically refused because the Accept-Encoding
062 *       field includes "identity;q=0", or because the field includes "*;q=0" and does not explicitly include the
063 *       "identity" content-coding.
064 *       If the Accept-Encoding field-value is empty, then only the "identity" encoding is acceptable.
065 * </ol>
066 * 
067 * <p>
068 * If an Accept-Encoding field is present in a request, and if the server cannot send a response which is acceptable
069 * according to the Accept-Encoding header, then the server SHOULD send an error response with the 406 (Not Acceptable)
070 * status code.
071 * 
072 * <p>
073 * If no Accept-Encoding field is present in a request, the server MAY assume that the client will accept any content
074 * coding.
075 * In this case, if "identity" is one of the available content-codings, then the server SHOULD use the "identity"
076 * content-coding, unless it has additional information that a different content-coding is meaningful to the client.
077 * 
078 * <p>
079 * Note: If the request does not include an Accept-Encoding field, and if the "identity" content-coding is unavailable,
080 * then content-codings commonly understood by HTTP/1.0 clients (i.e.,"gzip" and "compress") are preferred; some older
081 * clients improperly display messages sent with other content-codings.
082 * The server might also make this decision based on information about the particular user-agent or client.
083 * 
084 * <p>
085 * Note: Most HTTP/1.0 applications do not recognize or obey qvalues associated with content-codings.
086 * This means that qvalues will not work and are not permitted with x-gzip or x-compress.
087 * 
088 * <h5 class='section'>See Also:</h5>
089 * <ul class='doctree'>
090 *    <li class='extlink'><a class='doclink' href='https://www.w3.org/Protocols/rfc2616/rfc2616.html'>Hypertext Transfer Protocol -- HTTP/1.1</a>
091 * </ul>
092 */
093public final class AcceptEncoding extends HeaderRangeArray {
094
095   private static final Cache<String,AcceptEncoding> cache = new Cache<>(NOCACHE, CACHE_MAX_SIZE);
096
097   /**
098    * Returns a parsed <code>Accept-Encoding</code> header.
099    * 
100    * @param value The <code>Accept-Encoding</code> header string.
101    * @return The parsed <code>Accept-Encoding</code> header, or <jk>null</jk> if the string was null.
102    */
103   public static AcceptEncoding forString(String value) {
104      if (value == null)
105         return null;
106      AcceptEncoding a = cache.get(value);
107      if (a == null)
108         a = cache.put(value, new AcceptEncoding(value));
109      return a;
110   }
111
112   private AcceptEncoding(String value) {
113      super(value);
114   }
115}