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.HttpResponseFactory;
35  import org.apache.http.annotation.Immutable;
36  import org.apache.http.impl.DefaultHttpResponseFactory;
37  import org.apache.http.impl.nio.DefaultNHttpClientConnectionFactory;
38  import org.apache.http.impl.nio.SSLNHttpClientConnectionFactory;
39  import org.apache.http.nio.NHttpClientConnection;
40  import org.apache.http.nio.NHttpConnectionFactory;
41  import org.apache.http.nio.pool.NIOConnFactory;
42  import org.apache.http.nio.reactor.IOEventDispatch;
43  import org.apache.http.nio.reactor.IOSession;
44  import org.apache.http.nio.reactor.ssl.SSLSetupHandler;
45  import org.apache.http.nio.util.ByteBufferAllocator;
46  import org.apache.http.nio.util.HeapByteBufferAllocator;
47  import org.apache.http.params.HttpParams;
48  
49  /**
50   * A basic {@link NIOConnFactory} implementation that creates
51   * {@link NHttpClientConnection} instances given a {@link HttpHost} instance.
52   * <p>
53   * The following parameters can be used to customize the behavior of this
54   * class:
55   * <ul>
56   *  <li>{@link org.apache.http.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li>
57   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SOCKET_BUFFER_SIZE}</li>
58   *  <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_HEADER_COUNT}</li>
59   *  <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li>
60   * </ul>
61   *
62   * @since 4.2
63   */
64  @Immutable
65  public class BasicNIOConnFactory implements NIOConnFactory<HttpHost, NHttpClientConnection> {
66  
67      private final NHttpConnectionFactory<? extends NHttpClientConnection> plainFactory;
68      private final NHttpConnectionFactory<? extends NHttpClientConnection> sslFactory;
69  
70      public BasicNIOConnFactory(
71              final NHttpConnectionFactory<? extends NHttpClientConnection> plainFactory,
72              final NHttpConnectionFactory<? extends NHttpClientConnection> sslFactory) {
73          super();
74          if (plainFactory == null) {
75              throw new IllegalArgumentException("Plain HTTP client connection factory may not be null");
76          }
77          this.plainFactory = plainFactory;
78          this.sslFactory = sslFactory;
79      }
80  
81      public BasicNIOConnFactory(
82              final NHttpConnectionFactory<? extends NHttpClientConnection> plainFactory) {
83          this(plainFactory, null);
84      }
85  
86      public BasicNIOConnFactory(
87              final SSLContext sslcontext,
88              final SSLSetupHandler sslHandler,
89              final HttpResponseFactory responseFactory,
90              final ByteBufferAllocator allocator,
91              final HttpParams params) {
92          this(new DefaultNHttpClientConnectionFactory(
93                  responseFactory, allocator, params),
94                  new SSLNHttpClientConnectionFactory(
95                          sslcontext, sslHandler, responseFactory, allocator, params));
96      }
97  
98      public BasicNIOConnFactory(
99              final SSLContext sslcontext,
100             final SSLSetupHandler sslHandler,
101             final HttpParams params) {
102         this(sslcontext, sslHandler,
103                 new DefaultHttpResponseFactory(), new HeapByteBufferAllocator(), params);
104     }
105 
106     public BasicNIOConnFactory(final HttpParams params) {
107         this(null, null, params);
108     }
109 
110     public NHttpClientConnection create(final HttpHost route, final IOSession session) throws IOException {
111         NHttpClientConnection conn;
112         if (route.getSchemeName().equalsIgnoreCase("https")) {
113             if (this.sslFactory == null) {
114                 throw new IOException("SSL not supported");
115             }
116             conn = this.sslFactory.createConnection(session);
117         } else {
118             conn = this.plainFactory.createConnection(session);
119         }
120         session.setAttribute(IOEventDispatch.CONNECTION_KEY, conn);
121         return conn;
122     }
123 
124 }