hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1040983 - in /httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http: impl/nio/client/ nio/client/
Date Wed, 01 Dec 2010 12:42:21 GMT
Author: olegk
Date: Wed Dec  1 12:42:20 2010
New Revision: 1040983

URL: http://svn.apache.org/viewvc?rev=1040983&view=rev
Log:
API changes; more code refactoring

Modified:
    httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/BasicHttpAsyncClient.java
    httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/BasicHttpAsyncExchangeHandler.java
    httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/ConnState.java
    httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/HttpAsyncExchange.java
    httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/NHttpClientProtocolHandler.java
    httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/nio/client/HttpAsyncExchangeHandler.java

Modified: httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/BasicHttpAsyncClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/BasicHttpAsyncClient.java?rev=1040983&r1=1040982&r2=1040983&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/BasicHttpAsyncClient.java
(original)
+++ httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/BasicHttpAsyncClient.java
Wed Dec  1 12:42:20 2010
@@ -52,6 +52,7 @@ import org.apache.http.params.CoreConnec
 import org.apache.http.params.CoreProtocolPNames;
 import org.apache.http.params.HttpParams;
 import org.apache.http.params.SyncBasicHttpParams;
+import org.apache.http.protocol.BasicHttpContext;
 import org.apache.http.protocol.HttpProcessor;
 import org.apache.http.protocol.ImmutableHttpProcessor;
 import org.apache.http.protocol.RequestConnControl;
@@ -129,9 +130,7 @@ public class BasicHttpAsyncClient implem
 
     private void doExecute() {
         NHttpClientProtocolHandler handler = new NHttpClientProtocolHandler(
-                createHttpProcessor(),
-                createConnectionReuseStrategy(),
-                this.params);
+                createConnectionReuseStrategy());
         IOEventDispatch ioEventDispatch = new InternalClientEventDispatch(handler, this.params);
         try {
             this.ioReactor.execute(ioEventDispatch);
@@ -170,9 +169,13 @@ public class BasicHttpAsyncClient implem
 
     public <T> Future<T> execute(
             final HttpAsyncExchangeHandler<T> handler, final FutureCallback<T>
callback) {
-        HttpRoute route = new HttpRoute(handler.getTarget());
         HttpAsyncExchange<T> httpexchange = new HttpAsyncExchange<T>(
-                route, null, this.sessmrg, handler, callback);
+                handler,
+                callback,
+                this.sessmrg,
+                createHttpProcessor(),
+                new BasicHttpContext(),
+                this.params);
         return httpexchange.getResultFuture();
     }
 

Modified: httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/BasicHttpAsyncExchangeHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/BasicHttpAsyncExchangeHandler.java?rev=1040983&r1=1040982&r2=1040983&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/BasicHttpAsyncExchangeHandler.java
(original)
+++ httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/BasicHttpAsyncExchangeHandler.java
Wed Dec  1 12:42:20 2010
@@ -48,12 +48,14 @@ public class BasicHttpAsyncExchangeHandl
     private final HttpHost target;
     private final HttpRequest request;
 
-    private HttpResponse response;
-    private ProducingNHttpEntity contentProducingEntity;
-    private ConsumingNHttpEntity contentConsumingEntity;
+    private volatile HttpResponse response;
+    private volatile ProducingNHttpEntity contentProducingEntity;
+    private volatile ConsumingNHttpEntity contentConsumingEntity;
+    private volatile HttpResponse result;
+    private volatile Exception ex;
+    private volatile boolean completed;
 
