hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1767342 - in /httpcomponents/httpcore/trunk: httpcore5-osgi/src/test/java/org/apache/hc/core5/http/osgi/ httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ httpcore5-testing/src/main/java/org/apache/hc/core5/testing/frame...
Date Mon, 31 Oct 2016 17:34:34 GMT
Author: olegk
Date: Mon Oct 31 17:34:34 2016
New Revision: 1767342

URL: http://svn.apache.org/viewvc?rev=1767342&view=rev
Log:
HTTP requester based on classic I/O

Added:
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/bootstrap/HttpRequester.java   (with props)
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/bootstrap/RequesterBootstrap.java   (with props)
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/io/ResponseHandler.java   (with props)
Modified:
    httpcomponents/httpcore/trunk/httpcore5-osgi/src/test/java/org/apache/hc/core5/http/osgi/ActuallyConnectIT.java
    httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ClassicTestClient.java
    httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientAdapter.java
    httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/TestClassicHttp.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/HttpService.java

Modified: httpcomponents/httpcore/trunk/httpcore5-osgi/src/test/java/org/apache/hc/core5/http/osgi/ActuallyConnectIT.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-osgi/src/test/java/org/apache/hc/core5/http/osgi/ActuallyConnectIT.java?rev=1767342&r1=1767341&r2=1767342&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-osgi/src/test/java/org/apache/hc/core5/http/osgi/ActuallyConnectIT.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-osgi/src/test/java/org/apache/hc/core5/http/osgi/ActuallyConnectIT.java Mon Oct 31 17:34:34 2016
@@ -33,12 +33,12 @@ import org.apache.hc.core5.http.ClassicH
 import org.apache.hc.core5.http.ClassicHttpResponse;
 import org.apache.hc.core5.http.HttpException;
 import org.apache.hc.core5.http.HttpHost;
-import org.apache.hc.core5.http.impl.io.DefaultBHttpClientConnection;
 import org.apache.hc.core5.http.io.HttpRequestHandler;
 import org.apache.hc.core5.http.io.entity.EntityUtils;
 import org.apache.hc.core5.http.io.entity.StringEntity;
 import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
 import org.apache.hc.core5.http.protocol.HttpContext;
+import org.apache.hc.core5.http.protocol.HttpCoreContext;
 import org.apache.hc.core5.testing.classic.ClassicTestClient;
 import org.apache.hc.core5.testing.classic.ClassicTestServer;
 import org.junit.After;
@@ -104,23 +104,16 @@ public class ActuallyConnectIT {
 
         this.server.start();
 
-        final DefaultBHttpClientConnection conn = client.createConnection();
+        final HttpCoreContext context = HttpCoreContext.create();
         final HttpHost host = new HttpHost("localhost", this.server.getPort());
 
         try {
-            if (!conn.isOpen()) {
-                client.connect(host, conn);
-            }
-
             final BasicClassicHttpRequest get = new BasicClassicHttpRequest("GET", "/");
-            final ClassicHttpResponse response = this.client.execute(get, host, conn);
+            final ClassicHttpResponse response = this.client.execute(host, get, context);
             Assert.assertEquals(200, response.getCode());
             Assert.assertEquals("Hi there", EntityUtils.toString(response.getEntity()));
-            if (!this.client.keepAlive(get, response)) {
-                conn.close();
-            }
+            this.client.keepAlive(get, response, context);
         } finally {
-            conn.close();
             this.server.shutdown();
         }
     }

Modified: httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ClassicTestClient.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ClassicTestClient.java?rev=1767342&r1=1767341&r2=1767342&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ClassicTestClient.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ClassicTestClient.java Mon Oct 31 17:34:34 2016
@@ -31,53 +31,34 @@ import java.io.IOException;
 import java.net.InetSocketAddress;
 import java.net.Socket;
 
-import org.apache.hc.core5.http.ConnectionReuseStrategy;
-import org.apache.hc.core5.http.HttpException;
-import org.apache.hc.core5.http.HttpHost;
 import org.apache.hc.core5.http.ClassicHttpRequest;
 import org.apache.hc.core5.http.ClassicHttpResponse;
-import org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy;
+import org.apache.hc.core5.http.HttpException;
+import org.apache.hc.core5.http.HttpHost;
 import org.apache.hc.core5.http.impl.io.DefaultBHttpClientConnection;
-import org.apache.hc.core5.http.impl.io.HttpRequestExecutor;
-import org.apache.hc.core5.http.io.HttpClientConnection;
+import org.apache.hc.core5.http.impl.io.bootstrap.HttpRequester;
+import org.apache.hc.core5.http.impl.io.bootstrap.RequesterBootstrap;
 import org.apache.hc.core5.http.protocol.HttpContext;
-import org.apache.hc.core5.http.protocol.HttpCoreContext;
 import org.apache.hc.core5.http.protocol.HttpProcessor;
