1 /*
2 * JBoss, Home of Professional Open Source
3 *
4 * Copyright 2012, Red Hat Middleware LLC, and individual contributors
5 * by the @author tags. See the COPYRIGHT.txt in the distribution for a
6 * full listing of individual contributors.
7 *
8 * This is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as
10 * published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
12 *
13 * This software is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this software; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
22 */
23 package org.jboss.netty.handler.codec.http2;
24
25 import java.text.ParsePosition;
26 import java.text.SimpleDateFormat;
27 import java.util.Date;
28 import java.util.Locale;
29 import java.util.TimeZone;
30
31 /**
32 * This DateFormat decodes 3 formats of {@link Date}, but only encodes the one,
33 * the first:
34 * <ul>
35 * <li>Sun, 06 Nov 1994 08:49:37 GMT: standard specification, the only one with
36 * valid generation</li>
37 * <li>Sun, 06 Nov 1994 08:49:37 GMT: obsolete specification</li>
38 * <li>Sun Nov 6 08:49:37 1994: obsolete specification</li>
39 * </ul>
40 */
41 final class HttpHeaderDateFormat extends SimpleDateFormat {
42 private static final long serialVersionUID = -925286159755905325L;
43
44 private final SimpleDateFormat format1 = new HttpHeaderDateFormatObsolete1();
45 private final SimpleDateFormat format2 = new HttpHeaderDateFormatObsolete2();
46
47 /**
48 * Standard date format<p>
49 * Sun, 06 Nov 1994 08:49:37 GMT -> E, d MMM yyyy HH:mm:ss z
50 */
51 HttpHeaderDateFormat() {
52 super("E, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
53 setTimeZone(TimeZone.getTimeZone("GMT"));
54 }
55
56 @Override
57 public Date parse(String text, ParsePosition pos) {
58 Date date = super.parse(text, pos);
59 if (date == null) {
60 date = format1.parse(text, pos);
61 }
62 if (date == null) {
63 date = format2.parse(text, pos);
64 }
65 return date;
66 }
67
68 /**
69 * First obsolete format<p>
70 * Sunday, 06-Nov-94 08:49:37 GMT -> E, d-MMM-y HH:mm:ss z
71 */
72 private static final class HttpHeaderDateFormatObsolete1 extends SimpleDateFormat {
73 private static final long serialVersionUID = -3178072504225114298L;
74
75 HttpHeaderDateFormatObsolete1() {
76 super("E, dd-MMM-y HH:mm:ss z", Locale.ENGLISH);
77 setTimeZone(TimeZone.getTimeZone("GMT"));
78 }
79 }
80
81 /**
82 * Second obsolete format
83 * <p>
84 * Sun Nov 6 08:49:37 1994 -> EEE, MMM d HH:mm:ss yyyy
85 */
86 private static final class HttpHeaderDateFormatObsolete2 extends SimpleDateFormat {
87 private static final long serialVersionUID = 3010674519968303714L;
88
89 HttpHeaderDateFormatObsolete2() {
90 super("E MMM d HH:mm:ss yyyy", Locale.ENGLISH);
91 setTimeZone(TimeZone.getTimeZone("GMT"));
92 }
93 }
94 }