hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1772536 [2/2] - in /httpcomponents/httpcore/trunk: httpcore5-h2/src/examples/ httpcore5-h2/src/examples/org/ httpcore5-h2/src/examples/org/apache/ httpcore5-h2/src/examples/org/apache/hc/ httpcore5-h2/src/examples/org/apache/hc/core5/ http...
Date Sun, 04 Dec 2016 13:50:00 GMT
Modified: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncRequestExecutionExample.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncRequestExecutionExample.java?rev=1772536&r1=1772535&r2=1772536&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncRequestExecutionExample.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncRequestExecutionExample.java
Sun Dec  4 13:49:59 2016
@@ -26,21 +26,26 @@
  */
 package org.apache.hc.core5.http.examples;
 
-import java.net.URI;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.hc.core5.concurrent.FutureCallback;
+import org.apache.hc.core5.http.HttpConnection;
 import org.apache.hc.core5.http.HttpHost;
+import org.apache.hc.core5.http.HttpRequest;
 import org.apache.hc.core5.http.HttpResponse;
 import org.apache.hc.core5.http.Message;
+import org.apache.hc.core5.http.impl.Http1StreamListener;
 import org.apache.hc.core5.http.impl.nio.bootstrap.HttpAsyncRequester;
 import org.apache.hc.core5.http.impl.nio.bootstrap.PooledClientEndpoint;
 import org.apache.hc.core5.http.impl.nio.bootstrap.RequesterBootstrap;
+import org.apache.hc.core5.http.message.RequestLine;
+import org.apache.hc.core5.http.message.StatusLine;
 import org.apache.hc.core5.http.nio.BasicRequestProducer;
 import org.apache.hc.core5.http.nio.BasicResponseConsumer;
 import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer;
