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.HttpClientConnection}s. 045 * 046 * @since 4.3 047 */ 048@Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL) 049public class DefaultBHttpClientConnectionFactory implements HttpConnectionFactory<DefaultBHttpClientConnection> { 050 051 public static final DefaultBHttpClientConnectionFactory INSTANCE = new DefaultBHttpClientConnectionFactory(); 052 053 private final ConnectionConfig cconfig; 054 private final ContentLengthStrategy incomingContentStrategy; 055 private final ContentLengthStrategy outgoingContentStrategy; 056 private final HttpMessageWriterFactory<HttpRequest> requestWriterFactory; 057 private final HttpMessageParserFactory<HttpResponse> responseParserFactory; 058 059 public DefaultBHttpClientConnectionFactory( 060 final ConnectionConfig cconfig, 061 final ContentLengthStrategy incomingContentStrategy, 062 final ContentLengthStrategy outgoingContentStrategy, 063 final HttpMessageWriterFactory<HttpRequest> requestWriterFactory, 064 final HttpMessageParserFactory<HttpResponse> responseParserFactory) { 065 super(); 066 this.cconfig = cconfig != null ? cconfig : ConnectionConfig.DEFAULT; 067 this.incomingContentStrategy = incomingContentStrategy; 068 this.outgoingContentStrategy = outgoingContentStrategy; 069 this.requestWriterFactory = requestWriterFactory; 070 this.responseParserFactory = responseParserFactory; 071 } 072 073 public DefaultBHttpClientConnectionFactory( 074 final ConnectionConfig cconfig, 075 final HttpMessageWriterFactory<HttpRequest> requestWriterFactory, 076 final HttpMessageParserFactory<HttpResponse> responseParserFactory) { 077 this(cconfig, null, null, requestWriterFactory, responseParserFactory); 078 } 079 080 public DefaultBHttpClientConnectionFactory(final ConnectionConfig cconfig) { 081 this(cconfig, null, null, null, null); 082 } 083 084 public DefaultBHttpClientConnectionFactory() { 085 this(null, null, null, null, null); 086 } 087 088 @Override 089 public DefaultBHttpClientConnection createConnection(final Socket socket) throws IOException { 090 final DefaultBHttpClientConnection conn = new DefaultBHttpClientConnection( 091 this.cconfig.getBufferSize(), 092 this.cconfig.getFragmentSizeHint(), 093 ConnSupport.createDecoder(this.cconfig), 094 ConnSupport.createEncoder(this.cconfig), 095 this.cconfig.getMessageConstraints(), 096 this.incomingContentStrategy, 097 this.outgoingContentStrategy, 098 this.requestWriterFactory, 099 this.responseParserFactory); 100 conn.bind(socket); 101 return conn; 102 } 103 104}