hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1457536 - in /httpcomponents/httpcore/trunk/httpcore-nio/src: main/java/org/apache/http/nio/protocol/ test/java/org/apache/http/nio/protocol/
Date Sun, 17 Mar 2013 19:37:47 GMT
Author: olegk
Date: Sun Mar 17 19:37:47 2013
New Revision: 1457536

URL: http://svn.apache.org/r1457536
Log:
Moved connection keep-alive decision responsibility from HttpAsyncRequestExecutor to individual
HttpAsyncClientExchangeHandlers

Modified:
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncClientExchangeHandler.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncClientExchangeHandler.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutor.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncClientExchangeHandler.java
    httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncRequestExecutor.java

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncClientExchangeHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncClientExchangeHandler.java?rev=1457536&r1=1457535&r2=1457536&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncClientExchangeHandler.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/BasicAsyncClientExchangeHandler.java
Sun Mar 17 19:37:47 2013
@@ -65,6 +65,7 @@ public class BasicAsyncClientExchangeHan
     private final ConnectionReuseStrategy connReuseStrategy;
 
     private volatile boolean requestSent;
+    private volatile boolean keepAlive;
 
     /**
      * Creates new instance of BasicAsyncRequestExecutionHandler.
@@ -137,7 +138,7 @@ public class BasicAsyncClientExchangeHan
     }
 
     public HttpRequest generateRequest() throws IOException, HttpException {
-        HttpRequest request = this.requestProducer.generateRequest();
+        final HttpRequest request = this.requestProducer.generateRequest();
         this.localContext.setAttribute(ExecutionContext.HTTP_REQUEST, request);
         this.localContext.setAttribute(ExecutionContext.HTTP_CONNECTION, this.conn);
         this.httppocessor.process(request, this.localContext);
@@ -158,6 +159,7 @@ public class BasicAsyncClientExchangeHan
         this.localContext.setAttribute(ExecutionContext.HTTP_RESPONSE, response);
         this.httppocessor.process(response, this.localContext);
         this.responseConsumer.responseReceived(response);
+        this.keepAlive = this.connReuseStrategy.keepAlive(response, this.localContext);
     }
 
     public void consumeContent(
@@ -165,8 +167,11 @@ public class BasicAsyncClientExchangeHan
         this.responseConsumer.consumeContent(decoder, ioctrl);
     }
 
-    public void responseCompleted() {
+    public void responseCompleted() throws IOException {
         try {
+            if (!this.keepAlive) {
+                this.conn.close();
+            }
             this.responseConsumer.responseCompleted(this.localContext);
             final T result = this.responseConsumer.getResult();
             final Exception ex = this.responseConsumer.getException();
@@ -182,10 +187,6 @@ public class BasicAsyncClientExchangeHan
         }
     }
 
-    public boolean keepAlive(final HttpResponse response) {
-        return this.connReuseStrategy.keepAlive(response, this.localContext);
-    }
-
     public void inputTerminated() {
         failed(new ConnectionClosedException("Connection closed"));
     }

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncClientExchangeHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncClientExchangeHandler.java?rev=1457536&r1=1457535&r2=1457536&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncClientExchangeHandler.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncClientExchangeHandler.java
Sun Mar 17 19:37:47 2013
@@ -132,13 +132,6 @@ public interface HttpAsyncClientExchange
     void responseCompleted() throws IOException, HttpException;
 
     /**
-     * Determines if current connection can be kept alive after the given HTTP response.
-     * @param response message
-     * @return <code>true</code> to keep connection alive, <code>false</code>
otherwise.
-     */
-    boolean keepAlive(HttpResponse response);
-
-    /**
      * Invoked to signal that the connection has been terminated prematurely
      * by the opposite end.
      */

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutor.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutor.java?rev=1457536&r1=1457535&r2=1457536&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutor.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/main/java/org/apache/http/nio/protocol/HttpAsyncRequestExecutor.java
Sun Mar 17 19:37:47 2013
@@ -344,17 +344,7 @@ public class HttpAsyncRequestExecutor im
             final NHttpClientConnection conn,
             final State state,
             final HttpAsyncClientExchangeHandler handler) throws IOException, HttpException
{
-        if (state.isValid()) {
-            final HttpRequest request = state.getRequest();
-            final HttpResponse response = state.getResponse();
-            final String method = request.getRequestLine().getMethod();
-            final int status = response.getStatusLine().getStatusCode();
-            if (!(method.equalsIgnoreCase("CONNECT") && status < 300)) {
-                if (!handler.keepAlive(response)) {
-                    conn.close();
-                }
-            }
-        } else {
+        if (!state.isValid()) {
             conn.close();
         }
         handler.responseCompleted();

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncClientExchangeHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncClientExchangeHandler.java?rev=1457536&r1=1457535&r2=1457536&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncClientExchangeHandler.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestBasicAsyncClientExchangeHandler.java
Sun Mar 17 19:37:47 2013
@@ -344,10 +344,18 @@ public class TestBasicAsyncClientExchang
     }
 
     @Test
-    public void testKeepAlive() throws Exception {
+    public void testResponseNoKeepAlive() throws Exception {
+        final Object obj = new Object();
+        Mockito.when(this.responseConsumer.getResult()).thenReturn(obj);
+
         final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200,
"OK");
-        this.exchangeHandler.keepAlive(response);
-        Mockito.verify(this.reuseStrategy).keepAlive(response, this.context);
+
+        Mockito.when(reuseStrategy.keepAlive(response, this.context)).thenReturn(Boolean.FALSE);
+
+        this.exchangeHandler.responseReceived(response);
+        this.exchangeHandler.responseCompleted();
+
+        Mockito.verify(this.conn).close();
     }
 
     @Test

Modified: httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncRequestExecutor.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncRequestExecutor.java?rev=1457536&r1=1457535&r2=1457536&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncRequestExecutor.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore-nio/src/test/java/org/apache/http/nio/protocol/TestHttpAsyncRequestExecutor.java
Sun Mar 17 19:37:47 2013
@@ -377,7 +377,6 @@ public class TestHttpAsyncRequestExecuto
         this.connContext.setAttribute(HttpAsyncRequestExecutor.HTTP_HANDLER, this.exchangeHandler);
         final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200,
"OK");
         Mockito.when(this.conn.getHttpResponse()).thenReturn(response);
