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 package org.apache.http.impl.nio.bootstrap;
28
29 import java.net.InetAddress;
30 import java.util.HashMap;
31 import java.util.LinkedList;
32 import java.util.Map;
33
34 import javax.net.ssl.SSLContext;
35
36 import org.apache.http.ConnectionReuseStrategy;
37 import org.apache.http.ExceptionLogger;
38 import org.apache.http.HttpRequestInterceptor;
39 import org.apache.http.HttpResponseFactory;
40 import org.apache.http.HttpResponseInterceptor;
41 import org.apache.http.config.ConnectionConfig;
42 import org.apache.http.impl.DefaultConnectionReuseStrategy;
43 import org.apache.http.impl.DefaultHttpResponseFactory;
44 import org.apache.http.impl.nio.DefaultNHttpServerConnection;
45 import org.apache.http.impl.nio.DefaultNHttpServerConnectionFactory;
46 import org.apache.http.impl.nio.SSLNHttpServerConnectionFactory;
47 import org.apache.http.impl.nio.reactor.IOReactorConfig;
48 import org.apache.http.nio.NHttpConnectionFactory;
49 import org.apache.http.nio.protocol.HttpAsyncExpectationVerifier;
50 import org.apache.http.nio.protocol.HttpAsyncRequestHandler;
51 import org.apache.http.nio.protocol.HttpAsyncRequestHandlerMapper;
52 import org.apache.http.nio.protocol.HttpAsyncService;
53 import org.apache.http.nio.protocol.UriHttpAsyncRequestHandlerMapper;
54 import org.apache.http.nio.reactor.ssl.SSLSetupHandler;
55 import org.apache.http.protocol.HttpProcessor;
56 import org.apache.http.protocol.HttpProcessorBuilder;
57 import org.apache.http.protocol.ResponseConnControl;
58 import org.apache.http.protocol.ResponseContent;
59 import org.apache.http.protocol.ResponseDate;
60 import org.apache.http.protocol.ResponseServer;
61
62
63
64
65 public class ServerBootstrap {
66
67 private int listenerPort;
68 private InetAddress localAddress;
69 private IOReactorConfig ioReactorConfig;
70 private ConnectionConfig connectionConfig;
71 private LinkedList<HttpRequestInterceptor> requestFirst;
72 private LinkedList<HttpRequestInterceptor> requestLast;
73 private LinkedList<HttpResponseInterceptor> responseFirst;
74 private LinkedList<HttpResponseInterceptor> responseLast;
75 private String serverInfo;
76 private HttpProcessor httpProcessor;
77 private ConnectionReuseStrategy connStrategy;
78 private HttpResponseFactory responseFactory;
79 private HttpAsyncRequestHandlerMapper handlerMapper;
80 private Map<String, HttpAsyncRequestHandler<?>> handlerMap;
81 private HttpAsyncExpectationVerifier expectationVerifier;
82 private SSLContext sslContext;
83 private SSLSetupHandler sslSetupHandler;
84 private NHttpConnectionFactory<? extends DefaultNHttpServerConnection> connectionFactory;
85 private ExceptionLogger exceptionLogger;
86
87 private ServerBootstrap() {
88 }
89
90 public static ServerBootstrap bootstrap() {
91 return new ServerBootstrap();
92 }
93
94
95
96
97 public final ServerBootstrap setListenerPort(final int listenerPort) {
98 this.listenerPort = listenerPort;
99 return this;
100 }
101
102
103
104
105 public final ServerBootstrap setLocalAddress(final InetAddress localAddress) {
106 this.localAddress = localAddress;
107 return this;
108 }
109
110
111
112
113 public final ServerBootstrap setIOReactorConfig(final IOReactorConfig ioReactorConfig) {
114 this.ioReactorConfig = ioReactorConfig;
115 return this;
116 }
117
118
119
120
121
122
123
124 public final ServerBootstrap setConnectionConfig(final ConnectionConfig connectionConfig) {
125 this.connectionConfig = connectionConfig;
126 return this;
127 }
128
129
130
131
132 public final ServerBootstrap setHttpProcessor(final HttpProcessor httpProcessor) {
133 this.httpProcessor = httpProcessor;
134 return this;
135 }
136
137
138
139
140
141
142
143 public final ServerBootstrap addInterceptorFirst(final HttpResponseInterceptor itcp) {
144 if (itcp == null) {
145 return this;
146 }
147 if (responseFirst == null) {
148 responseFirst = new LinkedList<HttpResponseInterceptor>();
149 }
150 responseFirst.addFirst(itcp);
151 return this;
152 }
153
154
155
156
157
158
159
160 public final ServerBootstrap addInterceptorLast(final HttpResponseInterceptor itcp) {
161 if (itcp == null) {
162 return this;
163 }
164 if (responseLast == null) {
165 responseLast = new LinkedList<HttpResponseInterceptor>();
166 }
167 responseLast.addLast(itcp);
168 return this;
169 }
170
171
172
173
174
175
176
177 public final ServerBootstrap addInterceptorFirst(final HttpRequestInterceptor itcp) {
178 if (itcp == null) {
179 return this;
180 }
181 if (requestFirst == null) {
182 requestFirst = new LinkedList<HttpRequestInterceptor>();
183 }
184 requestFirst.addFirst(itcp);
185 return this;
186 }
187
188
189
190
191
192
193
194 public final ServerBootstrap addInterceptorLast(final HttpRequestInterceptor itcp) {
195 if (itcp == null) {
196 return this;
197 }
198 if (requestLast == null) {
199 requestLast = new LinkedList<HttpRequestInterceptor>();
200 }
201 requestLast.addLast(itcp);
202 return this;
203 }
204
205
206
207
208
209
210
211 public final ServerBootstrap setServerInfo(final String serverInfo) {
212 this.serverInfo = serverInfo;
213 return this;
214 }
215
216
217
218
219 public final ServerBootstrap setConnectionReuseStrategy(final ConnectionReuseStrategy connStrategy) {
220 this.connStrategy = connStrategy;
221 return this;
222 }
223
224
225
226
227 public final ServerBootstrap setResponseFactory(final HttpResponseFactory responseFactory) {
228 this.responseFactory = responseFactory;
229 return this;
230 }
231
232
233
234
235 public final ServerBootstrap setHandlerMapper(final HttpAsyncRequestHandlerMapper handlerMapper) {
236 this.handlerMapper = handlerMapper;
237 return this;
238 }
239
240
241
242
243
244
245
246
247
248
249
250 public final ServerBootstrap registerHandler(final String pattern, final HttpAsyncRequestHandler<?> handler) {
251 if (pattern == null || handler == null) {
252 return this;
253 }
254 if (handlerMap == null) {
255 handlerMap = new HashMap<String, HttpAsyncRequestHandler<?>>();
256 }
257 handlerMap.put(pattern, handler);
258 return this;
259 }
260
261
262
263
264 public final ServerBootstrap setExpectationVerifier(final HttpAsyncExpectationVerifier expectationVerifier) {
265 this.expectationVerifier = expectationVerifier;
266 return this;
267 }
268
269
270
271
272 public final ServerBootstrap setConnectionFactory(
273 final NHttpConnectionFactory<? extends DefaultNHttpServerConnection> connectionFactory) {
274 this.connectionFactory = connectionFactory;
275 return this;
276 }
277
278
279
280
281
282
283
284 public final ServerBootstrap setSslContext(final SSLContext sslContext) {
285 this.sslContext = sslContext;
286 return this;
287 }
288
289
290
291
292
293
294
295 public ServerBootstrap setSslSetupHandler(final SSLSetupHandler sslSetupHandler) {
296 this.sslSetupHandler = sslSetupHandler;
297 return this;
298 }
299
300
301
302
303 public final ServerBootstrap setExceptionLogger(final ExceptionLogger exceptionLogger) {
304 this.exceptionLogger = exceptionLogger;
305 return this;
306 }
307
308 public HttpServer create() {
309
310 HttpProcessor httpProcessorCopy = this.httpProcessor;
311 if (httpProcessorCopy == null) {
312
313 final HttpProcessorBuilder b = HttpProcessorBuilder.create();
314 if (requestFirst != null) {
315 for (final HttpRequestInterceptor i: requestFirst) {
316 b.addFirst(i);
317 }
318 }
319 if (responseFirst != null) {
320 for (final HttpResponseInterceptor i: responseFirst) {
321 b.addFirst(i);
322 }
323 }
324
325 String serverInfoCopy = this.serverInfo;
326 if (serverInfoCopy == null) {
327 serverInfoCopy = "Apache-HttpCore-NIO/1.1";
328 }
329
330 b.addAll(
331 new ResponseDate(),
332 new ResponseServer(serverInfoCopy),
333 new ResponseContent(),
334 new ResponseConnControl());
335 if (requestLast != null) {
336 for (final HttpRequestInterceptor i: requestLast) {
337 b.addLast(i);
338 }
339 }
340 if (responseLast != null) {
341 for (final HttpResponseInterceptor i: responseLast) {
342 b.addLast(i);
343 }
344 }
345 httpProcessorCopy = b.build();
346 }
347
348 HttpAsyncRequestHandlerMapper handlerMapperCopy = this.handlerMapper;
349 if (handlerMapperCopy == null) {
350 final UriHttpAsyncRequestHandlerMapperndlerMapper.html#UriHttpAsyncRequestHandlerMapper">UriHttpAsyncRequestHandlerMapper reqistry = new UriHttpAsyncRequestHandlerMapper();
351 if (handlerMap != null) {
352 for (final Map.Entry<String, HttpAsyncRequestHandler<?>> entry: handlerMap.entrySet()) {
353 reqistry.register(entry.getKey(), entry.getValue());
354 }
355 }
356 handlerMapperCopy = reqistry;
357 }
358
359 ConnectionReuseStrategy connStrategyCopy = this.connStrategy;
360 if (connStrategyCopy == null) {
361 connStrategyCopy = DefaultConnectionReuseStrategy.INSTANCE;
362 }
363
364 HttpResponseFactory responseFactoryCopy = this.responseFactory;
365 if (responseFactoryCopy == null) {
366 responseFactoryCopy = DefaultHttpResponseFactory.INSTANCE;
367 }
368
369 NHttpConnectionFactory<? extends DefaultNHttpServerConnection> connectionFactoryCopy = this.connectionFactory;
370 if (connectionFactoryCopy == null) {
371 if (this.sslContext != null) {
372 connectionFactoryCopy = new SSLNHttpServerConnectionFactory(
373 this.sslContext, this.sslSetupHandler, this.connectionConfig);
374 } else {
375 connectionFactoryCopy = new DefaultNHttpServerConnectionFactory(this.connectionConfig);
376 }
377 }
378
379 ExceptionLogger exceptionLoggerCopy = this.exceptionLogger;
380 if (exceptionLoggerCopy == null) {
381 exceptionLoggerCopy = ExceptionLogger.NO_OP;
382 }
383
384 final HttpAsyncServicecService.html#HttpAsyncService">HttpAsyncService httpService = new HttpAsyncService(
385 httpProcessorCopy, connStrategyCopy, responseFactoryCopy, handlerMapperCopy,
386 this.expectationVerifier, exceptionLoggerCopy);
387
388 return new HttpServer(this.listenerPort, this.localAddress, this.ioReactorConfig,
389 httpService, connectionFactoryCopy, exceptionLoggerCopy);
390
391 }
392
393 }