Index: java/org/apache/commons/httpclient/cookie/CookieSpecBase.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie/CookieSpecBase.java,v retrieving revision 1.27 diff -u -r1.27 CookieSpecBase.java --- java/org/apache/commons/httpclient/cookie/CookieSpecBase.java 14 Sep 2004 20:11:31 -0000 1.27 +++ java/org/apache/commons/httpclient/cookie/CookieSpecBase.java 1 Nov 2004 02:47:48 -0000 @@ -39,7 +39,7 @@ import org.apache.commons.httpclient.HeaderElement; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.util.DateParseException; -import org.apache.commons.httpclient.util.DateParser; +import org.apache.commons.httpclient.util.DateUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -157,7 +157,7 @@ i2 = header.length(); } try { - DateParser.parseDate(header.substring(i1, i2), this.datepatterns); + DateUtil.parseDate(header.substring(i1, i2), this.datepatterns); isNetscapeCookie = true; } catch (DateParseException e) { // Does not look like a valid expiry date @@ -324,7 +324,7 @@ } try { - cookie.setExpiryDate(DateParser.parseDate(paramValue, this.datepatterns)); + cookie.setExpiryDate(DateUtil.parseDate(paramValue, this.datepatterns)); } catch (DateParseException dpe) { LOG.debug("Error parsing cookie date", dpe); throw new MalformedCookieException( Index: java/org/apache/commons/httpclient/params/DefaultHttpParamsFactory.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/params/DefaultHttpParamsFactory.java,v retrieving revision 1.14 diff -u -r1.14 DefaultHttpParamsFactory.java --- java/org/apache/commons/httpclient/params/DefaultHttpParamsFactory.java 13 Oct 2004 00:56:56 -0000 1.14 +++ java/org/apache/commons/httpclient/params/DefaultHttpParamsFactory.java 1 Nov 2004 02:47:48 -0000 @@ -36,7 +36,7 @@ import org.apache.commons.httpclient.HttpVersion; import org.apache.commons.httpclient.SimpleHttpConnectionManager; import org.apache.commons.httpclient.cookie.CookiePolicy; -import org.apache.commons.httpclient.util.DateParser; +import org.apache.commons.httpclient.util.DateUtil; /** * TODO: comment type @@ -80,9 +80,9 @@ datePatterns.addAll( Arrays.asList( new String[] { - DateParser.PATTERN_RFC1123, - DateParser.PATTERN_RFC1036, - DateParser.PATTERN_ASCTIME, + DateUtil.PATTERN_RFC1123, + DateUtil.PATTERN_RFC1036, + DateUtil.PATTERN_ASCTIME, "EEE, dd-MMM-yyyy HH:mm:ss z", "EEE, dd-MMM-yyyy HH-mm-ss z", "EEE, dd MMM yy HH:mm:ss z", Index: java/org/apache/commons/httpclient/util/DateParseException.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util/DateParseException.java,v retrieving revision 1.4 diff -u -r1.4 DateParseException.java --- java/org/apache/commons/httpclient/util/DateParseException.java 18 Apr 2004 23:51:38 -0000 1.4 +++ java/org/apache/commons/httpclient/util/DateParseException.java 1 Nov 2004 02:47:49 -0000 @@ -33,7 +33,7 @@ /** * An exception to indicate an error parsing a date string. * - * @see DateParser + * @see DateUtil * * @author Michael Becke */ Index: java/org/apache/commons/httpclient/util/DateParser.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util/DateParser.java,v retrieving revision 1.10 diff -u -r1.10 DateParser.java --- java/org/apache/commons/httpclient/util/DateParser.java 14 Sep 2004 20:11:32 -0000 1.10 +++ java/org/apache/commons/httpclient/util/DateParser.java 1 Nov 2004 02:47:49 -0000 @@ -48,6 +48,8 @@ * * @author Christopher Brown * @author Michael Becke + * + * @deprecated Use {@link org.apache.commons.httpclient.util.DateUtil} */ public class DateParser { Index: java/org/apache/commons/httpclient/util/DateUtil.java =================================================================== RCS file: java/org/apache/commons/httpclient/util/DateUtil.java diff -N java/org/apache/commons/httpclient/util/DateUtil.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ java/org/apache/commons/httpclient/util/DateUtil.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,179 @@ +/* + * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util/DateParser.java,v 1.10 2004/09/14 20:11:32 olegk Exp $ + * $Revision: 1.10 $ + * $Date: 2004/09/14 20:11:32 $ + * + * ==================================================================== + * + * Copyright 1999-2004 The Apache Software Foundation + * + * Licensed 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.commons.httpclient.util; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Arrays; +import java.util.Collection; +import java.util.Date; +import java.util.Iterator; +import java.util.Locale; +import java.util.TimeZone; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * 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 class DateUtil { + + /** Log object for this class. */ + private static final Log LOG = LogFactory.getLog(DateUtil.class); + + /** + * 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 Collection DEFAULT_PATTERNS = Arrays.asList( + new String[] { PATTERN_ASCTIME, PATTERN_RFC1036, PATTERN_RFC1123 } ); + + /** + * 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); + } + + /** + * 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( + String dateValue, + Collection dateFormats + ) throws DateParseException { + + if (dateValue == null) { + throw new IllegalArgumentException("dateValue is null"); + } + if (dateFormats == null) { + dateFormats = DEFAULT_PATTERNS; + } + // trim single quotes around date if present + // see http://nagoya.apache.org/bugzilla/show_bug.cgi?id=5279 + if (dateValue.length() > 1 + && dateValue.startsWith("'") + && dateValue.endsWith("'") + ) { + dateValue = dateValue.substring (1, dateValue.length() - 1); + } + + SimpleDateFormat dateParser = null; + Iterator formatIter = dateFormats.iterator(); + + while (formatIter.hasNext()) { + String format = (String) formatIter.next(); + if (dateParser == null) { + dateParser = new SimpleDateFormat(format, Locale.US); + dateParser.setTimeZone(TimeZone.getTimeZone("GMT")); + } else { + dateParser.applyPattern(format); + } + 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); + return formatter.format(date); + } + + /** This class should not be instantiated. */ + private DateUtil() { } + +}