geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jgenen...@apache.org
Subject svn commit: r578990 - in /geronimo/sandbox/AsyncHttpClient/src: main/java/org/apache/ahc/ main/java/org/apache/ahc/codec/ test/catalina/webapps/ROOT/ test/java/org/apache/ahc/
Date Mon, 24 Sep 2007 21:48:47 GMT
Author: jgenender
Date: Mon Sep 24 14:48:44 2007
New Revision: 578990

URL: http://svn.apache.org/viewvc?rev=578990&view=rev
Log:
Added redirect support

Added:
    geronimo/sandbox/AsyncHttpClient/src/test/catalina/webapps/ROOT/redirect.jsp
Modified:
    geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java
    geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpIoHandler.java
    geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpRequestMessage.java
    geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/AbstractTest.java
    geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/AsyncHttpClientTest.java

Modified: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java?rev=578990&r1=578989&r2=578990&view=diff
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java (original)
+++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java Mon
Sep 24 14:48:44 2007
@@ -87,7 +87,7 @@
         String host = message.getHost();
         int port = message.getPort();
         ConnectFuture future = connector.connect(new InetSocketAddress(host, port), handler);
-        future.addListener(new FutureListener(message));
+        future.addListener(new FutureListener(this, message));
     }
 
     public int getConnectionTimeout() {
@@ -114,8 +114,10 @@
     class FutureListener implements IoFutureListener {
 
         HttpRequestMessage request;
+        AsyncHttpClient client;
 
-        public FutureListener(HttpRequestMessage request) {
+        public FutureListener(AsyncHttpClient client, HttpRequestMessage request) {
+            this.client = client;
             this.request = request;
         }
 
@@ -147,6 +149,8 @@
                                 new HttpProtocolCodecFactory(request.getUrl())));
 
                 sess.setAttribute(HttpIoHandler.CURRENT_REQUEST, request);
+                
+                sess.setAttachment(client);
 
                 sess.write(request);
             } else {

Modified: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpIoHandler.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpIoHandler.java?rev=578990&r1=578989&r2=578990&view=diff
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpIoHandler.java
(original)
+++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpIoHandler.java
Mon Sep 24 14:48:44 2007
@@ -23,8 +23,10 @@
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
+import java.net.URL;
 
 import org.apache.ahc.AsyncHttpClientCallback;
+import org.apache.ahc.AsyncHttpClient;
 import org.apache.mina.common.IoHandlerAdapter;
 import org.apache.mina.common.IoSession;
 
@@ -54,6 +56,26 @@
         HttpResponseMessage response = (HttpResponseMessage)object;
 
         HttpRequestMessage request = (HttpRequestMessage)ioSession.getAttribute(CURRENT_REQUEST);
+
+
+        //Check if we are to handle redirects
+        if ((response.getStatusCode() == 301
+            || response.getStatusCode() == 302
+            || response.getStatusCode() == 307)
+            && request.isFollowRedirects()){
+            AsyncHttpClient client = (AsyncHttpClient)ioSession.getAttachment();
+
+            //Change the request url to the redirect
+            String query = request.getUrl().getQuery();
+            request.setUrl(new URL(response.getLocation() + (query == null ? "" : "?" + query)));
+
+            //Send the redirect
+            client.sendRequest(request);
+
+            //Close the current session since we are done with it
+            ioSession.close();
+            return;
+        }
 
         cancelTasks(request);
 

Modified: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpRequestMessage.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpRequestMessage.java?rev=578990&r1=578989&r2=578990&view=diff
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpRequestMessage.java
(original)
+++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/HttpRequestMessage.java
Mon Sep 24 14:48:44 2007
@@ -97,6 +97,10 @@
         return url;
     }
 
+    public void setUrl(URL url) {
+        this.url = url;
+    }
+
     public String getPath() {
         return url.getPath();
     }

Added: geronimo/sandbox/AsyncHttpClient/src/test/catalina/webapps/ROOT/redirect.jsp
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/test/catalina/webapps/ROOT/redirect.jsp?rev=578990&view=auto
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/test/catalina/webapps/ROOT/redirect.jsp (added)
+++ geronimo/sandbox/AsyncHttpClient/src/test/catalina/webapps/ROOT/redirect.jsp Mon Sep 24
14:48:44 2007
@@ -0,0 +1,3 @@
+<%
+    response.sendRedirect("http://localhost:8282/index.jsp");
+%>

