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.DefaultConnectionReuseStrategy;
41 import org.apache.hc.core5.http.impl.HttpProcessors;
42 import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
43 import org.apache.hc.core5.http.nio.AsyncServerRequestHandler;
44 import org.apache.hc.core5.http.nio.support.BasicAsyncServerExpectationDecorator;
45 import org.apache.hc.core5.http.nio.support.BasicServerExchangeHandler;
46 import org.apache.hc.core5.http.nio.support.DefaultAsyncResponseExchangeHandlerFactory;
47 import org.apache.hc.core5.http.protocol.HttpProcessor;
48 import org.apache.hc.core5.http.protocol.RequestHandlerRegistry;
49 import org.apache.hc.core5.reactor.IOEventHandlerFactory;
50 import org.apache.hc.core5.reactor.IOReactorConfig;
51 import org.apache.hc.core5.reactor.ListenerEndpoint;
52 import org.apache.hc.core5.reactor.ssl.SSLSessionInitializer;
53 import org.apache.hc.core5.reactor.ssl.SSLSessionVerifier;
54
55 public class Http1TestServer extends AsyncServer {
56
57 private final RequestHandlerRegistry<Supplier<AsyncServerExchangeHandler>> registry;
58 private final SSLContext sslContext;
59 private final SSLSessionInitializer sslSessionInitializer;
60 private final SSLSessionVerifier sslSessionVerifier;
61
62 public Http1TestServer(
63 final IOReactorConfig ioReactorConfig,
64 final SSLContext sslContext,
65 final SSLSessionInitializer sslSessionInitializer,
66 final SSLSessionVerifier sslSessionVerifier) throws IOException {
67 super(ioReactorConfig);
68 this.registry = new RequestHandlerRegistry<>();
69 this.sslContext = sslContext;
70 this.sslSessionInitializer = sslSessionInitializer;
71 this.sslSessionVerifier = sslSessionVerifier;
72 }
73
74 public Http1TestServer() throws IOException {
75 this(IOReactorConfig.DEFAULT, null, null, null);
76 }
77
78 public void register(final String uriPattern, final Supplier<AsyncServerExchangeHandler> supplier) {
79 registry.register(null, uriPattern, supplier);
80 }
81
82 public <T> void register(
83 final String uriPattern,
84 final AsyncServerRequestHandler<T> requestHandler) {
85 register(uriPattern, () -> new BasicServerExchangeHandler<>(requestHandler));
86 }
87
88 public InetSocketAddress start(final IOEventHandlerFactory handlerFactory) throws Exception {
89 execute(handlerFactory);
90 final Future<ListenerEndpoint> future = listen(new InetSocketAddress(0));
91 final ListenerEndpoint listener = future.get();
92 return (InetSocketAddress) listener.getAddress();
93 }
94
95 public InetSocketAddress start(
96 final HttpProcessor httpProcessor,
97 final Decorator<AsyncServerExchangeHandler> exchangeHandlerDecorator,
98 final Http1Config http1Config) throws Exception {
99 return start(new InternalServerHttp1EventHandlerFactory(
100 httpProcessor != null ? httpProcessor : HttpProcessors.server(),
101 new DefaultAsyncResponseExchangeHandlerFactory(
102 registry,
103 exchangeHandlerDecorator != null ? exchangeHandlerDecorator : BasicAsyncServerExpectationDecorator::new),
104 http1Config,
105 CharCodingConfig.DEFAULT,
106 DefaultConnectionReuseStrategy.INSTANCE,
107 sslContext,
108 sslSessionInitializer,
109 sslSessionVerifier));
110 }
111
112 public InetSocketAddress start(final HttpProcessor httpProcessor, final Http1Config http1Config) throws Exception {
113 return start(httpProcessor, null, http1Config);
114 }
115
116 public InetSocketAddress start() throws Exception {
117 return start(null, null, Http1Config.DEFAULT);
118 }
119
120 }