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 java.nio.charset.Charset;
30  import java.nio.charset.CharsetDecoder;
31  import java.nio.charset.CharsetEncoder;
32  import java.nio.charset.CodingErrorAction;
33  
34  import javax.net.ssl.SSLContext;
35  
36  import org.apache.http.HttpRequest;
37  import org.apache.http.HttpRequestFactory;
38  import org.apache.http.annotation.Immutable;
39  import org.apache.http.config.ConnectionConfig;
40  import org.apache.http.impl.DefaultHttpRequestFactory;
41  import org.apache.http.impl.nio.codecs.DefaultHttpRequestParserFactory;
42  import org.apache.http.nio.NHttpConnectionFactory;
43  import org.apache.http.nio.NHttpMessageParserFactory;
44  import org.apache.http.nio.NHttpServerConnection;
45  import org.apache.http.nio.reactor.IOSession;
46  import org.apache.http.nio.reactor.ssl.SSLIOSession;
47  import org.apache.http.nio.reactor.ssl.SSLMode;
48  import org.apache.http.nio.reactor.ssl.SSLSetupHandler;
49  import org.apache.http.nio.util.ByteBufferAllocator;
50  import org.apache.http.nio.util.HeapByteBufferAllocator;
51  import org.apache.http.params.HttpParamConfig;
52  import org.apache.http.params.HttpParams;
53  import org.apache.http.util.Args;
54  
55  /**
56   * Default factory for SSL encrypted, non-blocking {@link 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 NHttpMessageParserFactory<HttpRequest> requestParserFactory;
66      private final ByteBufferAllocator allocator;
67      private final SSLContext sslcontext;
68      private final SSLSetupHandler sslHandler;
69      private final ConnectionConfig config;
70  
71      /**
72       * @deprecated (4.3) use {@link
73       *   SSLNHttpServerConnectionFactory#SSLNHttpServerConnectionFactory(SSLContext,
74       *      SSLSetupHandler, NHttpMessageParserFactory, ByteBufferAllocator, ConnectionConfig)}
75       */
76      @Deprecated
77      public SSLNHttpServerConnectionFactory(
78              final SSLContext sslcontext,
79              final SSLSetupHandler sslHandler,
80              final HttpRequestFactory requestFactory,
81              final ByteBufferAllocator allocator,
82              final HttpParams params) {
83          super();
84          Args.notNull(requestFactory, "HTTP request factory");
85          Args.notNull(allocator, "Byte buffer allocator");
86          Args.notNull(params, "HTTP parameters");
87          this.sslcontext = sslcontext;
88          this.sslHandler = sslHandler;
89          this.allocator = allocator;
90          this.requestParserFactory = new DefaultHttpRequestParserFactory(null, requestFactory);
91          this.config = HttpParamConfig.getConnectionConfig(params);
92      }
93  
94      /**
95       * @deprecated (4.3) use {@link
96       *   SSLNHttpServerConnectionFactory#SSLNHttpServerConnectionFactory(SSLContext,
97       *     SSLSetupHandler, ConnectionConfig)}
98       */
99      @Deprecated
100     public SSLNHttpServerConnectionFactory(
101             final SSLContext sslcontext,
102             final SSLSetupHandler sslHandler,
103             final HttpParams params) {
104         this(sslcontext, sslHandler, DefaultHttpRequestFactory.INSTANCE,
105                 HeapByteBufferAllocator.INSTANCE, params);
106     }
107 
108     /**
109      * @deprecated (4.3) use {@link
110      *   SSLNHttpServerConnectionFactory#SSLNHttpServerConnectionFactory(ConnectionConfig)}
111      */
112     @Deprecated
113     public SSLNHttpServerConnectionFactory(final HttpParams params) {
114         this(null, null, params);
115     }
116 
117     /**
118      * @since 4.3
119      */
120     public SSLNHttpServerConnectionFactory(
121             final SSLContext sslcontext,
122             final SSLSetupHandler sslHandler,
123             final NHttpMessageParserFactory<HttpRequest> requestParserFactory,
124             final ByteBufferAllocator allocator,
125             final ConnectionConfig config) {
126         super();
127         this.sslcontext = sslcontext;
128         this.sslHandler = sslHandler;
129         this.allocator = allocator != null ? allocator : HeapByteBufferAllocator.INSTANCE;
130         this.requestParserFactory = requestParserFactory != null ? requestParserFactory :
131             DefaultHttpRequestParserFactory.INSTANCE;
132         this.config = config != null ? config : ConnectionConfig.DEFAULT;
133     }
134 
135     /**
136      * @since 4.3
137      */
138     public SSLNHttpServerConnectionFactory(
139             final SSLContext sslcontext,
140             final SSLSetupHandler sslHandler,
141             final ConnectionConfig config) {
142         this(sslcontext, sslHandler, null, null, config);
143     }
144 
145     /**
146      * @since 4.3
147      */
148     public SSLNHttpServerConnectionFactory(final ConnectionConfig config) {
149         this(null, null, null, null, config);
150     }
151 
152     private SSLContext getDefaultSSLContext() {
153         SSLContext sslcontext;
154         try {
155             sslcontext = SSLContext.getInstance("TLS");
156             sslcontext.init(null, null, null);
157         } catch (final Exception ex) {
158             throw new IllegalStateException("Failure initializing default SSL context", ex);
159         }
160         return sslcontext;
161     }
162 
163     /**
164      * @deprecated (4.3) no longer used.
165      */
166     @Deprecated
167     protected DefaultNHttpServerConnection createConnection(
168             final IOSession session,
169             final HttpRequestFactory requestFactory,
170             final ByteBufferAllocator allocator,
171             final HttpParams params) {
172         return new DefaultNHttpServerConnection(session, requestFactory, allocator, params);
173     }
174 
175     /**
176      * @since 4.3
177      */
178     protected SSLIOSession createSSLIOSession(
179             final IOSession iosession,
180             final SSLContext sslcontext,
181             final SSLSetupHandler sslHandler) {
182         final SSLIOSession ssliosession = new SSLIOSession(iosession, SSLMode.SERVER,
183                 (sslcontext != null ? sslcontext : getDefaultSSLContext()),
184                 sslHandler);
185         iosession.setAttribute(SSLIOSession.SESSION_KEY, ssliosession);
186         return ssliosession;
187     }
188 
189     public DefaultNHttpServerConnection createConnection(final IOSession iosession) {
190         final SSLIOSession ssliosession = createSSLIOSession(iosession, this.sslcontext, this.sslHandler);
191         CharsetDecoder chardecoder = null;
192         CharsetEncoder charencoder = null;
193         final Charset charset = this.config.getCharset();
194         final CodingErrorAction malformedInputAction = this.config.getMalformedInputAction() != null ?
195                 this.config.getMalformedInputAction() : CodingErrorAction.REPORT;
196         final CodingErrorAction unmappableInputAction = this.config.getUnmappableInputAction() != null ?
197                 this.config.getUnmappableInputAction() : CodingErrorAction.REPORT;
198         if (charset != null) {
199             chardecoder = charset.newDecoder();
200             chardecoder.onMalformedInput(malformedInputAction);
201             chardecoder.onUnmappableCharacter(unmappableInputAction);
202             charencoder = charset.newEncoder();
203             charencoder.onMalformedInput(malformedInputAction);
204             charencoder.onUnmappableCharacter(unmappableInputAction);
205         }
206         return new DefaultNHttpServerConnection(ssliosession,
207                 this.config.getBufferSize(),
208                 this.config.getFragmentSizeHint(),
209                 this.allocator,
210                 chardecoder, charencoder, this.config.getMessageConstraints(),
211                 null, null,
212                 this.requestParserFactory,
213                 null);
214     }
215 
216 }