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