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;
28
29 import javax.net.ssl.SSLContext;
30
31 import org.apache.http.HttpResponseFactory;
32 import org.apache.http.annotation.Immutable;
33 import org.apache.http.impl.DefaultHttpResponseFactory;
34 import org.apache.http.nio.NHttpClientConnection;
35 import org.apache.http.nio.NHttpConnectionFactory;
36 import org.apache.http.nio.reactor.IOSession;
37 import org.apache.http.nio.reactor.ssl.SSLIOSession;
38 import org.apache.http.nio.reactor.ssl.SSLMode;
39 import org.apache.http.nio.reactor.ssl.SSLSetupHandler;
40 import org.apache.http.nio.util.ByteBufferAllocator;
41 import org.apache.http.nio.util.HeapByteBufferAllocator;
42 import org.apache.http.params.HttpConnectionParams;
43 import org.apache.http.params.HttpParams;
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 @Immutable
61 public class SSLNHttpClientConnectionFactory
62 implements NHttpConnectionFactory<DefaultNHttpClientConnection> {
63
64 private final HttpResponseFactory responseFactory;
65 private final ByteBufferAllocator allocator;
66 private final SSLContext sslcontext;
67 private final SSLSetupHandler sslHandler;
68 private final HttpParams params;
69
70 public SSLNHttpClientConnectionFactory(
71 final SSLContext sslcontext,
72 final SSLSetupHandler sslHandler,
73 final HttpResponseFactory responseFactory,
74 final ByteBufferAllocator allocator,
75 final HttpParams params) {
76 super();
77 if (responseFactory == null) {
78 throw new IllegalArgumentException("HTTP response factory may not be null");
79 }
80 if (allocator == null) {
81 throw new IllegalArgumentException("Byte buffer allocator may not be null");
82 }
83 if (params == null) {
84 throw new IllegalArgumentException("HTTP parameters may not be null");
85 }
86 this.sslcontext = sslcontext;
87 this.sslHandler = sslHandler;
88 this.responseFactory = responseFactory;
89 this.allocator = allocator;
90 this.params = params;
91 }
92
93 public SSLNHttpClientConnectionFactory(
94 final SSLContext sslcontext,
95 final SSLSetupHandler sslHandler,
96 final HttpParams params) {
97 this(sslcontext, sslHandler, new DefaultHttpResponseFactory(), new HeapByteBufferAllocator(), params);
98 }
99
100 public SSLNHttpClientConnectionFactory(final HttpParams params) {
101 this(null, null, params);
102 }
103
104 private SSLContext getDefaultSSLContext() {
105 SSLContext sslcontext;
106 try {
107 sslcontext = SSLContext.getInstance("TLS");
108 sslcontext.init(null, null, null);
109 } catch (Exception ex) {
110 throw new IllegalStateException("Failure initializing default SSL context", ex);
111 }
112 return sslcontext;
113 }
114
115 protected DefaultNHttpClientConnection createConnection(
116 final IOSession session,
117 final HttpResponseFactory responseFactory,
118 final ByteBufferAllocator allocator,
119 final HttpParams params) {
120 return new DefaultNHttpClientConnection(session, responseFactory, allocator, params);
121 }
122
123 public DefaultNHttpClientConnection createConnection(final IOSession session) {
124 SSLContext sslcontext = this.sslcontext != null ? this.sslcontext : getDefaultSSLContext();
125 SSLIOSession ssliosession = new SSLIOSession(session, SSLMode.CLIENT, sslcontext, this.sslHandler);
126 session.setAttribute(SSLIOSession.SESSION_KEY, ssliosession);
127 DefaultNHttpClientConnection conn = createConnection(
128 ssliosession, this.responseFactory, this.allocator, this.params);
129 int timeout = HttpConnectionParams.getSoTimeout(this.params);
130 conn.setSocketTimeout(timeout);
131 return conn;
132 }
133
134 }