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