Author: mheath
Date: Tue Feb  5 15:34:41 2008
New Revision: 618826

URL: http://svn.apache.org/viewvc?rev=618826&view=rev
Log:
ASYNCWEB-2 Refactored AHC code to use o.a.a.common.Cookie
Added support for expiration date to o.a.a.common.Cookie,MutableCookie, and 
DefaultCookie.

Modified:
    
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/HttpDecoder.java
    
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/HttpMessage.java
    
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/HttpRequestEncoder.java
    
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/util/ParameterFormatter.java
    
mina/asyncweb/trunk/common/src/main/java/org/apache/asyncweb/common/Cookie.java
    
mina/asyncweb/trunk/common/src/main/java/org/apache/asyncweb/common/DefaultCookie.java
    
mina/asyncweb/trunk/common/src/main/java/org/apache/asyncweb/common/MutableCookie.java

Modified: 
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/HttpDecoder.java
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/HttpDecoder.java?rev=618826&r1=618825&r2=618826&view=diff
==============================================================================
--- 
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/HttpDecoder.java 
(original)
+++ 
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/HttpDecoder.java 
Tue Feb  5 15:34:41 2008
@@ -22,10 +22,12 @@
 import java.io.IOException;
 import java.nio.charset.Charset;
 import java.nio.charset.CharsetDecoder;
-import java.util.Date;
 
 import org.apache.ahc.util.DateUtil;
 import org.apache.ahc.util.NameValuePair;
