View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.http.protocol;
29  
30  import java.time.Instant;
31  import java.time.ZoneId;
32  import java.time.format.DateTimeFormatter;
33  import java.time.format.DateTimeFormatterBuilder;
34  import java.util.TimeZone;
35  import java.util.concurrent.locks.ReentrantLock;
36  
37  import org.apache.hc.core5.annotation.Contract;
38  import org.apache.hc.core5.annotation.ThreadingBehavior;
39  
40  /**
41   * Generates a date in the format required by the HTTP protocol.
42   *
43   * @since 4.0
44   */
45  @Contract(threading = ThreadingBehavior.SAFE)
46  public class HttpDateGenerator {
47  
48      private static final int GRANULARITY_MILLIS = 1000;
49  
50      /**
51       * @deprecated Use {@link #INTERNET_MESSAGE_FORMAT}
52       */
53      @Deprecated
54      public static final String PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz";
55      public static final String INTERNET_MESSAGE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss zzz";
56  
57      /**
58       * @deprecated This attribute is no longer supported as a part of the public API.
59       * The time zone to use in the date header.
60       */
61      @Deprecated
62      public static final TimeZone GMT = TimeZone.getTimeZone("GMT");
63  
64      public static final ZoneId GMT_ID = ZoneId.of("GMT");
65  
66      /** Singleton instance. */
67      public static final HttpDateGenerator INSTANCE = new HttpDateGenerator(INTERNET_MESSAGE_FORMAT, GMT_ID);
68  
69      private final DateTimeFormatter dateTimeFormatter;
70      private long dateAsMillis;
71      private String dateAsText;
72      private ZoneId zoneId;
73  
74      private final ReentrantLock lock;
75  
76      private HttpDateGenerator(final String pattern, final ZoneId zoneId) {
77          dateTimeFormatter = new DateTimeFormatterBuilder()
78                  .parseLenient()
79                  .parseCaseInsensitive()
80                  .appendPattern(pattern)
81                  .toFormatter();
82          this.zoneId =  zoneId;
83          this.lock = new ReentrantLock();
84      }
85  
86      public String getCurrentDate() {
87          lock.lock();
88          try {
89              final long now = System.currentTimeMillis();
90              if (now - this.dateAsMillis > GRANULARITY_MILLIS) {
91                  // Generate new date string
92                  dateAsText = dateTimeFormatter.format(Instant.now().atZone(zoneId));
93                  dateAsMillis = now;
94              }
95              return dateAsText;
96          } finally {
97              lock.unlock();
98          }
99      }
100 
101 }