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   private static final long serialVersionUID = 1L;
092   private static final String NAME = "Date";
093
094   /**
095    * Static creator.
096    *
097    * @param value
098    *    The header value.
099    *    <br>Must be an RFC-1123 formated string (e.g. <js>"Sat, 29 Oct 1994 19:43:31 GMT"</js>).
100    *    <br>Can be <jk>null</jk>.
101    * @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>.
102    */
103   public static Date of(String value) {
104      return value == null ? null : new Date(value);
105   }
106
107   /**
108    * Static creator with delayed value.
109    *
110    * <p>
111    * Header value is re-evaluated on each call to {@link #getValue()}.
112    *
113    * @param value
114    *    The supplier of the header value.
115    *    <br>Can be <jk>null</jk>.
116    * @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>.
117    */
118   public static Date of(Supplier<ZonedDateTime> value) {
119      return value == null ? null : new Date(value);
120   }
121
122   /**
123    * Static creator.
124    *
125    * @param value
126    *    The header value.
127    *    <br>Can be <jk>null</jk>.
128    * @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>.
129    */
130   public static Date of(ZonedDateTime value) {
131      return value == null ? null : new Date(value);
132   }
133
134   /**
135    * Constructor.
136    *
137    * @param value
138    *    The header value.
139    *    <br>Must be an RFC-1123 formated string (e.g. <js>"Sat, 29 Oct 1994 19:43:31 GMT"</js>).
140    *    <br>Can be <jk>null</jk>.
141    */
142   public Date(String value) {
143      super(NAME, value);
144   }
145
146   /**
147    * Constructor with delayed value.
148    *
149    * <p>
150    * Header value is re-evaluated on each call to {@link #getValue()}.
151    *
152    * @param value
153    *    The supplier of the header value.
154    *    <br>Can be <jk>null</jk>.
155    */
156   public Date(Supplier<ZonedDateTime> value) {
157      super(NAME, value);
158   }
159
160   /**
161    * Constructor.
162    *
163    * @param value
164    *    The header value.
165    *    <br>Can be <jk>null</jk>.
166    */
167   public Date(ZonedDateTime value) {
168      super(NAME, value);
169   }
170}