+import org.apache.asyncweb.common.Cookie;
+import org.apache.asyncweb.common.DefaultCookie;
+import org.apache.asyncweb.common.MutableCookie;
 import org.apache.mina.common.IoBuffer;
 
 /**
@@ -272,7 +274,7 @@
      */
     public Cookie decodeCookie(String cookieStr) throws Exception {
 
-        Cookie cookie = null;
+        MutableCookie cookie = null;
 
         String pairs[] = cookieStr.split(";");
         for (int i = 0; i < pairs.length; i++) {
@@ -281,7 +283,7 @@
 
             //First one is the cookie name/value pair
             if (i == 0) {
-                cookie = new Cookie(name, nameValue[1].trim());
+                cookie = new DefaultCookie(name, nameValue[1].trim());
                 continue;
             }
 
@@ -304,11 +306,14 @@
 
             if (name.equalsIgnoreCase(COOKIE_MAX_AGE)) {
                 int age = Integer.parseInt(nameValue[1]);
-                cookie.setExpires(new Date(System.currentTimeMillis() + age * 
1000L));
+                cookie.setMaxAge(age);
             }
 
             if (name.equalsIgnoreCase(COOKIE_EXPIRES)) {
-                cookie.setExpires(DateUtil.parseDate(nameValue[1]));
+               long createdDate = System.currentTimeMillis();
+               int age = (int)(DateUtil.parseDate(nameValue[1]).getTime() - 
createdDate) / 1000;
+               cookie.setCreatedDate(createdDate);
+                cookie.setMaxAge(age);
             }
 
             if (name.equalsIgnoreCase(COOKIE_DOMAIN)) {

Modified: 
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/HttpMessage.java
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/HttpMessage.java?rev=618826&r1=618825&r2=618826&view=diff
==============================================================================
--- 
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/HttpMessage.java 
(original)
+++ 
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/HttpMessage.java 
Tue Feb  5 15:34:41 2008
@@ -26,6 +26,7 @@
 import java.util.List;
 
 import org.apache.ahc.util.NameValuePair;
+import org.apache.asyncweb.common.Cookie;
 
 /**
  * The Class HttpMessage.  The base class for [EMAIL PROTECTED] 
HttpRequestMessage} and [EMAIL PROTECTED] HttpResponseMessage}.

Modified: 
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/HttpRequestEncoder.java
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/HttpRequestEncoder.java?rev=618826&r1=618825&r2=618826&view=diff
==============================================================================
--- 
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/HttpRequestEncoder.java
 (original)
+++ 
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/codec/HttpRequestEncoder.java
 Tue Feb  5 15:34:41 2008
@@ -32,6 +32,7 @@
 import org.apache.ahc.auth.AuthState;
 import org.apache.ahc.util.EncodingUtil;
 import org.apache.ahc.util.NameValuePair;
+import org.apache.asyncweb.common.Cookie;
 import org.apache.mina.common.IoSession;
 import org.apache.mina.common.IoBuffer;
 import org.apache.mina.filter.codec.ProtocolEncoderAdapter;

Modified: 
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/util/ParameterFormatter.java
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/util/ParameterFormatter.java?rev=618826&r1=618825&r2=618826&view=diff
==============================================================================
--- 
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/util/ParameterFormatter.java
 (original)
+++ 
mina/asyncweb/trunk/client/src/main/java/org/apache/ahc/util/ParameterFormatter.java
 Tue Feb  5 15:34:41 2008
@@ -210,12 +210,12 @@
     }
 
     /**
-     * Produces textual representaion of the attribute/value pair using
+     * Produces textual representation of the attribute/value pair using
      * formatting rules defined in RFC 2616
      *
      * @param param the parameter to be formatted
      *
-     * @return RFC 2616 conformant textual representaion of the
+     * @return RFC 2616 conformant textual representation of the
      * attribute/value pair
      */
     public String format(final NameValuePair param) {

Modified: 
mina/asyncweb/trunk/common/src/main/java/org/apache/asyncweb/common/Cookie.java
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/common/src/main/java/org/apache/asyncweb/common/Cookie.java?rev=618826&r1=618825&r2=618826&view=diff
==============================================================================
--- 
mina/asyncweb/trunk/common/src/main/java/org/apache/asyncweb/common/Cookie.java 
(original)
+++ 
mina/asyncweb/trunk/common/src/main/java/org/apache/asyncweb/common/Cookie.java 
Tue Feb  5 15:34:41 2008
@@ -81,4 +81,22 @@
      * @return <tt>null</tt> if no comment is specified
      */
     String getComment();
+    
+    /**
+     * Returns the date the cookie was create.  This value is not included in 
the HTTP
+     * header.  This is a convenience method for clients to determine when the 
cookie
+     * will expire.
+     * 
+     * @return  the date the cookie was created in milliseconds after Jan. 1, 
1970
+     */
+    long getCreatedDate();
+    
+    /**
+     * Returns the date the expiration date of the cookie.  This value is 
calculated
+     * based on the the values returned by [EMAIL PROTECTED] #getMaxAge()} and
+     * [EMAIL PROTECTED] #getCreatedDate()}.
+     * 
+     * @return  the expiration date of the cookie in milliseconds after Jan. 
1, 1970.
+     */
+    long getExpirationDate();
 }

Modified: 
mina/asyncweb/trunk/common/src/main/java/org/apache/asyncweb/common/DefaultCookie.java
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/common/src/main/java/org/apache/asyncweb/common/DefaultCookie.java?rev=618826&r1=618825&r2=618826&view=diff
==============================================================================
--- 
mina/asyncweb/trunk/common/src/main/java/org/apache/asyncweb/common/DefaultCookie.java
 (original)
+++ 
mina/asyncweb/trunk/common/src/main/java/org/apache/asyncweb/common/DefaultCookie.java
 Tue Feb  5 15:34:41 2008
@@ -19,6 +19,8 @@
  */
 package org.apache.asyncweb.common;
 
+import java.util.concurrent.TimeUnit;
+
 /**
  * Default cookie implementation.
  *
@@ -44,20 +46,38 @@
     private int version = 0;
 
     private int maxAge = -1;
+    
+    private long createdDate = System.currentTimeMillis();
 
     /**
-     * Constructor used to allow users to create new cookies to be sent
-     * back to the client.
+     * Creates a new cookie with a specified name.
      *
-     * @param name the name of the new cookie.
+     * @param name the name of the new cookie
      */
     public DefaultCookie(String name) {
         if (name == null) {
-            throw new NullPointerException("name");
+               throw new IllegalArgumentException("name can NOT be null");
         }
 
         this.name = name;
     }
+    
+    /**
+     * Creates a new cookie with a specified name and value.
+     * 
+     * @param name  the name of the new cookie
+     * @param value  the default value of the new cookie
+     */
+    public DefaultCookie(String name, String value) {
+       if (name == null) {
+               throw new IllegalArgumentException("name can NOT be null");
+       }
+       if (value == null) {
+               throw new IllegalArgumentException("name can NOT be null");
+       }
+       this.name = name;
+       this.value = value;
+    }
 
     public String getComment() {
         return comment;
@@ -118,12 +138,36 @@
     public String getName() {
         return name;
     }
+    
+    public long getCreatedDate() {
+       return createdDate;
+    }
+    
+    public void setCreatedDate(long createdDate) {
+               this.createdDate = createdDate;
+       }
 
-    @Override
-    public int hashCode() {
-        return getName().hashCode();
+    public long getExpirationDate() {
+       return createdDate + TimeUnit.SECONDS.toMillis(maxAge);
     }
+    
+    /**
+     * Builds the hash code of this object based on the <em>name</em>, 
<em>path</em>, and <em>domain</em> fields.
+     */
+    @Override
+       public int hashCode() {
+               final int prime = 31;
+               int result = 1;
+               result = prime * result + ((domain == null) ? 0 : 
domain.hashCode());
+               result = prime * result + ((name == null) ? 0 : 
name.hashCode());
+               result = prime * result + ((path == null) ? 0 : 
path.hashCode());
+               return result;
+       }
 
+    /**
+     * Determines the equality of this cookie to another Cookie object based 
on the <em>name</em>, <em>path</em>, and
+     * <em>domain</em> fields of the cookies. 
+     */
     @Override
     public boolean equals(Object o) {
         if (o == this) {

Modified: 
mina/asyncweb/trunk/common/src/main/java/org/apache/asyncweb/common/MutableCookie.java
URL: 
http://svn.apache.org/viewvc/mina/asyncweb/trunk/common/src/main/java/org/apache/asyncweb/common/MutableCookie.java?rev=618826&r1=618825&r2=618826&view=diff
==============================================================================
--- 
mina/asyncweb/trunk/common/src/main/java/org/apache/asyncweb/common/MutableCookie.java
 (original)
+++ 
mina/asyncweb/trunk/common/src/main/java/org/apache/asyncweb/common/MutableCookie.java
 Tue Feb  5 15:34:41 2008
@@ -19,6 +19,7 @@
  */
 package org.apache.asyncweb.common;
 
+
 /**
  * A mutable [EMAIL PROTECTED] Cookie}.
  *
@@ -80,4 +81,12 @@
      * Sets the comment of this cookie.  Comments are not supported by version 
0 cookies.
      */
     void setComment(String comment);
+    
+    /**
+     * Sets the date the cookie was created.  This value is never sent over 
HTTP
+     * but is used by clients to calculate the expiration date of the cookie.
+     * 
+     * @param date  the date the cookie was created in milliseconds after Jan. 
1, 1970.
+     */
+    void setCreatedDate(long date);
 }


Reply via email to