You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wink.apache.org by lr...@apache.org on 2011/03/12 08:20:01 UTC

svn commit: r1080880 - in /incubator/wink/trunk/wink-client-asynchttpclient: pom.xml src/main/java/org/apache/wink/client/AsyncHttpClientConnectionHandler.java

Author: lresende
Date: Sat Mar 12 07:20:01 2011
New Revision: 1080880

URL: http://svn.apache.org/viewvc?rev=1080880&view=rev
Log:
WINK-340 - Applying patch from Jason Dilon to update AHC integration to use lates 1.6.2 release

Modified:
    incubator/wink/trunk/wink-client-asynchttpclient/pom.xml
    incubator/wink/trunk/wink-client-asynchttpclient/src/main/java/org/apache/wink/client/AsyncHttpClientConnectionHandler.java

Modified: incubator/wink/trunk/wink-client-asynchttpclient/pom.xml
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-client-asynchttpclient/pom.xml?rev=1080880&r1=1080879&r2=1080880&view=diff
==============================================================================
--- incubator/wink/trunk/wink-client-asynchttpclient/pom.xml (original)
+++ incubator/wink/trunk/wink-client-asynchttpclient/pom.xml Sat Mar 12 07:20:01 2011
@@ -36,7 +36,7 @@
         <dependency>
             <groupId>com.ning</groupId>
             <artifactId>async-http-client</artifactId>
-            <version>1.3.3</version>
+            <version>1.6.2</version>
         </dependency>
 
         <dependency>

Modified: incubator/wink/trunk/wink-client-asynchttpclient/src/main/java/org/apache/wink/client/AsyncHttpClientConnectionHandler.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-client-asynchttpclient/src/main/java/org/apache/wink/client/AsyncHttpClientConnectionHandler.java?rev=1080880&r1=1080879&r2=1080880&view=diff
==============================================================================
--- incubator/wink/trunk/wink-client-asynchttpclient/src/main/java/org/apache/wink/client/AsyncHttpClientConnectionHandler.java (original)
+++ incubator/wink/trunk/wink-client-asynchttpclient/src/main/java/org/apache/wink/client/AsyncHttpClientConnectionHandler.java Sat Mar 12 07:20:01 2011
@@ -39,6 +39,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import javax.ws.rs.core.MultivaluedMap;
+import java.io.Closeable;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -46,12 +47,14 @@ import java.net.URI;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ExecutionException;
+import java.util.concurrent.atomic.AtomicReference;
 
 /**
  * Extends {@link AbstractConnectionHandler} and uses {@link AsyncHttpClient} to perform HTTP request execution.
  */
 public class AsyncHttpClientConnectionHandler
     extends AbstractConnectionHandler
+    implements Closeable
 {
     private static final Logger logger = LoggerFactory.getLogger(AsyncHttpClientConnectionHandler.class);
 
@@ -61,6 +64,10 @@ public class AsyncHttpClientConnectionHa
         this.asyncHttpClient = asyncHttpClient;
     }
 
+    public void close() throws IOException {
+        asyncHttpClient.close();
+    }
+
     public ClientResponse handle(final ClientRequest request, final HandlerContext context) throws Exception {
         Response response = processRequest(request, context);
         return processResponse(request, context, response);
@@ -73,18 +80,20 @@ public class AsyncHttpClientConnectionHa
 
         Request request = setupHttpRequest(cr, ncos, os);
         Response response;
+        final AtomicReference<Throwable> failureHolder = new AtomicReference<Throwable>();
 
         try {
             response = asyncHttpClient.executeRequest(request, new AsyncCompletionHandlerBase()
             {
                 @Override
                 public Response onCompleted(final Response response) throws Exception {
-                    logger.debug("Response received: {}", response);
+                    logger.trace("Response received: {}", response);
                     return super.onCompleted(response);
                 }
 
                 public void onThrowable(Throwable t) {
-                    logger.error(AsyncCompletionHandlerBase.class.getName(), t);
+                    logger.trace("Request failed", t);
+                    failureHolder.set(t);
                 }
             }).get();
         }
@@ -95,6 +104,18 @@ public class AsyncHttpClientConnectionHa
             throw (IOException)new IOException().initCause(e);
         }
 
+        // If a failure occurred, then decode and re-throw
+        Throwable failure = failureHolder.get();
+        if (failure != null) {
+            if (failure instanceof RuntimeException) {
+                throw (RuntimeException)failure;
+            }
+            if (failure instanceof IOException) {
+                throw (IOException)failure;
+            }
+            throw (IOException)new IOException().initCause(failure);
+        }
+
         return response;
     }
 
@@ -148,11 +169,30 @@ public class AsyncHttpClientConnectionHa
         return new AsyncHttpClient(c.build());
     }
 
+    /**
+     * An empty input stream to simulate an empty message body.
+     */
+    private static class EmptyInputStream
+        extends InputStream
+    {
+        @Override
+        public int read() throws IOException {
+            return -1;
+        }
+    }
+
     private ClientResponse processResponse(final ClientRequest request, final HandlerContext context, final Response response)
         throws IllegalStateException, IOException
     {
         ClientResponse cr = createResponse(request, response);
-        InputStream is = adaptInputStream(response.getResponseBodyAsStream(), cr, context.getInputStreamAdapters());
+        InputStream is;
+        if (response.hasResponseBody()) {
+            is = response.getResponseBodyAsStream();
+        }
+        else {
+            is = new EmptyInputStream();
+        }
+        is = adaptInputStream(is, cr, context.getInputStreamAdapters());
         cr.setEntity(is);
         return cr;
     }
@@ -162,17 +202,7 @@ public class AsyncHttpClientConnectionHa
         cr.setStatusCode(response.getStatusCode());
         cr.setMessage(response.getStatusText());
         cr.getAttributes().putAll(request.getAttributes());
-
-        // FIXME: Should we use a constant here to avoid creating this dummy runnable every time?
-        cr.setContentConsumer(new Runnable()
-        {
-            public void run() {
-                // empty
-            }
-        });
-
         processResponseHeaders(cr, response);
-
         return cr;
     }