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