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