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