1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.client5.testing.extension.async;
29
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import org.apache.hc.client5.testing.SSLTestContexts;
34 import org.apache.hc.core5.function.Decorator;
35 import org.apache.hc.core5.function.Supplier;
36 import org.apache.hc.core5.http.URIScheme;
37 import org.apache.hc.core5.http.config.Http1Config;
38 import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
39 import org.apache.hc.core5.http.nio.AsyncServerRequestHandler;
40 import org.apache.hc.core5.http.nio.support.BasicServerExchangeHandler;
41 import org.apache.hc.core5.http.protocol.HttpProcessor;
42 import org.apache.hc.core5.http2.config.H2Config;
43 import org.apache.hc.core5.reactor.IOReactorConfig;
44 import org.apache.hc.core5.testing.nio.H2TestServer;
45 import org.apache.hc.core5.util.Args;
46 import org.apache.hc.core5.util.Timeout;
47
48 public class TestAsyncServerBootstrap {
49
50 static final class HandlerEntry<T> {
51
52 final String hostname;
53 final String uriPattern;
54 final T handler;
55
56 public HandlerEntry(final String hostname, final String uriPattern, final T handler) {
57 this.hostname = hostname;
58 this.uriPattern = uriPattern;
59 this.handler = handler;
60 }
61
62 public HandlerEntry(final String uriPattern, final T handler) {
63 this(null, uriPattern, handler);
64 }
65
66 }
67
68 private final URIScheme scheme;
69 private final ServerProtocolLevel serverProtocolLevel;
70
71 private final List<HandlerEntry<Supplier<AsyncServerExchangeHandler>>> handlerList;
72 private Timeout timeout;
73 private HttpProcessor httpProcessor;
74 private Decorator<AsyncServerExchangeHandler> exchangeHandlerDecorator;
75
76 public TestAsyncServerBootstrap(final URIScheme scheme, final ServerProtocolLevel serverProtocolLevel) {
77 this.scheme = scheme != null ? scheme : URIScheme.HTTP;
78 this.serverProtocolLevel = serverProtocolLevel != null ? serverProtocolLevel : ServerProtocolLevel.STANDARD;
79 this.handlerList = new ArrayList<>();
80 }
81
82 public ServerProtocolLevel getProtocolLevel() {
83 return serverProtocolLevel;
84 }
85
86 public TestAsyncServerBootstrap register(final String uriPattern, final Supplier<AsyncServerExchangeHandler> supplier) {
87 Args.notNull(uriPattern, "URI pattern");
88 Args.notNull(supplier, "Exchange handler supplier");
89 handlerList.add(new HandlerEntry<>(uriPattern, supplier));
90 return this;
91 }
92
93 public TestAsyncServerBootstrap register(
94 final String uriPattern,
95 final AsyncServerRequestHandler<AsyncServerExchangeHandler> requestHandler) {
96 return register(uriPattern, () -> new BasicServerExchangeHandler<>(requestHandler));
97 }
98
99 public TestAsyncServerBootstrap setTimeout(final Timeout timeout) {
100 this.timeout = timeout;
101 return this;
102 }
103
104 public TestAsyncServerBootstrap setHttpProcessor(final HttpProcessor httpProcessor) {
105 this.httpProcessor = httpProcessor;
106 return this;
107 }
108
109 public TestAsyncServerBootstrap setExchangeHandlerDecorator(final Decorator<AsyncServerExchangeHandler> exchangeHandlerDecorator) {
110 this.exchangeHandlerDecorator = exchangeHandlerDecorator;
111 return this;
112 }
113
114 public TestAsyncServer build() throws Exception {
115 final H2TestServer server = new H2TestServer(
116 IOReactorConfig.custom()
117 .setSoTimeout(timeout)
118 .build(),
119 scheme == URIScheme.HTTPS ? SSLTestContexts.createServerSSLContext() : null,
120 null,
121 null);
122 for (final HandlerEntry<Supplier<AsyncServerExchangeHandler>> entry: handlerList) {
123 server.register(entry.uriPattern, entry.handler);
124 }
125 return new TestAsyncServer(
126 server,
127 serverProtocolLevel == ServerProtocolLevel.H2_ONLY ? H2Config.DEFAULT : null,
128 serverProtocolLevel == ServerProtocolLevel.STANDARD ? Http1Config.DEFAULT : null,
129 httpProcessor,
130 exchangeHandlerDecorator);
131 }
132
133 }