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.classic;
29
30 import java.io.IOException;
31 import java.net.InetAddress;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.concurrent.atomic.AtomicReference;
35
36 import javax.net.ssl.SSLContext;
37
38 import org.apache.hc.core5.function.Decorator;
39 import org.apache.hc.core5.http.URIScheme;
40 import org.apache.hc.core5.http.config.CharCodingConfig;
41 import org.apache.hc.core5.http.config.Http1Config;
42 import org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy;
43 import org.apache.hc.core5.http.impl.HttpProcessors;
44 import org.apache.hc.core5.http.impl.bootstrap.HttpServer;
45 import org.apache.hc.core5.http.impl.io.HttpService;
46 import org.apache.hc.core5.http.impl.routing.RequestRouter;
47 import org.apache.hc.core5.http.io.HttpRequestHandler;
48 import org.apache.hc.core5.http.io.HttpServerRequestHandler;
49 import org.apache.hc.core5.http.io.SocketConfig;
50 import org.apache.hc.core5.http.io.support.BasicHttpServerExpectationDecorator;
51 import org.apache.hc.core5.http.io.support.BasicHttpServerRequestHandler;
52 import org.apache.hc.core5.http.protocol.HttpProcessor;
53 import org.apache.hc.core5.http.protocol.UriPatternType;
54 import org.apache.hc.core5.io.CloseMode;
55 import org.apache.hc.core5.util.Args;
56
57 public class ClassicTestServer {
58
59 private final SSLContext sslContext;
60 private final SocketConfig socketConfig;
61 private final List<RequestRouter.Entry<HttpRequestHandler>> routeEntries;
62
63 private final AtomicReference<HttpServer> serverRef;
64
65 public ClassicTestServer(final SSLContext sslContext, final SocketConfig socketConfig) {
66 super();
67 this.sslContext = sslContext;
68 this.socketConfig = socketConfig != null ? socketConfig : SocketConfig.DEFAULT;
69 this.routeEntries = new ArrayList<>();
70 this.serverRef = new AtomicReference<>();
71 }
72
73 public ClassicTestServer(final SocketConfig socketConfig) {
74 this(null, socketConfig);
75 }
76
77 public ClassicTestServer() {
78 this(null, null);
79 }
80
81
82
83
84 @Deprecated
85 public void registerHandler(final String pattern, final HttpRequestHandler handler) {
86 register(pattern, handler);
87 }
88
89
90
91
92 @Deprecated
93 public void registerHandlerVirtual(final String hostname, final String pattern, final HttpRequestHandler handler) {
94 register(hostname, pattern, handler);
95 }
96
97 public void register(final String uriPattern, final HttpRequestHandler handler) {
98 Args.notNull(uriPattern, "URI pattern");
99 Args.notNull(handler, "Request handler");
100 final HttpServer server = this.serverRef.get();
101 if (server != null) {
102 throw new IllegalStateException("Server is already running");
103 }
104 routeEntries.add(new RequestRouter.Entry<>(uriPattern, handler));
105 }
106
107 public void register(final String hostname, final String uriPattern, final HttpRequestHandler handler) {
108 Args.notNull(hostname, "Hostname");
109 Args.notNull(uriPattern, "URI pattern");
110 Args.notNull(handler, "Request handler");
111 final HttpServer server = this.serverRef.get();
112 if (server != null) {
113 throw new IllegalStateException("Server is already running");
114 }
115 routeEntries.add(new RequestRouter.Entry<>(hostname, uriPattern, handler));
116 }
117
118 public int getPort() {
119 final HttpServer server = this.serverRef.get();
120 if (server != null) {
121 return server.getLocalPort();
122 }
123 throw new IllegalStateException("Server not running");
124 }
125
126 public InetAddress getInetAddress() {
127 final HttpServer server = this.serverRef.get();
128 if (server != null) {
129 return server.getInetAddress();
130 }
131 throw new IllegalStateException("Server not running");
132 }
133
134 public void start(
135 final Http1Config http1Config,
136 final HttpProcessor httpProcessor,
137 final Decorator<HttpServerRequestHandler> handlerDecorator) throws IOException {
138 if (serverRef.get() == null) {
139 final HttpServerRequestHandler handler = new BasicHttpServerRequestHandler(
140 RequestRouter.create(RequestRouter.LOCAL_AUTHORITY, UriPatternType.URI_PATTERN, routeEntries, RequestRouter.LOCAL_AUTHORITY_RESOLVER, null));
141 final HttpService httpService = new HttpService(
142 httpProcessor != null ? httpProcessor : HttpProcessors.server(),
143 handlerDecorator != null ? handlerDecorator.decorate(handler) : new BasicHttpServerExpectationDecorator(handler),
144 DefaultConnectionReuseStrategy.INSTANCE,
145 LoggingHttp1StreamListener.INSTANCE);
146 final HttpServer server = new HttpServer(
147 0,
148 httpService,
149 null,
150 socketConfig,
151 null,
152 new LoggingBHttpServerConnectionFactory(
153 sslContext != null ? URIScheme.HTTPS.id : URIScheme.HTTP.id,
154 http1Config != null ? http1Config : Http1Config.DEFAULT,
155 CharCodingConfig.DEFAULT),
156 sslContext,
157 null,
158 LoggingExceptionListener.INSTANCE);
159 if (serverRef.compareAndSet(null, server)) {
160 server.start();
161 }
162 } else {
163 throw new IllegalStateException("Server already running");
164 }
165 }
166
167 public void start() throws IOException {
168 start(null, null, null);
169 }
170
171 public void shutdown(final CloseMode closeMode) {
172 final HttpServer server = serverRef.getAndSet(null);
173 if (server != null) {
174 server.close(closeMode);
175 }
176 }
177
178 }