+import org.apache.hc.core5.reactor.IOReactorConfig;
 
 /**
  * Example of asynchronous HTTP/1.1 request execution.
@@ -49,8 +54,39 @@ public class AsyncRequestExecutionExampl
 
     public static void main(String[] args) throws Exception {
 
+        IOReactorConfig ioReactorConfig = IOReactorConfig.custom()
+                .setConnectTimeout(5000)
+                .setSoTimeout(5000)
+                .build();
+
         // Create and start requester
-        final HttpAsyncRequester requester = RequesterBootstrap.bootstrap().create();
+        final HttpAsyncRequester requester = RequesterBootstrap.bootstrap()
+                .setIOReactorConfig(ioReactorConfig)
+                .setStreamListener(new Http1StreamListener() {
+
+                    @Override
+                    public void onRequestHead(final HttpConnection connection, final HttpRequest
request) {
+                        System.out.println(connection + " " + new RequestLine(request));
+
+                    }
+
+                    @Override
+                    public void onResponseHead(final HttpConnection connection, final HttpResponse
response) {
+                        System.out.println(connection + " " + new StatusLine(response));
+                    }
+
+                    @Override
+                    public void onExchangeComplete(final HttpConnection connection, final
boolean keepAlive) {
+                        if (keepAlive) {
+                            System.out.println(connection + " can be kept alive");
+                        } else {
+                            System.out.println(connection + " cannot be kept alive");
+                        }
+                    }
+
+                })
+                .create();
+
         Runtime.getRuntime().addShutdownHook(new Thread() {
             @Override
             public void run() {
@@ -60,18 +96,15 @@ public class AsyncRequestExecutionExampl
         });
         requester.start();
 
-        // Execute HTTP GETs to the following hosts and
-        HttpHost[] targets = new HttpHost[] {
-                new HttpHost("www.apache.org", 80, "http"),
-                new HttpHost("hc.apache.org", 80, "http")
-        };
+        HttpHost target = new HttpHost("httpbin.org");
+        String[] requestUris = new String[] {"/", "/ip", "/user-agent", "/headers"};
 
-        final CountDownLatch latch = new CountDownLatch(targets.length);
-        for (final HttpHost target: targets) {
+        final CountDownLatch latch = new CountDownLatch(requestUris.length);
+        for (final String requestUri: requestUris) {
             final Future<PooledClientEndpoint> future = requester.connect(target, 5,
TimeUnit.SECONDS);
             final PooledClientEndpoint clientEndpoint = future.get();
             clientEndpoint.executeAndRelease(
-                    new BasicRequestProducer("GET", URI.create("/")),
+                    new BasicRequestProducer("GET", target, requestUri),
                     new BasicResponseConsumer<>(new StringAsyncEntityConsumer()),
                     new FutureCallback<Message<HttpResponse, String>>() {
 
@@ -79,19 +112,21 @@ public class AsyncRequestExecutionExampl
                         public void completed(final Message<HttpResponse, String> message)
{
                             latch.countDown();
                             HttpResponse response = message.getHead();
-                            System.out.println(target + "->" + response.getCode());
+                            String body = message.getBody();
+                            System.out.println(requestUri + "->" + response.getCode());
+                            System.out.println(body);
                         }
 
                         @Override
                         public void failed(final Exception ex) {
                             latch.countDown();
-                            System.out.println(target + "->" + ex);
+                            System.out.println(requestUri + "->" + ex);
                         }
 
                         @Override
                         public void cancelled() {
                             latch.countDown();
-                            System.out.println(target + " cancelled");
+                            System.out.println(requestUri + " cancelled");
                         }
 
                     });

Modified: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicGetExecutionExample.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicGetExecutionExample.java?rev=1772536&r1=1772535&r2=1772536&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicGetExecutionExample.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicGetExecutionExample.java
Sun Dec  4 13:49:59 2016
@@ -74,22 +74,21 @@ public class ClassicGetExecutionExample
 
                 })
                 .create();
-        HttpHost host = new HttpHost("localhost", 8080);
 
         HttpCoreContext coreContext = HttpCoreContext.create();
-        String[] targets = {
-                "/",
-                "/examples/servlets/servlet/RequestInfoExample",
-                "/somewhere%20in%20pampa"};
+        HttpHost target = new HttpHost("httpbin.org");
+        String[] requestUris = new String[] {"/", "/ip", "/user-agent", "/headers"};
 
         SocketConfig socketConfig = SocketConfig.custom()
                 .setConnectTimeout(5000)
                 .setSoTimeout(5000)
                 .build();
 
-        for (int i = 0; i < targets.length; i++) {
-            ClassicHttpRequest request = new BasicClassicHttpRequest("GET", host, targets[i]);
-            try (ClassicHttpResponse response = httpRequester.execute(host, request, socketConfig,
coreContext)) {
+        for (int i = 0; i < requestUris.length; i++) {
+            String requestUri = requestUris[i];
+            ClassicHttpRequest request = new BasicClassicHttpRequest("GET", target, requestUri);
+            try (ClassicHttpResponse response = httpRequester.execute(target, request, socketConfig,
coreContext)) {
+                System.out.println(requestUri + "->" + response.getCode());
                 System.out.println(EntityUtils.toString(response.getEntity()));
                 System.out.println("==============");
             }

Modified: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostExecutionExample.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostExecutionExample.java?rev=1772536&r1=1772535&r2=1772536&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostExecutionExample.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostExecutionExample.java
Sun Dec  4 13:49:59 2016
@@ -82,9 +82,9 @@ public class ClassicPostExecutionExample
 
                 })
                 .create();
-        HttpHost host = new HttpHost("localhost", 8080);
-
         HttpCoreContext coreContext = HttpCoreContext.create();
+        HttpHost target = new HttpHost("httpbin.org");
+
         HttpEntity[] requestBodies = {
                 new StringEntity(
                         "This is the first test request",
@@ -104,11 +104,12 @@ public class ClassicPostExecutionExample
                 .setSoTimeout(5000)
                 .build();
 
+        String requestUri = "/post";
         for (int i = 0; i < requestBodies.length; i++) {
-            ClassicHttpRequest request = new BasicClassicHttpRequest("POST", host,
-                    "/examples/servlets/servlet/RequestInfoExample");
+            ClassicHttpRequest request = new BasicClassicHttpRequest("POST", target,requestUri);
             request.setEntity(requestBodies[i]);
-            try (ClassicHttpResponse response = httpRequester.execute(host, request, socketConfig,
coreContext)) {
+            try (ClassicHttpResponse response = httpRequester.execute(target, request, socketConfig,
coreContext)) {
+                System.out.println(requestUri + "->" + response.getCode());
                 System.out.println(EntityUtils.toString(response.getEntity()));
                 System.out.println("==============");
             }

Modified: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostWithTrailersExecutionExample.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostWithTrailersExecutionExample.java?rev=1772536&r1=1772535&r2=1772536&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostWithTrailersExecutionExample.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostWithTrailersExecutionExample.java
Sun Dec  4 13:49:59 2016
@@ -48,11 +48,12 @@ import org.apache.hc.core5.http.protocol
 public class ClassicPostWithTrailersExecutionExample {
     public static void main(String[] args) throws Exception {
         HttpRequester httpRequester = RequesterBootstrap.bootstrap().create();
-        HttpHost host = new HttpHost("localhost", 8080);
 
         HttpCoreContext coreContext = HttpCoreContext.create();
+        HttpHost target = new HttpHost("httpbin.org");
 
-        ClassicHttpRequest request = new BasicClassicHttpRequest("POST", host, "/");
+        String requestUri = "/post";
+        ClassicHttpRequest request = new BasicClassicHttpRequest("POST", target, requestUri);
         HttpEntity requestBody = new HttpEntityWithTrailers(
                 new StringEntity("Chunked message with trailers", ContentType.TEXT_PLAIN),
                 new BasicHeader("t1","Hello world"));
@@ -64,8 +65,8 @@ public class ClassicPostWithTrailersExec
                 .build();
 
         System.out.println(">> Request URI: " + request.getUri());
-        try (ClassicHttpResponse response = httpRequester.execute(host, request, socketConfig,
coreContext)) {
-            System.out.println("<< Response: " + response.getCode());
+        try (ClassicHttpResponse response = httpRequester.execute(target, request, socketConfig,
coreContext)) {
+            System.out.println(requestUri + "->" + response.getCode());
             System.out.println(EntityUtils.toString(response.getEntity()));
             System.out.println("==============");
         }

Modified: 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=1772536&r1=1772535&r2=1772536&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/bootstrap/HttpRequester.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/bootstrap/HttpRequester.java
Sun Dec  4 13:49:59 2016
@@ -78,7 +78,7 @@ public class HttpRequester implements Au
     private final HttpConnectionFactory<? extends HttpClientConnection> connectFactory;
     private final SSLSocketFactory sslSocketFactory;
 
-    HttpRequester(
+    public HttpRequester(
             final HttpRequestExecutor requestExecutor,
             final HttpProcessor httpProcessor,
             final ControlledConnPool<HttpHost, HttpClientConnection> connPool,

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/bootstrap/HttpServer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/bootstrap/HttpServer.java?rev=1772536&r1=1772535&r2=1772536&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/bootstrap/HttpServer.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/bootstrap/HttpServer.java
Sun Dec  4 13:49:59 2016
@@ -68,7 +68,7 @@ public class HttpServer implements AutoC
     private volatile ServerSocket serverSocket;
     private volatile RequestListener requestListener;
 
-    HttpServer(
+    public HttpServer(
             final int port,
             final InetAddress ifAddress,
             final SocketConfig socketConfig,

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/bootstrap/AsyncRequester.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/bootstrap/AsyncRequester.java?rev=1772536&r1=1772535&r2=1772536&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/bootstrap/AsyncRequester.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/bootstrap/AsyncRequester.java
Sun Dec  4 13:49:59 2016
@@ -34,6 +34,7 @@ import java.util.concurrent.TimeUnit;
 
 import org.apache.hc.core5.function.Callback;
 import org.apache.hc.core5.http.ExceptionListener;
+import org.apache.hc.core5.http.HttpHost;
 import org.apache.hc.core5.net.NamedEndpoint;
 import org.apache.hc.core5.reactor.ConnectionInitiator;
 import org.apache.hc.core5.reactor.DefaultConnectingIOReactor;
@@ -71,14 +72,30 @@ public class AsyncRequester extends IORe
         return new DefaultConnectingIOReactor(ioEventHandlerFactory, ioReactorConfig, threadFactory);
     }
 
+    private NamedEndpoint toEndpoint(final HttpHost host) {
+        int port = host.getPort();
+        if (port < 0) {
+            final String hostName = host.getHostName();
+            final String scheme = host.getSchemeName();
+            if ("http".equalsIgnoreCase(scheme)) {
+                port = 80;
+            } else if ("https".equalsIgnoreCase(scheme)) {
+                port = 443;
+            }
+            return new HttpHost(hostName, port, scheme);
+        } else {
+            return host;
+        }
+    }
+
     protected SessionRequest requestSession(
-            final NamedEndpoint remoteEndpoint,
+            final HttpHost host,
             final long timeout,
             final TimeUnit timeUnit,
             final SessionRequestCallback callback) {
-        Args.notNull(remoteEndpoint, "Remote endpoint");
+        Args.notNull(host, "Host");
         Args.notNull(timeUnit, "Time unit");
-        final SessionRequest  sessionRequest = reactor().connect(remoteEndpoint, null, null,
callback);
+        final SessionRequest  sessionRequest = reactor().connect(toEndpoint(host), null,
null, callback);
         sessionRequest.setConnectTimeout((int) timeUnit.toMillis(timeout));
         return sessionRequest;
     }

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/message/BasicHttpRequest.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/message/BasicHttpRequest.java?rev=1772536&r1=1772535&r2=1772536&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/message/BasicHttpRequest.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/message/BasicHttpRequest.java
Sun Dec  4 13:49:59 2016
@@ -196,7 +196,7 @@ public class BasicHttpRequest extends He
     @Override
     public String toString() {
         final StringBuilder sb = new StringBuilder();
-        sb.append(this.method).append(" ").append(this.scheme).append("://").append(this.authority).append("/").append(this.path);
+        sb.append(this.method).append(" ").append(this.scheme).append("://").append(this.authority).append(this.path);
         return sb.toString();
     }
 

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/BasicRequestProducer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/BasicRequestProducer.java?rev=1772536&r1=1772535&r2=1772536&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/BasicRequestProducer.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/BasicRequestProducer.java
Sun Dec  4 13:49:59 2016
@@ -30,6 +30,7 @@ import java.io.IOException;
 import java.net.URI;
 
 import org.apache.hc.core5.http.EntityDetails;
+import org.apache.hc.core5.http.HttpHost;
 import org.apache.hc.core5.http.HttpRequest;
 import org.apache.hc.core5.http.message.BasicHttpRequest;
 
@@ -43,6 +44,14 @@ public class BasicRequestProducer implem
         this.dataProducer = dataProducer;
     }
 
+    public BasicRequestProducer(final String method, final HttpHost host, final String path,
final AsyncEntityProducer dataProducer) {
+        this(new BasicHttpRequest(method, host, path), dataProducer);
+    }
+
+    public BasicRequestProducer(final String method, final HttpHost host, final String path)
{
+        this(method, host, path, null);
+    }
+
     public BasicRequestProducer(final String method, final URI requestUri, final AsyncEntityProducer
dataProducer) {
         this(new BasicHttpRequest(method, requestUri), dataProducer);
     }



Mime
View raw message