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.impl.nio.reactor.AbstractIODispatch;
36  import org.apache.http.nio.NHttpClientEventHandler;
37  import org.apache.http.nio.NHttpConnectionFactory;
38  import org.apache.http.nio.reactor.IOEventDispatch;
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  
43  /**
44   * Default {@link IOEventDispatch} implementation that supports both plain (non-encrypted)
45   * and SSL encrypted client side HTTP connections.
46   *
47   * @since 4.2
48   */
49  @Immutable // provided injected dependencies are immutable
50  public class DefaultHttpClientIODispatch
51                      extends AbstractIODispatch<DefaultNHttpClientConnection> {
52  
53      private final NHttpClientEventHandler handler;
54      private final NHttpConnectionFactory<DefaultNHttpClientConnection> connFactory;
55  
56      /**
57       * Creates a new instance of this class to be used for dispatching I/O event
58       * notifications to the given protocol handler.
59       *
60       * @param handler the client protocol handler.
61       * @param connFactory HTTP client connection factory.
62       */
63      public DefaultHttpClientIODispatch(
64              final NHttpClientEventHandler handler,
65              final NHttpConnectionFactory<DefaultNHttpClientConnection> connFactory) {
66          super();
67          if (handler == null) {
68              throw new IllegalArgumentException("HTTP client handler may not be null");
69          }
70          if (connFactory == null) {
71              throw new IllegalArgumentException("HTTP client connection factory may not null");
72          }
73          this.handler = handler;
74          this.connFactory = connFactory;
75      }
76  
77      public DefaultHttpClientIODispatch(
78              final NHttpClientEventHandler handler,
79              final HttpParams params) {
80          this(handler, new DefaultNHttpClientConnectionFactory(params));
81      }
82  
83      public DefaultHttpClientIODispatch(
84              final NHttpClientEventHandler handler,
85              final SSLContext sslcontext,
86              final SSLSetupHandler sslHandler,
87              final HttpParams params) {
88          this(handler, new SSLNHttpClientConnectionFactory(sslcontext, sslHandler, params));
89      }
90  
91      public DefaultHttpClientIODispatch(
92              final NHttpClientEventHandler handler,
93              final SSLContext sslcontext,
94              final HttpParams params) {
95          this(handler, sslcontext, null, params);
96      }
97  
98      @Override
99      protected DefaultNHttpClientConnection createConnection(final IOSession session) {
100         return this.connFactory.createConnection(session);
101     }
102 
103     @Override
104     protected void onConnected(final DefaultNHttpClientConnection conn) {
105         Object attachment = conn.getContext().getAttribute(IOSession.ATTACHMENT_KEY);
106         try {
107             this.handler.connected(conn, attachment);
108         } catch (Exception ex) {
109             this.handler.exception(conn, ex);
110         }
111     }
112 
113     @Override
114     protected void onClosed(final DefaultNHttpClientConnection conn) {
115         this.handler.closed(conn);
116     }
117 
118     @Override
119     protected void onException(final DefaultNHttpClientConnection conn, IOException ex) {
120         this.handler.exception(conn, ex);
121     }
122 
123     @Override
124     protected void onInputReady(final DefaultNHttpClientConnection conn) {
125         conn.consumeInput(this.handler);
126     }
127 
128     @Override
129     protected void onOutputReady(final DefaultNHttpClientConnection conn) {
130         conn.produceOutput(this.handler);
131     }
132 
133     @Override
134     protected void onTimeout(final DefaultNHttpClientConnection conn) {
135         try {
136             this.handler.timeout(conn);
137         } catch (Exception ex) {
138             this.handler.exception(conn, ex);
139         }
140     }
141 
142 }