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