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.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.ServerSocketFactory;
35 import javax.net.ssl.SSLContext;
36
37 import org.apache.http.ConnectionReuseStrategy;
38 import org.apache.http.ExceptionLogger;
39 import org.apache.http.HttpConnectionFactory;
40 import org.apache.http.HttpRequestInterceptor;
41 import org.apache.http.HttpResponseFactory;
42 import org.apache.http.HttpResponseInterceptor;
43 import org.apache.http.config.ConnectionConfig;
44 import org.apache.http.config.SocketConfig;
45 import org.apache.http.impl.DefaultBHttpServerConnection;
46 import org.apache.http.impl.DefaultBHttpServerConnectionFactory;
47 import org.apache.http.impl.DefaultConnectionReuseStrategy;
48 import org.apache.http.impl.DefaultHttpResponseFactory;
49 import org.apache.http.protocol.HttpExpectationVerifier;
50 import org.apache.http.protocol.HttpProcessor;
51 import org.apache.http.protocol.HttpProcessorBuilder;
52 import org.apache.http.protocol.HttpRequestHandler;
53 import org.apache.http.protocol.HttpRequestHandlerMapper;
54 import org.apache.http.protocol.HttpService;
55 import org.apache.http.protocol.ResponseConnControl;
56 import org.apache.http.protocol.ResponseContent;
57 import org.apache.http.protocol.ResponseDate;
58 import org.apache.http.protocol.ResponseServer;
59 import org.apache.http.protocol.UriHttpRequestHandlerMapper;
60
61
62
63
64 public class ServerBootstrap {
65
66 private int listenerPort;
67 private InetAddress localAddress;
68 private SocketConfig socketConfig;
69 private ConnectionConfig connectionConfig;
70 private LinkedList<HttpRequestInterceptor> requestFirst;
71 private LinkedList<HttpRequestInterceptor> requestLast;
72 private LinkedList<HttpResponseInterceptor> responseFirst;
73 private LinkedList<HttpResponseInterceptor> responseLast;
74 private String serverInfo;
75 private HttpProcessor httpProcessor;
76 private ConnectionReuseStrategy connStrategy;
77 private HttpResponseFactory responseFactory;
78 private HttpRequestHandlerMapper handlerMapper;
79 private Map<String, HttpRequestHandler> handlerMap;
80 private HttpExpectationVerifier expectationVerifier;
81 private ServerSocketFactory serverSocketFactory;
82 private SSLContext sslContext;
83 private SSLServerSetupHandler sslSetupHandler;
84 private HttpConnectionFactory<? extends DefaultBHttpServerConnection> 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
98
99 public final ServerBootstrap setListenerPort(final int listenerPort) {
100 this.listenerPort = listenerPort;
101 return this;
102 }
103
104
105
106
107
108
109 public final ServerBootstrap setLocalAddress(final InetAddress localAddress) {
110 this.localAddress = localAddress;
111 return this;
112 }
113
114
115
116
117
118
119 public final ServerBootstrap setSocketConfig(final SocketConfig socketConfig) {
120 this.socketConfig = socketConfig;
121 return this;
122 }
123
124
125
126
127
128
129
130
131
132
133 public final ServerBootstrap setConnectionConfig(final ConnectionConfig connectionConfig) {
134 this.connectionConfig = connectionConfig;
135 return this;
136 }
137
138
139
140
141
142
143 public final ServerBootstrap setHttpProcessor(final HttpProcessor httpProcessor) {
144 this.httpProcessor = httpProcessor;
145 return this;
146 }
147
148
149
150
151
152
153
154
155
156
157 public final ServerBootstrap addInterceptorFirst(final HttpResponseInterceptor itcp) {
158 if (itcp == null) {
159 return this;
160 }
161 if (responseFirst == null) {
162 responseFirst = new LinkedList<HttpResponseInterceptor>();
163 }
164 responseFirst.addFirst(itcp);
165 return this;
166 }
167
168
169
170
171
172
173
174
175
176
177 public final ServerBootstrap addInterceptorLast(final HttpResponseInterceptor itcp) {
178 if (itcp == null) {
179 return this;
180 }
181 if (responseLast == null) {
182 responseLast = new LinkedList<HttpResponseInterceptor>();
183 }
184 responseLast.addLast(itcp);
185 return this;
186 }
187
188
189
190
191
192
193
194
195
196
197 public final ServerBootstrap addInterceptorFirst(final HttpRequestInterceptor itcp) {
198 if (itcp == null) {
199 return this;
200 }
201 if (requestFirst == null) {
202 requestFirst = new LinkedList<HttpRequestInterceptor>();
203 }
204 requestFirst.addFirst(itcp);
205 return this;
206 }
207
208
209
210
211
212
213
214
215
216
217 public final ServerBootstrap addInterceptorLast(final HttpRequestInterceptor itcp) {
218 if (itcp == null) {
219 return this;
220 }
221 if (requestLast == null) {
222 requestLast = new LinkedList<HttpRequestInterceptor>();
223 }
224 requestLast.addLast(itcp);
225 return this;
226 }
227
228
229
230
231
232
233
234
235
236
237 public final ServerBootstrap setServerInfo(final String serverInfo) {
238 this.serverInfo = serverInfo;
239 return this;
240 }
241
242
243
244
245
246
247 public final ServerBootstrap setConnectionReuseStrategy(final ConnectionReuseStrategy connStrategy) {
248 this.connStrategy = connStrategy;
249 return this;
250 }
251
252
253
254
255
256
257 public final ServerBootstrap setResponseFactory(final HttpResponseFactory responseFactory) {
258 this.responseFactory = responseFactory;
259 return this;
260 }
261
262
263
264
265
266
267 public final ServerBootstrap setHandlerMapper(final HttpRequestHandlerMapper handlerMapper) {
268 this.handlerMapper = handlerMapper;
269 return this;
270 }
271
272
273
274
275
276
277
278
279
280
281
282
283
284 public final ServerBootstrap registerHandler(final String pattern, final HttpRequestHandler handler) {
285 if (pattern == null || handler == null) {
286 return this;
287 }
288 if (handlerMap == null) {
289 handlerMap = new HashMap<String, HttpRequestHandler>();
290 }
291 handlerMap.put(pattern, handler);
292 return this;
293 }
294
295
296
297
298
299
300 public final ServerBootstrap setExpectationVerifier(final HttpExpectationVerifier expectationVerifier) {
301 this.expectationVerifier = expectationVerifier;
302 return this;
303 }
304
305
306
307
308
309
310 public final ServerBootstrap setConnectionFactory(
311 final HttpConnectionFactory<? extends DefaultBHttpServerConnection> connectionFactory) {
312 this.connectionFactory = connectionFactory;
313 return this;
314 }
315
316
317
318
319
320
321 public final ServerBootstrap setSslSetupHandler(final SSLServerSetupHandler sslSetupHandler) {
322 this.sslSetupHandler = sslSetupHandler;
323 return this;
324 }
325
326
327
328
329
330
331 public final ServerBootstrap setServerSocketFactory(final ServerSocketFactory serverSocketFactory) {
332 this.serverSocketFactory = serverSocketFactory;
333 return this;
334 }
335
336
337
338
339
340
341
342
343
344
345 public final ServerBootstrap setSslContext(final SSLContext sslContext) {
346 this.sslContext = sslContext;
347 return this;
348 }
349
350
351
352
353
354
355 public final ServerBootstrap setExceptionLogger(final ExceptionLogger exceptionLogger) {
356 this.exceptionLogger = exceptionLogger;
357 return this;
358 }
359
360 public HttpServer create() {
361
362 HttpProcessor httpProcessorCopy = this.httpProcessor;
363 if (httpProcessorCopy == null) {
364
365 final HttpProcessorBuilder b = HttpProcessorBuilder.create();
366 if (requestFirst != null) {
367 for (final HttpRequestInterceptor i: requestFirst) {
368 b.addFirst(i);
369 }
370 }
371 if (responseFirst != null) {
372 for (final HttpResponseInterceptor i: responseFirst) {
373 b.addFirst(i);
374 }
375 }
376
377 String serverInfoCopy = this.serverInfo;
378 if (serverInfoCopy == null) {
379 serverInfoCopy = "Apache-HttpCore/1.1";
380 }
381
382 b.addAll(
383 new ResponseDate(),
384 new ResponseServer(serverInfoCopy),
385 new ResponseContent(),
386 new ResponseConnControl());
387 if (requestLast != null) {
388 for (final HttpRequestInterceptor i: requestLast) {
389 b.addLast(i);
390 }
391 }
392 if (responseLast != null) {
393 for (final HttpResponseInterceptor i: responseLast) {
394 b.addLast(i);
395 }
396 }
397 httpProcessorCopy = b.build();
398 }
399
400 HttpRequestHandlerMapper handlerMapperCopy = this.handlerMapper;
401 if (handlerMapperCopy == null) {
402 final UriHttpRequestHandlerMapperpper.html#UriHttpRequestHandlerMapper">UriHttpRequestHandlerMapper reqistry = new UriHttpRequestHandlerMapper();
403 if (handlerMap != null) {
404 for (final Map.Entry<String, HttpRequestHandler> entry: handlerMap.entrySet()) {
405 reqistry.register(entry.getKey(), entry.getValue());
406 }
407 }
408 handlerMapperCopy = reqistry;
409 }
410
411 ConnectionReuseStrategy connStrategyCopy = this.connStrategy;
412 if (connStrategyCopy == null) {
413 connStrategyCopy = DefaultConnectionReuseStrategy.INSTANCE;
414 }
415
416 HttpResponseFactory responseFactoryCopy = this.responseFactory;
417 if (responseFactoryCopy == null) {
418 responseFactoryCopy = DefaultHttpResponseFactory.INSTANCE;
419 }
420
421 final HttpServicee.html#HttpService">HttpService httpService = new HttpService(
422 httpProcessorCopy, connStrategyCopy, responseFactoryCopy, handlerMapperCopy,
423 this.expectationVerifier);
424
425 ServerSocketFactory serverSocketFactoryCopy = this.serverSocketFactory;
426 if (serverSocketFactoryCopy == null) {
427 if (this.sslContext != null) {
428 serverSocketFactoryCopy = this.sslContext.getServerSocketFactory();
429 } else {
430 serverSocketFactoryCopy = ServerSocketFactory.getDefault();
431 }
432 }
433
434 HttpConnectionFactory<? extends DefaultBHttpServerConnection> connectionFactoryCopy = this.connectionFactory;
435 if (connectionFactoryCopy == null) {
436 if (this.connectionConfig != null) {
437 connectionFactoryCopy = new DefaultBHttpServerConnectionFactory(this.connectionConfig);
438 } else {
439 connectionFactoryCopy = DefaultBHttpServerConnectionFactory.INSTANCE;
440 }
441 }
442
443 ExceptionLogger exceptionLoggerCopy = this.exceptionLogger;
444 if (exceptionLoggerCopy == null) {
445 exceptionLoggerCopy = ExceptionLogger.NO_OP;
446 }
447
448 return new HttpServer(
449 this.listenerPort > 0 ? this.listenerPort : 0,
450 this.localAddress,
451 this.socketConfig != null ? this.socketConfig : SocketConfig.DEFAULT,
452 serverSocketFactoryCopy,
453 httpService,
454 connectionFactoryCopy,
455 this.sslSetupHandler,
456 exceptionLoggerCopy);
457 }
458
459 }