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