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.concurrent.atomic.AtomicReference;
33
34 import javax.net.ServerSocketFactory;
35 import javax.net.ssl.SSLContext;
36
37 import org.apache.hc.core5.function.Decorator;
38 import org.apache.hc.core5.http.URIScheme;
39 import org.apache.hc.core5.http.config.CharCodingConfig;
40 import org.apache.hc.core5.http.config.Http1Config;
41 import org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy;
42 import org.apache.hc.core5.http.impl.HttpProcessors;
43 import org.apache.hc.core5.http.impl.bootstrap.HttpServer;
44 import org.apache.hc.core5.http.impl.io.HttpService;
45 import org.apache.hc.core5.http.io.HttpRequestHandler;
46 import org.apache.hc.core5.http.io.HttpServerRequestHandler;
47 import org.apache.hc.core5.http.io.SocketConfig;
48 import org.apache.hc.core5.http.io.support.BasicHttpServerExpectationDecorator;
49 import org.apache.hc.core5.http.io.support.BasicHttpServerRequestHandler;
50 import org.apache.hc.core5.http.protocol.HttpProcessor;
51 import org.apache.hc.core5.http.protocol.RequestHandlerRegistry;
52 import org.apache.hc.core5.io.CloseMode;
53
54 public class ClassicTestServer {
55
56 private final SSLContext sslContext;
57 private final SocketConfig socketConfig;
58 private final RequestHandlerRegistry<HttpRequestHandler> registry;
59
60 private final AtomicReference<HttpServer> serverRef;
61
62 public ClassicTestServer(final SSLContext sslContext, final SocketConfig socketConfig) {
63 super();
64 this.sslContext = sslContext;
65 this.socketConfig = socketConfig != null ? socketConfig : SocketConfig.DEFAULT;
66 this.registry = new RequestHandlerRegistry<>();
67 this.serverRef = new AtomicReference<>();
68 }
69
70 public ClassicTestServer(final SocketConfig socketConfig) {
71 this(null, socketConfig);
72 }
73
74 public ClassicTestServer() {
75 this(null, null);
76 }
77
78 public void registerHandler(final String pattern, final HttpRequestHandler handler) {
79 this.registry.register(null, pattern, handler);
80 }
81
82 public void registerHandlerVirtual(final String hostname, final String pattern, final HttpRequestHandler handler) {
83 this.registry.register(hostname, pattern, handler);
84 }
85
86 public int getPort() {
87 final HttpServer server = this.serverRef.get();
88 if (server != null) {
89 return server.getLocalPort();
90 }
91 throw new IllegalStateException("Server not running");
92 }
93
94 public InetAddress getInetAddress() {
95 final HttpServer server = this.serverRef.get();
96 if (server != null) {
97 return server.getInetAddress();
98 }
99 throw new IllegalStateException("Server not running");
100 }
101
102 public void start(
103 final Http1Config http1Config,
104 final HttpProcessor httpProcessor,
105 final Decorator<HttpServerRequestHandler> handlerDecorator) throws IOException {
106 if (serverRef.get() == null) {
107 final HttpServerRequestHandler handler = new BasicHttpServerRequestHandler(registry);
108 final HttpService httpService = new HttpService(
109 httpProcessor != null ? httpProcessor : HttpProcessors.server(),
110 handlerDecorator != null ? handlerDecorator.decorate(handler) : new BasicHttpServerExpectationDecorator(handler),
111 DefaultConnectionReuseStrategy.INSTANCE,
112 LoggingHttp1StreamListener.INSTANCE);
113 final HttpServer server = new HttpServer(
114 0,
115 httpService,
116 null,
117 socketConfig,
118 sslContext != null ? sslContext.getServerSocketFactory() : ServerSocketFactory.getDefault(),
119 new LoggingBHttpServerConnectionFactory(
120 sslContext != null ? URIScheme.HTTPS.id : URIScheme.HTTP.id,
121 http1Config != null ? http1Config : Http1Config.DEFAULT,
122 CharCodingConfig.DEFAULT),
123 null,
124 LoggingExceptionListener.INSTANCE);
125 if (serverRef.compareAndSet(null, server)) {
126 server.start();
127 }
128 } else {
129 throw new IllegalStateException("Server already running");
130 }
131 }
132
133 public void start() throws IOException {
134 start(null, null, null);
135 }
136
137 public void shutdown(final CloseMode closeMode) {
138 final HttpServer server = serverRef.getAndSet(null);
139 if (server != null) {
140 server.close(closeMode);
141 }
142 }
143
144 }