001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027package org.apache.http.impl.nio;
028
029import javax.net.ssl.SSLContext;
030
031import org.apache.http.HttpRequest;
032import org.apache.http.HttpRequestFactory;
033import org.apache.http.HttpResponse;
034import org.apache.http.annotation.ThreadingBehavior;
035import org.apache.http.annotation.Contract;
036import org.apache.http.config.ConnectionConfig;
037import org.apache.http.entity.ContentLengthStrategy;
038import org.apache.http.impl.ConnSupport;
039import org.apache.http.impl.DefaultHttpRequestFactory;
040import org.apache.http.impl.nio.codecs.DefaultHttpRequestParserFactory;
041import org.apache.http.nio.NHttpConnectionFactory;
042import org.apache.http.nio.NHttpMessageParserFactory;
043import org.apache.http.nio.NHttpMessageWriterFactory;
044import org.apache.http.nio.reactor.IOSession;
045import org.apache.http.nio.reactor.ssl.SSLIOSession;
046import org.apache.http.nio.reactor.ssl.SSLMode;
047import org.apache.http.nio.reactor.ssl.SSLSetupHandler;
048import org.apache.http.nio.util.ByteBufferAllocator;
049import org.apache.http.nio.util.HeapByteBufferAllocator;
050import org.apache.http.params.HttpParamConfig;
051import org.apache.http.params.HttpParams;
052import org.apache.http.ssl.SSLContexts;
053import org.apache.http.util.Args;
054
055/**
056 * Default factory for SSL encrypted, non-blocking
057 * {@link org.apache.http.nio.NHttpServerConnection}s.
058 *
059 * @since 4.2
060 */
061@SuppressWarnings("deprecation")
062@Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
063public class SSLNHttpServerConnectionFactory
064    implements NHttpConnectionFactory<DefaultNHttpServerConnection> {
065
066    private final SSLContext sslContext;
067    private final SSLSetupHandler sslHandler;
068    private final ContentLengthStrategy incomingContentStrategy;
069    private final ContentLengthStrategy outgoingContentStrategy;
070    private final NHttpMessageParserFactory<HttpRequest> requestParserFactory;
071    private final NHttpMessageWriterFactory<HttpResponse> responseWriterFactory;
072    private final ByteBufferAllocator allocator;
073    private final ConnectionConfig cconfig;
074
075    /**
076     * @deprecated (4.3) use {@link
077     *   SSLNHttpServerConnectionFactory#SSLNHttpServerConnectionFactory(SSLContext,
078     *      SSLSetupHandler, NHttpMessageParserFactory, NHttpMessageWriterFactory,
079     *      ByteBufferAllocator, ConnectionConfig)}
080     */
081    @Deprecated
082    public SSLNHttpServerConnectionFactory(
083            final SSLContext sslContext,
084            final SSLSetupHandler sslHandler,
085            final HttpRequestFactory requestFactory,
086            final ByteBufferAllocator allocator,
087            final HttpParams params) {
088        super();
089        Args.notNull(requestFactory, "HTTP request factory");
090        Args.notNull(allocator, "Byte buffer allocator");
091        Args.notNull(params, "HTTP parameters");
092        this.sslContext = sslContext != null ? sslContext : SSLContexts.createSystemDefault();
093        this.sslHandler = sslHandler;
094        this.incomingContentStrategy = null;
095        this.outgoingContentStrategy = null;
096        this.requestParserFactory = new DefaultHttpRequestParserFactory(null, requestFactory);
097        this.responseWriterFactory = null;
098        this.allocator = allocator;
099        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}