-        Mockito.when(this.exchangeHandler.keepAlive(response)).thenReturn(Boolean.TRUE);
 
         this.protocolHandler.responseReceived(this.conn);
 
@@ -399,7 +398,6 @@ public class TestHttpAsyncRequestExecuto
         this.connContext.setAttribute(HttpAsyncRequestExecutor.HTTP_HANDLER, this.exchangeHandler);
         final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200,
"OK");
         Mockito.when(this.conn.getHttpResponse()).thenReturn(response);
-        Mockito.when(this.exchangeHandler.keepAlive(response)).thenReturn(Boolean.TRUE);
 
         this.protocolHandler.responseReceived(this.conn);
 
@@ -423,7 +421,6 @@ public class TestHttpAsyncRequestExecuto
                 HttpStatus.SC_NOT_MODIFIED, "Not modified");
         response.setEntity(new BasicHttpEntity());
         Mockito.when(this.conn.getHttpResponse()).thenReturn(response);
-        Mockito.when(this.exchangeHandler.keepAlive(response)).thenReturn(Boolean.TRUE);
 
         this.protocolHandler.responseReceived(this.conn);
 
@@ -459,7 +456,6 @@ public class TestHttpAsyncRequestExecuto
         state.setResponse(response);
         this.connContext.setAttribute(HttpAsyncRequestExecutor.HTTP_EXCHANGE_STATE, state);
         this.connContext.setAttribute(HttpAsyncRequestExecutor.HTTP_HANDLER, this.exchangeHandler);
-        Mockito.when(this.exchangeHandler.keepAlive(response)).thenReturn(Boolean.TRUE);
         Mockito.when(this.decoder.isCompleted()).thenReturn(Boolean.TRUE);
 
         this.protocolHandler.inputReady(this.conn, this.decoder);
@@ -492,27 +488,6 @@ public class TestHttpAsyncRequestExecuto
     }
 
     @Test
-    public void testResponseNoKeepAlive() throws Exception {
-        final State state = new HttpAsyncRequestExecutor.State();
-        final HttpRequest request = new BasicHttpRequest("GET", "/");
-        state.setRequest(request);
-        final BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200,
"OK");
-        state.setResponse(response);
-        this.connContext.setAttribute(HttpAsyncRequestExecutor.HTTP_EXCHANGE_STATE, state);
-        this.connContext.setAttribute(HttpAsyncRequestExecutor.HTTP_HANDLER, this.exchangeHandler);
-        Mockito.when(this.exchangeHandler.keepAlive(response)).thenReturn(Boolean.FALSE);
-        Mockito.when(this.decoder.isCompleted()).thenReturn(Boolean.TRUE);
-
-        this.protocolHandler.inputReady(this.conn, this.decoder);
-
-        Assert.assertEquals(MessageState.READY, state.getRequestState());
-        Assert.assertEquals(MessageState.READY, state.getResponseState());
-        Mockito.verify(this.exchangeHandler).consumeContent(this.decoder, this.conn);
-        Mockito.verify(this.exchangeHandler).responseCompleted();
-        Mockito.verify(this.conn).close();
-    }
-
-    @Test
     public void testEndOfInput() throws Exception {
         final State state = new HttpAsyncRequestExecutor.State();
         this.connContext.setAttribute(HttpAsyncRequestExecutor.HTTP_EXCHANGE_STATE, state);



Mime
View raw message