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.net.InetSocketAddress;
31 import java.util.Set;
32 import java.util.concurrent.Future;
33
34 import org.apache.hc.core5.function.Decorator;
35 import org.apache.hc.core5.http.config.Http1Config;
36 import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
37 import org.apache.hc.core5.http.protocol.HttpProcessor;
38 import org.apache.hc.core5.http2.config.H2Config;
39 import org.apache.hc.core5.reactor.IOReactorStatus;
40 import org.apache.hc.core5.reactor.ListenerEndpoint;
41 import org.apache.hc.core5.testing.nio.H2TestServer;
42 import org.apache.hc.core5.util.TimeValue;
43
44 public class TestAsyncServer {
45
46 private final H2TestServer server;
47 private final H2Config h2Config;
48 private final Http1Config http1Config;
49 private final HttpProcessor httpProcessor;
50 private final Decorator<AsyncServerExchangeHandler> exchangeHandlerDecorator;
51
52 TestAsyncServer(
53 final H2TestServer server,
54 final H2Config h2Config,
55 final Http1Config http1Config,
56 final HttpProcessor httpProcessor,
57 final Decorator<AsyncServerExchangeHandler> exchangeHandlerDecorator) {
58 this.server = server;
59 this.h2Config = h2Config;
60 this.http1Config = http1Config;
61 this.httpProcessor = httpProcessor;
62 this.exchangeHandlerDecorator = exchangeHandlerDecorator;
63 }
64
65 public Future<ListenerEndpoint> listen(final InetSocketAddress address) {
66 return server.listen(address);
67 }
68
69 public Set<ListenerEndpoint> getEndpoints() {
70 return server.getEndpoints();
71 }
72
73 public IOReactorStatus getStatus() {
74 return server.getStatus();
75 }
76
77 public void awaitShutdown(final TimeValue waitTime) throws InterruptedException {
78 server.awaitShutdown(waitTime);
79 }
80
81 public void initiateShutdown() {
82 server.initiateShutdown();
83 }
84
85 public void shutdown(final TimeValue graceTime) {
86 server.shutdown(graceTime);
87 }
88
89 public void close() throws Exception {
90 server.close();
91 }
92
93 public InetSocketAddress start() throws Exception {
94 if (http1Config != null) {
95 server.configure(http1Config);
96 } else {
97 server.configure(h2Config);
98 }
99 server.configure(exchangeHandlerDecorator);
100 server.configure(httpProcessor);
101 return server.start();
102 }
103
104 }