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.impl.nio;
29
30 import java.io.IOException;
31
32 import javax.net.ssl.SSLContext;
33 import javax.net.ssl.SSLException;
34
35 import org.apache.http.HttpResponse;
36 import org.apache.http.HttpResponseFactory;
37 import org.apache.http.impl.DefaultHttpResponseFactory;
38 import org.apache.http.impl.nio.reactor.SSLIOSession;
39 import org.apache.http.impl.nio.reactor.SSLIOSessionHandler;
40 import org.apache.http.impl.nio.reactor.SSLMode;
41 import org.apache.http.nio.NHttpClientHandler;
42 import org.apache.http.nio.NHttpClientIOTarget;
43 import org.apache.http.nio.reactor.IOEventDispatch;
44 import org.apache.http.nio.reactor.IOSession;
45 import org.apache.http.nio.util.ByteBufferAllocator;
46 import org.apache.http.nio.util.HeapByteBufferAllocator;
47 import org.apache.http.params.HttpParams;
48 import org.apache.http.protocol.ExecutionContext;
49 import org.apache.http.util.Args;
50
51
52
53
54
55
56
57
58
59 @Deprecated
60 public class SSLClientIOEventDispatch implements IOEventDispatch {
61
62 private static final String SSL_SESSION = "SSL_SESSION";
63
64 protected final NHttpClientHandler handler;
65 protected final SSLContext sslcontext;
66 protected final SSLIOSessionHandler sslHandler;
67 protected final HttpParams params;
68
69
70
71
72
73
74
75
76
77
78
79
80 public SSLClientIOEventDispatch(
81 final NHttpClientHandler handler,
82 final SSLContext sslcontext,
83 final SSLIOSessionHandler sslHandler,
84 final HttpParams params) {
85 super();
86 Args.notNull(handler, "HTTP client handler");
87 Args.notNull(sslcontext, "SSL context");
88 Args.notNull(params, "HTTP parameters");
89 this.handler = handler;
90 this.params = params;
91 this.sslcontext = sslcontext;
92 this.sslHandler = sslHandler;
93 }
94
95
96
97
98
99
100
101
102
103
104
105 public SSLClientIOEventDispatch(
106 final NHttpClientHandler handler,
107 final SSLContext sslcontext,
108 final HttpParams params) {
109 this(handler, sslcontext, null, params);
110 }
111
112
113
114
115
116
117
118
119
120
121 protected ByteBufferAllocator createByteBufferAllocator() {
122 return HeapByteBufferAllocator.INSTANCE;
123 }
124
125
126
127
128
129
130
131
132
133
134 protected HttpResponseFactory createHttpResponseFactory() {
135 return DefaultHttpResponseFactory.INSTANCE;
136 }
137
138
139
140
141
142
143
144
145
146
147
148
149 protected NHttpClientIOTarget createConnection(final IOSession session) {
150 return new DefaultNHttpClientConnection(
151 session,
152 createHttpResponseFactory(),
153 createByteBufferAllocator(),
154 this.params);
155 }
156
157
158
159
160
161
162
163
164
165
166
167
168
169 protected SSLIOSession createSSLIOSession(
170 final IOSession session,
171 final SSLContext sslcontext,
172 final SSLIOSessionHandler sslHandler) {
173 return new SSLIOSession(session, sslcontext, sslHandler);
174 }
175
176 public void connected(final IOSession session) {
177
178 final SSLIOSession sslSession = createSSLIOSession(
179 session,
180 this.sslcontext,
181 this.sslHandler);
182
183 final NHttpClientIOTarget conn = createConnection(
184 sslSession);
185
186 session.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
187 session.setAttribute(SSL_SESSION, sslSession);
188
189 final Object attachment = session.getAttribute(IOSession.ATTACHMENT_KEY);
190 this.handler.connected(conn, attachment);
191
192 try {
193 sslSession.bind(SSLMode.CLIENT, this.params);
194 } catch (final SSLException ex) {
195 this.handler.exception(conn, ex);
196 sslSession.shutdown();
197 }
198 }
199
200 public void disconnected(final IOSession session) {
201 final NHttpClientIOTarget conn =
202 (NHttpClientIOTarget) session.getAttribute(ExecutionContext.HTTP_CONNECTION);
203 if (conn != null) {
204 this.handler.closed(conn);
205 }
206 }
207
208 public void inputReady(final IOSession session) {
209 final NHttpClientIOTarget conn =
210 (NHttpClientIOTarget) session.getAttribute(ExecutionContext.HTTP_CONNECTION);
211 final SSLIOSession sslSession =
212 (SSLIOSession) session.getAttribute(SSL_SESSION);
213
214 try {
215 if (sslSession.isAppInputReady()) {
216 conn.consumeInput(this.handler);
217 }
218 sslSession.inboundTransport();
219 } catch (final IOException ex) {
220 this.handler.exception(conn, ex);
221 sslSession.shutdown();
222 }
223 }
224
225 public void outputReady(final IOSession session) {
226 final NHttpClientIOTarget conn =
227 (NHttpClientIOTarget) session.getAttribute(ExecutionContext.HTTP_CONNECTION);
228 final SSLIOSession sslSession =
229 (SSLIOSession) session.getAttribute(SSL_SESSION);
230
231 try {
232 if (sslSession.isAppOutputReady()) {
233 conn.produceOutput(this.handler);
234 }
235 sslSession.outboundTransport();
236 } catch (final IOException ex) {
237 this.handler.exception(conn, ex);
238 sslSession.shutdown();
239 }
240 }
241
242 public void timeout(final IOSession session) {
243 final NHttpClientIOTarget conn =
244 (NHttpClientIOTarget) session.getAttribute(ExecutionContext.HTTP_CONNECTION);
245 final SSLIOSession sslSession =
246 (SSLIOSession) session.getAttribute(SSL_SESSION);
247
248 this.handler.timeout(conn);
249 synchronized (sslSession) {
250 if (sslSession.isOutboundDone() && !sslSession.isInboundDone()) {
251
252 sslSession.shutdown();
253 }
254 }
255 }
256
257 }