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  package org.apache.http.impl.nio.pool;
28  
29  import java.io.IOException;
30  
31  import javax.net.ssl.SSLContext;
32  
33  import org.apache.http.HttpHost;
34  import org.apache.http.HttpRequest;
35  import org.apache.http.HttpResponse;
36  import org.apache.http.HttpResponseFactory;
37  import org.apache.http.annotation.ThreadingBehavior;
38  import org.apache.http.annotation.Contract;
39  import org.apache.http.config.ConnectionConfig;
40  import org.apache.http.impl.DefaultHttpResponseFactory;
41  import org.apache.http.impl.nio.DefaultNHttpClientConnectionFactory;
42  import org.apache.http.impl.nio.SSLNHttpClientConnectionFactory;
43  import org.apache.http.nio.NHttpClientConnection;
44  import org.apache.http.nio.NHttpConnectionFactory;
45  import org.apache.http.nio.NHttpMessageParserFactory;
46  import org.apache.http.nio.NHttpMessageWriterFactory;
47  import org.apache.http.nio.pool.NIOConnFactory;
48  import org.apache.http.nio.reactor.IOEventDispatch;
49  import org.apache.http.nio.reactor.IOSession;
50  import org.apache.http.nio.reactor.ssl.SSLSetupHandler;
51  import org.apache.http.nio.util.ByteBufferAllocator;
52  import org.apache.http.nio.util.HeapByteBufferAllocator;
53  import org.apache.http.params.HttpParams;
54  import org.apache.http.util.Args;
55  
56  /**
57   * A basic {@link NIOConnFactory} implementation that creates
58   * {@link NHttpClientConnection} instances given a {@link HttpHost} instance.
59   *
60   * @since 4.2
61   */
62  @SuppressWarnings("deprecation")
63  @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
64  public class BasicNIOConnFactory implements NIOConnFactory<HttpHost, NHttpClientConnection> {
65  
66      private final NHttpConnectionFactory<? extends NHttpClientConnection> plainFactory;
67      private final NHttpConnectionFactory<? extends NHttpClientConnection> sslFactory;
68  
69      public BasicNIOConnFactory(
70              final NHttpConnectionFactory<? extends NHttpClientConnection> plainFactory,
71              final NHttpConnectionFactory<? extends NHttpClientConnection> sslFactory) {
72          super();
73          Args.notNull(plainFactory, "Plain HTTP client connection factory");
74          this.plainFactory = plainFactory;
75          this.sslFactory = sslFactory;
76      }
77  
78      public BasicNIOConnFactory(
79              final NHttpConnectionFactory<? extends NHttpClientConnection> plainFactory) {
80          this(plainFactory, null);
81      }
82  
83      /**
84       * @deprecated (4.3) use {@link BasicNIOConnFactory#BasicNIOConnFactory(SSLContext,
85       *   SSLSetupHandler, NHttpMessageParserFactory, NHttpMessageWriterFactory,
86       *   ByteBufferAllocator, ConnectionConfig)}
87       */
88      @Deprecated
89      public BasicNIOConnFactory(
90              final SSLContext sslContext,
91              final SSLSetupHandler sslHandler,
92              final HttpResponseFactory responseFactory,
93              final ByteBufferAllocator allocator,
94              final HttpParams params) {
95          this(new DefaultNHttpClientConnectionFactory(
96                  responseFactory, allocator, params),
97                  new SSLNHttpClientConnectionFactory(
98                          sslContext, sslHandler, responseFactory, allocator, params));
99      }
100 
101     /**
102      * @deprecated (4.3) use {@link BasicNIOConnFactory#BasicNIOConnFactory(SSLContext,
103      *   SSLSetupHandler, ConnectionConfig)}
104      */
105     @Deprecated
106     public BasicNIOConnFactory(
107             final SSLContext sslContext,
108             final SSLSetupHandler sslHandler,
109             final HttpParams params) {
110         this(sslContext, sslHandler,
111                 DefaultHttpResponseFactory.INSTANCE, HeapByteBufferAllocator.INSTANCE, params);
112     }
113 
114     /**
115      * @deprecated (4.3) use {@link BasicNIOConnFactory#BasicNIOConnFactory(ConnectionConfig)}
116      */
117     @Deprecated
118     public BasicNIOConnFactory(final HttpParams params) {
119         this(null, null, params);
120     }
121 
122     /**
123      * @since 4.3
124      */
125     public BasicNIOConnFactory(
126             final SSLContext sslContext,
127             final SSLSetupHandler sslHandler,
128             final NHttpMessageParserFactory<HttpResponse> responseParserFactory,
129             final NHttpMessageWriterFactory<HttpRequest> requestWriterFactory,
130             final ByteBufferAllocator allocator,
131             final ConnectionConfig config) {
132         this(new DefaultNHttpClientConnectionFactory(
133                     responseParserFactory, requestWriterFactory, allocator, config),
134                 new SSLNHttpClientConnectionFactory(
135                         sslContext, sslHandler, responseParserFactory, requestWriterFactory,
136                         allocator, config));
137     }
138 
139     /**
140      * @since 4.3
141      */
142     public BasicNIOConnFactory(
143             final SSLContext sslContext,
144             final SSLSetupHandler sslHandler,
145             final ConnectionConfig config) {
146         this(sslContext, sslHandler, null, null, null, config);
147     }
148 
149     /**
150      * @since 4.3
151      */
152     public BasicNIOConnFactory(final ConnectionConfig config) {
153         this(new DefaultNHttpClientConnectionFactory(config), null);
154     }
155 
156     @Override
157     public NHttpClientConnection create(final HttpHost route, final IOSession session) throws IOException {
158         final NHttpClientConnection conn;
159         if (route.getSchemeName().equalsIgnoreCase("https")) {
160             if (this.sslFactory == null) {
161                 throw new IOException("SSL not supported");
162             }
163             conn = this.sslFactory.createConnection(session);
164         } else {
165             conn = this.plainFactory.createConnection(session);
166         }
167         session.setAttribute(IOEventDispatch.CONNECTION_KEY, conn);
168         return conn;
169     }
170 
171 }