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 static org.apache.juneau.http.Constants.*;
016
017import java.util.function.*;
018
019import org.apache.juneau.http.*;
020import org.apache.juneau.http.annotation.*;
021import org.apache.juneau.internal.*;
022
023/**
024 * Represents a parsed <l>Accept-Language</l> HTTP request header.
025 *
026 * <p>
027 * List of acceptable human languages for response.
028 *
029 * <h5 class='figure'>Example</h5>
030 * <p class='bcode w800'>
031 *    Accept-Language: en-US
032 * </p>
033 *
034 * <h5 class='topic'>RFC2616 Specification</h5>
035 *
036 * The Accept-Language request-header field is similar to Accept, but restricts the set of natural languages that are
037 * preferred as a response to the request.
038 * Language tags are defined in section 3.10.
039 *
040 * <p class='bcode w800'>
041 *    Accept-Language = "Accept-Language" ":"
042 *                      1#( language-range [ ";" "q" "=" qvalue ] )
043 *    language-range  = ( ( 1*8ALPHA *( "-" 1*8ALPHA ) ) | "*" )
044 * </p>
045 *
046 * <p>
047 * Each language-range MAY be given an associated quality value which represents an estimate of the user's preference
048 * for the languages specified by that range.
049 * The quality value defaults to "q=1".
050 * For example...
051 * <p class='bcode w800'>
052 *    Accept-Language: da, en-gb;q=0.8, en;q=0.7
053 * </p>
054 * <p>
055 * ...would mean: "I prefer Danish, but will accept British English and other types of English."
056 *
057 * <p>
058 * A language-range matches a language-tag if it exactly equals the tag, or if it exactly equals a prefix of the tag
059 * such that the first tag character following the prefix is "-".
060 *
061 * <p>
062 * The special range "*", if present in the Accept-Language field, matches every tag not matched by any other range
063 * present in the Accept-Language field.
064 *
065 * <p>
066 * Note: This use of a prefix matching rule does not imply that language tags are assigned to languages in such a way
067 * that it is always true that if a user understands a language with a certain
068 * tag, then this user will also understand all languages with tags for which this tag is a prefix.
069 * The prefix rule simply allows the use of prefix tags if this is the case.
070 *
071 * <p>
072 * The language quality factor assigned to a language-tag by the Accept-Language field is the quality value of the
073 * longest language- range in the field that matches the language-tag.
074 *
075 * <p>
076 * If no language- range in the field matches the tag, the language quality factor assigned is 0.
077 *
078 * <p>
079 * If no Accept-Language header is present in the request, the server SHOULD assume that all languages are equally
080 * acceptable.
081 *
082 * <p>
083 * If an Accept-Language header is present, then all languages which are assigned a quality factor greater than 0 are
084 * acceptable.
085 *
086 * <p>
087 * It might be contrary to the privacy expectations of the user to send an Accept-Language header with the complete
088 * linguistic preferences of the user in every request.
089 * For a discussion of this issue, see section 15.1.4.
090 *
091 * <p>
092 * As intelligibility is highly dependent on the individual user, it is recommended that client applications make the
093 * choice of linguistic preference available to the user.
094 * If the choice is not made available, then the Accept-Language header field MUST NOT be given in the request.
095 *
096 * <p>
097 * Note: When making the choice of linguistic preference available to the user, we remind implementors of the fact that
098 * users are not familiar with the details of language matching as described above, and should provide appropriate
099 * guidance.
100 * As an example, users might assume that on selecting "en-gb", they will be served any kind of English document if
101 * British English is not available.
102 * A user agent might suggest in such a case to add "en" to get the best matching behavior.
103 *
104 * <ul class='seealso'>
105 *    <li class='extlink'>{@doc ExtRFC2616}
106 * </ul>
107 */
108@Header("Accept-Language")
109public class AcceptLanguage extends BasicStringRangeArrayHeader {
110
111   private static final long serialVersionUID = 1L;
112
113   private static final Cache<String,AcceptLanguage> CACHE = new Cache<>(NOCACHE, CACHE_MAX_SIZE);
114
115   /**
116    * Returns a parsed and cached header.
117    *
118    * @param value
119    *    The header value.
120    * @return A cached {@link AcceptLanguage} object.
121    */
122   public static AcceptLanguage of(String value) {
123      if (value == null)
124         return null;
125      AcceptLanguage x = CACHE.get(value);
126      if (x == null)
127         x = CACHE.put(value, new AcceptLanguage(value));
128      return x;
129   }
130
131   /**
132    * Convenience creator.
133    *
134    * @param value
135    *    The header value.
136    *    <br>Can be any of the following:
137    *    <ul>
138    *       <li>{@link String} - Converted using {@link StringRanges#of(String)}.
139    *       <li><c>StringRange[]</c> - Left as-is.
140    *       <li>Anything else - Converted to <c>String</c> then parsed.
141    *    </ul>
142    * @return A new {@link AcceptLanguage} object.
143    */
144   public static AcceptLanguage of(Object value) {
145      return new AcceptLanguage(value);
146   }
147
148   /**
149    * Convenience creator using supplier.
150    *
151    * <p>
152    * Header value is re-evaluated on each call to {@link #getValue()}.
153    *
154    * @param value
155    *    The header value supplier.
156    *    <br>Can be any of the following:
157    *    <ul>
158    *       <li>{@link String} - Converted using {@link StringRanges#of(String)}.
159    *       <li><c>StringRange[]</c> - Left as-is.
160    *       <li>Anything else - Converted to <c>String</c> then parsed.
161    *    </ul>
162    * @return A new {@link AcceptLanguage} object.
163    */
164   public static AcceptLanguage of(Supplier<?> value) {
165      return new AcceptLanguage(value);
166   }
167
168   /**
169    * Constructor
170    *
171    * @param value
172    *    The header value.
173    *    <br>Can be any of the following:
174    *    <ul>
175    *       <li>{@link String} - Converted using {@link StringRanges#of(String)}.
176    *       <li><c>StringRange[]</c> - Left as-is.
177    *       <li>Anything else - Converted to <c>String</c> then parsed.
178    *       <li>A {@link Supplier} of anything on this list.
179    *    </ul>
180    */
181   public AcceptLanguage(Object value) {
182      super("Accept-Language", value);
183   }
184
185   /**
186    * Constructor
187    *
188    * @param value
189    *    The header value.
190    */
191   public AcceptLanguage(String value) {
192      this((Object)value);
193   }
194}