You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by gg...@apache.org on 2019/02/06 21:12:34 UTC

[httpcomponents-core] 01/01: [HTTPCORE-570]

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

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

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

    [HTTPCORE-570]
    
    Add getters to AsyncServerBootstrap and refactor create() impl into
    protected methods
---
 .../http/impl/bootstrap/AsyncServerBootstrap.java  | 169 ++++++++++++++++-----
 1 file changed, 134 insertions(+), 35 deletions(-)

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..bf1e5a6 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
@@ -74,23 +74,23 @@ import org.apache.hc.core5.util.Timeout;
 public class AsyncServerBootstrap {
 
     private final List<HandlerEntry<Supplier<AsyncServerExchangeHandler>>> handlerList;
-    private final List<FilterEntry<AsyncFilterHandler>> filters;
+    private final List<FilterEntry<AsyncFilterHandler>> filterList;
     private String canonicalHostName;
     private UriPatternType uriPatternType;
     private IOReactorConfig ioReactorConfig;
     private H1Config h1Config;
     private CharCodingConfig charCodingConfig;
     private HttpProcessor httpProcessor;
-    private ConnectionReuseStrategy connStrategy;
+    private ConnectionReuseStrategy connectionReuseStrategy;
     private TlsStrategy tlsStrategy;
     private Timeout handshakeTimeout;
     private Decorator<IOSession> ioSessionDecorator;
-    private IOSessionListener sessionListener;
+    private IOSessionListener ioSessionListener;
     private Http1StreamListener streamListener;
 
     private AsyncServerBootstrap() {
         this.handlerList = new ArrayList<>();
-        this.filters = new ArrayList<>();
+        this.filterList = new ArrayList<>();
     }
 
     public static AsyncServerBootstrap bootstrap() {
@@ -141,7 +141,7 @@ public class AsyncServerBootstrap {
      * Assigns {@link org.apache.hc.core5.http.ConnectionReuseStrategy} instance.
      */
     public final AsyncServerBootstrap setConnectionReuseStrategy(final ConnectionReuseStrategy connStrategy) {
-        this.connStrategy = connStrategy;
+        this.connectionReuseStrategy = connStrategy;
         return this;
     }
 
@@ -173,7 +173,7 @@ public class AsyncServerBootstrap {
      * Assigns {@link IOSessionListener} instance.
      */
     public final AsyncServerBootstrap setIOSessionListener(final IOSessionListener sessionListener) {
-        this.sessionListener = sessionListener;
+        this.ioSessionListener = sessionListener;
         return this;
     }
 
@@ -276,7 +276,7 @@ public class AsyncServerBootstrap {
         Args.notBlank(existing, "Existing");
         Args.notBlank(name, "Name");
         Args.notNull(filterHandler, "Filter handler");
-        filters.add(new FilterEntry<>(FilterEntry.Postion.BEFORE, name, filterHandler, existing));
+        filterList.add(new FilterEntry<>(FilterEntry.Postion.BEFORE, name, filterHandler, existing));
         return this;
     }
 
@@ -287,7 +287,7 @@ public class AsyncServerBootstrap {
         Args.notBlank(existing, "Existing");
         Args.notBlank(name, "Name");
         Args.notNull(filterHandler, "Filter handler");
-        filters.add(new FilterEntry<>(FilterEntry.Postion.AFTER, name, filterHandler, existing));
+        filterList.add(new FilterEntry<>(FilterEntry.Postion.AFTER, name, filterHandler, existing));
         return this;
     }
 
@@ -297,7 +297,7 @@ public class AsyncServerBootstrap {
     public final AsyncServerBootstrap replaceFilter(final String existing, final AsyncFilterHandler filterHandler) {
         Args.notBlank(existing, "Existing");
         Args.notNull(filterHandler, "Filter handler");
-        filters.add(new FilterEntry<>(FilterEntry.Postion.REPLACE, existing, filterHandler, existing));
+        filterList.add(new FilterEntry<>(FilterEntry.Postion.REPLACE, existing, filterHandler, existing));
         return this;
     }
 
@@ -307,7 +307,7 @@ public class AsyncServerBootstrap {
     public final AsyncServerBootstrap addFilterFirst(final String name, final AsyncFilterHandler filterHandler) {
         Args.notNull(name, "Name");
         Args.notNull(filterHandler, "Filter handler");
-        filters.add(new FilterEntry<>(FilterEntry.Postion.FIRST, name, filterHandler, null));
+        filterList.add(new FilterEntry<>(FilterEntry.Postion.FIRST, name, filterHandler, null));
         return this;
     }
 
@@ -317,20 +317,50 @@ public class AsyncServerBootstrap {
     public final AsyncServerBootstrap addFilterLast(final String name, final AsyncFilterHandler filterHandler) {
         Args.notNull(name, "Name");
         Args.notNull(filterHandler, "Filter handler");
-        filters.add(new FilterEntry<>(FilterEntry.Postion.LAST, name, filterHandler, null));
+        filterList.add(new FilterEntry<>(FilterEntry.Postion.LAST, name, filterHandler, null));
         return this;
     }
 
     public HttpAsyncServer create() {
-        final RequestHandlerRegistry<Supplier<AsyncServerExchangeHandler>> registry = new RequestHandlerRegistry<>(
-                canonicalHostName != null ? canonicalHostName : InetAddressUtils.getCanonicalLocalHostName(),
-                uriPatternType);
-        for (final HandlerEntry<Supplier<AsyncServerExchangeHandler>> entry: handlerList) {
-            registry.register(entry.hostname, entry.uriPattern, entry.handler);
-        }
+        final RequestHandlerRegistry<Supplier<AsyncServerExchangeHandler>> registry = createRequestHandlerRegistry();
+        registerHandlers(registry);
+        final HandlerFactory<AsyncServerExchangeHandler> handlerFactory = createHandlerFactory(registry);
+        final ServerHttp1StreamDuplexerFactory streamHandlerFactory = createServerHttp1StreamDuplexerFactory(handlerFactory);
+        final IOEventHandlerFactory ioEventHandlerFactory = createIOEventHandlerFactory(streamHandlerFactory);
+        return createHttpAsyncServer(ioEventHandlerFactory);
+    }
+
+    protected HttpAsyncServer createHttpAsyncServer(final IOEventHandlerFactory ioEventHandlerFactory) {
+        return new HttpAsyncServer(ioEventHandlerFactory, ioReactorConfig, ioSessionDecorator, ioSessionListener);
+    }
+
+    protected ServerHttp1IOEventHandlerFactory createIOEventHandlerFactory(
+            final ServerHttp1StreamDuplexerFactory streamHandlerFactory) {
+        return new ServerHttp1IOEventHandlerFactory(
+                streamHandlerFactory,
+                tlsStrategy,
+                handshakeTimeout);
+    }
 
+    protected ServerHttp1StreamDuplexerFactory createServerHttp1StreamDuplexerFactory(
+            final HandlerFactory<AsyncServerExchangeHandler> handlerFactory) {
+        return new ServerHttp1StreamDuplexerFactory(
+                httpProcessor != null ? httpProcessor : HttpProcessors.server(),
+                handlerFactory,
+                h1Config != null ? h1Config : H1Config.DEFAULT,
+                charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT,
+                connectionReuseStrategy != null ? connectionReuseStrategy : DefaultConnectionReuseStrategy.INSTANCE,
+                DefaultHttpRequestParserFactory.INSTANCE,
+                DefaultHttpResponseWriterFactory.INSTANCE,
+                DefaultContentLengthStrategy.INSTANCE,
+                DefaultContentLengthStrategy.INSTANCE,
+                streamListener);
+    }
+
+    protected HandlerFactory<AsyncServerExchangeHandler> createHandlerFactory(
+            final RequestHandlerRegistry<Supplier<AsyncServerExchangeHandler>> registry) {
         final HandlerFactory<AsyncServerExchangeHandler> handlerFactory;
-        if (!filters.isEmpty()) {
+        if (!filterList.isEmpty()) {
             final NamedElementChain<AsyncFilterHandler> filterChainDefinition = new NamedElementChain<>();
             filterChainDefinition.addLast(
                     new TerminalAsyncServerFilter(new DefaultAsyncResponseExchangeHandlerFactory(registry)),
@@ -339,7 +369,7 @@ public class AsyncServerBootstrap {
                     new AsyncServerExpectationFilter(),
                     StandardFilters.EXPECT_CONTINUE.name());
 
-            for (final FilterEntry<AsyncFilterHandler> entry: filters) {
+            for (final FilterEntry<AsyncFilterHandler> entry: filterList) {
                 switch (entry.postion) {
                     case AFTER:
                         filterChainDefinition.addAfter(entry.existing, entry.filterHandler, entry.name);
@@ -377,23 +407,92 @@ public class AsyncServerBootstrap {
 
             });
         }
+        return handlerFactory;
+    }
 
-        final ServerHttp1StreamDuplexerFactory streamHandlerFactory = new ServerHttp1StreamDuplexerFactory(
-                httpProcessor != null ? httpProcessor : HttpProcessors.server(),
-                handlerFactory,
-                h1Config != null ? h1Config : H1Config.DEFAULT,
-                charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT,
-                connStrategy != null ? connStrategy : DefaultConnectionReuseStrategy.INSTANCE,
-                DefaultHttpRequestParserFactory.INSTANCE,
-                DefaultHttpResponseWriterFactory.INSTANCE,
-                DefaultContentLengthStrategy.INSTANCE,
-                DefaultContentLengthStrategy.INSTANCE,
-                streamListener);
-        final IOEventHandlerFactory ioEventHandlerFactory = new ServerHttp1IOEventHandlerFactory(
-                streamHandlerFactory,
-                tlsStrategy,
-                handshakeTimeout);
-        return new HttpAsyncServer(ioEventHandlerFactory, ioReactorConfig, ioSessionDecorator, sessionListener);
+    protected void registerHandlers(final RequestHandlerRegistry<Supplier<AsyncServerExchangeHandler>> registry) {
+        for (final HandlerEntry<Supplier<AsyncServerExchangeHandler>> entry: handlerList) {
+            registry.register(entry.hostname, entry.uriPattern, entry.handler);
+        }
+    }
+
+    protected RequestHandlerRegistry<Supplier<AsyncServerExchangeHandler>> createRequestHandlerRegistry() {
+        final RequestHandlerRegistry<Supplier<AsyncServerExchangeHandler>> registry = new RequestHandlerRegistry<>(
+                canonicalHostName != null ? canonicalHostName : InetAddressUtils.getCanonicalLocalHostName(),
+                uriPatternType);
+        return registry;
+    }
+
+    protected IOReactorConfig getIoReactorConfig() {
+        return ioReactorConfig;
+    }
+
+    protected void setIoReactorConfig(final IOReactorConfig ioReactorConfig) {
+        this.ioReactorConfig = ioReactorConfig;
+    }
+
+    protected Timeout getHandshakeTimeout() {
+        return handshakeTimeout;
+    }
+
+    protected void setHandshakeTimeout(final Timeout handshakeTimeout) {
+        this.handshakeTimeout = handshakeTimeout;
+    }
+
+    protected Decorator<IOSession> getIoSessionDecorator() {
+        return ioSessionDecorator;
+    }
+
+    protected void setIoSessionDecorator(final Decorator<IOSession> ioSessionDecorator) {
+        this.ioSessionDecorator = ioSessionDecorator;
+    }
+
+    protected IOSessionListener getIoSessionListener() {
+        return ioSessionListener;
+    }
+
+    protected void setIoSessionListener(final IOSessionListener ioSessionListener) {
+        this.ioSessionListener = ioSessionListener;
+    }
+
+    protected List<HandlerEntry<Supplier<AsyncServerExchangeHandler>>> getHandlerList() {
+        return handlerList;
+    }
+
+    protected List<FilterEntry<AsyncFilterHandler>> getFilterList() {
+        return filterList;
+    }
+
+    protected String getCanonicalHostName() {
+        return canonicalHostName;
+    }
+
+    protected UriPatternType getUriPatternType() {
+        return uriPatternType;
+    }
+
+    protected H1Config getH1Config() {
+        return h1Config;
+    }
+
+    protected CharCodingConfig getCharCodingConfig() {
+        return charCodingConfig;
+    }
+
+    protected HttpProcessor getHttpProcessor() {
+        return httpProcessor;
+    }
+
+    protected ConnectionReuseStrategy getConnectionReuseStrategy() {
+        return connectionReuseStrategy;
+    }
+
+    protected TlsStrategy getTlsStrategy() {
+        return tlsStrategy;
+    }
+
+    protected Http1StreamListener getStreamListener() {
+        return streamListener;
     }
 
 }