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.core5.testing.nio;
29
30 import java.io.IOException;
31 import java.net.InetSocketAddress;
32 import java.util.concurrent.Future;
33
34 import javax.net.ssl.SSLContext;
35
36 import org.apache.hc.core5.function.Decorator;
37 import org.apache.hc.core5.function.Supplier;
38 import org.apache.hc.core5.http.config.CharCodingConfig;
39 import org.apache.hc.core5.http.config.Http1Config;
40 import org.apache.hc.core5.http.impl.HttpProcessors;
41 import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
42 import org.apache.hc.core5.http.nio.AsyncServerRequestHandler;
43 import org.apache.hc.core5.http.nio.support.BasicAsyncServerExpectationDecorator;
44 import org.apache.hc.core5.http.nio.support.BasicServerExchangeHandler;
45 import org.apache.hc.core5.http.nio.support.DefaultAsyncResponseExchangeHandlerFactory;
46 import org.apache.hc.core5.http.protocol.HttpProcessor;
47 import org.apache.hc.core5.http.protocol.RequestHandlerRegistry;
48 import org.apache.hc.core5.http2.HttpVersionPolicy;
49 import org.apache.hc.core5.http2.config.H2Config;
50 import org.apache.hc.core5.http2.impl.H2Processors;
51 import org.apache.hc.core5.reactor.IOEventHandlerFactory;
52 import org.apache.hc.core5.reactor.IOReactorConfig;
53 import org.apache.hc.core5.reactor.ListenerEndpoint;
54 import org.apache.hc.core5.reactor.ssl.SSLSessionInitializer;
55 import org.apache.hc.core5.reactor.ssl.SSLSessionVerifier;
56
57 public class H2TestServer extends AsyncServer {
58
59 private final SSLContext sslContext;
60 private final SSLSessionInitializer sslSessionInitializer;
61 private final SSLSessionVerifier sslSessionVerifier;
62 private final RequestHandlerRegistry<Supplier<AsyncServerExchangeHandler>> registry;
63
64 public H2TestServer(
65 final IOReactorConfig ioReactorConfig,
66 final SSLContext sslContext,
67 final SSLSessionInitializer sslSessionInitializer,
68 final SSLSessionVerifier sslSessionVerifier) throws IOException {
69 super(ioReactorConfig);
70 this.sslContext = sslContext;
71 this.sslSessionInitializer = sslSessionInitializer;
72 this.sslSessionVerifier = sslSessionVerifier;
73 this.registry = new RequestHandlerRegistry<>();
74 }
75
76 public H2TestServer() throws IOException {
77 this(IOReactorConfig.DEFAULT, null, null, null);
78 }
79
80 public void register(final String uriPattern, final Supplier<AsyncServerExchangeHandler> supplier) {
81 registry.register(null, uriPattern, supplier);
82 }
83
84 public <T> void register(
85 final String uriPattern,
86 final AsyncServerRequestHandler<T> requestHandler) {
87 register(uriPattern, () -> new BasicServerExchangeHandler<>(requestHandler));
88 }
89
90 public void start(final IOEventHandlerFactory handlerFactory) throws IOException {
91 execute(handlerFactory);
92 }
93
94 public InetSocketAddress start(
95 final HttpProcessor httpProcessor,
96 final Decorator<AsyncServerExchangeHandler> exchangeHandlerDecorator,
97 final H2Config h2Config) throws Exception {
98 start(new InternalServerProtocolNegotiationStarter(
99 httpProcessor != null ? httpProcessor : H2Processors.server(),
100 new DefaultAsyncResponseExchangeHandlerFactory(
101 registry,
102 exchangeHandlerDecorator != null ? exchangeHandlerDecorator : BasicAsyncServerExpectationDecorator::new),
103 HttpVersionPolicy.FORCE_HTTP_2,
104 h2Config,
105 Http1Config.DEFAULT,
106 CharCodingConfig.DEFAULT,
107 sslContext,
108 sslSessionInitializer,
109 sslSessionVerifier));
110 final Future<ListenerEndpoint> future = listen(new InetSocketAddress(0));
111 final ListenerEndpoint listener = future.get();
112 return (InetSocketAddress) listener.getAddress();
113 }
114
115 public InetSocketAddress start(
116 final HttpProcessor httpProcessor,
117 final Decorator<AsyncServerExchangeHandler> exchangeHandlerDecorator,
118 final Http1Config http1Config) throws Exception {
119 start(new InternalServerProtocolNegotiationStarter(
120 httpProcessor != null ? httpProcessor : HttpProcessors.server(),
121 new DefaultAsyncResponseExchangeHandlerFactory(
122 registry,
123 exchangeHandlerDecorator != null ? exchangeHandlerDecorator : BasicAsyncServerExpectationDecorator::new),
124 HttpVersionPolicy.FORCE_HTTP_1,
125 H2Config.DEFAULT,
126 http1Config,
127 CharCodingConfig.DEFAULT,
128 sslContext,
129 sslSessionInitializer,
130 sslSessionVerifier));
131 final Future<ListenerEndpoint> future = listen(new InetSocketAddress(0));
132 final ListenerEndpoint listener = future.get();
133 return (InetSocketAddress) listener.getAddress();
134 }
135
136 public InetSocketAddress start(final H2Config h2Config) throws Exception {
137 return start(null, null, h2Config);
138 }
139
140 public InetSocketAddress start(final Http1Config http1Config) throws Exception {
141 return start(null, null, http1Config);
142 }
143
144 public InetSocketAddress start() throws Exception {
145 return start(H2Config.DEFAULT);
146 }
147
148 }