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 */
027
028package org.apache.http.impl;
029
030import org.apache.http.HttpConnectionFactory;
031import org.apache.http.HttpRequest;
032import org.apache.http.HttpResponse;
033import org.apache.http.annotation.ThreadingBehavior;
034import org.apache.http.annotation.Contract;
035import org.apache.http.config.ConnectionConfig;
036import org.apache.http.entity.ContentLengthStrategy;
037import org.apache.http.io.HttpMessageParserFactory;
038import org.apache.http.io.HttpMessageWriterFactory;
039
040import java.io.IOException;
041import java.net.Socket;
042
043/**
044 * Default factory for {@link org.apache.http.HttpServerConnection}s.
045 *
046 * @since 4.3
047 */
048@Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
049public class DefaultBHttpServerConnectionFactory
050        implements HttpConnectionFactory<DefaultBHttpServerConnection> {
051
052    public static final DefaultBHttpServerConnectionFactory INSTANCE = new DefaultBHttpServerConnectionFactory();
053
054    private final ConnectionConfig cconfig;
055    private final ContentLengthStrategy incomingContentStrategy;
056    private final ContentLengthStrategy outgoingContentStrategy;
057    private final HttpMessageParserFactory<HttpRequest> requestParserFactory;
058    private final HttpMessageWriterFactory<HttpResponse> responseWriterFactory;
059
060    public DefaultBHttpServerConnectionFactory(
061            final ConnectionConfig cconfig,
062            final ContentLengthStrategy incomingContentStrategy,
063            final ContentLengthStrategy outgoingContentStrategy,
064            final HttpMessageParserFactory<HttpRequest> requestParserFactory,
065            final HttpMessageWriterFactory<HttpResponse> responseWriterFactory) {
066        super();
067        this.cconfig = cconfig != null ? cconfig : ConnectionConfig.DEFAULT;
068        this.incomingContentStrategy = incomingContentStrategy;
069        this.outgoingContentStrategy = outgoingContentStrategy;
070        this.requestParserFactory = requestParserFactory;
071        this.responseWriterFactory = responseWriterFactory;
072    }
073
074    public DefaultBHttpServerConnectionFactory(
075            final ConnectionConfig cconfig,
076            final HttpMessageParserFactory<HttpRequest> requestParserFactory,
077            final HttpMessageWriterFactory<HttpResponse> responseWriterFactory) {
078        this(cconfig, null, null, requestParserFactory, responseWriterFactory);
079    }
080
081    public DefaultBHttpServerConnectionFactory(final ConnectionConfig cconfig) {
082        this(cconfig, null, null, null, null);
083    }
084
085    public DefaultBHttpServerConnectionFactory() {
086        this(null, null, null, null, null);
087    }
088
089    @Override
090    public DefaultBHttpServerConnection createConnection(final Socket socket) throws IOException {
091        final DefaultBHttpServerConnection conn = new DefaultBHttpServerConnection(
092                this.cconfig.getBufferSize(),
093                this.cconfig.getFragmentSizeHint(),
094                ConnSupport.createDecoder(this.cconfig),
095                ConnSupport.createEncoder(this.cconfig),
096                this.cconfig.getMessageConstraints(),
097                this.incomingContentStrategy,
098                this.outgoingContentStrategy,
099                this.requestParserFactory,
100                this.responseWriterFactory);
101        conn.bind(socket);
102        return conn;
103    }
104
105}