You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2020/08/24 16:50:15 UTC

[camel] branch master updated: camel-itests: fix deprecation warnings (#4122)

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new 9020c24  camel-itests: fix deprecation warnings (#4122)
9020c24 is described below

commit 9020c2444ef39c737949d4e652f762f45247f3fd
Author: Otavio Rodolfo Piske <or...@users.noreply.github.com>
AuthorDate: Mon Aug 24 18:49:49 2020 +0200

    camel-itests: fix deprecation warnings (#4122)
    
    This replaces the HTTP test server with the stock one from HttpComponents.
---
 .../apache/camel/itest/http/HttpEndpointTest.java  |   2 +-
 .../itest/http/HttpMaxConnectionPerHostTest.java   |   2 +-
 .../apache/camel/itest/http/HttpTestServer.java    | 326 ++-------------------
 3 files changed, 26 insertions(+), 304 deletions(-)

diff --git a/tests/camel-itest/src/test/java/org/apache/camel/itest/http/HttpEndpointTest.java b/tests/camel-itest/src/test/java/org/apache/camel/itest/http/HttpEndpointTest.java
index 2227556..44f0e5f 100644
--- a/tests/camel-itest/src/test/java/org/apache/camel/itest/http/HttpEndpointTest.java
+++ b/tests/camel-itest/src/test/java/org/apache/camel/itest/http/HttpEndpointTest.java
@@ -53,7 +53,7 @@ public class HttpEndpointTest {
 
     @BeforeAll
     public static void setUp() throws Exception {
-        localServer = new HttpTestServer(null, null);
+        localServer = new HttpTestServer();
         localServer.register("/", new HttpRequestHandler() {
             public void handle(
                     HttpRequest request, HttpResponse response,
diff --git a/tests/camel-itest/src/test/java/org/apache/camel/itest/http/HttpMaxConnectionPerHostTest.java b/tests/camel-itest/src/test/java/org/apache/camel/itest/http/HttpMaxConnectionPerHostTest.java
index 438a7d8..b137631 100644
--- a/tests/camel-itest/src/test/java/org/apache/camel/itest/http/HttpMaxConnectionPerHostTest.java
+++ b/tests/camel-itest/src/test/java/org/apache/camel/itest/http/HttpMaxConnectionPerHostTest.java
@@ -53,7 +53,7 @@ public class HttpMaxConnectionPerHostTest {
 
     @BeforeAll
     public static void setUp() throws Exception {
-        localServer = new HttpTestServer(null, null);
+        localServer = new HttpTestServer();
         localServer.register("/", new HttpRequestHandler() {
             public void handle(
                     HttpRequest request, HttpResponse response,
diff --git a/tests/camel-itest/src/test/java/org/apache/camel/itest/http/HttpTestServer.java b/tests/camel-itest/src/test/java/org/apache/camel/itest/http/HttpTestServer.java
index c8c9ec8..5073c03 100644
--- a/tests/camel-itest/src/test/java/org/apache/camel/itest/http/HttpTestServer.java
+++ b/tests/camel-itest/src/test/java/org/apache/camel/itest/http/HttpTestServer.java
@@ -16,45 +16,16 @@
  */
 package org.apache.camel.itest.http;
 
-import java.io.IOException;
-import java.net.InetSocketAddress;
-import java.net.ServerSocket;
-import java.net.Socket;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.Set;
-import java.util.concurrent.atomic.AtomicInteger;
-
-import javax.net.ssl.SSLContext;
-import javax.net.ssl.SSLServerSocketFactory;
-
 import org.apache.camel.test.AvailablePortFinder;
 import org.apache.http.ConnectionReuseStrategy;
-import org.apache.http.HttpResponseFactory;
-import org.apache.http.HttpResponseInterceptor;
-import org.apache.http.HttpServerConnection;
+import org.apache.http.config.SocketConfig;
 import org.apache.http.impl.DefaultConnectionReuseStrategy;
-import org.apache.http.impl.DefaultHttpResponseFactory;
-import org.apache.http.impl.DefaultHttpServerConnection;
+import org.apache.http.impl.bootstrap.HttpServer;
+import org.apache.http.impl.bootstrap.ServerBootstrap;
 import org.apache.http.localserver.EchoHandler;
 import org.apache.http.localserver.RandomHandler;
-import org.apache.http.params.CoreConnectionPNames;
-import org.apache.http.params.CoreProtocolPNames;
-import org.apache.http.params.HttpParams;
-import org.apache.http.params.SyncBasicHttpParams;
-import org.apache.http.protocol.BasicHttpContext;
-import org.apache.http.protocol.BasicHttpProcessor;
-import org.apache.http.protocol.HttpContext;
-import org.apache.http.protocol.HttpExpectationVerifier;
-import org.apache.http.protocol.HttpProcessor;
 import org.apache.http.protocol.HttpRequestHandler;
-import org.apache.http.protocol.HttpRequestHandlerRegistry;
-import org.apache.http.protocol.HttpService;
-import org.apache.http.protocol.ImmutableHttpProcessor;
-import org.apache.http.protocol.ResponseConnControl;
-import org.apache.http.protocol.ResponseContent;
-import org.apache.http.protocol.ResponseDate;
-import org.apache.http.protocol.ResponseServer;
+import org.apache.http.protocol.UriHttpRequestHandlerMapper;
 
 /**
  * Copy of org.apache.http.localserver.LocalTestServer to use a specific port.
@@ -63,31 +34,10 @@ public class HttpTestServer {
 
     public static final int PORT = AvailablePortFinder.getNextAvailable();
 
-    /**
-     * The local address to bind to. The host is an IP number rather than "localhost" to avoid surprises on hosts that
-     * map "localhost" to an IPv6 address or something else. The port is 0 to let the system pick one.
-     */
-    public static final InetSocketAddress TEST_SERVER_ADDR = new InetSocketAddress("localhost", PORT);
-
     /** The request handler registry. */
-    private final HttpRequestHandlerRegistry handlerRegistry;
-
-    private final HttpService httpservice;
-
-    /** Optional SSL context */
-    private final SSLContext sslcontext;
-
-    /** The server socket, while being served. */
-    private volatile ServerSocket servicedSocket;
-
-    /** The request listening thread, while listening. */
-    private volatile ListenerThread listenerThread;
+    private final UriHttpRequestHandlerMapper handlerRegistry;
 
-    /** Set of active worker threads */
-    private final Set<Worker> workers;
-
-    /** The number of connections this accepted. */
-    private final AtomicInteger acceptedConnections = new AtomicInteger(0);
+    private final HttpServer httpServer;
 
     static {
         //set them as system properties so Spring can use the property placeholder
@@ -95,105 +45,34 @@ public class HttpTestServer {
         System.setProperty("HttpTestServer.Port", Integer.toString(PORT));
     }
 
-    /**
-     * Creates a new test server.
-     *
-     * @param proc       the HTTP processors to be used by the server, or <code>null</code> to use a
-     *                   {@link #newProcessor default} processor
-     * @param reuseStrat the connection reuse strategy to be used by the server, or <code>null</code> to use
-     *                   {@link #newConnectionReuseStrategy() default} strategy.
-     * @param params     the parameters to be used by the server, or <code>null</code> to use {@link #newDefaultParams
-     *                   default} parameters
-     * @param sslcontext optional SSL context if the server is to leverage SSL/TLS transport security
-     */
-    public HttpTestServer(
-                          final BasicHttpProcessor proc,
-                          final ConnectionReuseStrategy reuseStrat,
-                          final HttpResponseFactory responseFactory,
-                          final HttpExpectationVerifier expectationVerifier,
-                          final HttpParams params,
-                          final SSLContext sslcontext) {
-        this.handlerRegistry = new HttpRequestHandlerRegistry();
-        this.workers = Collections.synchronizedSet(new HashSet<Worker>());
-        this.httpservice = new HttpService(
-                proc != null ? proc : newProcessor(),
-                reuseStrat != null ? reuseStrat : newConnectionReuseStrategy(),
-                responseFactory != null ? responseFactory : newHttpResponseFactory(),
-                handlerRegistry,
-                expectationVerifier,
-                params != null ? params : newDefaultParams());
-        this.sslcontext = sslcontext;
-    }
-
-    /**
-     * Creates a new test server with SSL/TLS encryption.
-     *
-     * @param sslcontext SSL context
-     */
-    public HttpTestServer(final SSLContext sslcontext) {
-        this(null, null, null, null, null, sslcontext);
-    }
 
     /**
      * Creates a new test server.
      *
-     * @param proc   the HTTP processors to be used by the server, or <code>null</code> to use a {@link #newProcessor
-     *               default} processor
-     * @param params the parameters to be used by the server, or <code>null</code> to use {@link #newDefaultParams
-     *               default} parameters
      */
-    public HttpTestServer(
-                          BasicHttpProcessor proc,
-                          HttpParams params) {
-        this(proc, null, null, null, params, null);
-    }
+    public HttpTestServer() {
+        this.handlerRegistry = new UriHttpRequestHandlerMapper();
 
-    /**
-     * Obtains an HTTP protocol processor with default interceptors.
-     *
-     * @return a protocol processor for server-side use
-     */
-    protected HttpProcessor newProcessor() {
-        return new ImmutableHttpProcessor(
-                new HttpResponseInterceptor[] {
-                        new ResponseDate(),
-                        new ResponseServer(),
-                        new ResponseContent(),
-                        new ResponseConnControl() });
-    }
+        SocketConfig socketConfig = SocketConfig.custom()
+                .setSoTimeout(60000)
+                .setRcvBufSize(8 * 1024)
+                .setSndBufSize(8 * 1024)
+                .setTcpNoDelay(true)
+                .build();
 
-    /**
-     * Obtains a set of reasonable default parameters for a server.
-     *
-     * @return default parameters
-     */
-    protected HttpParams newDefaultParams() {
-        HttpParams params = new SyncBasicHttpParams();
-        params
-                .setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000)
-                .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
-                .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
-                .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
-                .setParameter(CoreProtocolPNames.ORIGIN_SERVER,
-                        "LocalTestServer/1.1");
-        return params;
+        this.httpServer = ServerBootstrap.bootstrap()
+                .setListenerPort(PORT)
+                .setConnectionReuseStrategy(newConnectionReuseStrategy())
+                .setHandlerMapper(handlerRegistry)
+                .setSocketConfig(socketConfig)
+                .setServerInfo("LocalTestServer/1.1")
+                .create();
     }
 
     protected ConnectionReuseStrategy newConnectionReuseStrategy() {
         return new DefaultConnectionReuseStrategy();
     }
 
-    protected HttpResponseFactory newHttpResponseFactory() {
-        return new DefaultHttpResponseFactory();
-    }
-
-    /**
-     * Returns the number of connections this test server has accepted.
-     */
-    public int getAcceptedConnectionCount() {
-        return acceptedConnections.get();
-    }
-
     /**
      * {@link #register Registers} a set of default request handlers.
      * 
@@ -220,173 +99,16 @@ public class HttpTestServer {
     }
 
     /**
-     * Unregisters a handler from the local registry.
-     *
-     * @param pattern the URL pattern
-     */
-    public void unregister(String pattern) {
-        handlerRegistry.unregister(pattern);
-    }
-
-    /**
      * Starts this test server.
      */
     public void start() throws Exception {
-        if (servicedSocket != null) {
-            throw new IllegalStateException(this.toString() + " already running");
-        }
-        ServerSocket ssock;
-        if (sslcontext != null) {
-            SSLServerSocketFactory sf = sslcontext.getServerSocketFactory();
-            ssock = sf.createServerSocket();
-        } else {
-            ssock = new ServerSocket();
-        }
-
-        ssock.setReuseAddress(true); // probably pointless for port '0'
-        ssock.bind(TEST_SERVER_ADDR);
-        servicedSocket = ssock;
-
-        listenerThread = new ListenerThread();
-        listenerThread.setDaemon(false);
-        listenerThread.start();
+        httpServer.start();
     }
 
     /**
      * Stops this test server.
      */
-    public void stop() throws Exception {
-        if (servicedSocket == null) {
-            return; // not running
-        }
-        ListenerThread t = listenerThread;
-        if (t != null) {
-            t.shutdown();
-        }
-        synchronized (workers) {
-            for (Worker worker : workers) {
-                worker.shutdown();
-            }
-        }
-    }
-
-    public void awaitTermination(long timeMs) throws InterruptedException {
-        if (listenerThread != null) {
-            listenerThread.join(timeMs);
-        }
-    }
-
-    @Override
-    public String toString() {
-        ServerSocket ssock = servicedSocket; // avoid synchronization
-        StringBuilder sb = new StringBuilder(80);
-        sb.append("LocalTestServer/");
-        if (ssock == null) {
-            sb.append("stopped");
-        } else {
-            sb.append(ssock.getLocalSocketAddress());
-        }
-        return sb.toString();
-    }
-
-    /**
-     * Obtains the local address the server is listening on
-     *
-     * @return the service address
-     */
-    public InetSocketAddress getServiceAddress() {
-        ServerSocket ssock = servicedSocket; // avoid synchronization
-        if (ssock == null) {
-            throw new IllegalStateException("not running");
-        }
-        return (InetSocketAddress) ssock.getLocalSocketAddress();
-    }
-
-    /**
-     * The request listener. Accepts incoming connections and launches a service thread.
-     */
-    class ListenerThread extends Thread {
-
-        private volatile Exception exception;
-
-        @Override
-        public void run() {
-            try {
-                while (!interrupted()) {
-                    Socket socket = servicedSocket.accept();
-                    acceptedConnections.incrementAndGet();
-                    DefaultHttpServerConnection conn = new DefaultHttpServerConnection();
-                    conn.bind(socket, httpservice.getParams());
-                    // Start worker thread
-                    Worker worker = new Worker(conn);
-                    workers.add(worker);
-                    worker.setDaemon(true);
-                    worker.start();
-                }
-            } catch (Exception ex) {
-                this.exception = ex;
-            } finally {
-                try {
-                    servicedSocket.close();
-                } catch (IOException ignore) {
-                }
-            }
-        }
-
-        public void shutdown() {
-            interrupt();
-            try {
-                servicedSocket.close();
-            } catch (IOException ignore) {
-            }
-        }
-
-        public Exception getException() {
-            return this.exception;
-        }
-
+    public void stop() {
+        httpServer.stop();
     }
-
-    class Worker extends Thread {
-
-        private final HttpServerConnection conn;
-
-        private volatile Exception exception;
-
-        Worker(final HttpServerConnection conn) {
-            this.conn = conn;
-        }
-
-        @Override
-        public void run() {
-            HttpContext context = new BasicHttpContext();
-            try {
-                while (this.conn.isOpen() && !Thread.interrupted()) {
-                    httpservice.handleRequest(this.conn, context);
-                }
-            } catch (Exception ex) {
-                this.exception = ex;
-            } finally {
-                workers.remove(this);
-                try {
-                    this.conn.shutdown();
-                } catch (IOException ignore) {
-                }
-            }
-        }
-
-        public void shutdown() {
-            interrupt();
-            try {
-                this.conn.shutdown();
-            } catch (IOException ignore) {
-            }
-        }
-
-        public Exception getException() {
-            return this.exception;
-        }
-
-    }
-
 }