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    *   http://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  
20  import java.text.ParsePosition;
21  import java.text.SimpleDateFormat;
22  import java.util.Date;
23  import java.util.Locale;
24  import java.util.TimeZone;
25  
26  /**
27   * This DateFormat decodes 3 formats of {@link Date}, but only encodes the one,
28   * the first:
29   * <ul>
30   * <li>Sun, 06 Nov 1994 08:49:37 GMT: standard specification, the only one with
31   * valid generation</li>
32   * <li>Sun, 06 Nov 1994 08:49:37 GMT: obsolete specification</li>
33   * <li>Sun Nov 6 08:49:37 1994: obsolete specification</li>
34   * </ul>
35   */
36  public final class HttpHeaderDateFormat extends SimpleDateFormat {
37      private static final long serialVersionUID = -925286159755905325L;
38  
39      private final SimpleDateFormat format1 = new HttpHeaderDateFormatObsolete1();
40      private final SimpleDateFormat format2 = new HttpHeaderDateFormatObsolete2();
41  
42      private static final FastThreadLocal<HttpHeaderDateFormat> dateFormatThreadLocal =
43              new FastThreadLocal<HttpHeaderDateFormat>() {
44                  @Override
45                  protected HttpHeaderDateFormat initialValue() {
46                      return new HttpHeaderDateFormat();
47                  }
48              };
49  
50      public static HttpHeaderDateFormat get() {
51          return dateFormatThreadLocal.get();
52      }
53  
54      /**
55       * Standard date format<p>
56       * Sun, 06 Nov 1994 08:49:37 GMT -> E, d MMM yyyy HH:mm:ss z
57       */
58      private HttpHeaderDateFormat() {
59          super("E, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
60          setTimeZone(TimeZone.getTimeZone("GMT"));
61      }
62  
63      @Override
64      public Date parse(String text, ParsePosition pos) {
65          Date date = super.parse(text, pos);
66          if (date == null) {
67              date = format1.parse(text, pos);
68          }
69          if (date == null) {
70              date = format2.parse(text, pos);
71          }
72          return date;
73      }
74  
75      /**
76       * First obsolete format<p>
77       * Sunday, 06-Nov-94 08:49:37 GMT -> E, d-MMM-y HH:mm:ss z
78       */
79      private static final class HttpHeaderDateFormatObsolete1 extends SimpleDateFormat {
80          private static final long serialVersionUID = -3178072504225114298L;
81  
82          HttpHeaderDateFormatObsolete1() {
83              super("E, dd-MMM-yy HH:mm:ss z", Locale.ENGLISH);
84              setTimeZone(TimeZone.getTimeZone("GMT"));
85          }
86      }
87  
88      /**
89       * Second obsolete format
90       * <p>
91       * Sun Nov 6 08:49:37 1994 -> EEE, MMM d HH:mm:ss yyyy
92       */
93      private static final class HttpHeaderDateFormatObsolete2 extends SimpleDateFormat {
94          private static final long serialVersionUID = 3010674519968303714L;
95  
96          HttpHeaderDateFormatObsolete2() {
97              super("E MMM d HH:mm:ss yyyy", Locale.ENGLISH);
98              setTimeZone(TimeZone.getTimeZone("GMT"));
99          }
100     }
101 }