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  
34  import org.apache.http.annotation.Contract;
35  import org.apache.http.annotation.ThreadingBehavior;
36  import org.apache.http.config.ConnectionConfig;
37  import org.apache.http.impl.nio.reactor.AbstractIODispatch;
38  import org.apache.http.nio.NHttpClientEventHandler;
39  import org.apache.http.nio.NHttpConnectionFactory;
40  import org.apache.http.nio.reactor.IOSession;
41  import org.apache.http.nio.reactor.ssl.SSLSetupHandler;
42  import org.apache.http.params.HttpParams;
43  import org.apache.http.util.Args;
44  
45  /**
46   * Default {@link org.apache.http.nio.reactor.IOEventDispatch} implementation
47   * that supports both plain (non-encrypted) and SSL encrypted client side HTTP
48   * connections.
49   *
50   * @since 4.2
51   */
52  @SuppressWarnings("deprecation")
53  @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
54  public class DefaultHttpClientIODispatch
55                      extends AbstractIODispatch<DefaultNHttpClientConnection> {
56  
57      /**
58       * Creates a new instance of this class to be used for dispatching I/O event
59       * notifications to the given protocol handler.
60       *
61       * @param handler the client protocol handler.
62       * @param sslContext an SSLContext or null (for a plain text connection.)
63       * @param config a connection configuration
64       * @return a new instance
65       * @since 4.4.7
66       */
67      public static DefaultHttpClientIODispatch create(final NHttpClientEventHandler handler,
68              final SSLContext sslContext,
69              final ConnectionConfig config) {
70          return sslContext == null ? new DefaultHttpClientIODispatch(handler, config)
71                  : new DefaultHttpClientIODispatch(handler, sslContext, config);
72      }
73  
74      /**
75       * Creates a new instance of this class to be used for dispatching I/O event
76       * notifications to the given protocol handler.
77       *
78       * @param handler the client protocol handler.
79       * @param sslContext an SSLContext or null (for a plain text connection.)
80       * @param sslHandler customizes various aspects of the TLS/SSL protocol.
81       * @param config a connection configuration
82       * @return a new instance
83       * @since 4.4.7
84       */
85      public static DefaultHttpClientIODispatch create(final NHttpClientEventHandler handler,
86              final SSLContext sslContext,
87              final SSLSetupHandler sslHandler,
88              final ConnectionConfig config) {
89          return sslContext == null ? new DefaultHttpClientIODispatch(handler, config)
90                  : new DefaultHttpClientIODispatch(handler, sslContext, sslHandler, config);
91      }
92  
93      private final NHttpClientEventHandler handler;
94      private final NHttpConnectionFactory<DefaultNHttpClientConnection> connFactory;
95  
96      /**
97       * Creates a new instance of this class to be used for dispatching I/O event
98       * notifications to the given protocol handler.
99       *
100      * @param handler the client protocol handler.
101      * @param connFactory HTTP client connection factory.
102      */
103     public DefaultHttpClientIODispatch(
104             final NHttpClientEventHandler handler,
105             final NHttpConnectionFactory<DefaultNHttpClientConnection> connFactory) {
106         super();
107         this.handler = Args.notNull(handler, "HTTP client handler");
108         this.connFactory = Args.notNull(connFactory, "HTTP client connection factory");
109     }
110 
111     /**
112      * @deprecated (4.3) use {@link DefaultHttpClientIODispatch#DefaultHttpClientIODispatch(
113      *  NHttpClientEventHandler, ConnectionConfig)}
114      */
115     @Deprecated
116     public DefaultHttpClientIODispatch(
117             final NHttpClientEventHandler handler,
118             final HttpParams params) {
119         this(handler, new DefaultNHttpClientConnectionFactory(params));
120     }
121 
122     /**
123      * @deprecated (4.3) use {@link DefaultHttpClientIODispatch#DefaultHttpClientIODispatch(
124      *  NHttpClientEventHandler, SSLContext, SSLSetupHandler, ConnectionConfig)}
125      */
126     @Deprecated
127     public DefaultHttpClientIODispatch(
128             final NHttpClientEventHandler handler,
129             final SSLContext sslContext,
130             final SSLSetupHandler sslHandler,
131             final HttpParams params) {
132         this(handler, new SSLNHttpClientConnectionFactory(sslContext, sslHandler, params));
133     }
134 
135     /**
136      * @deprecated (4.3) use {@link DefaultHttpClientIODispatch#DefaultHttpClientIODispatch(
137      *   NHttpClientEventHandler, SSLContext, ConnectionConfig)}
138      */
139     @Deprecated
140     public DefaultHttpClientIODispatch(
141             final NHttpClientEventHandler handler,
142             final SSLContext sslContext,
143             final HttpParams params) {
144         this(handler, sslContext, null, params);
145     }
146 
147     /**
148      * @since 4.3
149      */
150     public DefaultHttpClientIODispatch(final NHttpClientEventHandler handler, final ConnectionConfig config) {
151         this(handler, new DefaultNHttpClientConnectionFactory(config));
152     }
153 
154     /**
155      * @since 4.3
156      */
157     public DefaultHttpClientIODispatch(
158             final NHttpClientEventHandler handler,
159             final SSLContext sslContext,
160             final SSLSetupHandler sslHandler,
161             final ConnectionConfig config) {
162         this(handler, new SSLNHttpClientConnectionFactory(sslContext, sslHandler, config));
163     }
164 
165     /**
166      * @since 4.3
167      */
168     public DefaultHttpClientIODispatch(
169             final NHttpClientEventHandler handler,
170             final SSLContext sslContext,
171             final ConnectionConfig config) {
172         this(handler, new SSLNHttpClientConnectionFactory(sslContext, null, config));
173     }
174 
175     @Override
176     protected DefaultNHttpClientConnection createConnection(final IOSession session) {
177         return this.connFactory.createConnection(session);
178     }
179 
180     @Override
181     protected void onConnected(final DefaultNHttpClientConnection conn) {
182         final Object attachment = conn.getContext().getAttribute(IOSession.ATTACHMENT_KEY);
183         try {
184             this.handler.connected(conn, attachment);
185         } catch (final Exception ex) {
186             this.handler.exception(conn, ex);
187         }
188     }
189 
190     @Override
191     protected void onClosed(final DefaultNHttpClientConnection conn) {
192         this.handler.closed(conn);
193     }
194 
195     @Override
196     protected void onException(final DefaultNHttpClientConnection conn, final IOException ex) {
197         this.handler.exception(conn, ex);
198     }
199 
200     @Override
201     protected void onInputReady(final DefaultNHttpClientConnection conn) {
202         conn.consumeInput(this.handler);
203     }
204 
205     @Override
206     protected void onOutputReady(final DefaultNHttpClientConnection conn) {
207         conn.produceOutput(this.handler);
208     }
209 
210     @Override
211     protected void onTimeout(final DefaultNHttpClientConnection conn) {
212         try {
213             this.handler.timeout(conn);
214         } catch (final Exception ex) {
215             this.handler.exception(conn, ex);
216         }
217     }
218 
219 }