You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by ri...@apache.org on 2008/02/15 12:17:14 UTC

svn commit: r628013 - in /geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec: HttpIoHandler.java HttpMessage.java HttpRequestEncoder.java

Author: rickmcguire
Date: Fri Feb 15 03:17:12 2008
New Revision: 628013

URL: http://svn.apache.org/viewvc?rev=628013&view=rev
Log:
GERONIMO-3846 cookies are not carried over when requests are redirected

Patch provided by Sangjin Lee 


Modified:
    geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpIoHandler.java
    geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpMessage.java
    geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpRequestEncoder.java

Modified: geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpIoHandler.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpIoHandler.java?rev=628013&r1=628012&r2=628013&view=diff
==============================================================================
--- geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpIoHandler.java (original)
+++ geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpIoHandler.java Fri Feb 15 03:17:12 2008
@@ -154,6 +154,8 @@
             // we also need to clear out the parameters and the content
             request.clearAllParameters();
             request.clearContent();
+            // make sure to add the cookies from the response
+            request.addCookies(response.getCookies());
 
             //Send the redirect
             client.sendRequest(request);
@@ -194,8 +196,11 @@
                     // cache the session before we return
                     cache.cacheSession(ioSession);
                 }
-                
+
+                // make sure to add the cookies from the response
+                request.addCookies(response.getCookies());
                 request.setAuthCount(authCount);
+                
                 client.sendRequest(request);
 
                 return;

Modified: geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpMessage.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpMessage.java?rev=628013&r1=628012&r2=628013&view=diff
==============================================================================
--- geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpMessage.java (original)
+++ geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpMessage.java Fri Feb 15 03:17:12 2008
@@ -22,8 +22,11 @@
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.ahc.util.NameValuePair;
 
@@ -48,7 +51,7 @@
     protected List<NameValuePair> headers = new ArrayList<NameValuePair>();
     
     /** The cookies associated with the message. */
-    protected List<Cookie> cookies = new ArrayList<Cookie>();
+    protected Map<String,Cookie> cookies = new HashMap<String,Cookie>();
     
     /** The content type. */
     protected String contentType;
@@ -119,17 +122,43 @@
      * 
      * @return the cookies
      */
-    public List<Cookie> getCookies() {
-        return cookies;
+    public Collection<Cookie> getCookies() {
+        return cookies.values();
     }
 
     /**
-     * Sets the cookies.
+     * Sets the cookies on the message.  Any existing cookies will be completely
+     * discarded.
      * 
      * @param cookies the new cookies
+     * @see #addCookies(Collection)
      */
-    public void setCookies(List<Cookie> cookies) {
-        this.cookies = cookies;
+    public void setCookies(Collection<Cookie> cookies) {
+        if (cookies == null) {
+            throw new IllegalArgumentException("null cookie set was passed in");
+        }
+        
+        Map<String,Cookie> newCookies = new HashMap<String,Cookie>();
+        for (Cookie cookie : cookies) {
+            newCookies.put(cookie.getName(), cookie);
+        }
+        this.cookies = newCookies;
+    }
+    
+    /**
+     * Adds the cookies to the message.  Only the existing cookies with the same
+     * names are replaced by the ones in the argument.
+     * 
+     * @see #setCookies(Collection)
+     */
+    public void addCookies(Collection<Cookie> cookies) {
+        if (cookies == null) {
+            return;
+        }
+        
+        for (Cookie cookie : cookies) {
+            addCookie(cookie);
+        }
     }
 
     /**
@@ -138,7 +167,11 @@
      * @param cookie the cookie
      */
     public void addCookie(Cookie cookie) {
-        this.cookies.add(cookie);
+        if (cookie == null) {
+            return;
+        }
+        
+        this.cookies.put(cookie.getName(), cookie);
     }
 
 

Modified: geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpRequestEncoder.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpRequestEncoder.java?rev=628013&r1=628012&r2=628013&view=diff
==============================================================================
--- geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpRequestEncoder.java (original)
+++ geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpRequestEncoder.java Fri Feb 15 03:17:12 2008
@@ -22,6 +22,7 @@
 import java.nio.charset.CharacterCodingException;
 import java.nio.charset.Charset;
 import java.nio.charset.CharsetEncoder;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
@@ -257,7 +258,7 @@
      */
     private void processCookies(HttpRequestMessage msg, IoBuffer buf, CharsetEncoder encoder)
         throws Exception {
-        List<Cookie> cookies = msg.getCookies();
+        Collection<Cookie> cookies = msg.getCookies();
         if (cookies.size() > 0) {
             buf.putString("Cookie: ", encoder);
             for (Cookie cookie : cookies) {