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.http.localserver;
29
30 import java.net.InetSocketAddress;
31 import java.net.URL;
32 import java.util.concurrent.TimeUnit;
33
34 import javax.net.ssl.SSLContext;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38 import org.apache.http.ExceptionLogger;
39 import org.apache.http.HttpHost;
40 import org.apache.http.config.Registry;
41 import org.apache.http.config.RegistryBuilder;
42 import org.apache.http.conn.ssl.DefaultHostnameVerifier;
43 import org.apache.http.impl.nio.bootstrap.HttpServer;
44 import org.apache.http.impl.nio.bootstrap.ServerBootstrap;
45 import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
46 import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
47 import org.apache.http.impl.nio.reactor.IOReactorConfig;
48 import org.apache.http.nio.conn.NoopIOSessionStrategy;
49 import org.apache.http.nio.conn.SchemeIOSessionStrategy;
50 import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy;
51 import org.apache.http.nio.reactor.ListenerEndpoint;
52 import org.apache.http.ssl.SSLContextBuilder;
53 import org.junit.After;
54 import org.junit.Before;
55
56 public abstract class AbstractAsyncTest {
57
58 public enum ProtocolScheme { http, https };
59
60 protected final ProtocolScheme scheme;
61
62 protected ServerBootstrap serverBootstrap;
63 protected HttpServer server;
64 protected PoolingNHttpClientConnectionManager connMgr;
65
66 public AbstractAsyncTest(final ProtocolScheme scheme) {
67 this.scheme = scheme;
68 }
69
70 public AbstractAsyncTest() {
71 this(ProtocolScheme.http);
72 }
73
74 public String getSchemeName() {
75 return this.scheme.name();
76 }
77
78 protected SSLContext createServerSSLContext() throws Exception {
79 final URL keyStoreURL = getClass().getResource("/test.keystore");
80 final String storePassword = "nopassword";
81 return SSLContextBuilder.create()
82 .loadTrustMaterial(keyStoreURL, storePassword.toCharArray())
83 .loadKeyMaterial(keyStoreURL, storePassword.toCharArray(), storePassword.toCharArray())
84 .build();
85 }
86
87 protected SSLContext createClientSSLContext() throws Exception {
88 final URL keyStoreURL = getClass().getResource("/test.keystore");
89 final String storePassword = "nopassword";
90 return SSLContextBuilder.create()
91 .loadTrustMaterial(keyStoreURL, storePassword.toCharArray())
92 .build();
93 }
94
95 public HttpHost startServer() throws Exception {
96 this.server = this.serverBootstrap.create();
97 this.server.start();
98
99 final ListenerEndpoint endpoint = this.server.getEndpoint();
100 endpoint.waitFor();
101
102 final InetSocketAddress address = (InetSocketAddress) endpoint.getAddress();
103 return new HttpHost("localhost", address.getPort(), this.scheme.name());
104 }
105
106 @Before
107 public void setUp() throws Exception {
108 this.serverBootstrap = ServerBootstrap.bootstrap();
109 final IOReactorConfig ioReactorConfig = IOReactorConfig.custom()
110 .setSoTimeout(15000)
111 .build();
112 this.serverBootstrap.setServerInfo("TEST/1.1");
113 this.serverBootstrap.setIOReactorConfig(ioReactorConfig);
114 this.serverBootstrap.setExceptionLogger(new ExceptionLogger() {
115
116 private final Log log = LogFactory.getLog(AbstractAsyncTest.class);
117
118 @Override
119 public void log(final Exception ex) {
120 log.error(ex.getMessage(), ex);
121 }
122 });
123 if (this.scheme.equals(ProtocolScheme.https)) {
124 this.serverBootstrap.setSslContext(createServerSSLContext());
125 }
126
127 final RegistryBuilder<SchemeIOSessionStrategy> builder = RegistryBuilder.create();
128 builder.register("http", NoopIOSessionStrategy.INSTANCE);
129 if (this.scheme.equals(ProtocolScheme.https)) {
130 builder.register("https", new SSLIOSessionStrategy(
131 createClientSSLContext(),
132 new DefaultHostnameVerifier()));
133 }
134 final Registry<SchemeIOSessionStrategy> registry = builder.build();
135 final DefaultConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(ioReactorConfig);
136 this.connMgr = new PoolingNHttpClientConnectionManager(ioReactor, registry);
137 }
138
139 @After
140 public void shutDown() throws Exception {
141 if (this.server != null) {
142 this.server.shutdown(10, TimeUnit.SECONDS);
143 }
144 }
145
146 }