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.HttpRequest;
32  import org.apache.http.HttpResponse;
33  import org.apache.http.HttpResponseFactory;
34  import org.apache.http.annotation.Immutable;
35  import org.apache.http.config.ConnectionConfig;
36  import org.apache.http.entity.ContentLengthStrategy;
37  import org.apache.http.impl.ConnSupport;
38  import org.apache.http.impl.DefaultHttpResponseFactory;
39  import org.apache.http.impl.nio.codecs.DefaultHttpResponseParserFactory;
40  import org.apache.http.nio.NHttpConnectionFactory;
41  import org.apache.http.nio.NHttpMessageParserFactory;
42  import org.apache.http.nio.NHttpMessageWriterFactory;
43  import org.apache.http.nio.reactor.IOSession;
44  import org.apache.http.nio.reactor.ssl.SSLIOSession;
45  import org.apache.http.nio.reactor.ssl.SSLMode;
46  import org.apache.http.nio.reactor.ssl.SSLSetupHandler;
47  import org.apache.http.nio.util.ByteBufferAllocator;
48  import org.apache.http.nio.util.HeapByteBufferAllocator;
49  import org.apache.http.params.HttpParamConfig;
50  import org.apache.http.params.HttpParams;
51  import org.apache.http.ssl.SSLContexts;
52  import org.apache.http.util.Args;
53  
54  /**
55   * Default factory for SSL encrypted, non-blocking
56   * {@link org.apache.http.nio.NHttpClientConnection}s.
57   *
58   * @since 4.2
59   */
60  @SuppressWarnings("deprecation")
61  @Immutable
62  public class SSLNHttpClientConnectionFactory
63      implements NHttpConnectionFactory<DefaultNHttpClientConnection> {
64  
65      public static final SSLNHttpClientConnectionFactory INSTANCE = new SSLNHttpClientConnectionFactory();
66  
67      private final ContentLengthStrategy incomingContentStrategy;
68      private final ContentLengthStrategy outgoingContentStrategy;
69      private final NHttpMessageParserFactory<HttpResponse> responseParserFactory;
70      private final NHttpMessageWriterFactory<HttpRequest> requestWriterFactory;
71      private final ByteBufferAllocator allocator;
72      private final SSLContext sslcontext;
73      private final SSLSetupHandler sslHandler;
74      private final ConnectionConfig cconfig;
75  
76      /**
77       * @deprecated (4.3) use {@link
78       *   SSLNHttpClientConnectionFactory#SSLNHttpClientConnectionFactory(SSLContext,
79       *      SSLSetupHandler, NHttpMessageParserFactory, NHttpMessageWriterFactory,
80       *      ByteBufferAllocator, ConnectionConfig)}
81       */
82      @Deprecated
83      public SSLNHttpClientConnectionFactory(
84              final SSLContext sslcontext,
85              final SSLSetupHandler sslHandler,
86              final HttpResponseFactory responseFactory,
87              final ByteBufferAllocator allocator,
88              final HttpParams params) {
89          super();
90          Args.notNull(responseFactory, "HTTP response factory");
91          Args.notNull(allocator, "Byte buffer allocator");
92          Args.notNull(params, "HTTP parameters");
93          this.sslcontext = sslcontext != null ? sslcontext : SSLContexts.createSystemDefault();
94          this.sslHandler = sslHandler;
95          this.allocator = allocator;
96          this.incomingContentStrategy = null;
97          this.outgoingContentStrategy = null;
98          this.responseParserFactory = new DefaultHttpResponseParserFactory(null, responseFactory);
99          this.requestWriterFactory = null;
100         this.cconfig = HttpParamConfig.getConnectionConfig(params);
101     }
102 
103     /**
104      * @deprecated (4.3) use {@link
105      *   SSLNHttpClientConnectionFactory#SSLNHttpClientConnectionFactory(SSLContext,
106      *     SSLSetupHandler, ConnectionConfig)}
107      */
108     @Deprecated
109     public SSLNHttpClientConnectionFactory(
110             final SSLContext sslcontext,
111             final SSLSetupHandler sslHandler,
112             final HttpParams params) {
113         this(sslcontext, sslHandler, DefaultHttpResponseFactory.INSTANCE,
114                 HeapByteBufferAllocator.INSTANCE, params);
115     }
116 
117     /**
118      * @deprecated (4.3) use {@link
119      *   SSLNHttpClientConnectionFactory#SSLNHttpClientConnectionFactory(ConnectionConfig)}
120      */
121     @Deprecated
122     public SSLNHttpClientConnectionFactory(final HttpParams params) {
123         this(null, null, params);
124     }
125 
126     /**
127      * @since 4.3
128      */
129     public SSLNHttpClientConnectionFactory(
130             final SSLContext sslcontext,
131             final SSLSetupHandler sslHandler,
132             final ContentLengthStrategy incomingContentStrategy,
133             final ContentLengthStrategy outgoingContentStrategy,
134             final NHttpMessageParserFactory<HttpResponse> responseParserFactory,
135             final NHttpMessageWriterFactory<HttpRequest> requestWriterFactory,
136             final ByteBufferAllocator allocator,
137             final ConnectionConfig cconfig) {
138         super();
139         this.sslcontext = sslcontext != null ? sslcontext : SSLContexts.createSystemDefault();
140         this.sslHandler = sslHandler;
141         this.incomingContentStrategy = incomingContentStrategy;
142         this.outgoingContentStrategy = outgoingContentStrategy;
143         this.responseParserFactory = responseParserFactory;
144         this.requestWriterFactory = requestWriterFactory;
145         this.allocator = allocator;
146         this.cconfig = cconfig != null ? cconfig : ConnectionConfig.DEFAULT;
147     }
148 
149     /**
150      * @since 4.3
151      */
152     public SSLNHttpClientConnectionFactory(
153             final SSLContext sslcontext,
154             final SSLSetupHandler sslHandler,
155             final NHttpMessageParserFactory<HttpResponse> responseParserFactory,
156             final NHttpMessageWriterFactory<HttpRequest> requestWriterFactory,
157             final ByteBufferAllocator allocator,
158             final ConnectionConfig cconfig) {
159         this(sslcontext, sslHandler,
160                 null, null, responseParserFactory, requestWriterFactory, allocator, cconfig);
161     }
162 
163     /**
164      * @since 4.3
165      */
166     public SSLNHttpClientConnectionFactory(
167             final SSLContext sslcontext,
168             final SSLSetupHandler sslHandler,
169             final NHttpMessageParserFactory<HttpResponse> responseParserFactory,
170             final NHttpMessageWriterFactory<HttpRequest> requestWriterFactory,
171             final ConnectionConfig cconfig) {
172         this(sslcontext, sslHandler,
173                 null, null, responseParserFactory, requestWriterFactory, null, cconfig);
174     }
175 
176     /**
177      * @since 4.3
178      */
179     public SSLNHttpClientConnectionFactory(
180             final SSLContext sslcontext,
181             final SSLSetupHandler sslHandler,
182             final ConnectionConfig config) {
183         this(sslcontext, sslHandler, null, null, null, null, null, config);
184     }
185 
186     /**
187      * @since 4.3
188      */
189     public SSLNHttpClientConnectionFactory(final ConnectionConfig config) {
190         this(null, null, null, null, null, null, null, config);
191     }
192 
193     /**
194      * @since 4.3
195      */
196     public SSLNHttpClientConnectionFactory() {
197         this(null, null, null, null, null, null);
198     }
199 
200     /**
201      * @deprecated (4.3) no longer used.
202      */
203     @Deprecated
204     protected DefaultNHttpClientConnection createConnection(
205             final IOSession session,
206             final HttpResponseFactory responseFactory,
207             final ByteBufferAllocator allocator,
208             final HttpParams params) {
209         return new DefaultNHttpClientConnection(session, responseFactory, allocator, params);
210     }
211 
212     /**
213      * @since 4.3
214      */
215     protected SSLIOSession createSSLIOSession(
216             final IOSession iosession,
217             final SSLContext sslcontext,
218             final SSLSetupHandler sslHandler) {
219         final SSLIOSession ssliosession = new SSLIOSession(iosession, SSLMode.CLIENT,
220                 sslcontext, sslHandler);
221         return ssliosession;
222     }
223 
224     @Override
225     public DefaultNHttpClientConnection createConnection(final IOSession iosession) {
226         final SSLIOSession ssliosession = createSSLIOSession(iosession, this.sslcontext, this.sslHandler);
227         iosession.setAttribute(SSLIOSession.SESSION_KEY, ssliosession);
228         return new DefaultNHttpClientConnection(
229                 ssliosession,
230                 this.cconfig.getBufferSize(),
231                 this.cconfig.getFragmentSizeHint(),
232                 this.allocator,
233                 ConnSupport.createDecoder(this.cconfig),
234                 ConnSupport.createEncoder(this.cconfig),
235                 this.cconfig.getMessageConstraints(),
236                 this.incomingContentStrategy,
237                 this.outgoingContentStrategy,
238                 this.requestWriterFactory,
239                 this.responseParserFactory);
240     }
241 
242 }