-    public BasicHttpAsyncExchangeHandler(
-            final HttpHost target, final HttpRequest request) {
+    public BasicHttpAsyncExchangeHandler(final HttpHost target, final HttpRequest request)
{
         super();
         if (target == null) {
             throw new IllegalArgumentException("Target host may not be null");
@@ -65,7 +67,7 @@ public class BasicHttpAsyncExchangeHandl
         this.request = request;
     }
 
-    public HttpRequest getRequest() {
+    public HttpRequest generateRequest() {
         return this.request;
     }
 
@@ -121,56 +123,87 @@ public class BasicHttpAsyncExchangeHandl
         return this.contentProducingEntity;
     }
 
-    public void produceContent(
+    public synchronized void produceContent(
             final ContentEncoder encoder, final IOControl ioctrl) throws IOException {
         ProducingNHttpEntity producer = getProducingHttpEntity();
         producer.produceContent(encoder, ioctrl);
     }
 
-    public void responseReceived(final HttpResponse response) {
+    public synchronized void responseReceived(final HttpResponse response) {
         if (this.response != null) {
             throw new IllegalStateException("HTTP response already set");
         }
         this.response = response;
     }
 
-    public void consumeContent(
+    public synchronized void consumeContent(
             final ContentDecoder decoder, final IOControl ioctrl) throws IOException {
         ConsumingNHttpEntity consumer = getConsumingHttpEntity();
         consumer.consumeContent(decoder, ioctrl);
     }
 
-    private void shutdown() {
+    private void reset() {
         if (this.contentProducingEntity != null) {
             try {
                 this.contentProducingEntity.finish();
+                this.contentProducingEntity = null;
             } catch (IOException ex) {
             }
         }
         if (this.contentConsumingEntity != null) {
             try {
                 this.contentConsumingEntity.finish();
+                this.contentConsumingEntity = null;
             } catch (IOException ex) {
             }
         }
     }
 
-    public void cancelled() {
-        shutdown();
+    public synchronized void cancel() {
+        if (this.completed) {
+            return;
+        }
+        this.completed = true;
+        this.response = null;
+        reset();
     }
 
-    public void failed(final Exception ex) {
-        shutdown();
+    public synchronized void failed(final Exception ex) {
+        if (this.completed) {
+            return;
+        }
+        this.completed = true;
+        this.ex = ex;
+        this.response = null;
+        reset();
     }
 
-    public HttpResponse completed() {
-        if (this.response == null) {
-            throw new IllegalStateException("HTTP response is null");
+    public synchronized void completed() {
+        if (this.completed) {
+            return;
+        }
+        this.completed = true;
+        if (this.response != null) {
+            this.result = this.response;
+            this.result.setEntity(this.contentConsumingEntity);
         }
-        HttpResponse response = this.response;
-        response.setEntity(this.contentConsumingEntity);
-        shutdown();
-        return response;
+        reset();
+    }
+
+    public boolean isCompleted() {
+        return this.completed;
+    }
+
+    public Exception getException() {
+        return this.ex;
+    }
+
+    public void setEx(Exception ex) {
+        this.ex = ex;
+    }
+
+    public HttpResponse getResult() {
+        return this.response;
     }
 
 }

Modified: httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/ConnState.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/ConnState.java?rev=1040983&r1=1040982&r2=1040983&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/ConnState.java
(original)
+++ httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/ConnState.java
Wed Dec  1 12:42:20 2010
@@ -28,6 +28,7 @@ package org.apache.http.impl.nio.client;
 
 import org.apache.http.HttpRequest;
 import org.apache.http.HttpResponse;
+import org.apache.http.nio.client.HttpAsyncExchangeHandler;
 
 class ConnState {
 
@@ -35,7 +36,7 @@ class ConnState {
     private MessageState responseState;
     private HttpRequest request;
     private HttpResponse response;
-    private HttpAsyncExchange<?> httpexchange;
+    private HttpAsyncExchangeHandler<?> handler;
     private boolean valid;
     private int timeout;
 
@@ -78,12 +79,12 @@ class ConnState {
         this.response = response;
     }
 
-    public void setHttpExchange(final HttpAsyncExchange<?> httpexchange) {
-        this.httpexchange = httpexchange;
+    public void setHandler(final HttpAsyncExchangeHandler<?> handler) {
+        this.handler = handler;
     }
 
-    public HttpAsyncExchange<?> getHttpExchange() {
-        return this.httpexchange;
+    public HttpAsyncExchangeHandler<?> getHandler() {
+        return this.handler;
     }
 
     public int getTimeout() {
@@ -107,7 +108,7 @@ class ConnState {
     public void reset() {
         resetInput();
         resetOutput();
-        this.httpexchange = null;
+        this.handler = null;
     }
 
     public boolean isValid() {

Modified: httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/HttpAsyncExchange.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/HttpAsyncExchange.java?rev=1040983&r1=1040982&r2=1040983&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/HttpAsyncExchange.java
(original)
+++ httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/HttpAsyncExchange.java
Wed Dec  1 12:42:20 2010
@@ -26,91 +26,150 @@
  */
 package org.apache.http.impl.nio.client;
 
+import java.io.IOException;
 import java.nio.channels.SelectionKey;
 import java.util.concurrent.Future;
 
+import org.apache.http.HttpException;
+import org.apache.http.HttpHost;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
 import org.apache.http.conn.routing.HttpRoute;
+import org.apache.http.nio.ContentDecoder;
+import org.apache.http.nio.ContentEncoder;
+import org.apache.http.nio.IOControl;
 import org.apache.http.nio.client.HttpAsyncExchangeHandler;
 import org.apache.http.nio.concurrent.BasicFuture;
 import org.apache.http.nio.concurrent.FutureCallback;
 import org.apache.http.nio.conn.IOSessionManager;
 import org.apache.http.nio.conn.ManagedIOSession;
 import org.apache.http.nio.reactor.IOSession;
+import org.apache.http.params.DefaultedHttpParams;
+import org.apache.http.params.HttpParams;
+import org.apache.http.protocol.ExecutionContext;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.HttpProcessor;
 
-class HttpAsyncExchange<T> {
+class HttpAsyncExchange<T> implements HttpAsyncExchangeHandler<T> {
 
     public static final String HTTP_EXCHANGE = "http.nio.http-exchange";
 
-    private final Future<ManagedIOSession> sessionFuture;
+    private final IOSessionManager<HttpRoute> sessmrg;
+    private final HttpProcessor httppocessor;
+    private final HttpContext localContext;
+    private final HttpParams params;
     private final BasicFuture<T> resultFuture;
     private final HttpAsyncExchangeHandler<T> handler;
 
+    private Future<ManagedIOSession> sessionFuture;
     private ManagedIOSession managedSession;
 
     public HttpAsyncExchange(
-            final HttpRoute route,
-            final Object state,
-            final IOSessionManager<HttpRoute> sessmrg,
             final HttpAsyncExchangeHandler<T> handler,
-            final FutureCallback<T> callback) {
+            final FutureCallback<T> callback,
+            final IOSessionManager<HttpRoute> sessmrg,
+            final HttpProcessor httppocessor,
+            final HttpContext localContext,
+            final HttpParams params) {
         super();
-        this.sessionFuture = sessmrg.leaseSession(route, state, new InternalFutureCallback());
-        this.resultFuture = new BasicFuture<T>(callback);
         this.handler = handler;
-    }
+        this.resultFuture = new BasicFuture<T>(callback);
+        this.sessmrg = sessmrg;
+        this.httppocessor = httppocessor;
+        this.localContext = localContext;
+        this.params = params;
 
-    public HttpAsyncExchangeHandler<T> getHandler() {
-        return this.handler;
+        HttpRoute route = new HttpRoute(handler.getTarget());
+        this.sessionFuture = this.sessmrg.leaseSession(route, null, new InternalFutureCallback());
     }
 
     public Future<T> getResultFuture() {
         return this.resultFuture;
     }
 
+    public HttpHost getTarget() {
+        return this.handler.getTarget();
+    }
+
+    public HttpRequest generateRequest() throws IOException, HttpException {
+        HttpRequest request = this.handler.generateRequest();
+        HttpHost target = this.handler.getTarget();
+        request.setParams(new DefaultedHttpParams(request.getParams(), this.params));
+        this.localContext.setAttribute(ExecutionContext.HTTP_REQUEST, request);
+        this.localContext.setAttribute(ExecutionContext.HTTP_TARGET_HOST, target);
+        this.httppocessor.process(request, this.localContext);
+        return request;
+    }
+
+    public void produceContent(
+            final ContentEncoder encoder, final IOControl ioctrl) throws IOException {
+        this.handler.produceContent(encoder, ioctrl);
+    }
+
+    public void responseReceived(final HttpResponse response) throws IOException, HttpException
{
+        response.setParams(new DefaultedHttpParams(response.getParams(), this.params));
+        this.localContext.setAttribute(ExecutionContext.HTTP_RESPONSE, response);
+        this.httppocessor.process(response, this.localContext);
+        this.handler.responseReceived(response);
+    }
+
+    public void consumeContent(
+            final ContentDecoder decoder, final IOControl ioctrl) throws IOException {
+        this.handler.consumeContent(decoder, ioctrl);
+    }
+
     public synchronized void completed() {
         try {
             if (this.managedSession != null) {
                 this.managedSession.releaseSession();
             }
             this.managedSession = null;
-            T result = this.handler.completed();
-            this.resultFuture.completed(result);
+            this.handler.completed();
+            this.resultFuture.completed(this.handler.getResult());
         } catch (RuntimeException runex) {
             this.resultFuture.failed(runex);
             throw runex;
         }
     }
 
-    public synchronized void shutdown() {
+    public synchronized void failed(final Exception ex) {
         try {
             this.sessionFuture.cancel(true);
             if (this.managedSession != null) {
                 this.managedSession.abortSession();
             }
             this.managedSession = null;
-            this.handler.cancelled();
-            this.resultFuture.cancel(true);
+            this.handler.failed(ex);
+            this.resultFuture.failed(ex);
         } catch (RuntimeException runex) {
-            this.resultFuture.failed(runex);
+            this.resultFuture.failed(ex);
             throw runex;
         }
     }
 
-    public synchronized void failed(final Exception ex) {
+    public synchronized void cancel() {
         try {
             this.sessionFuture.cancel(true);
             if (this.managedSession != null) {
                 this.managedSession.abortSession();
             }
             this.managedSession = null;
-            this.handler.failed(ex);
-            this.resultFuture.failed(ex);
+            this.handler.cancel();
+            this.resultFuture.cancel(true);
         } catch (RuntimeException runex) {
-            this.resultFuture.failed(ex);
+            this.resultFuture.failed(runex);
             throw runex;
         }
     }
 
+    public boolean isCompleted() {
+        return this.handler.isCompleted();
+    }
+
+    public T getResult() {
+        return this.handler.getResult();
+    }
+
     private synchronized void sessionRequestCompleted(final ManagedIOSession session) {
         this.managedSession = session;
         IOSession iosession = session.getSession();
@@ -128,7 +187,7 @@ class HttpAsyncExchange<T> {
 
     private synchronized void sessionRequestCancelled() {
         try {
-            this.handler.cancelled();
+            this.handler.cancel();
         } finally {
             this.resultFuture.cancel(true);
         }

Modified: httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/NHttpClientProtocolHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/NHttpClientProtocolHandler.java?rev=1040983&r1=1040982&r2=1040983&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/NHttpClientProtocolHandler.java
(original)
+++ httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/impl/nio/client/NHttpClientProtocolHandler.java
Wed Dec  1 12:42:20 2010
@@ -45,11 +45,8 @@ import org.apache.http.nio.NHttpClientHa
 import org.apache.http.nio.NHttpConnection;
 import org.apache.http.nio.client.HttpAsyncExchangeHandler;
 import org.apache.http.params.CoreProtocolPNames;
-import org.apache.http.params.DefaultedHttpParams;
-import org.apache.http.params.HttpParams;
 import org.apache.http.protocol.ExecutionContext;
 import org.apache.http.protocol.HttpContext;
-import org.apache.http.protocol.HttpProcessor;
 
 /**
  * Fully asynchronous HTTP client side protocol handler that implements the
@@ -66,26 +63,11 @@ class NHttpClientProtocolHandler impleme
 
     private static final String CONN_STATE = "http.nio.conn-state";
 
-    private final HttpProcessor httpProcessor;
     private final ConnectionReuseStrategy connStrategy;
-    private final HttpParams params;
 
     public NHttpClientProtocolHandler(
-            final HttpProcessor httpProcessor,
-            final ConnectionReuseStrategy connStrategy,
-            final HttpParams params) {
-        if (httpProcessor == null) {
-            throw new IllegalArgumentException("HTTP processor may not be null.");
-        }
-        if (connStrategy == null) {
-            throw new IllegalArgumentException("Connection reuse strategy may not be null");
-        }
-        if (params == null) {
-            throw new IllegalArgumentException("HTTP parameters may not be null");
-        }
-        this.httpProcessor = httpProcessor;
+            final ConnectionReuseStrategy connStrategy) {
         this.connStrategy = connStrategy;
-        this.params = params;
         this.log = LogFactory.getLog(getClass());
     }
 
@@ -127,9 +109,9 @@ class NHttpClientProtocolHandler impleme
             this.log.debug("Disconnected " + formatState(conn, connState));
         }
         if (connState != null) {
-            HttpAsyncExchange<?> httpexchange = connState.getHttpExchange();
-            if (httpexchange != null) {
-                httpexchange.shutdown();
+            HttpAsyncExchangeHandler<?> handler = connState.getHandler();
+            if (handler != null && !handler.isCompleted()) {
+                handler.cancel();
             }
         }
     }
@@ -139,9 +121,9 @@ class NHttpClientProtocolHandler impleme
         ConnState connState = getConnState(context);
         this.log.error("HTTP protocol exception: " + ex.getMessage(), ex);
         if (connState != null) {
-            HttpAsyncExchange<?> httpexchange = connState.getHttpExchange();
-            if (httpexchange != null) {
-                httpexchange.failed(ex);
+            HttpAsyncExchangeHandler<?> handler = connState.getHandler();
+            if (handler != null && !handler.isCompleted()) {
+                handler.failed(ex);
             }
         }
         closeConnection(conn);
@@ -152,9 +134,9 @@ class NHttpClientProtocolHandler impleme
         ConnState connState = getConnState(context);
         this.log.error("I/O error: " + ex.getMessage(), ex);
         if (connState != null) {
-            HttpAsyncExchange<?> httpexchange = connState.getHttpExchange();
-            if (httpexchange != null) {
-                httpexchange.failed(ex);
+            HttpAsyncExchangeHandler<?> handler = connState.getHandler();
+            if (handler != null && !handler.isCompleted()) {
+                handler.failed(ex);
             }
         }
         shutdownConnection(conn);
@@ -163,30 +145,24 @@ class NHttpClientProtocolHandler impleme
     public void requestReady(final NHttpClientConnection conn) {
         HttpContext context = conn.getContext();
         ConnState connState = getConnState(context);
-        HttpAsyncExchange<?> httpexchange = getHttpExchange(context);
+        HttpAsyncExchangeHandler<?> handler = getHandler(context);
         if (this.log.isDebugEnabled()) {
             this.log.debug("Request ready " + formatState(conn, connState));
         }
         if (connState.getRequestState() != MessageState.READY) {
             return;
         }
-        if (httpexchange == null || httpexchange.getResultFuture().isDone()) {
+        if (handler == null || handler.isCompleted()) {
             if (this.log.isDebugEnabled()) {
                 this.log.debug("No request submitted " + formatState(conn, connState));
             }
             return;
         }
-        connState.setHttpExchange(httpexchange);
-        HttpAsyncExchangeHandler<?> handler = connState.getHttpExchange().getHandler();
+        connState.setHandler(handler);
         try {
-            HttpRequest request = handler.getRequest();
-            request.setParams(new DefaultedHttpParams(request.getParams(), this.params));
-
+            HttpRequest request = handler.generateRequest();
             connState.setRequest(request);
 
-            context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
-            this.httpProcessor.process(request, context);
-
             HttpEntityEnclosingRequest entityReq = null;
             if (request instanceof HttpEntityEnclosingRequest) {
                 entityReq = (HttpEntityEnclosingRequest) request;
@@ -198,7 +174,7 @@ class NHttpClientProtocolHandler impleme
                 if (entityReq.expectContinue()) {
                     int timeout = conn.getSocketTimeout();
                     connState.setTimeout(timeout);
-                    timeout = this.params.getIntParameter(
+                    timeout = request.getParams().getIntParameter(
                             CoreProtocolPNames.WAIT_FOR_CONTINUE, 3000);
                     conn.setSocketTimeout(timeout);
                     connState.setRequestState(MessageState.ACK);
@@ -211,11 +187,11 @@ class NHttpClientProtocolHandler impleme
         } catch (IOException ex) {
             this.log.error("I/O error: " + ex.getMessage(), ex);
             shutdownConnection(conn);
-            httpexchange.failed(ex);
+            handler.failed(ex);
         } catch (HttpException ex) {
             this.log.error("HTTP protocol exception: " + ex.getMessage(), ex);
             closeConnection(conn);
-            httpexchange.failed(ex);
+            handler.failed(ex);
         }
     }
 
@@ -225,8 +201,7 @@ class NHttpClientProtocolHandler impleme
         if (this.log.isDebugEnabled()) {
             this.log.debug("Input ready " + formatState(conn, connState));
         }
-        HttpAsyncExchange<?> httpexchange = connState.getHttpExchange();
-        HttpAsyncExchangeHandler<?> handler = httpexchange.getHandler();
+        HttpAsyncExchangeHandler<?> handler = connState.getHandler();
         try {
             handler.consumeContent(decoder, conn);
             if (decoder.isCompleted()) {
@@ -234,8 +209,8 @@ class NHttpClientProtocolHandler impleme
             }
         } catch (IOException ex) {
             this.log.error("I/O error: " + ex.getMessage(), ex);
+            handler.failed(ex);
             shutdownConnection(conn);
-            httpexchange.failed(ex);
         }
     }
 
@@ -245,8 +220,7 @@ class NHttpClientProtocolHandler impleme
         if (this.log.isDebugEnabled()) {
             this.log.debug("Output ready " + formatState(conn, connState));
         }
-        HttpAsyncExchange<?> httpexchange = connState.getHttpExchange();
-        HttpAsyncExchangeHandler<?> handler = httpexchange.getHandler();
+        HttpAsyncExchangeHandler<?> handler = connState.getHandler();
         try {
             if (connState.getRequestState() == MessageState.ACK) {
                 conn.suspendOutput();
@@ -259,7 +233,7 @@ class NHttpClientProtocolHandler impleme
         } catch (IOException ex) {
             this.log.error("I/O error: " + ex.getMessage(), ex);
             shutdownConnection(conn);
-            httpexchange.failed(ex);
+            handler.failed(ex);
         }
     }
 
@@ -269,12 +243,9 @@ class NHttpClientProtocolHandler impleme
         if (this.log.isDebugEnabled()) {
             this.log.debug("Response received " + formatState(conn, connState));
         }
-        HttpAsyncExchange<?> httpexchange = connState.getHttpExchange();
-        HttpAsyncExchangeHandler<?> handler = httpexchange.getHandler();
+        HttpAsyncExchangeHandler<?> handler = connState.getHandler();
         try {
             HttpResponse response = conn.getHttpResponse();
-            response.setParams(new DefaultedHttpParams(response.getParams(), this.params));
-
             HttpRequest request = connState.getRequest();
 
             int statusCode = response.getStatusLine().getStatusCode();
@@ -298,25 +269,18 @@ class NHttpClientProtocolHandler impleme
                     conn.suspendOutput();
                 }
             }
-            context.setAttribute(ExecutionContext.HTTP_RESPONSE, response);
+            handler.responseReceived(response);
             if (!canResponseHaveBody(request, response)) {
-                conn.resetInput();
-                response.setEntity(null);
-                this.httpProcessor.process(response, context);
-                handler.responseReceived(response);
                 processResponse(conn, connState);
-            } else {
-                this.httpProcessor.process(response, context);
-                handler.responseReceived(response);
             }
         } catch (IOException ex) {
             this.log.error("I/O error: " + ex.getMessage(), ex);
             shutdownConnection(conn);
-            httpexchange.failed(ex);
+            handler.failed(ex);
         } catch (HttpException ex) {
             this.log.error("HTTP protocol exception: " + ex.getMessage(), ex);
             closeConnection(conn);
-            httpexchange.failed(ex);
+            handler.failed(ex);
         }
     }
 
@@ -326,7 +290,7 @@ class NHttpClientProtocolHandler impleme
         if (this.log.isDebugEnabled()) {
             this.log.debug("Timeout " + formatState(conn, connState));
         }
-        HttpAsyncExchange<?> httpexchange = connState.getHttpExchange();
+        HttpAsyncExchangeHandler<?> handler = connState.getHandler();
         try {
             if (connState.getRequestState() == MessageState.ACK) {
                 continueRequest(conn, connState);
@@ -346,7 +310,7 @@ class NHttpClientProtocolHandler impleme
         } catch (IOException ex) {
             this.log.error("I/O error: " + ex.getMessage(), ex);
             shutdownConnection(conn);
-            httpexchange.failed(ex);
+            handler.failed(ex);
         }
     }
 
@@ -354,8 +318,8 @@ class NHttpClientProtocolHandler impleme
         return (ConnState) context.getAttribute(CONN_STATE);
     }
 
-    private HttpAsyncExchange<?> getHttpExchange(final HttpContext context) {
-        return (HttpAsyncExchange<?>) context.getAttribute(HttpAsyncExchange.HTTP_EXCHANGE);
+    private HttpAsyncExchangeHandler<?> getHandler(final HttpContext context) {
+        return (HttpAsyncExchangeHandler<?>) context.getAttribute(HttpAsyncExchange.HTTP_EXCHANGE);
     }
 
     private void continueRequest(
@@ -378,7 +342,7 @@ class NHttpClientProtocolHandler impleme
     private void processResponse(
             final NHttpClientConnection conn,
             final ConnState connState) throws IOException {
-        HttpAsyncExchange<?> httpexchange = connState.getHttpExchange();
+        HttpAsyncExchangeHandler<?> handler = connState.getHandler();
         if (!connState.isValid()) {
             conn.close();
         }
@@ -390,7 +354,7 @@ class NHttpClientProtocolHandler impleme
         if (this.log.isDebugEnabled()) {
             this.log.debug("Response processed " + formatState(conn, connState));
         }
-        httpexchange.completed();
+        handler.completed();
         if (conn.isOpen()) {
             // Ready for another request
             connState.reset();

Modified: httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/nio/client/HttpAsyncExchangeHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/nio/client/HttpAsyncExchangeHandler.java?rev=1040983&r1=1040982&r2=1040983&view=diff
==============================================================================
--- httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/nio/client/HttpAsyncExchangeHandler.java
(original)
+++ httpcomponents/httpasyncclient/trunk/src/main/java/org/apache/http/nio/client/HttpAsyncExchangeHandler.java
Wed Dec  1 12:42:20 2010
@@ -28,6 +28,7 @@ package org.apache.http.nio.client;
 
 import java.io.IOException;
 
+import org.apache.http.HttpException;
 import org.apache.http.HttpHost;
 import org.apache.http.HttpRequest;
 import org.apache.http.HttpResponse;
@@ -39,18 +40,22 @@ public interface HttpAsyncExchangeHandle
 
     HttpHost getTarget();
 
-    HttpRequest getRequest();
+    HttpRequest generateRequest() throws IOException, HttpException;
 
     void produceContent(ContentEncoder encoder, IOControl ioctrl) throws IOException;
 
-    void responseReceived(HttpResponse response);
+    void responseReceived(HttpResponse response) throws IOException, HttpException;
 
     void consumeContent(ContentDecoder decoder, IOControl ioctrl) throws IOException;
 
+    void completed();
+
     void failed(Exception ex);
 
-    void cancelled();
+    void cancel();
+
+    boolean isCompleted();
 
-    T completed();
+    T getResult();
 
 }



Mime
View raw message