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.time.*;
016import java.util.function.*;
017
018import org.apache.juneau.http.annotation.*;
019
020/**
021 * Represents a parsed <l>If-Modified-Since</l> HTTP request header.
022 *
023 * <p>
024 * Allows a 304 Not Modified to be returned if content is unchanged.
025 *
026 * <h5 class='figure'>Example</h5>
027 * <p class='bcode'>
028 *    If-Modified-Since: Sat, 29 Oct 1994 19:43:31 GMT
029 * </p>
030 *
031 * <h5 class='topic'>RFC2616 Specification</h5>
032 *
033 * The If-Modified-Since request-header field is used with a method to make it conditional:
034 * if the requested variant has not been modified since the time specified in this field, an entity will not be returned
035 * from the server; instead, a 304 (not modified) response will be returned without any message-body.
036 *
037 * <p class='bcode'>
038 *    If-Modified-Since = "If-Modified-Since" ":" HTTP-date
039 * </p>
040 *
041 * <p>
042 * An example of the field is:
043 * <p class='bcode'>
044 *    If-Modified-Since: Sat, 29 Oct 1994 19:43:31 GMT
045 * </p>
046 *
047 * <p>
048 * A GET method with an If-Modified-Since header and no Range header requests that the identified entity be transferred
049 * only if it has been modified since the date given by the If-Modified-Since header.
050 * The algorithm for determining this includes the following cases:
051 * <ol>
052 *    <li>If the request would normally result in anything other than a 200 (OK) status, or if the passed
053 *       If-Modified-Since date is invalid, the response is exactly the same as for a normal GET.
054 *       A date which is later than the server's current time is invalid.
055 *    <li>If the variant has been modified since the If-Modified-Since date, the response is exactly the same as for a
056 *       normal GET.
057 *    <li>If the variant has not been modified since a valid If-Modified-Since date, the server SHOULD return a 304
058 *       (Not Modified) response.
059 * </ol>
060 *
061 * <p>
062 * The purpose of this feature is to allow efficient updates of cached information with a minimum amount of transaction
063 * overhead.
064 *
065 * <p>
066 * Note: The Range request-header field modifies the meaning of If-Modified-Since; see section 14.35 for full details.
067 *
068 * <p>
069 * Note: If-Modified-Since times are interpreted by the server, whose clock might not be synchronized with the client.
070 *
071 * <p>
072 * Note: When handling an If-Modified-Since header field, some servers will use an exact date comparison function,
073 * rather than a less-than function, for deciding whether to send a 304 (Not Modified) response.
074 * To get best results when sending an If-Modified-Since header field for cache validation, clients are
075 * advised to use the exact date string received in a previous Last-Modified header field whenever possible.
076 *
077 * <p>
078 * Note: If a client uses an arbitrary date in the If-Modified-Since header instead of a date taken from the
079 * Last-Modified header for the same request, the client should be aware of the fact that this date is interpreted in
080 * the server's understanding of time.
081 * The client should consider unsynchronized clocks and rounding problems due to the different encodings of time between
082 * the client and server.
083 * This includes the possibility of race conditions if the document has changed between the time it was first requested
084 * and the If-Modified-Since date of a subsequent request, and the possibility of clock-skew-related problems if the
085 * If-Modified-Since date is derived from the client's clock without correction to the server's clock.
086 * Corrections for different time bases between client and server are at best approximate due to network latency.
087 * The result of a request having both an If-Modified-Since header field and either an If-Match or an
088 * If-Unmodified-Since header fields is undefined by this specification.
089 *
090 * <h5 class='section'>See Also:</h5><ul>
091 *    <li class='link'><a class="doclink" href="../../../../../index.html#juneau-rest-common">juneau-rest-common</a>
092 *    <li class='extlink'><a class="doclink" href="https://www.w3.org/Protocols/rfc2616/rfc2616.html">Hypertext Transfer Protocol -- HTTP/1.1</a>
093 * </ul>
094 *
095 * @serial exclude
096 */
097@Header("If-Modified-Since")
098public class IfModifiedSince extends BasicDateHeader {
099
100   //-----------------------------------------------------------------------------------------------------------------
101   // Static
102   //-----------------------------------------------------------------------------------------------------------------
103
104   private static final long serialVersionUID = 1L;
105   private static final String NAME = "If-Modified-Since";
106
107   /**
108    * Static creator.
109    *
110    * @param value
111    *    The header value.
112    *    <br>Must be an RFC-1123 formated string (e.g. <js>"Sat, 29 Oct 1994 19:43:31 GMT"</js>).
113    *    <br>Can be <jk>null</jk>.
114    * @return A new header bean, or <jk>null</jk> if the name is <jk>null</jk> or empty or the value is <jk>null</jk>.
115    */
116   public static IfModifiedSince of(String value) {
117      return value == null ? null : new IfModifiedSince(value);
118   }
119
120   /**
121    * Static creator.
122    *
123    * @param value
124    *    The header value.
125    *    <br>Can be <jk>null</jk>.
126    * @return A new header bean, or <jk>null</jk> if the name is <jk>null</jk> or empty or the value is <jk>null</jk>.
127    */
128   public static IfModifiedSince of(ZonedDateTime value) {
129      return value == null ? null : new IfModifiedSince(value);
130   }
131
132   /**
133    * Static creator with delayed value.
134    *
135    * <p>
136    * Header value is re-evaluated on each call to {@link #getValue()}.
137    *
138    * @param value
139    *    The supplier of the header value.
140    *    <br>Can be <jk>null</jk>.
141    * @return A new header bean, or <jk>null</jk> if the name is <jk>null</jk> or empty or the value is <jk>null</jk>.
142    */
143   public static IfModifiedSince of(Supplier<ZonedDateTime> value) {
144      return value == null ? null : new IfModifiedSince(value);
145   }
146
147   //-----------------------------------------------------------------------------------------------------------------
148   // Instance
149   //-----------------------------------------------------------------------------------------------------------------
150
151   /**
152    * Constructor.
153    *
154    * @param value
155    *    The header value.
156    *    <br>Must be an RFC-1123 formated string (e.g. <js>"Sat, 29 Oct 1994 19:43:31 GMT"</js>).
157    *    <br>Can be <jk>null</jk>.
158    */
159   public IfModifiedSince(String value) {
160      super(NAME, value);
161   }
162
163   /**
164    * Constructor.
165    *
166    * @param value
167    *    The header value.
168    *    <br>Can be <jk>null</jk>.
169    */
170   public IfModifiedSince(ZonedDateTime value) {
171      super(NAME, value);
172   }
173
174   /**
175    * Constructor with delayed value.
176    *
177    * <p>
178    * Header value is re-evaluated on each call to {@link #getValue()}.
179    *
180    * @param value
181    *    The supplier of the header value.
182    *    <br>Can be <jk>null</jk>.
183    */
184   public IfModifiedSince(Supplier<ZonedDateTime> value) {
185      super(NAME, value);
186   }
187}