Modified: geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/AbstractTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/AbstractTest.java?rev=578990&r1=578989&r2=578990&view=diff
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/AbstractTest.java (original)
+++ geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/AbstractTest.java Mon Sep
24 14:48:44 2007
@@ -128,9 +128,6 @@
 
         public void onClosed() {
             closed = true;
-            synchronized (semaphore) {
-                semaphore.notify();
-            }
         }
 
         public void onTimeout() {

Modified: geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/AsyncHttpClientTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/AsyncHttpClientTest.java?rev=578990&r1=578989&r2=578990&view=diff
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/AsyncHttpClientTest.java
(original)
+++ geronimo/sandbox/AsyncHttpClient/src/test/java/org/apache/ahc/AsyncHttpClientTest.java
Mon Sep 24 14:48:44 2007
@@ -32,7 +32,7 @@
 
     public void testHtmlConnection() throws Exception {
         TestCallback callback = new TestCallback();
-        doGetConnection(callback, "http://localhost:8282/", false);
+        doGetConnection(callback, "http://localhost:8282/", false, true);
 
         HttpResponseMessage msg = callback.getMessage();
         assertEquals("\nHello World!", msg.getStringContent());
@@ -40,12 +40,30 @@
 
     public void testSSLHtmlConnection() throws Exception {
         TestCallback callback = new TestCallback();
-        doGetConnection(callback, "https://localhost:8383/", false);
+        doGetConnection(callback, "https://localhost:8383/", false, true);
 
         HttpResponseMessage msg = callback.getMessage();
         assertEquals("\nHello World!", msg.getStringContent());
     }
 
+    public void testRedirect() throws Exception {
+        TestCallback callback = new TestCallback();
+
+        //Test that we are following redirects
+        doGetConnection(callback, "http://localhost:8282/redirect.jsp", false, true);
+
+        HttpResponseMessage msg = callback.getMessage();
+        assertEquals("\nHello World!", msg.getStringContent());
+
+        //Test that we are not following redirects
+        callback.clear();
+        doGetConnection(callback, "http://localhost:8282/redirect.jsp", false, false);
+
+        msg = callback.getMessage();
+        assertEquals(302, msg.getStatusCode());
+        assertEquals(msg.getLocation(), "http://localhost:8282/index.jsp"); 
+    }
+
     public void testBinaryRequest() throws Exception {
 
         //Get the real file
@@ -55,7 +73,7 @@
         fis.read(realFile);
 
         TestCallback callback = new TestCallback();
-        doGetConnection(callback, "http://localhost:8282/pwrd_apache.gif", false);
+        doGetConnection(callback, "http://localhost:8282/pwrd_apache.gif", false, true);
 
         HttpResponseMessage msg = callback.getMessage();
 
@@ -71,7 +89,7 @@
         fis.read(realFile);
 
         TestCallback callback = new TestCallback();
-        doGetConnection(callback, "https://localhost:8383/pwrd_apache.gif", false);
+        doGetConnection(callback, "https://localhost:8383/pwrd_apache.gif", false, true);
 
         HttpResponseMessage msg = callback.getMessage();
 
@@ -80,7 +98,7 @@
 
     public void testGetParameters() throws Exception {
         TestCallback callback = new TestCallback();
-        doGetConnection(callback, "http://localhost:8282/params.jsp", false);
+        doGetConnection(callback, "http://localhost:8282/params.jsp", false, true);
 
         HttpResponseMessage msg = callback.getMessage();
         assertEquals("Test One Test Two", msg.getStringContent());
@@ -94,9 +112,12 @@
         assertEquals("Test One Test Two", msg.getStringContent());
     }
 
-    private void doGetConnection(TestCallback callback, String url, boolean testForException)
+    private void doGetConnection(TestCallback callback, String url,
+                                 boolean testForException, boolean followRedirects)
         throws Exception {
         HttpRequestMessage request = new HttpRequestMessage(new URL(url), callback);
+        request.setFollowRedirects(followRedirects);
+
         request.setParameter("TEST1", "Test One");
         request.setParameter("TEST2", "Test Two");
         doConnection(request, false);



Mime
View raw message