You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2016/10/31 17:34:55 UTC
svn commit: r1767343 -
/httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/
Author: olegk
Date: Mon Oct 31 17:34:54 2016
New Revision: 1767343
URL: http://svn.apache.org/viewvc?rev=1767343&view=rev
Log:
Reworked classic I/O examples; renamed example classes
Added:
httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncFileServerExample.java (contents, props changed)
- copied, changed from r1767342, httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/NHttpFileServer.java
httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncRequestExecutionExample.java (contents, props changed)
- copied, changed from r1767342, httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/NHttpClient.java
httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncReverseProxyExample.java (contents, props changed)
- copied, changed from r1767342, httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/NHttpReverseProxy.java
httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicFileServerExample.java (contents, props changed)
- copied, changed from r1767342, httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/HttpFileServer.java
httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicGetExecutionExample.java (contents, props changed)
- copied, changed from r1767342, httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ElementalHttpGet.java
httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostExecutionExample.java (contents, props changed)
- copied, changed from r1767342, httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ElementalHttpPost.java
httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostWithTrailersExecutionExample.java (contents, props changed)
- copied, changed from r1767342, httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ElementalHttpPostTrailers.java
httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicReverseProxyExample.java (with props)
Removed:
httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ElementalHttpGet.java
httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ElementalHttpPost.java
httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ElementalHttpPostTrailers.java
httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ElementalPoolingHttpGet.java
httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ElementalReverseProxy.java
httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/HttpFileServer.java
httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/NHttpClient.java
httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/NHttpFileServer.java
httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/NHttpReverseProxy.java
Copied: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncFileServerExample.java (from r1767342, httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/NHttpFileServer.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncFileServerExample.java?p2=httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncFileServerExample.java&p1=httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/NHttpFileServer.java&r1=1767342&r2=1767343&rev=1767343&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/NHttpFileServer.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncFileServerExample.java Mon Oct 31 17:34:54 2016
@@ -61,9 +61,9 @@ import org.apache.hc.core5.reactor.IORea
import org.apache.hc.core5.reactor.ListenerEndpoint;
/**
- * Asynchronous embedded HTTP/1.1 file server.
+ * Example of asynchronous embedded HTTP/1.1 file server.
*/
-public class NHttpFileServer {
+public class AsyncFileServerExample {
public static void main(String[] args) throws Exception {
if (args.length < 1) {
Propchange: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncFileServerExample.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncFileServerExample.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncFileServerExample.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Copied: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncRequestExecutionExample.java (from r1767342, httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/NHttpClient.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncRequestExecutionExample.java?p2=httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncRequestExecutionExample.java&p1=httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/NHttpClient.java&r1=1767342&r2=1767343&rev=1767343&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/NHttpClient.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncRequestExecutionExample.java Mon Oct 31 17:34:54 2016
@@ -45,9 +45,9 @@ import org.apache.hc.core5.http.nio.enti
import org.apache.hc.core5.http.protocol.HttpCoreContext;
/**
- * Asynchronous HTTP/1.1 request executor.
+ * Example of asynchronous HTTP/1.1 request execution.
*/
-public class NHttpClient {
+public class AsyncRequestExecutionExample {
public static void main(String[] args) throws Exception {
Propchange: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncRequestExecutionExample.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncRequestExecutionExample.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncRequestExecutionExample.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Copied: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncReverseProxyExample.java (from r1767342, httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/NHttpReverseProxy.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncReverseProxyExample.java?p2=httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncReverseProxyExample.java&p1=httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/NHttpReverseProxy.java&r1=1767342&r2=1767343&rev=1767343&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/NHttpReverseProxy.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncReverseProxyExample.java Mon Oct 31 17:34:54 2016
@@ -87,13 +87,13 @@ import org.apache.hc.core5.reactor.IOSes
import org.apache.hc.core5.util.HeapByteBufferAllocator;
/**
- * Asynchronous embedded HTTP/1.1 reverse proxy with full content streaming.
+ * Example of asynchronous embedded HTTP/1.1 reverse proxy with full content streaming.
*/
-public class NHttpReverseProxy {
+public class AsyncReverseProxyExample {
public static void main(String[] args) throws Exception {
if (args.length < 1) {
- System.out.println("Usage: NHttpReverseProxy <hostname> [listener port]");
+ System.out.println("Usage: <hostname[:port]> [listener port]");
System.exit(1);
}
// Target host
@@ -210,6 +210,7 @@ public class NHttpReverseProxy {
requester.start();
server.start();
server.listen(new InetSocketAddress(port));
+ System.out.println("Listening on port " + port);
server.awaitShutdown(Integer.MAX_VALUE, TimeUnit.DAYS);
}
Propchange: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncReverseProxyExample.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncReverseProxyExample.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncReverseProxyExample.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Copied: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicFileServerExample.java (from r1767342, httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/HttpFileServer.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicFileServerExample.java?p2=httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicFileServerExample.java&p1=httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/HttpFileServer.java&r1=1767342&r2=1767343&rev=1767343&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/HttpFileServer.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicFileServerExample.java Mon Oct 31 17:34:54 2016
@@ -60,9 +60,9 @@ import org.apache.hc.core5.http.protocol
import org.apache.hc.core5.ssl.SSLContexts;
/**
- * Embedded HTTP/1.1 file server based on a classic (blocking) I/O model.
+ * Example of embedded HTTP/1.1 file server using classic I/O.
*/
-public class HttpFileServer {
+public class ClassicFileServerExample {
public static void main(String[] args) throws Exception {
if (args.length < 1) {
@@ -79,7 +79,7 @@ public class HttpFileServer {
SSLContext sslcontext = null;
if (port == 8443) {
// Initialize SSL context
- URL url = HttpFileServer.class.getResource("/my.keystore");
+ URL url = ClassicFileServerExample.class.getResource("/my.keystore");
if (url == null) {
System.out.println("Keystore not found");
System.exit(1);
@@ -98,33 +98,33 @@ public class HttpFileServer {
.setListenerPort(port)
.setSocketConfig(socketConfig)
.setSslContext(sslcontext)
- .setExceptionListener(new StdErrorExceptionListener())
+ .setExceptionListener(new ExceptionListener() {
+
+ @Override
+ public void onError(final Exception ex) {
+ if (ex instanceof SocketTimeoutException) {
+ System.err.println("Connection timed out");
+ } else if (ex instanceof ConnectionClosedException) {
+ System.err.println(ex.getMessage());
+ } else {
+ ex.printStackTrace();
+ }
+ }
+
+ })
.registerHandler("*", new HttpFileHandler(docRoot))
.create();
server.start();
- server.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
-
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
server.shutdown(5, TimeUnit.SECONDS);
}
});
- }
+ System.out.println("Listening on port " + port);
- static class StdErrorExceptionListener implements ExceptionListener {
-
- @Override
- public void onError(final Exception ex) {
- if (ex instanceof SocketTimeoutException) {
- System.err.println("Connection timed out");
- } else if (ex instanceof ConnectionClosedException) {
- System.err.println(ex.getMessage());
- } else {
- ex.printStackTrace();
- }
- }
+ server.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
}
Propchange: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicFileServerExample.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicFileServerExample.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicFileServerExample.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Copied: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicGetExecutionExample.java (from r1767342, httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ElementalHttpGet.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicGetExecutionExample.java?p2=httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicGetExecutionExample.java&p1=httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ElementalHttpGet.java&r1=1767342&r2=1767343&rev=1767343&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ElementalHttpGet.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicGetExecutionExample.java Mon Oct 31 17:34:54 2016
@@ -27,41 +27,36 @@
package org.apache.hc.core5.http.examples;
+import java.io.IOException;
import java.net.Socket;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.ClassicHttpResponse;
-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.impl.HttpProcessors;
-import org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy;
import org.apache.hc.core5.http.impl.io.DefaultBHttpClientConnection;
-import org.apache.hc.core5.http.impl.io.HttpRequestExecutor;
+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.io.ResponseHandler;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
import org.apache.hc.core5.http.protocol.HttpCoreContext;
-import org.apache.hc.core5.http.protocol.HttpProcessor;
/**
- * Elemental example for executing multiple GET requests sequentially.
+ * Example of GET requests execution using classic I/O.
*/
-public class ElementalHttpGet {
+public class ClassicGetExecutionExample {
public static void main(String[] args) throws Exception {
- HttpProcessor httpproc = HttpProcessors.client();
- HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
-
- HttpCoreContext coreContext = HttpCoreContext.create();
+ HttpRequester httpRequester = RequesterBootstrap.bootstrap().create();
HttpHost host = new HttpHost("localhost", 8080);
- DefaultBHttpClientConnection conn = new DefaultBHttpClientConnection(8 * 1024);
- ConnectionReuseStrategy connStrategy = DefaultConnectionReuseStrategy.INSTANCE;
-
- try {
+ try (DefaultBHttpClientConnection conn = new DefaultBHttpClientConnection(8 * 1024)) {
+ HttpCoreContext coreContext = HttpCoreContext.create();
String[] targets = {
"/",
- "/servlets-examples/servlet/RequestInfoExample",
+ "/examples/servlets/servlet/RequestInfoExample",
"/somewhere%20in%20pampa"};
for (int i = 0; i < targets.length; i++) {
@@ -71,22 +66,18 @@ public class ElementalHttpGet {
}
ClassicHttpRequest request = new BasicClassicHttpRequest("GET", host, targets[i]);
System.out.println(">> Request URI: " + request.getUri());
+ httpRequester.execute(conn, request, coreContext, new ResponseHandler<Void>() {
- httpexecutor.preProcess(request, httpproc, coreContext);
- ClassicHttpResponse response = httpexecutor.execute(request, conn, coreContext);
- httpexecutor.postProcess(response, httpproc, coreContext);
-
- System.out.println("<< Response: " + response.getCode());
- System.out.println(EntityUtils.toString(response.getEntity()));
- System.out.println("==============");
- if (!connStrategy.keepAlive(request, response, coreContext)) {
- conn.close();
- } else {
- System.out.println("Connection kept alive...");
- }
+ @Override
+ public Void handleResponse(final ClassicHttpResponse response) throws HttpException, IOException {
+ System.out.println("<< Response: " + response.getCode());
+ System.out.println(EntityUtils.toString(response.getEntity()));
+ System.out.println("==============");
+ return null;
+ }
+
+ });
}
- } finally {
- conn.close();
}
}
Propchange: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicGetExecutionExample.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicGetExecutionExample.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicGetExecutionExample.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Copied: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostExecutionExample.java (from r1767342, httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ElementalHttpPost.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostExecutionExample.java?p2=httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostExecutionExample.java&p1=httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ElementalHttpPost.java&r1=1767342&r2=1767343&rev=1767343&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ElementalHttpPost.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostExecutionExample.java Mon Oct 31 17:34:54 2016
@@ -28,18 +28,19 @@
package org.apache.hc.core5.http.examples;
import java.io.ByteArrayInputStream;
+import java.io.IOException;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.ClassicHttpResponse;
-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.HttpHost;
-import org.apache.hc.core5.http.impl.HttpProcessors;
-import org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy;
import org.apache.hc.core5.http.impl.io.DefaultBHttpClientConnection;
-import org.apache.hc.core5.http.impl.io.HttpRequestExecutor;
+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.io.ResponseHandler;
import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
import org.apache.hc.core5.http.io.entity.ContentType;
import org.apache.hc.core5.http.io.entity.EntityUtils;
@@ -47,25 +48,19 @@ import org.apache.hc.core5.http.io.entit
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.http.protocol.HttpProcessor;
/**
- * Elemental example for executing multiple POST requests sequentially.
+ * Example of POST requests execution using classic I/O.
*/
-public class ElementalHttpPost {
+public class ClassicPostExecutionExample {
public static void main(String[] args) throws Exception {
- HttpProcessor httpproc = HttpProcessors.client();
- HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
-
- HttpCoreContext coreContext = HttpCoreContext.create();
+ HttpRequester httpRequester = RequesterBootstrap.bootstrap().create();
HttpHost host = new HttpHost("localhost", 8080);
- DefaultBHttpClientConnection conn = new DefaultBHttpClientConnection(8 * 1024);
- ConnectionReuseStrategy connStrategy = DefaultConnectionReuseStrategy.INSTANCE;
-
- try {
+ try (DefaultBHttpClientConnection conn = new DefaultBHttpClientConnection(8 * 1024)) {
+ HttpCoreContext coreContext = HttpCoreContext.create();
HttpEntity[] requestBodies = {
new StringEntity(
"This is the first test request",
@@ -76,7 +71,7 @@ public class ElementalHttpPost {
new InputStreamEntity(
new ByteArrayInputStream(
"This is the third test request (will be chunked)"
- .getBytes(StandardCharsets.UTF_8)),
+ .getBytes(StandardCharsets.UTF_8)),
ContentType.APPLICATION_OCTET_STREAM)
};
@@ -86,25 +81,21 @@ public class ElementalHttpPost {
conn.bind(socket);
}
ClassicHttpRequest request = new BasicClassicHttpRequest("POST", host,
- "/servlets-examples/servlet/RequestInfoExample");
+ "/examples/servlets/servlet/RequestInfoExample");
request.setEntity(requestBodies[i]);
System.out.println(">> Request URI: " + request.getUri());
+ httpRequester.execute(conn, request, coreContext, new ResponseHandler<Void>() {
- httpexecutor.preProcess(request, httpproc, coreContext);
- ClassicHttpResponse response = httpexecutor.execute(request, conn, coreContext);
- httpexecutor.postProcess(response, httpproc, coreContext);
-
- System.out.println("<< Response: " + response.getCode());
- System.out.println(EntityUtils.toString(response.getEntity()));
- System.out.println("==============");
- if (!connStrategy.keepAlive(request, response, coreContext)) {
- conn.close();
- } else {
- System.out.println("Connection kept alive...");
- }
+ @Override
+ public Void handleResponse(final ClassicHttpResponse response) throws HttpException, IOException {
+ System.out.println("<< Response: " + response.getCode());
+ System.out.println(EntityUtils.toString(response.getEntity()));
+ System.out.println("==============");
+ return null;
+ }
+
+ });
}
- } finally {
- conn.close();
}
}
Propchange: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostExecutionExample.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostExecutionExample.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostExecutionExample.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Copied: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostWithTrailersExecutionExample.java (from r1767342, httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ElementalHttpPostTrailers.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostWithTrailersExecutionExample.java?p2=httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostWithTrailersExecutionExample.java&p1=httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ElementalHttpPostTrailers.java&r1=1767342&r2=1767343&rev=1767343&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ElementalHttpPostTrailers.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostWithTrailersExecutionExample.java Mon Oct 31 17:34:54 2016
@@ -27,15 +27,17 @@
package org.apache.hc.core5.http.examples;
-import java.net.Socket;
+import java.io.IOException;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.HttpEntity;
+import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpHost;
-import org.apache.hc.core5.http.impl.HttpProcessors;
import org.apache.hc.core5.http.impl.io.DefaultBHttpClientConnection;
-import org.apache.hc.core5.http.impl.io.HttpRequestExecutor;
+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.io.ResponseHandler;
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.HttpEntityWithTrailers;
@@ -43,33 +45,38 @@ import org.apache.hc.core5.http.io.entit
import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
import org.apache.hc.core5.http.message.BasicHeader;
import org.apache.hc.core5.http.protocol.HttpCoreContext;
-import org.apache.hc.core5.http.protocol.HttpProcessor;
/**
- * Elemental example for executing POST request with trailing headers
+ * Example of POST request with trailers execution using classic I/O.
*/
-public class ElementalHttpPostTrailers {
+public class ClassicPostWithTrailersExecutionExample {
public static void main(String[] args) throws Exception {
- HttpProcessor httpproc = HttpProcessors.client();
- HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
- HttpCoreContext coreContext = HttpCoreContext.create();
+ HttpRequester httpRequester = RequesterBootstrap.bootstrap().create();
HttpHost host = new HttpHost("localhost", 8080);
- DefaultBHttpClientConnection conn = new DefaultBHttpClientConnection(8 * 1024);
- HttpEntity requestBody = new HttpEntityWithTrailers(
- new StringEntity("Chunked message with trailers", ContentType.TEXT_PLAIN),
- new BasicHeader("t1","Hello world"));
- Socket socket = new Socket(host.getHostName(), host.getPort());
- conn.bind(socket);
- ClassicHttpRequest request = new BasicClassicHttpRequest("POST", host, "/");
- request.setEntity(requestBody);
- httpexecutor.preProcess(request, httpproc, coreContext);
- ClassicHttpResponse response = httpexecutor.execute(request, conn, coreContext);
- httpexecutor.postProcess(response, httpproc, coreContext);
-
- System.out.println("<< Response: " + response.getCode());
- System.out.println(EntityUtils.toString(response.getEntity()));
- System.out.println("==============");
- conn.close();
+
+ try (DefaultBHttpClientConnection conn = new DefaultBHttpClientConnection(8 * 1024)) {
+
+ HttpCoreContext coreContext = HttpCoreContext.create();
+
+ ClassicHttpRequest request = new BasicClassicHttpRequest("POST", host, "/");
+ HttpEntity requestBody = new HttpEntityWithTrailers(
+ new StringEntity("Chunked message with trailers", ContentType.TEXT_PLAIN),
+ new BasicHeader("t1","Hello world"));
+ request.setEntity(requestBody);
+
+ System.out.println(">> Request URI: " + request.getUri());
+ httpRequester.execute(conn, request, coreContext, new ResponseHandler<Void>() {
+
+ @Override
+ public Void handleResponse(final ClassicHttpResponse response) throws HttpException, IOException {
+ System.out.println("<< Response: " + response.getCode());
+ System.out.println(EntityUtils.toString(response.getEntity()));
+ System.out.println("==============");
+ return null;
+ }
+
+ });
+ }
}
}
Propchange: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostWithTrailersExecutionExample.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostWithTrailersExecutionExample.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostWithTrailersExecutionExample.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicReverseProxyExample.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicReverseProxyExample.java?rev=1767343&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicReverseProxyExample.java (added)
+++ httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicReverseProxyExample.java Mon Oct 31 17:34:54 2016
@@ -0,0 +1,203 @@
+/*
+ * ====================================================================
+ * 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.examples;
+
+import java.io.IOException;
+import java.io.InterruptedIOException;
+import java.net.SocketTimeoutException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Locale;
+import java.util.Set;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+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.ConnectionClosedException;
+import org.apache.hc.core5.http.ExceptionListener;
+import org.apache.hc.core5.http.Header;
+import org.apache.hc.core5.http.HttpException;
+import org.apache.hc.core5.http.HttpHeaders;
+import org.apache.hc.core5.http.HttpHost;
+import org.apache.hc.core5.http.impl.io.bootstrap.HttpRequester;
+import org.apache.hc.core5.http.impl.io.bootstrap.HttpServer;
+import org.apache.hc.core5.http.impl.io.bootstrap.RequesterBootstrap;
+import org.apache.hc.core5.http.impl.io.bootstrap.ServerBootstrap;
+import org.apache.hc.core5.http.impl.io.pool.BasicConnPool;
+import org.apache.hc.core5.http.impl.io.pool.BasicPoolEntry;
+import org.apache.hc.core5.http.io.HttpClientConnection;
+import org.apache.hc.core5.http.io.HttpRequestHandler;
+import org.apache.hc.core5.http.io.entity.HttpEntityWrapper;
+import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
+import org.apache.hc.core5.http.protocol.HttpContext;
+import org.apache.hc.core5.http.protocol.HttpCoreContext;
+
+/**
+ * Example of embedded HTTP/1.1 reverse proxy using classic I/O.
+ */
+public class ClassicReverseProxyExample {
+
+ public static void main(final String[] args) throws Exception {
+ if (args.length < 1) {
+ System.out.println("Usage: <hostname[:port]> [listener port]");
+ System.exit(1);
+ }
+ final HttpHost targetHost = HttpHost.create(args[0]);
+ int port = 8080;
+ if (args.length > 1) {
+ port = Integer.parseInt(args[1]);
+ }
+
+ System.out.println("Reverse proxy to " + targetHost);
+
+ final HttpRequester requester = RequesterBootstrap.bootstrap()
+ .create();
+
+ final BasicConnPool connPool = new BasicConnPool();
+ connPool.setDefaultMaxPerRoute(20);
+ connPool.setMaxTotal(100);
+
+ final HttpServer server = ServerBootstrap.bootstrap()
+ .setListenerPort(port)
+ .setExceptionListener(new ExceptionListener() {
+
+ @Override
+ public void onError(final Exception ex) {
+ if (ex instanceof SocketTimeoutException) {
+ System.err.println("Connection timed out");
+ } else if (ex instanceof ConnectionClosedException) {
+ System.err.println(ex.getMessage());
+ } else {
+ ex.printStackTrace();
+ }
+ }
+
+ })
+ .registerHandler("*", new ProxyHandler(targetHost, connPool, requester))
+ .create();
+
+ server.start();
+ Runtime.getRuntime().addShutdownHook(new Thread() {
+ @Override
+ public void run() {
+ server.shutdown(5, TimeUnit.SECONDS);
+ }
+ });
+
+ System.out.println("Listening on port " + port);
+ server.awaitTermination(Integer.MAX_VALUE, TimeUnit.DAYS);
+ }
+
+ private final static Set<String> HOP_BY_HOP = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(
+ HttpHeaders.CONTENT_LENGTH.toLowerCase(Locale.ROOT),
+ HttpHeaders.TRANSFER_ENCODING.toLowerCase(Locale.ROOT),
+ HttpHeaders.CONNECTION.toLowerCase(Locale.ROOT),
+ "Keep-Alive".toLowerCase(Locale.ROOT),
+ "Proxy-Authenticate".toLowerCase(Locale.ROOT),
+ HttpHeaders.TE.toLowerCase(Locale.ROOT),
+ HttpHeaders.TRAILER.toLowerCase(Locale.ROOT),
+ HttpHeaders.UPGRADE.toLowerCase(Locale.ROOT))));
+
+
+ static class ProxyHandler implements HttpRequestHandler {
+
+ private final HttpHost targetHost;
+ private final BasicConnPool connPool;
+ private final HttpRequester requester;
+
+ public ProxyHandler(
+ final HttpHost targetHost,
+ final BasicConnPool connPool,
+ final HttpRequester requester) {
+ super();
+ this.targetHost = targetHost;
+ this.connPool = connPool;
+ this.requester = requester;
+ }
+
+ @Override
+ public void handle(
+ final ClassicHttpRequest incomingRequest,
+ final ClassicHttpResponse outgoingResponse,
+ final HttpContext serverContext) throws HttpException, IOException {
+
+ final Future<BasicPoolEntry> future = connPool.lease(targetHost, null);
+ final BasicPoolEntry poolEntry;
+ try {
+ poolEntry = future.get();
+ } catch (InterruptedException ex) {
+ throw new InterruptedIOException();
+ } catch (ExecutionException ex) {
+ Throwable cause = ex.getCause();
+ if (cause instanceof RuntimeException) {
+ throw (RuntimeException) cause;
+ } else if (cause instanceof IOException) {
+ throw (IOException) cause;
+ } else {
+ throw new IOException("Failure obtaining connection to " + targetHost);
+ }
+ }
+ final HttpCoreContext clientContext = HttpCoreContext.create();
+ final ClassicHttpRequest outgoingRequest = new BasicClassicHttpRequest(incomingRequest.getMethod(), incomingRequest.getPath());
+ for (Iterator<Header> it = incomingRequest.headerIterator(); it.hasNext(); ) {
+ Header header = it.next();
+ if (!HOP_BY_HOP.contains(header.getName().toLowerCase(Locale.ROOT))) {
+ outgoingRequest.addHeader(header);
+ }
+ }
+ final HttpClientConnection connection = poolEntry.getConnection();
+ final ClassicHttpResponse incomingResponse = requester.execute(connection, outgoingRequest, clientContext);
+ outgoingResponse.setCode(incomingResponse.getCode());
+ for (Iterator<Header> it = incomingResponse.headerIterator(); it.hasNext(); ) {
+ Header header = it.next();
+ if (!HOP_BY_HOP.contains(header.getName().toLowerCase(Locale.ROOT))) {
+ outgoingResponse.addHeader(header);
+ }
+ }
+ outgoingResponse.setEntity(new HttpEntityWrapper(incomingResponse.getEntity()) {
+
+ @Override
+ public void close() throws IOException {
+ boolean keepAlive = false;
+ try {
+ super.close();
+ keepAlive = requester.keepAlive(connection, outgoingRequest, incomingResponse, clientContext);
+ } finally {
+ connPool.release(poolEntry, keepAlive);
+ }
+ }
+
+ });
+ }
+ }
+
+}
Propchange: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicReverseProxyExample.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicReverseProxyExample.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpcore/trunk/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicReverseProxyExample.java
------------------------------------------------------------------------------
svn:mime-type = text/plain