From 848ff6f6529d7c6864a14a6dba48cee783c1a294 Mon Sep 17 00:00:00 2001 From: Roland Weber Date: Sun, 17 Jun 2007 16:52:20 +0000 Subject: [PATCH] moved DateUtils/DateParseException from core to cookie (test cases to follow) git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@548067 13f79535-47bb-0310-9956-ffa450edef68 --- .../http/impl/cookie/BasicExpiresHandler.java | 3 +- .../http/impl/cookie/BrowserCompatSpec.java | 3 +- .../http/impl/cookie/DateParseException.java | 60 +++++ .../apache/http/impl/cookie/DateUtils.java | 210 ++++++++++++++++++ .../apache/http/impl/cookie/RFC2109Spec.java | 4 +- 5 files changed, 274 insertions(+), 6 deletions(-) create mode 100644 module-client/src/main/java/org/apache/http/impl/cookie/DateParseException.java create mode 100644 module-client/src/main/java/org/apache/http/impl/cookie/DateUtils.java diff --git a/module-client/src/main/java/org/apache/http/impl/cookie/BasicExpiresHandler.java b/module-client/src/main/java/org/apache/http/impl/cookie/BasicExpiresHandler.java index 79d600d8e..453daad4c 100644 --- a/module-client/src/main/java/org/apache/http/impl/cookie/BasicExpiresHandler.java +++ b/module-client/src/main/java/org/apache/http/impl/cookie/BasicExpiresHandler.java @@ -32,8 +32,7 @@ package org.apache.http.impl.cookie; import org.apache.http.cookie.Cookie; import org.apache.http.cookie.MalformedCookieException; -import org.apache.http.util.DateParseException; -import org.apache.http.util.DateUtils; + public class BasicExpiresHandler extends AbstractCookieAttributeHandler { diff --git a/module-client/src/main/java/org/apache/http/impl/cookie/BrowserCompatSpec.java b/module-client/src/main/java/org/apache/http/impl/cookie/BrowserCompatSpec.java index 6f197a677..50af61a3b 100644 --- a/module-client/src/main/java/org/apache/http/impl/cookie/BrowserCompatSpec.java +++ b/module-client/src/main/java/org/apache/http/impl/cookie/BrowserCompatSpec.java @@ -40,8 +40,7 @@ import org.apache.http.cookie.SM; import org.apache.http.message.BasicHeaderElement; import org.apache.http.message.BufferedHeader; import org.apache.http.util.CharArrayBuffer; -import org.apache.http.util.DateParseException; -import org.apache.http.util.DateUtils; + /** * Cookie specification that stives to closely mimic (mis)behavior of diff --git a/module-client/src/main/java/org/apache/http/impl/cookie/DateParseException.java b/module-client/src/main/java/org/apache/http/impl/cookie/DateParseException.java new file mode 100644 index 000000000..5832befbd --- /dev/null +++ b/module-client/src/main/java/org/apache/http/impl/cookie/DateParseException.java @@ -0,0 +1,60 @@ +/* + * $HeadURL$ + * $Revision$ + * $Date$ + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.http.impl.cookie; + + +/** + * An exception to indicate an error parsing a date string. + * + * @see DateUtils + * + * @author Michael Becke + */ +public class DateParseException extends Exception { + + static final long serialVersionUID = 4417696455000643370L; + + /** + * + */ + public DateParseException() { + super(); + } + + /** + * @param message the exception message + */ + public DateParseException(String message) { + super(message); + } + +} diff --git a/module-client/src/main/java/org/apache/http/impl/cookie/DateUtils.java b/module-client/src/main/java/org/apache/http/impl/cookie/DateUtils.java new file mode 100644 index 000000000..853bee35f --- /dev/null +++ b/module-client/src/main/java/org/apache/http/impl/cookie/DateUtils.java @@ -0,0 +1,210 @@ +/* + * $HeadURL$ + * $Revision$ + * $Date$ + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.http.impl.cookie; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.Locale; +import java.util.TimeZone; + +/** + * A utility class for parsing and formatting HTTP dates as used in cookies and + * other headers. This class handles dates as defined by RFC 2616 section + * 3.3.1 as well as some other common non-standard formats. + * + * @author Christopher Brown + * @author Michael Becke + */ +public final class DateUtils { + + /** + * Date format pattern used to parse HTTP date headers in RFC 1123 format. + */ + public static final String PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz"; + + /** + * Date format pattern used to parse HTTP date headers in RFC 1036 format. + */ + public static final String PATTERN_RFC1036 = "EEEE, dd-MMM-yy HH:mm:ss zzz"; + + /** + * Date format pattern used to parse HTTP date headers in ANSI C + * asctime() format. + */ + public static final String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy"; + + private static final String[] DEFAULT_PATTERNS = new String[] { + PATTERN_ASCTIME, + PATTERN_RFC1036, + PATTERN_RFC1123 }; + + private static final Date DEFAULT_TWO_DIGIT_YEAR_START; + + public static final TimeZone GMT = TimeZone.getTimeZone("GMT"); + + static { + Calendar calendar = Calendar.getInstance(); + calendar.setTimeZone(GMT); + calendar.set(2000, Calendar.JANUARY, 1, 0, 0, 0); + calendar.set(Calendar.MILLISECOND, 0); + DEFAULT_TWO_DIGIT_YEAR_START = calendar.getTime(); + } + + /** + * Parses a date value. The formats used for parsing the date value are retrieved from + * the default http params. + * + * @param dateValue the date value to parse + * + * @return the parsed date + * + * @throws DateParseException if the value could not be parsed using any of the + * supported date formats + */ + public static Date parseDate(String dateValue) throws DateParseException { + return parseDate(dateValue, null, null); + } + + /** + * Parses the date value using the given date formats. + * + * @param dateValue the date value to parse + * @param dateFormats the date formats to use + * + * @return the parsed date + * + * @throws DateParseException if none of the dataFormats could parse the dateValue + */ + public static Date parseDate(final String dateValue, String[] dateFormats) + throws DateParseException { + return parseDate(dateValue, dateFormats, null); + } + + /** + * Parses the date value using the given date formats. + * + * @param dateValue the date value to parse + * @param dateFormats the date formats to use + * @param startDate During parsing, two digit years will be placed in the range + * startDate to startDate + 100 years. This value may + * be null. When null is given as a parameter, year + * 2000 will be used. + * + * @return the parsed date + * + * @throws DateParseException if none of the dataFormats could parse the dateValue + */ + public static Date parseDate( + String dateValue, + String[] dateFormats, + Date startDate + ) throws DateParseException { + + if (dateValue == null) { + throw new IllegalArgumentException("dateValue is null"); + } + if (dateFormats == null) { + dateFormats = DEFAULT_PATTERNS; + } + if (startDate == null) { + startDate = DEFAULT_TWO_DIGIT_YEAR_START; + } + // trim single quotes around date if present + // see issue #5279 + if (dateValue.length() > 1 + && dateValue.startsWith("'") + && dateValue.endsWith("'") + ) { + dateValue = dateValue.substring (1, dateValue.length() - 1); + } + + SimpleDateFormat dateParser = null; + for (int i = 0; i < dateFormats.length; i++) { + if (dateParser == null) { + dateParser = new SimpleDateFormat(dateFormats[i], Locale.US); + dateParser.setTimeZone(TimeZone.getTimeZone("GMT")); + dateParser.set2DigitYearStart(startDate); + } else { + dateParser.applyPattern(dateFormats[i]); + } + try { + return dateParser.parse(dateValue); + } catch (ParseException pe) { + // ignore this exception, we will try the next format + } + } + + // we were unable to parse the date + throw new DateParseException("Unable to parse the date " + dateValue); + } + + /** + * Formats the given date according to the RFC 1123 pattern. + * + * @param date The date to format. + * @return An RFC 1123 formatted date string. + * + * @see #PATTERN_RFC1123 + */ + public static String formatDate(Date date) { + return formatDate(date, PATTERN_RFC1123); + } + + /** + * Formats the given date according to the specified pattern. The pattern + * must conform to that used by the {@link SimpleDateFormat simple date + * format} class. + * + * @param date The date to format. + * @param pattern The pattern to use for formatting the date. + * @return A formatted date string. + * + * @throws IllegalArgumentException If the given date pattern is invalid. + * + * @see SimpleDateFormat + */ + public static String formatDate(Date date, String pattern) { + if (date == null) throw new IllegalArgumentException("date is null"); + if (pattern == null) throw new IllegalArgumentException("pattern is null"); + + SimpleDateFormat formatter = new SimpleDateFormat(pattern, Locale.US); + formatter.setTimeZone(GMT); + return formatter.format(date); + } + + /** This class should not be instantiated. */ + private DateUtils() { + } + +} diff --git a/module-client/src/main/java/org/apache/http/impl/cookie/RFC2109Spec.java b/module-client/src/main/java/org/apache/http/impl/cookie/RFC2109Spec.java index 59698c52d..745c37684 100644 --- a/module-client/src/main/java/org/apache/http/impl/cookie/RFC2109Spec.java +++ b/module-client/src/main/java/org/apache/http/impl/cookie/RFC2109Spec.java @@ -1,5 +1,5 @@ /* - * $HeadRL$ + * $HeadURL$ * $Revision$ * $Date$ * @@ -42,7 +42,7 @@ import org.apache.http.cookie.MalformedCookieException; import org.apache.http.cookie.SM; import org.apache.http.message.BufferedHeader; import org.apache.http.util.CharArrayBuffer; -import org.apache.http.util.DateUtils; + /** * RFC 2109 compliant cookie policy