View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
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.HttpResponseFactory;
36  import org.apache.http.impl.DefaultHttpResponseFactory;
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.NHttpClientHandler;
41  import org.apache.http.nio.NHttpClientIOTarget;
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   * Default implementation of {@link IOEventDispatch} interface for SSL
52   * (encrypted) client-side HTTP connections.
53   *
54   * @since 4.0
55   *
56   * @deprecated (4.2) use {@link org.apache.http.impl.nio.ssl.SSLClientIOEventDispatch}
57   */
58  @Deprecated
59  public class SSLClientIOEventDispatch implements IOEventDispatch {
60  
61      private static final String SSL_SESSION = "SSL_SESSION";
62  
63      protected final NHttpClientHandler handler;
64      protected final SSLContext sslcontext;
65      protected final SSLIOSessionHandler sslHandler;
66      protected final HttpParams params;
67  
68      /**
69       * Creates a new instance of this class to be used for dispatching I/O event
70       * notifications to the given protocol handler using the given
71       * {@link SSLContext}. This I/O dispatcher will transparently handle SSL
72       * protocol aspects for HTTP connections.
73       *
74       * @param handler the client protocol handler.
75       * @param sslContext the SSL context.
76       * @param sslHandler the SSL handler.
77       * @param params HTTP parameters.
78       */
79      public SSLClientIOEventDispatch(
80              final NHttpClientHandler handler,
81              final SSLContext sslContext,
82              final SSLIOSessionHandler sslHandler,
83              final HttpParams params) {
84          super();
85          Args.notNull(handler, "HTTP client 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       * Creates a new instance of this class to be used for dispatching I/O event
96       * notifications to the given protocol handler using the given
97       * {@link SSLContext}. This I/O dispatcher will transparently handle SSL
98       * protocol aspects for HTTP connections.
99       *
100      * @param handler the client protocol handler.
101      * @param sslContext the SSL context.
102      * @param params HTTP parameters.
103      */
104     public SSLClientIOEventDispatch(
105             final NHttpClientHandler handler,
106             final SSLContext sslContext,
107             final HttpParams params) {
108         this(handler, sslContext, null, params);
109     }
110 
111     /**
112      * Creates an instance of {@link HeapByteBufferAllocator} to be used
113      * by HTTP connections for allocating {@link java.nio.ByteBuffer} objects.
114      * <p>
115      * This method can be overridden in a super class in order to provide
116      * a different implementation of the {@link ByteBufferAllocator} interface.
117      *
118      * @return byte buffer allocator.
119      */
120     protected ByteBufferAllocator createByteBufferAllocator() {
121         return HeapByteBufferAllocator.INSTANCE;
122     }
123 
124     /**
125      * Creates an instance of {@link DefaultHttpResponseFactory} to be used
126      * by HTTP connections for creating {@link org.apache.http.HttpResponse}
127      * objects.
128      * <p>
129      * This method can be overridden in a super class in order to provide
130      * a different implementation of the {@link HttpResponseFactory} interface.
131      *
132      * @return HTTP response factory.
133      */
134     protected HttpResponseFactory createHttpResponseFactory() {
135         return DefaultHttpResponseFactory.INSTANCE;
136     }
137 
138     /**
139      * Creates an instance of {@link DefaultNHttpClientConnection} based on the
140      * given SSL {@link IOSession}.
141      * <p>
142      * This method can be overridden in a super class in order to provide
143      * a different implementation of the {@link NHttpClientIOTarget} interface.
144      *
145      * @param session the underlying SSL I/O session.
146      *
147      * @return newly created HTTP connection.
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      * Creates an instance of {@link SSLIOSession} decorating the given
159      * {@link IOSession}.
160      * <p>
161      * This method can be overridden in a super class in order to provide
162      * a different implementation of SSL I/O session.
163      *
164      * @param session the underlying I/O session.
165      * @param sslContext the SSL context.
166      * @param sslHandler the SSL handler.
167      * @return newly created SSL I/O session.
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 NHttpClientIOTarget conn = createConnection(
185                 sslSession);
186 
187         session.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
188         session.setAttribute(SSL_SESSION, sslSession);
189 
190         final Object attachment = session.getAttribute(IOSession.ATTACHMENT_KEY);
191         this.handler.connected(conn, attachment);
192 
193         try {
194             sslSession.bind(SSLMode.CLIENT, this.params);
195         } catch (final SSLException ex) {
196             this.handler.exception(conn, ex);
197             sslSession.shutdown();
198         }
199     }
200 
201     @Override
202     public void disconnected(final IOSession session) {
203         final NHttpClientIOTarget conn =
204             (NHttpClientIOTarget) session.getAttribute(ExecutionContext.HTTP_CONNECTION);
205         if (conn != null) {
206             this.handler.closed(conn);
207         }
208     }
209 
210     @Override
211     public void inputReady(final IOSession session) {
212         final NHttpClientIOTarget conn =
213             (NHttpClientIOTarget) 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 NHttpClientIOTarget conn =
231             (NHttpClientIOTarget) 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 NHttpClientIOTarget conn =
249             (NHttpClientIOTarget) 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                 // The session failed to terminate cleanly
257                 sslSession.shutdown();
258             }
259         }
260     }
261 
262 }