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 Supplier<AsyncServerExchangeHandler>() {
86
87 @Override
88 public AsyncServerExchangeHandler get() {
89 return new BasicServerExchangeHandler<>(requestHandler);
90 }
91
92 });
93 }
94
95 public InetSocketAddress start(final IOEventHandlerFactory handlerFactory) throws Exception {
96 execute(handlerFactory);
97 final Future<ListenerEndpoint> future = listen(new InetSocketAddress(0));
98 final ListenerEndpoint listener = future.get();
99 return (InetSocketAddress) listener.getAddress();
100 }
101
102 public InetSocketAddress start(
103 final HttpProcessor httpProcessor,
104 final Decorator<AsyncServerExchangeHandler> exchangeHandlerDecorator,
105 final Http1Config http1Config) throws Exception {
106 return start(new InternalServerHttp1EventHandlerFactory(
107 httpProcessor != null ? httpProcessor : HttpProcessors.server(),
108 new DefaultAsyncResponseExchangeHandlerFactory(
109 registry,
110 exchangeHandlerDecorator != null ? exchangeHandlerDecorator : new Decorator<AsyncServerExchangeHandler>() {
111
112 @Override
113 public AsyncServerExchangeHandler decorate(final AsyncServerExchangeHandler handler) {
114 return new BasicAsyncServerExpectationDecorator(handler);
115 }
116
117 }),
118 http1Config,
119 CharCodingConfig.DEFAULT,
120 DefaultConnectionReuseStrategy.INSTANCE,
121 sslContext,
122 sslSessionInitializer,
123 sslSessionVerifier));
124 }
125
126 public InetSocketAddress start(final HttpProcessor httpProcessor, final Http1Config http1Config) throws Exception {
127 return start(httpProcessor, null, http1Config);
128 }
129
130 public InetSocketAddress start() throws Exception {
131 return start(null, null, Http1Config.DEFAULT);
132 }
133
134 }