001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.transport.http.client;
038:
039: import java.text.SimpleDateFormat;
040: import java.text.ParseException;
041: import java.util.Date;
042: import java.util.Locale;
043: import java.util.TimeZone;
044:
045: /**
046: * A parser for date strings commonly found in http and email headers that
047: * follow various RFC conventions. Given a date-string, the parser will
048: * attempt to parse it by trying matches with a set of patterns, returning
049: * null on failure, a Date object on success.
050: *
051: * @author WS Development Team
052: */
053: final class RfcDateParser {
054: private boolean isGMT = false;
055: static final String[] standardFormats = {
056: "EEEE', 'dd-MMM-yy HH:mm:ss z", // RFC 850 (obsoleted by 1036)
057: "EEEE', 'dd-MMM-yy HH:mm:ss", // ditto but no tz. Happens too often
058: "EEE', 'dd-MMM-yyyy HH:mm:ss z", // RFC 822/1123
059: "EEE', 'dd MMM yyyy HH:mm:ss z", // REMIND what rfc? Apache/1.1
060: "EEEE', 'dd MMM yyyy HH:mm:ss z", // REMIND what rfc? Apache/1.1
061: "EEE', 'dd MMM yyyy hh:mm:ss z", // REMIND what rfc? Apache/1.1
062: "EEEE', 'dd MMM yyyy hh:mm:ss z", // REMIND what rfc? Apache/1.1
063: "EEE MMM dd HH:mm:ss z yyyy", // Date's string output format
064: "EEE MMM dd HH:mm:ss yyyy", // ANSI C asctime format()
065: "EEE', 'dd-MMM-yy HH:mm:ss", // No time zone 2 digit year RFC 1123
066: "EEE', 'dd-MMM-yyyy HH:mm:ss" // No time zone RFC 822/1123
067: };
068: static final String[] gmtStandardFormats = {
069: "EEEE',' dd-MMM-yy HH:mm:ss 'GMT'", // RFC 850 (obsoleted by 1036)
070: "EEE',' dd-MMM-yyyy HH:mm:ss 'GMT'", // RFC 822/1123
071: "EEE',' dd MMM yyyy HH:mm:ss 'GMT'", // REMIND what rfc? Apache/1.1
072: "EEEE',' dd MMM yyyy HH:mm:ss 'GMT'", // REMIND what rfc? Apache/1.1
073: "EEE',' dd MMM yyyy hh:mm:ss 'GMT'", // REMIND what rfc? Apache/1.1
074: "EEEE',' dd MMM yyyy hh:mm:ss 'GMT'", // REMIND what rfc? Apache/1.1
075: "EEE MMM dd HH:mm:ss 'GMT' yyyy" // Date's string output format
076: };
077: String dateString;
078:
079: public RfcDateParser(String dateString) {
080:
081: this .dateString = dateString.trim();
082:
083: if (this .dateString.indexOf("GMT") != -1) {
084: isGMT = true;
085: }
086: }
087:
088: public Date getDate() {
089:
090: // format is wdy, DD-Mon-yyyy HH:mm:ss GMT
091: int arrayLen = isGMT ? gmtStandardFormats.length
092: : standardFormats.length;
093:
094: for (int i = 0; i < arrayLen; i++) {
095: Date d;
096:
097: if (isGMT) {
098: d = tryParsing(gmtStandardFormats[i]);
099: } else {
100: d = tryParsing(standardFormats[i]);
101: }
102:
103: if (d != null) {
104: return d;
105: }
106: }
107:
108: return null;
109: }
110:
111: private Date tryParsing(String format) {
112:
113: SimpleDateFormat df = new SimpleDateFormat(format, Locale.US);
114:
115: if (isGMT) {
116: df.setTimeZone(TimeZone.getTimeZone("GMT"));
117: }
118:
119: try {
120: return df.parse(dateString);
121: } catch (ParseException e) {
122: return null;
123: }
124: }
125: }
|