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 org.apache.http.HttpRequest;
030import org.apache.http.HttpRequestFactory;
031import org.apache.http.HttpResponse;
032import org.apache.http.annotation.ThreadingBehavior;
033import org.apache.http.annotation.Contract;
034import org.apache.http.config.ConnectionConfig;
035import org.apache.http.entity.ContentLengthStrategy;
036import org.apache.http.impl.ConnSupport;
037import org.apache.http.impl.DefaultHttpRequestFactory;
038import org.apache.http.impl.nio.codecs.DefaultHttpRequestParserFactory;
039import org.apache.http.nio.NHttpConnectionFactory;
040import org.apache.http.nio.NHttpMessageParserFactory;
041import org.apache.http.nio.NHttpMessageWriterFactory;
042import org.apache.http.nio.reactor.IOSession;
043import org.apache.http.nio.util.ByteBufferAllocator;
044import org.apache.http.nio.util.HeapByteBufferAllocator;
045import org.apache.http.params.HttpParamConfig;
046import org.apache.http.params.HttpParams;
047import org.apache.http.util.Args;
048
049/**
050 * Default factory for plain (non-encrypted), non-blocking
051 * {@link org.apache.http.nio.NHttpServerConnection}s.
052 *
053 * @since 4.2
054 */
055@SuppressWarnings("deprecation")
056@Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
057public class DefaultNHttpServerConnectionFactory
058    implements NHttpConnectionFactory<DefaultNHttpServerConnection> {
059
060    private final ContentLengthStrategy incomingContentStrategy;
061    private final ContentLengthStrategy outgoingContentStrategy;
062    private final NHttpMessageParserFactory<HttpRequest> requestParserFactory;
063    private final NHttpMessageWriterFactory<HttpResponse> responseWriterFactory;
064    private final ByteBufferAllocator allocator;
065    private final ConnectionConfig cconfig;
066
067    /**
068     * @deprecated (4.3) use {@link
069     *   DefaultNHttpServerConnectionFactory#DefaultNHttpServerConnectionFactory(
070     *      ByteBufferAllocator, NHttpMessageParserFactory, NHttpMessageWriterFactory,
071     *      ConnectionConfig)}
072     */
073    @Deprecated
074    public DefaultNHttpServerConnectionFactory(
075            final HttpRequestFactory requestFactory,
076            final ByteBufferAllocator allocator,
077            final HttpParams params) {
078        super();
079        Args.notNull(requestFactory, "HTTP request factory");
080        Args.notNull(allocator, "Byte buffer allocator");
081        Args.notNull(params, "HTTP parameters");
082        this.incomingContentStrategy = null;
083        this.outgoingContentStrategy = null;
084        this.requestParserFactory = new DefaultHttpRequestParserFactory(null, requestFactory);
085        this.responseWriterFactory = null;
086        this.allocator = allocator;
087        this.cconfig = HttpParamConfig.getConnectionConfig(params);
088    }
089
090    /**
091     * @deprecated (4.3) use {@link
092     *   DefaultNHttpServerConnectionFactory#DefaultNHttpServerConnectionFactory(ConnectionConfig)}
093     */
094    @Deprecated
095    public DefaultNHttpServerConnectionFactory(final HttpParams params) {
096        this(DefaultHttpRequestFactory.INSTANCE, HeapByteBufferAllocator.INSTANCE, params);
097    }
098
099    /**
100     * @deprecated (4.3) no longer used.
101     */
102    @Deprecated
103    protected DefaultNHttpServerConnection createConnection(
104            final IOSession session,
105            final HttpRequestFactory requestFactory,
106            final ByteBufferAllocator allocator,
107            final HttpParams params) {
108        return new DefaultNHttpServerConnection(session, requestFactory, allocator, params);
109    }
110
111    /**
112     * @since 4.3
113     */
114    public DefaultNHttpServerConnectionFactory(
115            final ContentLengthStrategy incomingContentStrategy,
116            final ContentLengthStrategy outgoingContentStrategy,
117            final NHttpMessageParserFactory<HttpRequest> requestParserFactory,
118            final NHttpMessageWriterFactory<HttpResponse> responseWriterFactory,
119            final ByteBufferAllocator allocator,
120            final ConnectionConfig cconfig) {
121        super();
122        this.incomingContentStrategy = incomingContentStrategy;
123        this.outgoingContentStrategy = outgoingContentStrategy;
124        this.requestParserFactory = requestParserFactory;
125        this.responseWriterFactory = responseWriterFactory;
126        this.allocator = allocator;
127        this.cconfig = cconfig != null ? cconfig : ConnectionConfig.DEFAULT;
128    }
129
130    /**
131     * @since 4.3
132     */
133    public DefaultNHttpServerConnectionFactory(
134            final ByteBufferAllocator allocator,
135            final NHttpMessageParserFactory<HttpRequest> requestParserFactory,
136            final NHttpMessageWriterFactory<HttpResponse> responseWriterFactory,
137            final ConnectionConfig cconfig) {
138        this(null, null, requestParserFactory, responseWriterFactory, allocator, cconfig);
139    }
140
141    /**
142     * @since 4.3
143     */
144    public DefaultNHttpServerConnectionFactory(final ConnectionConfig config) {
145        this(null, null, null, null, null, config);
146    }
147
148    /**
149     * @since 4.3
150     */
151    public DefaultNHttpServerConnectionFactory() {
152        this(null, null, null, null, null, null);
153    }
154
155    @Override
156    public DefaultNHttpServerConnection createConnection(final IOSession session) {
157        return new DefaultNHttpServerConnection(session,
158                this.cconfig.getBufferSize(),
159                this.cconfig.getFragmentSizeHint(),
160                this.allocator,
161                ConnSupport.createDecoder(this.cconfig),
162                ConnSupport.createEncoder(this.cconfig),
163                this.cconfig.getMessageConstraints(),
164                this.incomingContentStrategy,
165                this.outgoingContentStrategy,
166                this.requestParserFactory,
167                this.responseWriterFactory);
168    }
169
170}