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