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