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 2019/02/07 18:04:41 UTC

[httpcomponents-core] 01/01: HTTPCORE-570: support for setting of custom LookupRegistry by server bootstrap classes

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

olegk pushed a commit to branch HTTPCORE-570
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git

commit c9c79cfd18345fd50879543abc72f8e2aa2c040e
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed Feb 6 16:12:22 2019 -0500

    HTTPCORE-570: support for setting of custom LookupRegistry by server bootstrap classes
---
 .../impl/nio/bootstrap/H2ServerBootstrap.java      | 21 ++++++--
 .../core5/testing/framework/TestingFramework.java  |  4 +-
 .../core5/testing/nio/Http1AuthenticationTest.java |  1 +
 .../testing/nio/Http1ServerAndRequesterTest.java   |  2 +
 .../testing/nio/Http2ServerAndRequesterTest.java   |  2 +
 .../http/impl/bootstrap/AsyncServerBootstrap.java  | 27 +++++++---
 .../core5/http/impl/bootstrap/ServerBootstrap.java | 23 ++++++--
 .../TestAsyncServerBootstrapLookupRegistry.java    | 61 ++++++++++++++++++++++
 8 files changed, 122 insertions(+), 19 deletions(-)

diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/bootstrap/H2ServerBootstrap.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/bootstrap/H2ServerBootstrap.java
index f5c1759..e446895 100644
--- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/bootstrap/H2ServerBootstrap.java
+++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/bootstrap/H2ServerBootstrap.java
@@ -56,6 +56,7 @@ import org.apache.hc.core5.http.nio.support.BasicServerExchangeHandler;
 import org.apache.hc.core5.http.nio.support.DefaultAsyncResponseExchangeHandlerFactory;
 import org.apache.hc.core5.http.nio.support.TerminalAsyncServerFilter;
 import org.apache.hc.core5.http.protocol.HttpProcessor;
+import org.apache.hc.core5.http.protocol.LookupRegistry;
 import org.apache.hc.core5.http.protocol.RequestHandlerRegistry;
 import org.apache.hc.core5.http.protocol.UriPatternType;
 import org.apache.hc.core5.http2.HttpVersionPolicy;
