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;
28  
29  import javax.net.ssl.SSLContext;
30  
31  import org.apache.http.HttpResponseFactory;
32  import org.apache.http.annotation.Immutable;
33  import org.apache.http.impl.DefaultHttpResponseFactory;
34  import org.apache.http.nio.NHttpClientConnection;
35  import org.apache.http.nio.NHttpConnectionFactory;
36  import org.apache.http.nio.reactor.IOSession;
37  import org.apache.http.nio.reactor.ssl.SSLIOSession;
38  import org.apache.http.nio.reactor.ssl.SSLMode;
39  import org.apache.http.nio.reactor.ssl.SSLSetupHandler;
40  import org.apache.http.nio.util.ByteBufferAllocator;
41  import org.apache.http.nio.util.HeapByteBufferAllocator;
42  import org.apache.http.params.HttpConnectionParams;
43  import org.apache.http.params.HttpParams;
44  
45  /**
46   * Factory for SSL encrypted, non-blocking {@link NHttpClientConnection}s.
47   * <p>
48   * The following parameters can be used to customize the behavior of this
49   * class:
50   * <ul>
51   *  <li>{@link org.apache.http.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li>
52   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SO_TIMEOUT}</li>
53   *  <li>{@link org.apache.http.params.CoreConnectionPNames#SOCKET_BUFFER_SIZE}</li>
54   *  <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_HEADER_COUNT}</li>
55   *  <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li>
56   * </ul>
57   *
58   * @since 4.2
59   */
60  @Immutable
61  public class SSLNHttpClientConnectionFactory
62      implements NHttpConnectionFactory<DefaultNHttpClientConnection> {
63  
64      private final HttpResponseFactory responseFactory;
65      private final ByteBufferAllocator allocator;
66      private final SSLContext sslcontext;
67      private final SSLSetupHandler sslHandler;
68      private final HttpParams params;
69  
70      public SSLNHttpClientConnectionFactory(
71              final SSLContext sslcontext,
72              final SSLSetupHandler sslHandler,
73              final HttpResponseFactory responseFactory,
74              final ByteBufferAllocator allocator,
75              final HttpParams params) {
76          super();
77          if (responseFactory == null) {
78              throw new IllegalArgumentException("HTTP response factory may not be null");
79          }
80          if (allocator == null) {
81              throw new IllegalArgumentException("Byte buffer allocator may not be null");
82          }
83          if (params == null) {
84              throw new IllegalArgumentException("HTTP parameters may not be null");
85          }
86          this.sslcontext = sslcontext;
87          this.sslHandler = sslHandler;
88          this.responseFactory = responseFactory;
89          this.allocator = allocator;
90          this.params = params;
91      }
92  
93      public SSLNHttpClientConnectionFactory(
94              final SSLContext sslcontext,
95              final SSLSetupHandler sslHandler,
96              final HttpParams params) {
97          this(sslcontext, sslHandler, new DefaultHttpResponseFactory(), new HeapByteBufferAllocator(), params);
98      }
99  
100     public SSLNHttpClientConnectionFactory(final HttpParams params) {
101         this(null, null, params);
102     }
103 
104     private SSLContext getDefaultSSLContext() {
105         SSLContext sslcontext;
106         try {
107             sslcontext = SSLContext.getInstance("TLS");
108             sslcontext.init(null, null, null);
109         } catch (Exception ex) {
110             throw new IllegalStateException("Failure initializing default SSL context", ex);
111         }
112         return sslcontext;
113     }
114 
115     protected DefaultNHttpClientConnection createConnection(
116             final IOSession session,
117             final HttpResponseFactory responseFactory,
118             final ByteBufferAllocator allocator,
119             final HttpParams params) {
120         return new DefaultNHttpClientConnection(session, responseFactory, allocator, params);
121     }
122 
123     public DefaultNHttpClientConnection createConnection(final IOSession session) {
124         SSLContext sslcontext = this.sslcontext != null ? this.sslcontext : getDefaultSSLContext();
125         SSLIOSession ssliosession = new SSLIOSession(session, SSLMode.CLIENT, sslcontext, this.sslHandler);
126         session.setAttribute(SSLIOSession.SESSION_KEY, ssliosession);
127         DefaultNHttpClientConnection conn = createConnection(
128                 ssliosession, this.responseFactory, this.allocator, this.params);
129         int timeout = HttpConnectionParams.getSoTimeout(this.params);
130         conn.setSocketTimeout(timeout);
131         return conn;
132     }
133 
134 }