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.http.annotation.*;
018
019/**
020 * Represents a parsed <l>Warning</l> HTTP request/response header.
021 *
022 * <p>
023 * A general warning about possible problems with the entity body.
024 *
025 * <h5 class='figure'>Example</h5>
026 * <p class='bcode'>
027 *    Warning: 199 Miscellaneous warning
028 * </p>
029 *
030 * <h5 class='topic'>RFC2616 Specification</h5>
031 *
032 * The Warning general-header field is used to carry additional information about the status or transformation of a
033 * message which might not be reflected in the message.
034 * This information is typically used to warn about a possible lack of semantic transparency from caching operations
035 * or transformations applied to the entity body of the message.
036 *
037 * <p>
038 * Warning headers are sent with responses using:
039 * <p class='bcode'>
040 *    Warning    = "Warning" ":" 1#warning-value
041 *    warning-value = warn-code SP warn-agent SP warn-text
042 *                                          [SP warn-date]
043 *    warn-code  = 3DIGIT
044 *    warn-agent = ( host [ ":" port ] ) | pseudonym
045 *                    ; the name or pseudonym of the server adding
046 *                    ; the Warning header, for use in debugging
047 *    warn-text  = quoted-string
048 *    warn-date  = &lt;"&gt; HTTP-date &lt;"&gt;
049 * </p>
050 *
051 * <p>
052 * A response MAY carry more than one Warning header.
053 *
054 * <p>
055 * The warn-text SHOULD be in a natural language and character set that is most likely to be intelligible to the human
056 * user receiving the response.
057 * This decision MAY be based on any available knowledge, such as the location of the cache or user, the
058 * Accept-Language field in a request, the Content-Language field in a response, etc.
059 * The default language is English and the default character set is ISO-8859-1.
060 *
061 * <p>
062 * If a character set other than ISO-8859-1 is used, it MUST be encoded in the warn-text using the method described in
063 * RFC 2047.
064 *
065 * <p>
066 * Warning headers can in general be applied to any message, however some specific warn-codes are specific to caches
067 * and can only be applied to response messages.
068 * New Warning headers SHOULD be added after any existing Warning headers.
069 * A cache MUST NOT delete any Warning header that it received with a message.
070 * However, if a cache successfully validates a cache entry, it SHOULD remove any Warning headers previously attached
071 * to that entry except as specified for specific Warning codes.
072 * It MUST then add any Warning headers received in the validating response.
073 * In other words, Warning headers are those that would be attached to the most recent relevant response.
074 *
075 * <p>
076 * When multiple Warning headers are attached to a response, the user agent ought to inform the user of as many of them
077 * as possible, in the order that they appear in the response.
078 * If it is not possible to inform the user of all of the warnings, the user agent SHOULD follow these heuristics:
079 * <ul>
080 *    <li>Warnings that appear early in the response take priority over those appearing later in the response.
081 *    <li>Warnings in the user's preferred character set take priority over warnings in other character sets but with
082 * identical warn-codes and warn-agents.
083 * </ul>
084 *
085 * <p>
086 * Systems that generate multiple Warning headers SHOULD order them with this user agent behavior in mind.
087 *
088 * <p>
089 * Requirements for the behavior of caches with respect to Warnings are stated in section 13.1.2.
090 *
091 * <p>
092 * This is a list of the currently-defined warn-codes, each with a recommended warn-text in English, and a description
093 * of its meaning.
094 * <ul>
095 *    <li>110 Response is stale MUST be included whenever the returned response is stale.
096 *    <li>111 Revalidation failed MUST be included if a cache returns a stale response because an attempt to revalidate
097 *       the response failed, due to an inability to reach the server.
098 *    <li>112 Disconnected operation SHOULD be included if the cache is intentionally disconnected from the rest of the
099 *       network for a period of time.
100 *    <li>113 Heuristic expiration MUST be included if the cache heuristically chose a freshness lifetime greater than
101 *       24 hours and the response's age is greater than 24 hours.
102 *    <li>199 Miscellaneous warning The warning text MAY include arbitrary information to be presented to a human user,
103 *       or logged. A system receiving this warning MUST NOT take any automated action, besides presenting the warning
104 *       to the user.
105 *    <li>214 Transformation applied MUST be added by an intermediate cache or proxy if it applies any transformation
106 *       changing the content-coding (as specified in the Content-Encoding header) or media-type (as specified in the
107 *       Content-Type header) of the response, or the entity-body of the response, unless this Warning code already
108 *       appears in the response.
109 *    <li>299 Miscellaneous persistent warning The warning text MAY include arbitrary information to be presented to a
110 *       human user, or logged. A system receiving this warning MUST NOT take any automated action.
111 * </ul>
112 *
113 * <p>
114 * If an implementation sends a message with one or more Warning headers whose version is HTTP/1.0 or lower, then the
115 * sender MUST include in each warning-value a warn-date that matches the date in the response.
116 *
117 * <p>
118 * If an implementation receives a message with a warning-value that includes a warn-date, and that warn-date is
119 * different from the Date value in the response, then that warning-value MUST be deleted from the message before
120 * storing, forwarding, or using it.
121 * (This prevents bad consequences of naive caching of Warning header fields.)
122 * If all of the warning-values are deleted for this reason, the Warning header MUST be deleted as well.
123 *
124 * <h5 class='section'>See Also:</h5><ul>
125 *    <li class='link'><a class="doclink" href="../../../../../index.html#juneau-rest-common">juneau-rest-common</a>
126 *    <li class='extlink'><a class="doclink" href="https://www.w3.org/Protocols/rfc2616/rfc2616.html">Hypertext Transfer Protocol -- HTTP/1.1</a>
127 * </ul>
128 *
129 * @serial exclude
130 */
131@Header("Warning")
132public class Warning extends BasicStringHeader {
133
134   //-----------------------------------------------------------------------------------------------------------------
135   // Static
136   //-----------------------------------------------------------------------------------------------------------------
137
138   private static final long serialVersionUID = 1L;
139   private static final String NAME = "Warning";
140
141   /**
142    * Static creator.
143    *
144    * @param value
145    *    The header value.
146    *    <br>Can be <jk>null</jk>.
147    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
148    */
149   public static Warning of(String value) {
150      return value == null ? null : new Warning(value);
151   }
152
153   /**
154    * Static creator with delayed value.
155    *
156    * <p>
157    * Header value is re-evaluated on each call to {@link #getValue()}.
158    *
159    * @param value
160    *    The supplier of the header value.
161    *    <br>Can be <jk>null</jk>.
162    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
163    */
164   public static Warning of(Supplier<String> value) {
165      return value == null ? null : new Warning(value);
166   }
167
168   //-----------------------------------------------------------------------------------------------------------------
169   // Instance
170   //-----------------------------------------------------------------------------------------------------------------
171
172   /**
173    * Constructor.
174    *
175    * @param value
176    *    The header value.
177    *    <br>Can be <jk>null</jk>.
178    */
179   public Warning(String value) {
180      super(NAME, value);
181   }
182
183   /**
184    * Constructor with delayed value.
185    *
186    * <p>
187    * Header value is re-evaluated on each call to {@link #getValue()}.
188    *
189    * @param value
190    *    The supplier of the header value.
191    *    <br>Can be <jk>null</jk>.
192    */
193   public Warning(Supplier<String> value) {
194      super(NAME, value);
195   }
196}