@@ -83,7 +84,7 @@ public class H2ServerBootstrap {
     private final List<HandlerEntry<Supplier<AsyncServerExchangeHandler>>> handlerList;
     private final List<FilterEntry<AsyncFilterHandler>> filters;
     private String canonicalHostName;
-    private UriPatternType uriPatternType;
+    private LookupRegistry<AsyncServerExchangeHandler> lookupRegistry;
     private IOReactorConfig ioReactorConfig;
     private HttpProcessor httpProcessor;
     private CharCodingConfig charCodingConfig;
@@ -210,10 +211,13 @@ public class H2ServerBootstrap {
     }
 
     /**
-     * Assigns {@link UriPatternType} for handler registration.
+     * Assigns {@link LookupRegistry} instance.
+     *
+     * @return this
+     * @since 5.0
      */
-    public final H2ServerBootstrap setUriPatternType(final UriPatternType uriPatternType) {
-        this.uriPatternType = uriPatternType;
+    public final H2ServerBootstrap setLookupRegistry(final LookupRegistry<AsyncServerExchangeHandler> lookupRegistry) {
+        this.lookupRegistry = lookupRegistry;
         return this;
     }
 
@@ -346,7 +350,14 @@ public class H2ServerBootstrap {
     public HttpAsyncServer create() {
         final RequestHandlerRegistry<Supplier<AsyncServerExchangeHandler>> registry = new RequestHandlerRegistry<>(
                 canonicalHostName != null ? canonicalHostName : InetAddressUtils.getCanonicalLocalHostName(),
-                uriPatternType);
+                new Supplier() {
+
+                    @Override
+                    public LookupRegistry get() {
+                        return lookupRegistry != null ? lookupRegistry : UriPatternType.newMatcher(UriPatternType.URI_PATTERN);
+                    }
+
+                });
         for (final HandlerEntry<Supplier<AsyncServerExchangeHandler>> entry: handlerList) {
             registry.register(entry.hostname, entry.uriPattern, entry.handler);
         }
diff --git a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFramework.java b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFramework.java
index 4cb64cf..7c2f9f8 100644
--- a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFramework.java
+++ b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFramework.java
@@ -50,9 +50,10 @@ import java.util.concurrent.TimeUnit;
 
 import org.apache.hc.core5.http.HttpVersion;
 import org.apache.hc.core5.http.ProtocolVersion;
-import org.apache.hc.core5.http.io.SocketConfig;
 import org.apache.hc.core5.http.impl.bootstrap.HttpServer;
 import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap;
+import org.apache.hc.core5.http.io.SocketConfig;
+import org.apache.hc.core5.http.protocol.UriPatternMatcher;
 import org.apache.hc.core5.io.CloseMode;
 
 public class TestingFramework {
@@ -231,6 +232,7 @@ public class TestingFramework {
                                           .build();
 
         final ServerBootstrap serverBootstrap = ServerBootstrap.bootstrap()
+                                          .setLookupRegistry(new UriPatternMatcher())
                                           .setSocketConfig(socketConfig)
                                           .register("/*", requestHandler);
 
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1AuthenticationTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1AuthenticationTest.java
index 63cb2e6..2e88994 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1AuthenticationTest.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1AuthenticationTest.java
@@ -103,6 +103,7 @@ public class Http1AuthenticationTest {
         protected void before() throws Throwable {
             log.debug("Starting up test server");
             server = AsyncServerBootstrap.bootstrap()
+                    .setLookupRegistry(null) // same as the default
                     .setIOReactorConfig(
                             IOReactorConfig.custom()
                                     .setSoTimeout(TIMEOUT)
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1ServerAndRequesterTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1ServerAndRequesterTest.java
index 9b8351c..04c3496 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1ServerAndRequesterTest.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1ServerAndRequesterTest.java
@@ -67,6 +67,7 @@ import org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer;
 import org.apache.hc.core5.http.nio.ssl.BasicClientTlsStrategy;
 import org.apache.hc.core5.http.nio.ssl.BasicServerTlsStrategy;
 import org.apache.hc.core5.http.protocol.HttpContext;
+import org.apache.hc.core5.http.protocol.UriPatternMatcher;
 import org.apache.hc.core5.io.CloseMode;
 import org.apache.hc.core5.reactor.ExceptionEvent;
 import org.apache.hc.core5.reactor.IOReactorConfig;
@@ -113,6 +114,7 @@ public class Http1ServerAndRequesterTest {
         protected void before() throws Throwable {
             log.debug("Starting up test server");
             server = AsyncServerBootstrap.bootstrap()
+                    .setLookupRegistry(new UriPatternMatcher<AsyncServerExchangeHandler>()) // same as the default
                     .setIOReactorConfig(
                             IOReactorConfig.custom()
                                     .setSoTimeout(TIMEOUT)
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http2ServerAndRequesterTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http2ServerAndRequesterTest.java
index 36a4678..4db8b51 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http2ServerAndRequesterTest.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http2ServerAndRequesterTest.java
@@ -50,6 +50,7 @@ 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.http.nio.entity.StringAsyncEntityProducer;
+import org.apache.hc.core5.http.protocol.UriPatternMatcher;
 import org.apache.hc.core5.http2.HttpVersionPolicy;
 import org.apache.hc.core5.http2.impl.nio.bootstrap.H2RequesterBootstrap;
 import org.apache.hc.core5.http2.impl.nio.bootstrap.H2ServerBootstrap;
@@ -105,6 +106,7 @@ public class Http2ServerAndRequesterTest {
         protected void before() throws Throwable {
             log.debug("Starting up test server");
             server = H2ServerBootstrap.bootstrap()
+                    .setLookupRegistry(new UriPatternMatcher())
                     .setVersionPolicy(HttpVersionPolicy.NEGOTIATE)
                     .setIOReactorConfig(
                             IOReactorConfig.custom()
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/AsyncServerBootstrap.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/AsyncServerBootstrap.java
index ce74518..0115961 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/AsyncServerBootstrap.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/AsyncServerBootstrap.java
@@ -56,6 +56,7 @@ import org.apache.hc.core5.http.nio.support.BasicServerExchangeHandler;
 import org.apache.hc.core5.http.nio.support.DefaultAsyncResponseExchangeHandlerFactory;
 import org.apache.hc.core5.http.nio.support.TerminalAsyncServerFilter;
 import org.apache.hc.core5.http.protocol.HttpProcessor;
+import org.apache.hc.core5.http.protocol.LookupRegistry;
 import org.apache.hc.core5.http.protocol.RequestHandlerRegistry;
 import org.apache.hc.core5.http.protocol.UriPatternType;
 import org.apache.hc.core5.net.InetAddressUtils;
@@ -76,7 +77,7 @@ public class AsyncServerBootstrap {
     private final List<HandlerEntry<Supplier<AsyncServerExchangeHandler>>> handlerList;
     private final List<FilterEntry<AsyncFilterHandler>> filters;
     private String canonicalHostName;
-    private UriPatternType uriPatternType;
+    private LookupRegistry<AsyncServerExchangeHandler> lookupRegistry;
     private IOReactorConfig ioReactorConfig;
     private H1Config h1Config;
     private CharCodingConfig charCodingConfig;
@@ -178,20 +179,23 @@ public class AsyncServerBootstrap {
     }
 
     /**
-     * Assigns {@link Http1StreamListener} instance.
+     * Assigns {@link LookupRegistry} instance.
      *
+     * @return this
      * @since 5.0
      */
-    public final AsyncServerBootstrap setStreamListener(final Http1StreamListener streamListener) {
-        this.streamListener = streamListener;
+    public final AsyncServerBootstrap setLookupRegistry(final LookupRegistry<AsyncServerExchangeHandler> lookupRegistry) {
+        this.lookupRegistry = lookupRegistry;
         return this;
     }
 
     /**
-     * Assigns {@link UriPatternType} for handler registration.
+     * Assigns {@link Http1StreamListener} instance.
+     *
+     * @since 5.0
      */
-    public final AsyncServerBootstrap setUriPatternType(final UriPatternType uriPatternType) {
-        this.uriPatternType = uriPatternType;
+    public final AsyncServerBootstrap setStreamListener(final Http1StreamListener streamListener) {
+        this.streamListener = streamListener;
         return this;
     }
 
@@ -324,7 +328,14 @@ public class AsyncServerBootstrap {
     public HttpAsyncServer create() {
         final RequestHandlerRegistry<Supplier<AsyncServerExchangeHandler>> registry = new RequestHandlerRegistry<>(
                 canonicalHostName != null ? canonicalHostName : InetAddressUtils.getCanonicalLocalHostName(),
-                uriPatternType);
+                new Supplier() {
+
+                    @Override
+                    public LookupRegistry get() {
+                        return lookupRegistry != null ? lookupRegistry : UriPatternType.newMatcher(UriPatternType.URI_PATTERN);
+                    }
+
+                });
         for (final HandlerEntry<Supplier<AsyncServerExchangeHandler>> entry: handlerList) {
             registry.register(entry.hostname, entry.uriPattern, entry.handler);
         }
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/ServerBootstrap.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/ServerBootstrap.java
index 629e6c7..2af402d 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/ServerBootstrap.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/ServerBootstrap.java
@@ -34,6 +34,7 @@ import javax.net.ServerSocketFactory;
 import javax.net.ssl.SSLContext;
 import javax.net.ssl.SSLServerSocketFactory;
 
+import org.apache.hc.core5.function.Supplier;
 import org.apache.hc.core5.http.ClassicHttpResponse;
 import org.apache.hc.core5.http.ConnectionReuseStrategy;
 import org.apache.hc.core5.http.ExceptionListener;
@@ -60,7 +61,9 @@ import org.apache.hc.core5.http.io.support.HttpServerExpectationFilter;
 import org.apache.hc.core5.http.io.support.HttpServerFilterChainElement;
 import org.apache.hc.core5.http.io.support.HttpServerFilterChainRequestHandler;
 import org.apache.hc.core5.http.io.support.TerminalServerFilter;
+import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
 import org.apache.hc.core5.http.protocol.HttpProcessor;
+import org.apache.hc.core5.http.protocol.LookupRegistry;
 import org.apache.hc.core5.http.protocol.RequestHandlerRegistry;
 import org.apache.hc.core5.http.protocol.UriPatternType;
 import org.apache.hc.core5.net.InetAddressUtils;
@@ -76,7 +79,7 @@ public class ServerBootstrap {
     private final List<HandlerEntry<HttpRequestHandler>> handlerList;
     private final List<FilterEntry<HttpFilterHandler>> filters;
     private String canonicalHostName;
-    private UriPatternType uriPatternType;
+    private LookupRegistry<AsyncServerExchangeHandler> lookupRegistry;
     private int listenerPort;
     private InetAddress localAddress;
     private SocketConfig socketConfig;
@@ -176,10 +179,13 @@ public class ServerBootstrap {
     }
 
     /**
-     * Assigns {@link UriPatternType} for handler registration.
+     * Assigns {@link LookupRegistry} instance.
+     *
+     * @return this
+     * @since 5.0
      */
-    public final ServerBootstrap  setUriPatternType(final UriPatternType uriPatternType) {
-        this.uriPatternType = uriPatternType;
+    public final ServerBootstrap setLookupRegistry(final LookupRegistry<AsyncServerExchangeHandler> lookupRegistry) {
+        this.lookupRegistry = lookupRegistry;
         return this;
     }
 
@@ -320,7 +326,14 @@ public class ServerBootstrap {
     public HttpServer create() {
         final RequestHandlerRegistry<HttpRequestHandler> handlerRegistry = new RequestHandlerRegistry<>(
                 canonicalHostName != null ? canonicalHostName : InetAddressUtils.getCanonicalLocalHostName(),
-                uriPatternType);
+                new Supplier() {
+
+                    @Override
+                    public LookupRegistry get() {
+                        return lookupRegistry != null ? lookupRegistry : UriPatternType.newMatcher(UriPatternType.URI_PATTERN);
+                    }
+
+                });
         for (final HandlerEntry<HttpRequestHandler> entry: handlerList) {
             handlerRegistry.register(entry.hostname, entry.uriPattern, entry.handler);
         }
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/impl/bootstrap/TestAsyncServerBootstrapLookupRegistry.java b/httpcore5/src/test/java/org/apache/hc/core5/http/impl/bootstrap/TestAsyncServerBootstrapLookupRegistry.java
new file mode 100644
index 0000000..96225df
--- /dev/null
+++ b/httpcore5/src/test/java/org/apache/hc/core5/http/impl/bootstrap/TestAsyncServerBootstrapLookupRegistry.java
@@ -0,0 +1,61 @@
+/*
+ * ====================================================================
+ * 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.bootstrap;
+
+import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
+import org.apache.hc.core5.http.protocol.LookupRegistry;
+import org.junit.Test;
+
+public class TestAsyncServerBootstrapLookupRegistry {
+
+    @Test
+    public void testCreateNullLookupRegistry() {
+        AsyncServerBootstrap.bootstrap().setLookupRegistry(null).create();
+    }
+
+    @Test
+    public void testCreateCustomLookupRegistry() {
+        AsyncServerBootstrap.bootstrap().setLookupRegistry(new LookupRegistry<AsyncServerExchangeHandler>() {
+
+            @Override
+            public void register(final String pattern, final AsyncServerExchangeHandler obj) {
+                // noop
+            }
+
+            @Override
+            public AsyncServerExchangeHandler lookup(final String value) {
+                // noop
+                return null;
+            }
+
+            @Override
+            public void unregister(final String pattern) {
+                // noop
+            }
+        }).create();
+    }
+}