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