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