commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mbe...@apache.org
Subject cvs commit: jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie CookieSpecBase.java
Date Thu, 17 Apr 2003 03:00:31 GMT
mbecke      2003/04/16 20:00:31

  Modified:    httpclient/src/java/org/apache/commons/httpclient/cookie
                        CookieSpecBase.java
  Added:       httpclient/src/java/org/apache/commons/httpclient/util
                        DateParser.java DateParseException.java
  Log:
  Moved header/cookie date parsing to a new DateParser class.
  
  PR: 19050
  Submitted by: Chris Brown and Michael Becke
  Reviewed by: Oleg Kalnichevski
  
  Revision  Changes    Path
  1.1                  jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util/DateParser.java
  
  Index: DateParser.java
  ===================================================================
  package org.apache.commons.httpclient.util;
  
  import java.text.ParseException;
  import java.text.SimpleDateFormat;
  import java.util.Date;
  import java.util.Locale;
  import java.util.TimeZone;
  
  /**
   * A utility class for parsing 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 DateParser {
  
      /**
       * 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 
       * <code>asctime()</code> format.
       */
      public static final String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy";
  
      /** The patterns used for parsing dates */
      private static final String[] DATE_PATTERNS = {
          PATTERN_RFC1123,
          PATTERN_RFC1036,
          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",
          "EEE dd-MMM-yyyy HH:mm:ss z",
          "EEE dd MMM yyyy HH:mm:ss z",
          "EEE dd-MMM-yyyy HH-mm-ss z",
          "EEE dd-MMM-yy HH:mm:ss z",
          "EEE dd MMM yy HH:mm:ss z",
      };
  
      /**
       * Parses a date value.
       *
       * @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, DATE_PATTERNS);
      }
      
      /**
       * Parses the date value using the array of 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
       */
      private static Date parseDate(
          String dateValue, 
          String[] dateFormats
      ) throws DateParseException {
          
          if (dateValue == null) {
              throw new IllegalArgumentException("dateValue is null");
          }
          
          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"));
              } 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);        
      }
  
      /** This class should not be instantiated. */    
      private DateParser() { }
      
  }
  
  
  1.1                  jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util/DateParseException.java
  
  Index: DateParseException.java
  ===================================================================
  package org.apache.commons.httpclient.util;
  
  
  /**
   * An exception to indicate an error parsing a date string.
   * 
   * @see DateParser
   * 
   * @author Michael Becke
   */
  public class DateParseException extends Exception {
  
      /**
       * 
       */
      public DateParseException() {
          super();
      }
  
      /**
       * @param message the exception message
       */
      public DateParseException(String message) {
          super(message);
      }
  
  }
  
  
  
  1.14      +16 -41    jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie/CookieSpecBase.java
  
  Index: CookieSpecBase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie/CookieSpecBase.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- CookieSpecBase.java	7 Mar 2003 18:23:45 -0000	1.13
  +++ CookieSpecBase.java	17 Apr 2003 03:00:31 -0000	1.14
  @@ -63,18 +63,17 @@
   
   package org.apache.commons.httpclient.cookie;
   
  -import java.util.List;
  -import java.util.LinkedList;
   import java.util.Date;
  -import java.util.Locale;   
  -import java.text.DateFormat; 
  -import java.text.ParseException; 
  -import java.text.SimpleDateFormat;  
  -import org.apache.commons.httpclient.NameValuePair;
  -import org.apache.commons.httpclient.HttpException;
  +import java.util.LinkedList;
  +import java.util.List;
  +
  +import org.apache.commons.httpclient.Cookie;
   import org.apache.commons.httpclient.Header;
   import org.apache.commons.httpclient.HeaderElement;
  -import org.apache.commons.httpclient.Cookie;
  +import org.apache.commons.httpclient.HttpException;
  +import org.apache.commons.httpclient.NameValuePair;
  +import org.apache.commons.httpclient.util.DateParseException;
  +import org.apache.commons.httpclient.util.DateParser;
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
   
  @@ -95,26 +94,11 @@
    * 
    * @since 2.0 
    */
  -
   public class CookieSpecBase implements CookieSpec {
  +    
       /** Log object */
       protected static final Log LOG = LogFactory.getLog(CookieSpec.class);
   
  -    /** List of valid date formats for the "expires" cookie attribute. */
  -    private static final DateFormat[] EXPIRY_FORMATS = {
  -       // RFC 1123, 822, Date and time specification is English.
  -       new SimpleDateFormat("EEE, dd-MMM-yy HH:mm:ss z", Locale.US),
  -       new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss z", Locale.US),
  -       new SimpleDateFormat("EEE dd-MMM-yy HH:mm:ss z", Locale.US),
  -       new SimpleDateFormat("EEE dd-MMM-yyyy HH:mm:ss z", Locale.US),
  -       new SimpleDateFormat("EEE dd MMM yy HH:mm:ss z", Locale.US),
  -       new SimpleDateFormat("EEE dd MMM yyyy HH:mm:ss z", Locale.US),
  -       new SimpleDateFormat("EEE, dd MMM yy HH:mm:ss z", Locale.US),
  -       new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US),
  -       new SimpleDateFormat("EEE, dd-MMM-yyyy HH-mm-ss z", Locale.US),
  -       new SimpleDateFormat("EEE dd-MMM-yyyy HH-mm-ss z", Locale.US)
  -    };
  -
       /** Default constructor */
       public CookieSpecBase() {
           super();
  @@ -348,7 +332,6 @@
                   throw new MalformedCookieException(
                       "Missing value for expires attribute");
               }
  -            boolean set = false;
               // trim single quotes around expiry if present
               // see http://nagoya.apache.org/bugzilla/show_bug.cgi?id=5279
               if (paramValue.length() > 1 
  @@ -358,18 +341,10 @@
                       = paramValue.substring (1, paramValue.length() - 1);
               }
   
  -            for (int k = 0; k < EXPIRY_FORMATS.length; k++) {
  -
  -                try {
  -                    Date date = EXPIRY_FORMATS[k].parse(paramValue);
  -                    cookie.setExpiryDate(date);
  -                    set = true;
  -                    break;
  -                } catch (ParseException e) {
  -                    //Ignore and move on
  -                }
  -            }
  -            if (!set) {
  +            try {
  +                cookie.setExpiryDate(DateParser.parseDate(paramValue));
  +            } catch (DateParseException dpe) {
  +                LOG.debug("Error parsing cookie date", dpe);
                   throw new MalformedCookieException(
                       "Unable to parse expiration date parameter: " 
                       + paramValue);
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message