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