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.sync;
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.http.URIScheme;
36 import org.apache.hc.core5.http.config.Http1Config;
37 import org.apache.hc.core5.http.io.HttpRequestHandler;
38 import org.apache.hc.core5.http.io.HttpServerRequestHandler;
39 import org.apache.hc.core5.http.io.SocketConfig;
40 import org.apache.hc.core5.http.protocol.HttpProcessor;
41 import org.apache.hc.core5.testing.classic.ClassicTestServer;
42 import org.apache.hc.core5.util.Args;
43 import org.apache.hc.core5.util.Timeout;
44
45 public class TestServerBootstrap {
46
47 static final class HandlerEntry<T> {
48
49 final String hostname;
50 final String uriPattern;
51 final T handler;
52
53 public HandlerEntry(final String hostname, final String uriPattern, final T handler) {
54 this.hostname = hostname;
55 this.uriPattern = uriPattern;
56 this.handler = handler;
57 }
58
59 public HandlerEntry(final String uriPattern, final T handler) {
60 this(null, uriPattern, handler);
61 }
62
63 }
64
65 private final URIScheme scheme;
66
67 private final List<HandlerEntry<HttpRequestHandler>> handlerList;
68 private Timeout timeout;
69 private HttpProcessor httpProcessor;
70 private Decorator<HttpServerRequestHandler> exchangeHandlerDecorator;
71
72 public TestServerBootstrap(final URIScheme scheme) {
73 this.scheme = scheme != null ? scheme : URIScheme.HTTP;
74 this.handlerList = new ArrayList<>();
75 }
76
77 public TestServerBootstrap register(final String uriPattern, final HttpRequestHandler requestHandler) {
78 return register(null, uriPattern, requestHandler);
79 }
80
81 public TestServerBootstrap register(final String hostname, final String uriPattern, final HttpRequestHandler requestHandler) {
82 Args.notNull(uriPattern, "URI pattern");
83 Args.notNull(requestHandler, "Exchange handler");
84 handlerList.add(new HandlerEntry<>(hostname, uriPattern, requestHandler));
85 return this;
86 }
87
88 public TestServerBootstrap setTimeout(final Timeout timeout) {
89 this.timeout = timeout;
90 return this;
91 }
92
93 public TestServerBootstrap setHttpProcessor(final HttpProcessor httpProcessor) {
94 this.httpProcessor = httpProcessor;
95 return this;
96 }
97
98 public TestServerBootstrap setExchangeHandlerDecorator(final Decorator<HttpServerRequestHandler> exchangeHandlerDecorator) {
99 this.exchangeHandlerDecorator = exchangeHandlerDecorator;
100 return this;
101 }
102
103 public TestServer build() throws Exception {
104 final ClassicTestServer server = new ClassicTestServer(
105 scheme == URIScheme.HTTPS ? SSLTestContexts.createServerSSLContext() : null,
106 SocketConfig.custom()
107 .setSoTimeout(timeout)
108 .build());
109 for (final HandlerEntry<HttpRequestHandler> entry: handlerList) {
110 if (entry.hostname != null) {
111 server.register(entry.hostname, entry.uriPattern, entry.handler);
112 } else {
113 server.register(entry.uriPattern, entry.handler);
114 }
115 }
116 return new TestServer(
117 server,
118 Http1Config.DEFAULT,
119 httpProcessor,
120 exchangeHandlerDecorator);
121 }
122
123 }