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>Date</l> HTTP request/response header.
022 *
023 * <p>
024 * The date and time that the message was sent (in "HTTP-date" format as defined by RFC 7231).
025 *
026 * <h5 class='figure'>Example</h5>
027 * <p class='bcode'>
028 *    Date: Tue, 15 Nov 1994 08:12:31 GMT
029 * </p>
030 *
031 * <h5 class='topic'>RFC2616 Specification</h5>
032 *
033 * The Date general-header field represents the date and time at which the message was originated, having the same
034 * semantics as orig-date in RFC 822.
035 * The field value is an HTTP-date, as described in section 3.3.1; it MUST be sent in RFC 1123 [8]-date format.
036 * <p class='bcode'>
037 *    Date  = "Date" ":" HTTP-date
038 * </p>
039 *
040 * <p>
041 * An example is...
042 * <p class='bcode'>
043 *    Date: Tue, 15 Nov 1994 08:12:31 GMT
044 * </p>
045 *
046 * <p>
047 * Origin servers MUST include a Date header field in all responses, except in these cases:
048 * <ol>
049 *    <li>If the response status code is 100 (Continue) or 101 (Switching Protocols), the response MAY include a Date
050 *       header field, at the server's option.
051 *    <li>If the response status code conveys a server error, e.g. 500 (Internal Server Error) or 503 (Service
052 *       Unavailable), and it is inconvenient or impossible to generate a valid Date.
053 *    <li>If the server does not have a clock that can provide a reasonable approximation of the current time, its
054 *       responses MUST NOT include a Date header field.
055 *       In this case, the rules in section 14.18.1 MUST be followed.
056 * </ol>
057 *
058 * <p>
059 * A received message that does not have a Date header field MUST be assigned one by the recipient if the message will
060 * be cached by that recipient or gatewayed via a protocol which requires a Date.
061 * An HTTP implementation without a clock MUST NOT cache responses without revalidating them on every use.
062 * An HTTP cache, especially a shared cache, SHOULD use a mechanism, such as NTP, to synchronize its clock with a
063 * reliable external standard.
064 *
065 * <p>
066 * Clients SHOULD only send a Date header field in messages that include an entity-body, as in the case of the PUT and
067 * POST requests, and even then it is optional.
068 * A client without a clock MUST NOT send a Date header field in a request.
069 *
070 * <p>
071 * The HTTP-date sent in a Date header SHOULD NOT represent a date and time subsequent to the generation of the message.
072 * It SHOULD represent the best available approximation of the date and time of message generation, unless the
073 * implementation has no means of generating a reasonably accurate date and time.
074 * In theory, the date ought to represent the moment just before the entity is generated.
075 * In practice, the date can be generated at any time during the message origination without affecting its semantic
076 * value.
077 *
078 * <h5 class='section'>See Also:</h5><ul>
079 *    <li class='link'><a class="doclink" href="../../../../../index.html#juneau-rest-common">juneau-rest-common</a>
080 *    <li class='extlink'><a class="doclink" href="https://www.w3.org/Protocols/rfc2616/rfc2616.html">Hypertext Transfer Protocol -- HTTP/1.1</a>
081 * </ul>
082 *
083 * @serial exclude
084 */
085@Header("Date")
086public class Date extends BasicDateHeader {
087
088   //-----------------------------------------------------------------------------------------------------------------
089   // Static
090   //-----------------------------------------------------------------------------------------------------------------
091
092   private static final long serialVersionUID = 1L;
093   private static final String NAME = "Date";
094
095   /**
096    * Static creator.
097    *
098    * @param value
099    *    The header value.
100    *    <br>Must be an RFC-1123 formated string (e.g. <js>"Sat, 29 Oct 1994 19:43:31 GMT"</js>).
101    *    <br>Can be <jk>null</jk>.
102    * @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>.
103    */
104   public static Date of(String value) {
105      return value == null ? null : new Date(value);
106   }
107
108   /**
109    * Static creator.
110    *
111    * @param value
112    *    The header value.
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 Date of(ZonedDateTime value) {
117      return value == null ? null : new Date(value);
118   }
119
120   /**
121    * Static creator with delayed value.
122    *
123    * <p>
124    * Header value is re-evaluated on each call to {@link #getValue()}.
125    *
126    * @param value
127    *    The supplier of the header value.
128    *    <br>Can be <jk>null</jk>.
129    * @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>.
130    */
131   public static Date of(Supplier<ZonedDateTime> value) {
132      return value == null ? null : new Date(value);
133   }
134
135   //-----------------------------------------------------------------------------------------------------------------
136   // Instance
137   //-----------------------------------------------------------------------------------------------------------------
138
139   /**
140    * Constructor.
141    *
142    * @param value
143    *    The header value.
144    *    <br>Must be an RFC-1123 formated string (e.g. <js>"Sat, 29 Oct 1994 19:43:31 GMT"</js>).
145    *    <br>Can be <jk>null</jk>.
146    */
147   public Date(String value) {
148      super(NAME, value);
149   }
150
151   /**
152    * Constructor.
153    *
154    * @param value
155    *    The header value.
156    *    <br>Can be <jk>null</jk>.
157    */
158   public Date(ZonedDateTime value) {
159      super(NAME, value);
160   }
161
162   /**
163    * Constructor with delayed value.
164    *
165    * <p>
166    * Header value is re-evaluated on each call to {@link #getValue()}.
167    *
168    * @param value
169    *    The supplier of the header value.
170    *    <br>Can be <jk>null</jk>.
171    */
172   public Date(Supplier<ZonedDateTime> value) {
173      super(NAME, value);
174   }
175}