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 2007/12/18 11:27:39 UTC

svn commit: r605170 - in /geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc: AsyncHttpClient.java codec/ResponseFuture.java

Author: rickmcguire
Date: Tue Dec 18 02:27:38 2007
New Revision: 605170

URL: http://svn.apache.org/viewvc?rev=605170&view=rev
Log:
GERONIMO-3711 NPE if connection fails and callback is not provided

Patch provided by Sangjin Lee. 


Modified:
    geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/AsyncHttpClient.java
    geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/ResponseFuture.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=605170&r1=605169&r2=605170&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 Tue Dec 18 02:27:38 2007
@@ -427,8 +427,9 @@
         if (future == null) {
             future = openConnection(message);
         }
-        future.addListener(new FutureListener(message));
-        return message.getResponseFuture();
+        ResponseFuture response = message.getResponseFuture();
+        future.addListener(new FutureListener(message, response));
+        return response;
     }
     
     private ConnectFuture openConnection(HttpRequestMessage message) {
@@ -501,14 +502,18 @@
 
         /** The request. */
         final HttpRequestMessage request;
+        /** The response future. */
+        final ResponseFuture response;
 
         /**
          * Instantiates a new future listener for a connection.
          *
          * @param request the <code>HttpRequestMessage</code> request that is to be sent.
+         * @param response the response future object.
          */
-        public FutureListener(HttpRequestMessage request) {
+        public FutureListener(HttpRequestMessage request, ResponseFuture response) {
             this.request = request;
+            this.response = response;
         }
 
         /**
@@ -558,10 +563,10 @@
             } else {
                 try {
                     future.getSession();
-                    request.getCallback().onException(new AsyncHttpClientException("Connection failed."));
+                    response.setException(new AsyncHttpClientException("Connection failed."));
                 } catch (RuntimeIOException e) {
-                    //Set the callback exception
-                    request.getCallback().onException(e);
+                    //Set the future exception
+                    response.setException(e);
                 }
             }
         }

Modified: geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/ResponseFuture.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/ResponseFuture.java?rev=605170&r1=605169&r2=605170&view=diff
==============================================================================
--- geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/ResponseFuture.java (original)
+++ geronimo/sandbox/AsyncHttpClient/src/main/java/org/apache/ahc/codec/ResponseFuture.java Tue Dec 18 02:27:38 2007
@@ -36,94 +36,94 @@
  * Also, cancellation through this future is not supported.
  */
 public class ResponseFuture extends FutureTask<HttpResponseMessage> 
-		implements Future<HttpResponseMessage> {
-	// dummy instance because I use the FutureTask constructor
-	// is not really used...
-	private static final Callable<HttpResponseMessage> DUMMY = 
-			new Callable<HttpResponseMessage>() {
-				public HttpResponseMessage call() throws Exception {
-					return null;
-				}
-	};
-	
-	private final HttpRequestMessage request;
-	private final BlockingQueue<ResponseFuture> queue;
-	private final AsyncHttpClientCallback callback;
-	
-	/**
-	 * Constructor.  Optionally one can pass in the completion queue and/or the
-	 * callback object.
-	 * 
-	 * @param queue optional completion queue.  If not null, this future will be
-	 * placed in the queue on completion.
-	 * @param callback optional callback object.  If not null, the callback will
-	 * be invoked at proper stages on completion.
-	 */
-	public ResponseFuture(HttpRequestMessage request, 
-			BlockingQueue<ResponseFuture> queue) {
-		super(DUMMY);
-		this.request = request;
-		this.queue = queue;
-		this.callback = request.getCallback();
-	}
-	
-	public HttpRequestMessage getRequest() {
-		return request;
-	}
-	
-	/**
-	 * On completion, adds this future object to the completion queue if it is 
-	 * not null.
-	 */
-	@Override
-	protected void done() {
-		// add itself to the blocking queue so clients can pick it up
-		if (queue != null) {
-			queue.add(this);
-		}
-	}
+        implements Future<HttpResponseMessage> {
+    // dummy instance because I use the FutureTask constructor
+    // is not really used...
+    private static final Callable<HttpResponseMessage> DUMMY = 
+            new Callable<HttpResponseMessage>() {
+                public HttpResponseMessage call() throws Exception {
+                    return null;
+                }
+    };
+    
+    private final HttpRequestMessage request;
+    private final BlockingQueue<ResponseFuture> queue;
+    private final AsyncHttpClientCallback callback;
+    
+    /**
+     * Constructor.  Optionally one can pass in the completion queue and/or the
+     * callback object.
+     * 
+     * @param queue optional completion queue.  If not null, this future will be
+     * placed in the queue on completion.
+     * @param callback optional callback object.  If not null, the callback will
+     * be invoked at proper stages on completion.
+     */
+    public ResponseFuture(HttpRequestMessage request, 
+            BlockingQueue<ResponseFuture> queue) {
+        super(DUMMY);
+        this.request = request;
+        this.queue = queue;
+        this.callback = request.getCallback();
+    }
+    
+    public HttpRequestMessage getRequest() {
+        return request;
+    }
+    
+    /**
+     * On completion, adds this future object to the completion queue if it is 
+     * not null.
+     */
+    @Override
+    protected void done() {
+        // add itself to the blocking queue so clients can pick it up
+        if (queue != null) {
+            queue.add(this);
+        }
+    }
 
-	/**
-	 * Sets the response and completes the future.  If a non-null callback was
-	 * provided, the callback will be invoked 
-	 * (<tt>AsyncHttpClientCallback.onResponse()</tt>) on the thread on which
-	 * this method is invoked.
-	 */
-	@Override
-	protected void set(HttpResponseMessage v) {
-		super.set(v);
-		// fire the callback once the future is done
-		if (callback != null) {
-			callback.onResponse(v);
-		}
-	}
+    /**
+     * Sets the response and completes the future.  If a non-null callback was
+     * provided, the callback will be invoked 
+     * (<tt>AsyncHttpClientCallback.onResponse()</tt>) on the thread on which
+     * this method is invoked.
+     */
+    @Override
+    public void set(HttpResponseMessage v) {
+        super.set(v);
+        // fire the callback once the future is done
+        if (callback != null) {
+            callback.onResponse(v);
+        }
+    }
 
-	/**
-	 * Sets the exception and completes the future.  If a non-null callback was
-	 * provided, the callback will be invoked 
-	 * (<tt>AsyncHttpClientCallback.onException()</tt> or 
-	 * <tt>AsyncHttpClientCallback.onTimeout()</tt>) on the thread on which
-	 * this method is invoked.
-	 */
-	@Override
-	protected void setException(Throwable t) {
-		super.setException(t);
-		// fire the callback once the future is done
-		if (callback != null) {
-			if (t instanceof TimeoutException) {
-				callback.onTimeout();
-			} else {
-				callback.onException(t);
-			}
-		}
-	}
-	
-	/**
-	 * Canceling via this method is not allowed.  An 
-	 * UnsupportedOperationException will be thrown.
-	 */
-	@Override
-	public boolean cancel(boolean mayInterruptIfRunning) {
-		throw new UnsupportedOperationException("we don't support canceling asynchronous HTTP request via this method...");
-	}
+    /**
+     * Sets the exception and completes the future.  If a non-null callback was
+     * provided, the callback will be invoked 
+     * (<tt>AsyncHttpClientCallback.onException()</tt> or 
+     * <tt>AsyncHttpClientCallback.onTimeout()</tt>) on the thread on which
+     * this method is invoked.
+     */
+    @Override
+    public void setException(Throwable t) {
+        super.setException(t);
+        // fire the callback once the future is done
+        if (callback != null) {
+            if (t instanceof TimeoutException) {
+                callback.onTimeout();
+            } else {
+                callback.onException(t);
+            }
+        }
+    }
+    
+    /**
+     * Canceling via this method is not allowed.  An 
+     * UnsupportedOperationException will be thrown.
+     */
+    @Override
+    public boolean cancel(boolean mayInterruptIfRunning) {
+        throw new UnsupportedOperationException("we don't support canceling asynchronous HTTP request via this method...");
+    }
 }