View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.http;
17  
18  import io.netty.util.concurrent.FastThreadLocal;
19  import io.netty.handler.codec.DateFormatter;
20  
21  import java.text.ParsePosition;
22  import java.text.SimpleDateFormat;
23  import java.util.Date;
24  import java.util.Locale;
25  import java.util.TimeZone;
26  
27  /**
28   * This DateFormat decodes 3 formats of {@link Date}, but only encodes the one,
29   * the first:
30   * <ul>
31   * <li>Sun, 06 Nov 1994 08:49:37 GMT: standard specification, the only one with
32   * valid generation</li>
33   * <li>Sunday, 06-Nov-94 08:49:37 GMT: obsolete specification</li>
34   * <li>Sun Nov  6 08:49:37 1994: obsolete specification</li>
35   * </ul>
36   * @deprecated Use {@link DateFormatter} instead
37   */
38  @Deprecated
39  public final class HttpHeaderDateFormat extends SimpleDateFormat {
40      private static final long serialVersionUID = -925286159755905325L;
41  
42      private final SimpleDateFormat format1 = new HttpHeaderDateFormatObsolete1();
43      private final SimpleDateFormat format2 = new HttpHeaderDateFormatObsolete2();
44  
45      private static final FastThreadLocal<HttpHeaderDateFormat> dateFormatThreadLocal =
46              new FastThreadLocal<HttpHeaderDateFormat>() {
47                  @Override
48                  protected HttpHeaderDateFormat initialValue() {
49                      return new HttpHeaderDateFormat();
50                  }
51              };
52  
53      public static HttpHeaderDateFormat get() {
54          return dateFormatThreadLocal.get();
55      }
56  
57      /**
58       * Standard date format<p>
59       * Sun, 06 Nov 1994 08:49:37 GMT -> E, d MMM yyyy HH:mm:ss z
60       */
61      private HttpHeaderDateFormat() {
62          super("E, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
63          setTimeZone(TimeZone.getTimeZone("GMT"));
64      }
65  
66      @Override
67      public Date parse(String text, ParsePosition pos) {
68          Date date = super.parse(text, pos);
69          if (date == null) {
70              date = format1.parse(text, pos);
71          }
72          if (date == null) {
73              date = format2.parse(text, pos);
74          }
75          return date;
76      }
77  
78      /**
79       * First obsolete format<p>
80       * Sunday, 06-Nov-94 08:49:37 GMT -> E, d-MMM-y HH:mm:ss z
81       */
82      private static final class HttpHeaderDateFormatObsolete1 extends SimpleDateFormat {
83          private static final long serialVersionUID = -3178072504225114298L;
84  
85          HttpHeaderDateFormatObsolete1() {
86              super("E, dd-MMM-yy HH:mm:ss z", Locale.ENGLISH);
87              setTimeZone(TimeZone.getTimeZone("GMT"));
88          }
89      }
90  
91      /**
92       * Second obsolete format
93       * <p>
94       * Sun Nov 6 08:49:37 1994 -> EEE, MMM d HH:mm:ss yyyy
95       */
96      private static final class HttpHeaderDateFormatObsolete2 extends SimpleDateFormat {
97          private static final long serialVersionUID = 3010674519968303714L;
98  
99          HttpHeaderDateFormatObsolete2() {
100             super("E MMM d HH:mm:ss yyyy", Locale.ENGLISH);
101             setTimeZone(TimeZone.getTimeZone("GMT"));
102         }
103     }
104 }