-import org.apache.hc.core5.http.protocol.DefaultHttpProcessor;
-import org.apache.hc.core5.http.protocol.RequestConnControl;
-import org.apache.hc.core5.http.protocol.RequestContent;
-import org.apache.hc.core5.http.protocol.RequestExpectContinue;
-import org.apache.hc.core5.http.protocol.RequestTargetHost;
-import org.apache.hc.core5.http.protocol.RequestUserAgent;
+import org.apache.hc.core5.util.Asserts;
+import org.apache.hc.core5.util.LangUtils;
 
 public class ClassicTestClient {
 
-    private final HttpProcessor httpproc;
-    private final HttpRequestExecutor httpexecutor;
-    private final ConnectionReuseStrategy connStrategy;
-    private final HttpCoreContext context;
-
+    private final DefaultBHttpClientConnection connection;
+    private volatile HttpProcessor httpProcessor;
     private volatile int timeout;
+    private volatile HttpHost host;
 
-    public ClassicTestClient(final HttpProcessor httpproc) {
-        super();
-        this.httpproc = httpproc;
-        this.connStrategy = DefaultConnectionReuseStrategy.INSTANCE;
-        this.httpexecutor = new HttpRequestExecutor();
-        this.context = new HttpCoreContext();
-    }
+    private volatile HttpRequester requester;
 
     public ClassicTestClient() {
-        this(new DefaultHttpProcessor(
-                new RequestContent(),
-                new RequestTargetHost(),
-                new RequestConnControl(),
-                new RequestUserAgent("TEST-CLIENT/1.1"),
-                new RequestExpectContinue()));
+        super();
+        this.connection = new DefaultBHttpClientConnection(8192);
     }
 
-    public HttpContext getContext() {
-        return this.context;
+    public void setHttpProcessor(final HttpProcessor httpProcessor) {
+        this.httpProcessor = httpProcessor;
     }
 
     public int getTimeout() {
@@ -88,33 +69,48 @@ public class ClassicTestClient {
         this.timeout = timeout;
     }
 
-    public DefaultBHttpClientConnection createConnection() {
-        return new LoggingBHttpClientConnection(8 * 1024);
+    public void start() {
+        Asserts.check(this.requester == null, "Client already running");
+        this.requester = RequesterBootstrap.bootstrap()
+                .setHttpProcessor(httpProcessor)
+                .create();
+
     }
 
-    public void connect(final HttpHost host, final DefaultBHttpClientConnection conn) throws IOException {
-        final Socket socket = new Socket();
-        socket.connect(new InetSocketAddress(host.getHostName(), host.getPort()), this.timeout);
-        conn.bind(socket);
-        conn.setSocketTimeout(this.timeout);
+    public void shutdown() {
+        try {
+            this.connection.close();
+        } catch (IOException ignore) {
+        }
     }
 
     public ClassicHttpResponse execute(
-            final ClassicHttpRequest request,
             final HttpHost targetHost,
-            final HttpClientConnection conn) throws HttpException, IOException {
+            final ClassicHttpRequest request,
+            final HttpContext context) throws HttpException, IOException {
+        Asserts.check(this.requester != null, "Client not running");
+        if (LangUtils.equals(this.host, targetHost)) {
+            this.connection.close();
+        }
+        this.host = targetHost;
+        if (!this.connection.isOpen()) {
+            final Socket socket = new Socket();
+            socket.connect(new InetSocketAddress(this.host.getHostName(), this.host.getPort()), this.timeout);
+            this.connection.bind(socket);
+            this.connection.setSocketTimeout(this.timeout);
+        }
         if (request.getAuthority() == null) {
             request.setAuthority(targetHost.toHostString());
             request.setScheme(targetHost.getSchemeName());
         }
-        this.httpexecutor.preProcess(request, this.httpproc, this.context);
-        final ClassicHttpResponse response = this.httpexecutor.execute(request, conn, this.context);
-        this.httpexecutor.postProcess(response, this.httpproc, this.context);
-        return response;
+        return this.requester.execute(this.connection, request, context);
     }
 
-    public boolean keepAlive(final ClassicHttpRequest request, final ClassicHttpResponse response) {
-        return this.connStrategy.keepAlive(request, response, this.context);
+    public boolean keepAlive(
+            final ClassicHttpRequest request,
+            final ClassicHttpResponse response,
+            final HttpContext context) throws IOException {
+        return this.requester.keepAlive(this.connection, request, response, context);
     }
 
 }

Modified: httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientAdapter.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientAdapter.java?rev=1767342&r1=1767341&r2=1767342&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientAdapter.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientAdapter.java Mon Oct 31 17:34:34 2016
@@ -38,11 +38,11 @@ import org.apache.hc.core5.http.HttpEnti
 import org.apache.hc.core5.http.HttpException;
 import org.apache.hc.core5.http.HttpHost;
 import org.apache.hc.core5.http.ProtocolVersion;
-import org.apache.hc.core5.http.impl.io.DefaultBHttpClientConnection;
 import org.apache.hc.core5.http.io.entity.ContentType;
 import org.apache.hc.core5.http.io.entity.EntityUtils;
 import org.apache.hc.core5.http.io.entity.StringEntity;
 import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
+import org.apache.hc.core5.http.protocol.HttpCoreContext;
 import org.apache.hc.core5.testing.classic.ClassicTestClient;
 
 /**
@@ -135,25 +135,19 @@ public class ClassicTestClientAdapter ex
             final long timeout = (long) request.get(TIMEOUT);
             client.setTimeout((int) timeout);
         }
+        client.start();
 
         // Now start the request.
-        final DefaultBHttpClientConnection conn = client.createConnection();
         final HttpHost host = new HttpHost(uri.getHost(), uri.getPort());
-
-        if (!conn.isOpen()) {
-            client.connect(host, conn);
-        }
-
-        final ClassicHttpResponse response = client.execute(httpRequest, host, conn);
+        final HttpCoreContext context = HttpCoreContext.create();
+        final ClassicHttpResponse response = client.execute(host, httpRequest, context);
 
         // Prepare the response.  It will contain status, body, headers, and contentType.
         final HttpEntity entity = response.getEntity();
         final String body = entity == null ? null : EntityUtils.toString(entity);
         final String contentType = entity == null ? null : entity.getContentType();
 
-        if (!client.keepAlive(httpRequest, response)) {
-            conn.close();
-        }
+        client.keepAlive(httpRequest, response, context);
 
         // prepare the returned information
         final Map<String, Object> ret = new HashMap<String, Object>();

Modified: httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/TestClassicHttp.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/TestClassicHttp.java?rev=1767342&r1=1767341&r2=1767342&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/TestClassicHttp.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/TestClassicHttp.java Mon Oct 31 17:34:34 2016
@@ -37,12 +37,12 @@ import java.nio.charset.StandardCharsets
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Random;
+import java.util.concurrent.TimeUnit;
 
 import org.apache.hc.core5.http.ClassicHttpRequest;
 import org.apache.hc.core5.http.ClassicHttpResponse;
 import org.apache.hc.core5.http.EntityDetails;
 import org.apache.hc.core5.http.Header;
-import org.apache.hc.core5.http.HttpConnectionMetrics;
 import org.apache.hc.core5.http.HttpEntity;
 import org.apache.hc.core5.http.HttpException;
 import org.apache.hc.core5.http.HttpHeaders;
@@ -51,7 +51,6 @@ import org.apache.hc.core5.http.HttpRequ
 import org.apache.hc.core5.http.HttpRequestInterceptor;
 import org.apache.hc.core5.http.HttpStatus;
 import org.apache.hc.core5.http.HttpVersion;
-import org.apache.hc.core5.http.impl.io.DefaultBHttpClientConnection;
 import org.apache.hc.core5.http.io.HttpExpectationVerifier;
 import org.apache.hc.core5.http.io.HttpRequestHandler;
 import org.apache.hc.core5.http.io.entity.AbstractHttpEntity;
@@ -62,39 +61,66 @@ import org.apache.hc.core5.http.io.entit
 import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
 import org.apache.hc.core5.http.protocol.DefaultHttpProcessor;
 import org.apache.hc.core5.http.protocol.HttpContext;
+import org.apache.hc.core5.http.protocol.HttpCoreContext;
 import org.apache.hc.core5.http.protocol.RequestConnControl;
 import org.apache.hc.core5.http.protocol.RequestContent;
 import org.apache.hc.core5.http.protocol.RequestExpectContinue;
 import org.apache.hc.core5.http.protocol.RequestTargetHost;
 import org.apache.hc.core5.http.protocol.RequestUserAgent;
-import org.junit.After;
 import org.junit.Assert;
-import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExternalResource;
 
 public class TestClassicHttp {
 
     private ClassicTestServer server;
+
+    @Rule
+    public ExternalResource serverResource = new ExternalResource() {
+
+        @Override
+        protected void before() throws Throwable {
+            server = new ClassicTestServer();
+            server.setTimeout(5000);
+        }
+
+        @Override
+        protected void after() {
+            if (server != null) {
+                try {
+                    server.shutdown(3, TimeUnit.SECONDS);
+                    server = null;
+                } catch (Exception ignore) {
+                }
+            }
+        }
+
+    };
+
     private ClassicTestClient client;
 
-    @Before
-    public void initServer() throws Exception {
-        this.server = new ClassicTestServer();
-        this.server.setTimeout(5000);
-    }
-
-    @Before
-    public void initClient() throws Exception {
-        this.client = new ClassicTestClient();
-        this.client.setTimeout(5000);
-    }
-
-    @After
-    public void shutDownServer() throws Exception {
-        if (this.server != null) {
-            this.server.shutdown();
+    @Rule
+    public ExternalResource clientResource = new ExternalResource() {
+
+        @Override
+        protected void before() throws Throwable {
+            client = new ClassicTestClient();
+            client.setTimeout(5000);
         }
-    }
+
+        @Override
+        protected void after() {
+            if (client != null) {
+                try {
+                    client.shutdown();
+                    client = null;
+                } catch (Exception ignore) {
+                }
+            }
+        }
+
+    };
 
     /**
      * This test case executes a series of simple GET requests
@@ -137,38 +163,22 @@ public class TestClassicHttp {
         });
 
         this.server.start();
+        this.client.start();
 
-        final DefaultBHttpClientConnection conn = client.createConnection();
+        final HttpCoreContext context = HttpCoreContext.create();
         final HttpHost host = new HttpHost("localhost", this.server.getPort());
 
-        try {
-            for (int r = 0; r < reqNo; r++) {
-                if (!conn.isOpen()) {
-                    client.connect(host, conn);
-                }
-
-                final BasicClassicHttpRequest get = new BasicClassicHttpRequest("GET", "/?" + r);
-                final ClassicHttpResponse response = this.client.execute(get, host, conn);
-                final byte[] received = EntityUtils.toByteArray(response.getEntity());
-                final byte[] expected = testData.get(r);
+        for (int r = 0; r < reqNo; r++) {
+            final BasicClassicHttpRequest get = new BasicClassicHttpRequest("GET", "/?" + r);
+            final ClassicHttpResponse response = this.client.execute(host, get, context);
+            final byte[] received = EntityUtils.toByteArray(response.getEntity());
+            final byte[] expected = testData.get(r);
 
-                Assert.assertEquals(expected.length, received.length);
-                for (int i = 0; i < expected.length; i++) {
-                    Assert.assertEquals(expected[i], received[i]);
-                }
-                if (!this.client.keepAlive(get, response)) {
-                    conn.close();
-                }
+            Assert.assertEquals(expected.length, received.length);
+            for (int i = 0; i < expected.length; i++) {
+                Assert.assertEquals(expected[i], received[i]);
             }
-
-            //Verify the connection metrics
-            final HttpConnectionMetrics cm = conn.getMetrics();
-            Assert.assertEquals(reqNo, cm.getRequestCount());
-            Assert.assertEquals(reqNo, cm.getResponseCount());
-
-        } finally {
-            conn.close();
-            this.server.shutdown();
+            this.client.keepAlive(get, response, context);
         }
     }
 
@@ -214,41 +224,26 @@ public class TestClassicHttp {
         });
 
         this.server.start();
+        this.client.start();
 
-        final DefaultBHttpClientConnection conn = client.createConnection();
+        final HttpCoreContext context = HttpCoreContext.create();
         final HttpHost host = new HttpHost("localhost", this.server.getPort());
 
-        try {
-            for (int r = 0; r < reqNo; r++) {
-                if (!conn.isOpen()) {
-                    client.connect(host, conn);
-                }
+        for (int r = 0; r < reqNo; r++) {
+            final BasicClassicHttpRequest post = new BasicClassicHttpRequest("POST", "/");
+            final byte[] data = testData.get(r);
+            final ByteArrayEntity outgoing = new ByteArrayEntity(data);
+            post.setEntity(outgoing);
 
-                final BasicClassicHttpRequest post = new BasicClassicHttpRequest("POST", "/");
-                final byte[] data = testData.get(r);
-                final ByteArrayEntity outgoing = new ByteArrayEntity(data);
-                post.setEntity(outgoing);
+            final ClassicHttpResponse response = this.client.execute(host, post, context);
+            final byte[] received = EntityUtils.toByteArray(response.getEntity());
+            final byte[] expected = testData.get(r);
 
-                final ClassicHttpResponse response = this.client.execute(post, host, conn);
-                final byte[] received = EntityUtils.toByteArray(response.getEntity());
-                final byte[] expected = testData.get(r);
-
-                Assert.assertEquals(expected.length, received.length);
-                for (int i = 0; i < expected.length; i++) {
-                    Assert.assertEquals(expected[i], received[i]);
-                }
-                if (!this.client.keepAlive(post, response)) {
-                    conn.close();
-                }
-            }
-            //Verify the connection metrics
-            final HttpConnectionMetrics cm = conn.getMetrics();
-            Assert.assertEquals(reqNo, cm.getRequestCount());
-            Assert.assertEquals(reqNo, cm.getResponseCount());
-
-        } finally {
-            conn.close();
-            this.server.shutdown();
+            Assert.assertEquals(expected.length, received.length);
+            for (int i = 0; i < expected.length; i++) {
+                Assert.assertEquals(expected[i], received[i]);
+            }
+            this.client.keepAlive(post, response, context);
         }
     }
 
@@ -293,41 +288,27 @@ public class TestClassicHttp {
         });
 
         this.server.start();
+        this.client.start();
 
-        final DefaultBHttpClientConnection conn = client.createConnection();
+        final HttpCoreContext context = HttpCoreContext.create();
         final HttpHost host = new HttpHost("localhost", this.server.getPort());
 
-        try {
-            for (int r = 0; r < reqNo; r++) {
-                if (!conn.isOpen()) {
-                    client.connect(host, conn);
-                }
+        for (int r = 0; r < reqNo; r++) {
+            final BasicClassicHttpRequest post = new BasicClassicHttpRequest("POST", "/");
+            final byte[] data = testData.get(r);
+            final ByteArrayEntity outgoing = new ByteArrayEntity(data);
+            outgoing.setChunked(true);
+            post.setEntity(outgoing);
 
-                final BasicClassicHttpRequest post = new BasicClassicHttpRequest("POST", "/");
-                final byte[] data = testData.get(r);
-                final ByteArrayEntity outgoing = new ByteArrayEntity(data);
-                outgoing.setChunked(true);
-                post.setEntity(outgoing);
+            final ClassicHttpResponse response = this.client.execute(host, post, context);
+            final byte[] received = EntityUtils.toByteArray(response.getEntity());
+            final byte[] expected = testData.get(r);
 
-                final ClassicHttpResponse response = this.client.execute(post, host, conn);
-                final byte[] received = EntityUtils.toByteArray(response.getEntity());
-                final byte[] expected = testData.get(r);
-
-                Assert.assertEquals(expected.length, received.length);
-                for (int i = 0; i < expected.length; i++) {
-                    Assert.assertEquals(expected[i], received[i]);
-                }
-                if (!this.client.keepAlive(post, response)) {
-                    conn.close();
-                }
-            }
-            //Verify the connection metrics
-            final HttpConnectionMetrics cm = conn.getMetrics();
-            Assert.assertEquals(reqNo, cm.getRequestCount());
-            Assert.assertEquals(reqNo, cm.getResponseCount());
-        } finally {
-            conn.close();
-            this.server.shutdown();
+            Assert.assertEquals(expected.length, received.length);
+            for (int i = 0; i < expected.length; i++) {
+                Assert.assertEquals(expected[i], received[i]);
+            }
+            this.client.keepAlive(post, response, context);
         }
     }
 
@@ -374,47 +355,32 @@ public class TestClassicHttp {
         });
 
         this.server.start();
+        this.client.start();
 
-        final DefaultBHttpClientConnection conn = client.createConnection();
+        final HttpCoreContext context = HttpCoreContext.create();
         final HttpHost host = new HttpHost("localhost", this.server.getPort());
 
-        try {
-            for (int r = 0; r < reqNo; r++) {
-                if (!conn.isOpen()) {
-                    client.connect(host, conn);
-                }
-
-                // Set protocol level to HTTP/1.0
-                final BasicClassicHttpRequest post = new BasicClassicHttpRequest("POST", "/");
-                post.setVersion(HttpVersion.HTTP_1_0);
-                final byte[] data = testData.get(r);
-                final ByteArrayEntity outgoing = new ByteArrayEntity(data);
-                post.setEntity(outgoing);
+        for (int r = 0; r < reqNo; r++) {
+            // Set protocol level to HTTP/1.0
+            final BasicClassicHttpRequest post = new BasicClassicHttpRequest("POST", "/");
+            post.setVersion(HttpVersion.HTTP_1_0);
+            final byte[] data = testData.get(r);
+            final ByteArrayEntity outgoing = new ByteArrayEntity(data);
+            post.setEntity(outgoing);
+
+            final ClassicHttpResponse response = this.client.execute(host, post, context);
+            Assert.assertEquals(HttpVersion.HTTP_1_1, response.getVersion());
+            final Header h1 = response.getFirstHeader("Version");
+            Assert.assertNotNull(h1);
+            Assert.assertEquals("1.0", h1.getValue());
+            final byte[] received = EntityUtils.toByteArray(response.getEntity());
+            final byte[] expected = testData.get(r);
 
-                final ClassicHttpResponse response = this.client.execute(post, host, conn);
-                Assert.assertEquals(HttpVersion.HTTP_1_1, response.getVersion());
-                final Header h1 = response.getFirstHeader("Version");
-                Assert.assertNotNull(h1);
-                Assert.assertEquals("1.0", h1.getValue());
-                final byte[] received = EntityUtils.toByteArray(response.getEntity());
-                final byte[] expected = testData.get(r);
-
-                Assert.assertEquals(expected.length, received.length);
-                for (int i = 0; i < expected.length; i++) {
-                    Assert.assertEquals(expected[i], received[i]);
-                }
-                if (!this.client.keepAlive(post, response)) {
-                    conn.close();
-                }
+            Assert.assertEquals(expected.length, received.length);
+            for (int i = 0; i < expected.length; i++) {
+                Assert.assertEquals(expected[i], received[i]);
             }
-
-            //Verify the connection metrics
-            final HttpConnectionMetrics cm = conn.getMetrics();
-            Assert.assertEquals(reqNo, cm.getRequestCount());
-            Assert.assertEquals(reqNo, cm.getResponseCount());
-        } finally {
-            conn.close();
-            this.server.shutdown();
+            this.client.keepAlive(post, response, context);
         }
     }
 
@@ -460,43 +426,27 @@ public class TestClassicHttp {
         });
 
         this.server.start();
+        this.client.start();
 
-        // Activate 'expect: continue' handshake
-        final DefaultBHttpClientConnection conn = client.createConnection();
+        final HttpCoreContext context = HttpCoreContext.create();
         final HttpHost host = new HttpHost("localhost", this.server.getPort());
 
-        try {
-            for (int r = 0; r < reqNo; r++) {
-                if (!conn.isOpen()) {
-                    client.connect(host, conn);
-                }
+        for (int r = 0; r < reqNo; r++) {
+            final BasicClassicHttpRequest post = new BasicClassicHttpRequest("POST", "/");
+            final byte[] data = testData.get(r);
+            final ByteArrayEntity outgoing = new ByteArrayEntity(data);
+            outgoing.setChunked(true);
+            post.setEntity(outgoing);
 
-                final BasicClassicHttpRequest post = new BasicClassicHttpRequest("POST", "/");
-                final byte[] data = testData.get(r);
-                final ByteArrayEntity outgoing = new ByteArrayEntity(data);
-                outgoing.setChunked(true);
-                post.setEntity(outgoing);
+            final ClassicHttpResponse response = this.client.execute(host, post, context);
+            final byte[] received = EntityUtils.toByteArray(response.getEntity());
+            final byte[] expected = testData.get(r);
 
-                final ClassicHttpResponse response = this.client.execute(post, host, conn);
-                final byte[] received = EntityUtils.toByteArray(response.getEntity());
-                final byte[] expected = testData.get(r);
-
-                Assert.assertEquals(expected.length, received.length);
-                for (int i = 0; i < expected.length; i++) {
-                    Assert.assertEquals(expected[i], received[i]);
-                }
-                if (!this.client.keepAlive(post, response)) {
-                    conn.close();
-                }
+            Assert.assertEquals(expected.length, received.length);
+            for (int i = 0; i < expected.length; i++) {
+                Assert.assertEquals(expected[i], received[i]);
             }
-
-            //Verify the connection metrics
-            final HttpConnectionMetrics cm = conn.getMetrics();
-            Assert.assertEquals(reqNo, cm.getRequestCount());
-            Assert.assertEquals(reqNo, cm.getResponseCount());
-        } finally {
-            conn.close();
-            this.server.shutdown();
+            this.client.keepAlive(post, response, context);
         }
     }
 
@@ -552,50 +502,35 @@ public class TestClassicHttp {
         });
 
         this.server.start();
+        this.client.start();
 
-        final DefaultBHttpClientConnection conn = client.createConnection();
+        final HttpCoreContext context = HttpCoreContext.create();
         final HttpHost host = new HttpHost("localhost", this.server.getPort());
 
-        try {
-            for (int r = 0; r < reqNo; r++) {
-                if (!conn.isOpen()) {
-                    client.connect(host, conn);
-                }
-
-                final BasicClassicHttpRequest post = new BasicClassicHttpRequest("POST", "/");
-                post.addHeader("Secret", Integer.toString(r));
-
-                final byte[] b = new byte[2048];
-                for (int i = 0; i < b.length; i++) {
-                    b[i] = (byte) ('a' + r);
-                }
-                final ByteArrayEntity requestEntity = new ByteArrayEntity(b, ContentType.TEXT_PLAIN);
-                requestEntity.setChunked(false);
-                post.setEntity(requestEntity);
+        for (int r = 0; r < reqNo; r++) {
+            final BasicClassicHttpRequest post = new BasicClassicHttpRequest("POST", "/");
+            post.addHeader("Secret", Integer.toString(r));
 
-                final ClassicHttpResponse response = this.client.execute(post, host, conn);
+            final byte[] b = new byte[2048];
+            for (int i = 0; i < b.length; i++) {
+                b[i] = (byte) ('a' + r);
+            }
+            final ByteArrayEntity requestEntity = new ByteArrayEntity(b, ContentType.TEXT_PLAIN);
+            requestEntity.setChunked(false);
+            post.setEntity(requestEntity);
 
-                final HttpEntity responseEntity = response.getEntity();
-                Assert.assertNotNull(responseEntity);
-                EntityUtils.consume(responseEntity);
+            final ClassicHttpResponse response = this.client.execute(host, post, context);
 
-                if (r >= 2) {
-                    Assert.assertEquals(HttpStatus.SC_EXPECTATION_FAILED, response.getCode());
-                } else {
-                    Assert.assertEquals(HttpStatus.SC_OK, response.getCode());
-                }
+            final HttpEntity responseEntity = response.getEntity();
+            Assert.assertNotNull(responseEntity);
+            EntityUtils.consume(responseEntity);
 
-                if (!conn.isConsistent() || !this.client.keepAlive(post, response)) {
-                    conn.close();
-                }
+            if (r >= 2) {
+                Assert.assertEquals(HttpStatus.SC_EXPECTATION_FAILED, response.getCode());
+            } else {
+                Assert.assertEquals(HttpStatus.SC_OK, response.getCode());
             }
-            //Verify the connection metrics
-            final HttpConnectionMetrics cm = conn.getMetrics();
-            Assert.assertEquals(reqNo, cm.getRequestCount());
-            Assert.assertEquals(reqNo, cm.getResponseCount());
-        } finally {
-            conn.close();
-            this.server.shutdown();
+            this.client.keepAlive(post, response, context);
         }
     }
 
@@ -679,86 +614,73 @@ public class TestClassicHttp {
                     final ClassicHttpResponse response,
                     final HttpContext context) throws HttpException, IOException {
 
-                if (request instanceof ClassicHttpRequest) {
-                    int n = 1;
-                    String s = request.getPath();
-                    if (s.startsWith("/?n=")) {
-                        s = s.substring(4);
-                        try {
-                            n = Integer.parseInt(s);
-                            if (n <= 0) {
-                                throw new HttpException("Invalid request: " +
-                                        "number of repetitions cannot be negative or zero");
-                            }
-                        } catch (final NumberFormatException ex) {
+                int n = 1;
+                String s = request.getPath();
+                if (s.startsWith("/?n=")) {
+                    s = s.substring(4);
+                    try {
+                        n = Integer.parseInt(s);
+                        if (n <= 0) {
                             throw new HttpException("Invalid request: " +
-                                    "number of repetitions is invalid");
+                                    "number of repetitions cannot be negative or zero");
                         }
+                    } catch (final NumberFormatException ex) {
+                        throw new HttpException("Invalid request: " +
+                                "number of repetitions is invalid");
                     }
+                }
 
-                    final HttpEntity incoming = request.getEntity();
-                    if (incoming != null) {
-                        final String line = EntityUtils.toString(incoming);
-                        final ContentType contentType = ContentType.getOrDefault(incoming);
-                        Charset charset = contentType.getCharset();
-                        if (charset == null) {
-                            charset = StandardCharsets.ISO_8859_1;
-                        }
-                        final RepeatingEntity outgoing = new RepeatingEntity(line, charset, n);
-                        outgoing.setChunked(n % 2 == 0);
-                        response.setEntity(outgoing);
+                final HttpEntity incoming = request.getEntity();
+                if (incoming != null) {
+                    final String line = EntityUtils.toString(incoming);
+                    final ContentType contentType = ContentType.getOrDefault(incoming);
+                    Charset charset = contentType.getCharset();
+                    if (charset == null) {
+                        charset = StandardCharsets.ISO_8859_1;
                     }
-                } else {
-                    throw new HttpException("Invalid request: POST request expected");
+                    final RepeatingEntity outgoing = new RepeatingEntity(line, charset, n);
+                    outgoing.setChunked(n % 2 == 0);
+                    response.setEntity(outgoing);
                 }
             }
 
         });
 
         this.server.start();
-        final DefaultBHttpClientConnection conn = client.createConnection();
-        final HttpHost host = new HttpHost("localhost", this.server.getPort());
+        this.client.start();
 
-        try {
-            for (final String pattern : patterns) {
-                for (int n = 1000; n < 1020; n++) {
-                    if (!conn.isOpen()) {
-                        client.connect(host, conn);
-                    }
-
-                    final BasicClassicHttpRequest post = new BasicClassicHttpRequest(
-                            "POST", "/?n=" + n);
-                    final StringEntity outgoing = new StringEntity(pattern);
-                    outgoing.setChunked(n % 2 == 0);
-                    post.setEntity(outgoing);
+        final HttpCoreContext context = HttpCoreContext.create();
+        final HttpHost host = new HttpHost("localhost", this.server.getPort());
 
-                    final ClassicHttpResponse response = this.client.execute(post, host, conn);
-                    final HttpEntity incoming = response.getEntity();
-                    Assert.assertNotNull(incoming);
-                    final InputStream instream = incoming.getContent();
-                    final ContentType contentType = ContentType.getOrDefault(incoming);
-                    Charset charset = contentType.getCharset();
-                    if (charset == null) {
-                        charset = StandardCharsets.ISO_8859_1;
-                    }
-                    Assert.assertNotNull(instream);
-                    final BufferedReader reader = new BufferedReader(new InputStreamReader(instream, charset));
+        for (final String pattern : patterns) {
+            for (int n = 1000; n < 1020; n++) {
+                final BasicClassicHttpRequest post = new BasicClassicHttpRequest(
+                        "POST", "/?n=" + n);
+                final StringEntity outgoing = new StringEntity(pattern);
+                outgoing.setChunked(n % 2 == 0);
+                post.setEntity(outgoing);
 
-                    String line;
-                    int count = 0;
-                    while ((line = reader.readLine()) != null) {
-                        Assert.assertEquals(pattern, line);
-                        count++;
-                    }
-                    Assert.assertEquals(n, count);
-                    if (!this.client.keepAlive(post, response)) {
-                        conn.close();
-                    }
+                final ClassicHttpResponse response = this.client.execute(host, post, context);
+                final HttpEntity incoming = response.getEntity();
+                Assert.assertNotNull(incoming);
+                final InputStream instream = incoming.getContent();
+                final ContentType contentType = ContentType.getOrDefault(incoming);
+                Charset charset = contentType.getCharset();
+                if (charset == null) {
+                    charset = StandardCharsets.ISO_8859_1;
+                }
+                Assert.assertNotNull(instream);
+                final BufferedReader reader = new BufferedReader(new InputStreamReader(instream, charset));
+
+                String line;
+                int count = 0;
+                while ((line = reader.readLine()) != null) {
+                    Assert.assertEquals(pattern, line);
+                    count++;
                 }
+                Assert.assertEquals(n, count);
+                this.client.keepAlive(post, response, context);
             }
-        } finally {
-            conn.close();
-            this.server.shutdown();
         }
     }
 
@@ -783,26 +705,19 @@ public class TestClassicHttp {
         });
 
         this.server.start();
+        this.client.start();
 
-        final DefaultBHttpClientConnection conn = client.createConnection();
+        final HttpCoreContext context = HttpCoreContext.create();
         final HttpHost host = new HttpHost("localhost", this.server.getPort());
 
-        try {
-            if (!conn.isOpen()) {
-                client.connect(host, conn);
-            }
-
-            final BasicClassicHttpRequest post = new BasicClassicHttpRequest("POST", "/");
-            post.setEntity(null);
+        final BasicClassicHttpRequest post = new BasicClassicHttpRequest("POST", "/");
+        post.setEntity(null);
 
-            final ClassicHttpResponse response = this.client.execute(post, host, conn);
-            Assert.assertEquals(HttpStatus.SC_OK, response.getCode());
-            final byte[] received = EntityUtils.toByteArray(response.getEntity());
-            Assert.assertEquals(0, received.length);
-        } finally {
-            conn.close();
-            this.server.shutdown();
-        }
+        final ClassicHttpResponse response = this.client.execute(host, post, context);
+        Assert.assertEquals(HttpStatus.SC_OK, response.getCode());
+        final byte[] received = EntityUtils.toByteArray(response.getEntity());
+        Assert.assertEquals(0, received.length);
+        this.client.keepAlive(post, response, context);
     }
 
     @Test
@@ -826,32 +741,24 @@ public class TestClassicHttp {
         });
 
         this.server.start();
-
-        final DefaultBHttpClientConnection conn = client.createConnection();
-        final HttpHost host = new HttpHost("localhost", this.server.getPort());
-
-        try {
-            if (!conn.isOpen()) {
-                client.connect(host, conn);
-            }
-
-            final BasicClassicHttpRequest post = new BasicClassicHttpRequest("POST", "/");
-            post.setEntity(null);
-
-            this.client = new ClassicTestClient(new DefaultHttpProcessor(
-                    new RequestTargetHost(),
-                    new RequestConnControl(),
-                    new RequestUserAgent(),
-                    new RequestExpectContinue()));
-
-            final ClassicHttpResponse response = this.client.execute(post, host, conn);
-            Assert.assertEquals(HttpStatus.SC_OK, response.getCode());
-            final byte[] received = EntityUtils.toByteArray(response.getEntity());
-            Assert.assertEquals(0, received.length);
-        } finally {
-            conn.close();
-            this.server.shutdown();
-        }
+        this.client.setHttpProcessor(new DefaultHttpProcessor(
+                new RequestTargetHost(),
+                new RequestConnControl(),
+                new RequestUserAgent(),
+                new RequestExpectContinue()));
+        this.client.start();
+
+        final HttpCoreContext context = HttpCoreContext.create();
+        final HttpHost host = new HttpHost("localhost", this.server.getPort());
+
+        final BasicClassicHttpRequest post = new BasicClassicHttpRequest("POST", "/");
+        post.setEntity(null);
+
+        final ClassicHttpResponse response = this.client.execute(host, post, context);
+        Assert.assertEquals(HttpStatus.SC_OK, response.getCode());
+        final byte[] received = EntityUtils.toByteArray(response.getEntity());
+        Assert.assertEquals(0, received.length);
+        this.client.keepAlive(post, response, context);
     }
 
     @Test
@@ -875,41 +782,33 @@ public class TestClassicHttp {
         });
 
         this.server.start();
+        this.client.setHttpProcessor(new DefaultHttpProcessor(
+                new HttpRequestInterceptor() {
 
-        final DefaultBHttpClientConnection conn = client.createConnection();
-        final HttpHost host = new HttpHost("localhost", this.server.getPort());
-
-        try {
-            if (!conn.isOpen()) {
-                client.connect(host, conn);
-            }
-
-            final BasicClassicHttpRequest post = new BasicClassicHttpRequest("POST", "/");
-            post.setEntity(null);
-
-            this.client = new ClassicTestClient(new DefaultHttpProcessor(
-                    new HttpRequestInterceptor() {
-
-                        @Override
-                        public void process(
-                                final HttpRequest request,
-                                final EntityDetails entity,
-                                final HttpContext context) throws HttpException, IOException {
-                            request.addHeader(HttpHeaders.TRANSFER_ENCODING, "identity");
-                        }
+                    @Override
+                    public void process(
+                            final HttpRequest request,
+                            final EntityDetails entity,
+                            final HttpContext context) throws HttpException, IOException {
+                        request.addHeader(HttpHeaders.TRANSFER_ENCODING, "identity");
+                    }
 
-                    },
-                    new RequestTargetHost(),
-                    new RequestConnControl(),
-                    new RequestUserAgent(),
-                    new RequestExpectContinue()));
-
-            final ClassicHttpResponse response = this.client.execute(post, host, conn);
-            Assert.assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, response.getCode());
-        } finally {
-            conn.close();
-            this.server.shutdown();
-        }
+                },
+                new RequestTargetHost(),
+                new RequestConnControl(),
+                new RequestUserAgent(),
+                new RequestExpectContinue()));
+        this.client.start();
+
+        final HttpCoreContext context = HttpCoreContext.create();
+        final HttpHost host = new HttpHost("localhost", this.server.getPort());
+
+        final BasicClassicHttpRequest post = new BasicClassicHttpRequest("POST", "/");
+        post.setEntity(null);
+
+        final ClassicHttpResponse response = this.client.execute(host, post, context);
+        Assert.assertEquals(HttpStatus.SC_NOT_IMPLEMENTED, response.getCode());
+        this.client.keepAlive(post, response, context);
     }
 
     @Test
@@ -931,33 +830,18 @@ public class TestClassicHttp {
         });
 
         this.server.start();
+        this.client.start();
 
-        final DefaultBHttpClientConnection conn = client.createConnection();
+        final HttpCoreContext context = HttpCoreContext.create();
         final HttpHost host = new HttpHost("localhost", this.server.getPort());
 
-        try {
-            for (int r = 0; r < reqNo; r++) {
-                if (!conn.isOpen()) {
-                    client.connect(host, conn);
-                }
-
-                final BasicClassicHttpRequest get = new BasicClassicHttpRequest("GET", "/?" + r);
-                final ClassicHttpResponse response = this.client.execute(get, host, conn);
-                Assert.assertNull(response.getEntity());
-                if (!this.client.keepAlive(get, response)) {
-                    conn.close();
-                    Assert.fail("Connection expected to be re-usable");
-                }
+        for (int r = 0; r < reqNo; r++) {
+            final BasicClassicHttpRequest get = new BasicClassicHttpRequest("GET", "/?" + r);
+            final ClassicHttpResponse response = this.client.execute(host, get, context);
+            Assert.assertNull(response.getEntity());
+            if (!this.client.keepAlive(get, response, context)) {
+                Assert.fail("Connection expected to be re-usable");
             }
-
-            //Verify the connection metrics
-            final HttpConnectionMetrics cm = conn.getMetrics();
-            Assert.assertEquals(reqNo, cm.getRequestCount());
-            Assert.assertEquals(reqNo, cm.getResponseCount());
-
-        } finally {
-            conn.close();
-            this.server.shutdown();
         }
     }
 
@@ -977,28 +861,25 @@ public class TestClassicHttp {
 
         });
 
-        this.client = new ClassicTestClient(new DefaultHttpProcessor(new RequestContent(), new RequestConnControl()));
         this.server.start();
+        this.client.setHttpProcessor(new DefaultHttpProcessor(new RequestContent(), new RequestConnControl()));
+        this.client.start();
 
-        final DefaultBHttpClientConnection conn = client.createConnection();
+        final HttpCoreContext context = HttpCoreContext.create();
         final HttpHost host = new HttpHost("localhost", this.server.getPort());
-        client.connect(host, conn);
 
-        try {
-            final BasicClassicHttpRequest get1 = new BasicClassicHttpRequest("GET", "/");
-            get1.setVersion(HttpVersion.HTTP_1_0);
-            final ClassicHttpResponse response1 = this.client.execute(get1, host, conn);
-            Assert.assertEquals(200, response1.getCode());
-            EntityUtils.consume(response1.getEntity());
-            final BasicClassicHttpRequest get2 = new BasicClassicHttpRequest("GET", "/");
-            final ClassicHttpResponse response2 = this.client.execute(get2, host, conn);
-            Assert.assertEquals(400, response2.getCode());
-            EntityUtils.consume(response2.getEntity());
-
-        } finally {
-            conn.close();
-            this.server.shutdown();
-        }
+        final BasicClassicHttpRequest get1 = new BasicClassicHttpRequest("GET", "/");
+        get1.setVersion(HttpVersion.HTTP_1_0);
+        final ClassicHttpResponse response1 = this.client.execute(host, get1, context);
+        Assert.assertEquals(200, response1.getCode());
+        EntityUtils.consume(response1.getEntity());
+        this.client.keepAlive(get1, response1, context);
+
+        final BasicClassicHttpRequest get2 = new BasicClassicHttpRequest("GET", "/");
+        final ClassicHttpResponse response2 = this.client.execute(host, get2, context);
+        Assert.assertEquals(400, response2.getCode());
+        EntityUtils.consume(response2.getEntity());
+        this.client.keepAlive(get2, response2, context);
     }
 
 }

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/HttpService.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/HttpService.java?rev=1767342&r1=1767341&r2=1767342&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/HttpService.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/HttpService.java Mon Oct 31 17:34:34 2016
@@ -52,7 +52,6 @@ import org.apache.hc.core5.http.io.HttpR
 import org.apache.hc.core5.http.io.HttpRequestHandlerMapper;
 import org.apache.hc.core5.http.io.HttpServerConnection;
 import org.apache.hc.core5.http.io.entity.ContentType;
-import org.apache.hc.core5.http.io.entity.EntityUtils;
 import org.apache.hc.core5.http.io.entity.StringEntity;
 import org.apache.hc.core5.http.protocol.HttpContext;
 import org.apache.hc.core5.http.protocol.HttpCoreContext;

Added: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/bootstrap/HttpRequester.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/bootstrap/HttpRequester.java?rev=1767342&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/bootstrap/HttpRequester.java (added)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/bootstrap/HttpRequester.java Mon Oct 31 17:34:34 2016
@@ -0,0 +1,114 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.hc.core5.http.impl.io.bootstrap;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.hc.core5.http.ClassicHttpRequest;
+import org.apache.hc.core5.http.ClassicHttpResponse;
+import org.apache.hc.core5.http.ConnectionClosedException;
+import org.apache.hc.core5.http.ConnectionReuseStrategy;
+import org.apache.hc.core5.http.HttpEntity;
+import org.apache.hc.core5.http.HttpException;
+import org.apache.hc.core5.http.impl.io.HttpRequestExecutor;
+import org.apache.hc.core5.http.io.HttpClientConnection;
+import org.apache.hc.core5.http.io.ResponseHandler;
+import org.apache.hc.core5.http.protocol.HttpContext;
+import org.apache.hc.core5.http.protocol.HttpProcessor;
+import org.apache.hc.core5.util.Args;
+
+/**
+ * @since 5.0
+ */
+public class HttpRequester {
+
+    private final HttpRequestExecutor requestExecutor;
+    private final HttpProcessor httpProcessor;
+    private final ConnectionReuseStrategy connStrategy;
+
+    public HttpRequester(
+            final HttpRequestExecutor requestExecutor,
+            final HttpProcessor httpProcessor,
+            final ConnectionReuseStrategy connStrategy) {
+        this.requestExecutor = requestExecutor;
+        this.httpProcessor = httpProcessor;
+        this.connStrategy = connStrategy;
+    }
+
+    public ClassicHttpResponse execute(
+            final HttpClientConnection connection,
+            final ClassicHttpRequest request,
+            final HttpContext context) throws HttpException, IOException {
+        Args.notNull(connection, "HTTP connection");
+        Args.notNull(request, "HTTP request");
+        Args.notNull(context, "HTTP context");
+        if (!connection.isOpen()) {
+            throw new ConnectionClosedException("Connection is closed");
+        }
+        requestExecutor.preProcess(request, httpProcessor, context);
+        final ClassicHttpResponse response = requestExecutor.execute(request, connection, context);
+        requestExecutor.postProcess(response, httpProcessor, context);
+        return response;
+    }
+
+    public boolean keepAlive(
+            final HttpClientConnection connection,
+            final ClassicHttpRequest request,
+            final ClassicHttpResponse response,
+            final HttpContext context) throws IOException {
+        Args.notNull(connection, "HTTP connection");
+        Args.notNull(request, "HTTP request");
+        Args.notNull(response, "HTTP response");
+        Args.notNull(context, "HTTP context");
+        final HttpEntity entity = response.getEntity();
+        if (entity != null && entity.isStreaming()) {
+            final InputStream instream = entity.getContent();
+            if (instream != null) {
+                instream.close();
+            }
+        }
+        if (connStrategy.keepAlive(request, response, context)) {
+            return true;
+        } else {
+            connection.close();
+            return false;
+        }
+    }
+
+    public <T> T execute(
+            final HttpClientConnection connection,
+            final ClassicHttpRequest request,
+            final HttpContext context,
+            final ResponseHandler<T> responseHandler) throws HttpException, IOException {
+        final ClassicHttpResponse response = execute(connection, request, context);
+        final T result = responseHandler.handleResponse(response);
+        keepAlive(connection, request, response, context);
+        return result;
+    }
+
+}

Propchange: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/bootstrap/HttpRequester.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/bootstrap/HttpRequester.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/bootstrap/HttpRequester.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/bootstrap/RequesterBootstrap.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/bootstrap/RequesterBootstrap.java?rev=1767342&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/bootstrap/RequesterBootstrap.java (added)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/bootstrap/RequesterBootstrap.java Mon Oct 31 17:34:34 2016
@@ -0,0 +1,82 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.hc.core5.http.impl.io.bootstrap;
+
+import org.apache.hc.core5.http.ConnectionReuseStrategy;
+import org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy;
+import org.apache.hc.core5.http.impl.HttpProcessors;
+import org.apache.hc.core5.http.impl.io.HttpRequestExecutor;
+import org.apache.hc.core5.http.protocol.HttpProcessor;
+
+/**
+ * @since 5.0
+ */
+public class RequesterBootstrap {
+
+    private HttpRequestExecutor requestExecutor;
+    private HttpProcessor httpProcessor;
+    private ConnectionReuseStrategy connStrategy;
+
+    private RequesterBootstrap() {
+    }
+
+    public static RequesterBootstrap bootstrap() {
+        return new RequesterBootstrap();
+    }
+
+    /**
+     * Assigns {@link HttpProcessor} instance.
+     */
+    public final RequesterBootstrap setHttpProcessor(final HttpProcessor httpProcessor) {
+        this.httpProcessor = httpProcessor;
+        return this;
+    }
+
+    /**
+     * Assigns {@link ConnectionReuseStrategy} instance.
+     */
+    public final RequesterBootstrap setConnectionReuseStrategy(final ConnectionReuseStrategy connStrategy) {
+        this.connStrategy = connStrategy;
+        return this;
+    }
+
+    /**
+     * Assigns {@link HttpRequestExecutor} instance.
+     */
+    public final RequesterBootstrap setHttpRequestExecutor(final HttpRequestExecutor requestExecutor) {
+        this.requestExecutor = requestExecutor;
+        return this;
+    }
+
+    public HttpRequester create() {
+        return new HttpRequester(
+                requestExecutor != null ? requestExecutor : new HttpRequestExecutor(),
+                httpProcessor != null ? httpProcessor : HttpProcessors.client(),
+                connStrategy != null ? connStrategy : DefaultConnectionReuseStrategy.INSTANCE);
+    }
+
+}

Propchange: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/bootstrap/RequesterBootstrap.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/bootstrap/RequesterBootstrap.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/bootstrap/RequesterBootstrap.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/io/ResponseHandler.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/io/ResponseHandler.java?rev=1767342&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/io/ResponseHandler.java (added)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/io/ResponseHandler.java Mon Oct 31 17:34:34 2016
@@ -0,0 +1,54 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.hc.core5.http.io;
+
+import java.io.IOException;
+
+import org.apache.hc.core5.http.ClassicHttpResponse;
+import org.apache.hc.core5.http.HttpException;
+
+/**
+ * Handler that encapsulates the process of generating a response object
+ * from a {@link ClassicHttpResponse}.
+ *
+ *
+ * @since 4.0
+ */
+public interface ResponseHandler<T> {
+
+    /**
+     * Processes an {@link ClassicHttpResponse} and returns some value
+     * corresponding to that response.
+     *
+     * @param response The response to process
+     * @return A value determined by the response
+     *
+     * @throws IOException in case of a problem or the connection was aborted
+     */
+    T handleResponse(ClassicHttpResponse response) throws HttpException, IOException;
+
+}

Propchange: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/io/ResponseHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/io/ResponseHandler.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/io/ResponseHandler.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message