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