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-Language</l> HTTP request header.
021 * 
022 * <p>
023 * List of acceptable human languages for response.
024 * 
025 * <h5 class='figure'>Example</h5>
026 * <p class='bcode'>
027 *    Accept-Language: en-US
028 * </p>
029 * 
030 * <h5 class='topic'>RFC2616 Specification</h5>
031 * 
032 * The Accept-Language request-header field is similar to Accept, but restricts the set of natural languages that are
033 * preferred as a response to the request.
034 * Language tags are defined in section 3.10.
035 * 
036 * <p class='bcode'>
037 *    Accept-Language = "Accept-Language" ":"
038 *                      1#( language-range [ ";" "q" "=" qvalue ] )
039 *    language-range  = ( ( 1*8ALPHA *( "-" 1*8ALPHA ) ) | "*" )
040 * </p>
041 * 
042 * <p>
043 * Each language-range MAY be given an associated quality value which represents an estimate of the user's preference
044 * for the languages specified by that range.
045 * The quality value defaults to "q=1".
046 * For example...
047 * <p class='bcode'>
048 *    Accept-Language: da, en-gb;q=0.8, en;q=0.7
049 * </p>
050 * <p>
051 * ...would mean: "I prefer Danish, but will accept British English and other types of English."
052 * 
053 * <p>
054 * A language-range matches a language-tag if it exactly equals the tag, or if it exactly equals a prefix of the tag
055 * such that the first tag character following the prefix is "-".
056 * 
057 * <p>
058 * The special range "*", if present in the Accept-Language field, matches every tag not matched by any other range
059 * present in the Accept-Language field.
060 * 
061 * <p>
062 * Note: This use of a prefix matching rule does not imply that language tags are assigned to languages in such a way
063 * that it is always true that if a user understands a language with a certain
064 * tag, then this user will also understand all languages with tags for which this tag is a prefix.
065 * The prefix rule simply allows the use of prefix tags if this is the case.
066 * 
067 * <p>
068 * The language quality factor assigned to a language-tag by the Accept-Language field is the quality value of the
069 * longest language- range in the field that matches the language-tag.
070 * 
071 * <p>
072 * If no language- range in the field matches the tag, the language quality factor assigned is 0.
073 * 
074 * <p>
075 * If no Accept-Language header is present in the request, the server SHOULD assume that all languages are equally
076 * acceptable.
077 * 
078 * <p>
079 * If an Accept-Language header is present, then all languages which are assigned a quality factor greater than 0 are
080 * acceptable.
081 * 
082 * <p>
083 * It might be contrary to the privacy expectations of the user to send an Accept-Language header with the complete
084 * linguistic preferences of the user in every request.
085 * For a discussion of this issue, see section 15.1.4.
086 * 
087 * <p>
088 * As intelligibility is highly dependent on the individual user, it is recommended that client applications make the
089 * choice of linguistic preference available to the user.
090 * If the choice is not made available, then the Accept-Language header field MUST NOT be given in the request.
091 * 
092 * <p>
093 * Note: When making the choice of linguistic preference available to the user, we remind implementors of the fact that
094 * users are not familiar with the details of language matching as described above, and should provide appropriate
095 * guidance.
096 * As an example, users might assume that on selecting "en-gb", they will be served any kind of English document if
097 * British English is not available.
098 * A user agent might suggest in such a case to add "en" to get the best matching behavior.
099 * 
100 * <h5 class='section'>See Also:</h5>
101 * <ul class='doctree'>
102 *    <li class='extlink'><a class='doclink' href='https://www.w3.org/Protocols/rfc2616/rfc2616.html'>Hypertext Transfer Protocol -- HTTP/1.1</a>
103 * </ul>
104 */
105public final class AcceptLanguage extends HeaderRangeArray {
106
107   private static final Cache<String,AcceptLanguage> cache = new Cache<>(NOCACHE, CACHE_MAX_SIZE);
108
109   /**
110    * Returns a parsed <code>Accept-Language</code> header.
111    * 
112    * @param value The <code>Accept-Language</code> header string.
113    * @return The parsed <code>Accept-Language</code> header, or <jk>null</jk> if the string was null.
114    */
115   public static AcceptLanguage forString(String value) {
116      if (value == null)
117         return null;
118      AcceptLanguage a = cache.get(value);
119      if (a == null)
120         a = cache.put(value, new AcceptLanguage(value));
121      return a;
122   }
123
124   private AcceptLanguage(String raw) {
125      super(raw);
126   }
127}