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.HttpResponse; 031import org.apache.http.HttpResponseFactory; 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.DefaultHttpResponseFactory; 038import org.apache.http.impl.nio.codecs.DefaultHttpResponseParserFactory; 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.NHttpClientConnection}s. 052 * 053 * @since 4.2 054 */ 055@SuppressWarnings("deprecation") 056@Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL) 057public class DefaultNHttpClientConnectionFactory 058 implements NHttpConnectionFactory<DefaultNHttpClientConnection> { 059 060 public static final DefaultNHttpClientConnectionFactory INSTANCE = new DefaultNHttpClientConnectionFactory(); 061 062 private final ContentLengthStrategy incomingContentStrategy; 063 private final ContentLengthStrategy outgoingContentStrategy; 064 private final NHttpMessageParserFactory<HttpResponse> responseParserFactory; 065 private final NHttpMessageWriterFactory<HttpRequest> requestWriterFactory; 066 private final ByteBufferAllocator allocator; 067 private final ConnectionConfig cconfig; 068 069 /** 070 * @deprecated (4.3) use {@link 071 * DefaultNHttpClientConnectionFactory#DefaultNHttpClientConnectionFactory( 072 * NHttpMessageParserFactory, NHttpMessageWriterFactory, ByteBufferAllocator, 073 * ConnectionConfig)} 074 */ 075 @Deprecated 076 public DefaultNHttpClientConnectionFactory( 077 final HttpResponseFactory responseFactory, 078 final ByteBufferAllocator allocator, 079 final HttpParams params) { 080 super(); 081 Args.notNull(responseFactory, "HTTP response factory"); 082 Args.notNull(allocator, "Byte buffer allocator"); 083 Args.notNull(params, "HTTP parameters"); 084 this.allocator = allocator; 085 this.incomingContentStrategy = null; 086 this.outgoingContentStrategy = null; 087 this.responseParserFactory = new DefaultHttpResponseParserFactory(null, responseFactory); 088 this.requestWriterFactory = null; 089 this.cconfig = HttpParamConfig.getConnectionConfig(params); 090 } 091 092 /** 093 * @deprecated (4.3) use {@link 094 * DefaultNHttpClientConnectionFactory#DefaultNHttpClientConnectionFactory( 095 * ConnectionConfig)} 096 */ 097 @Deprecated 098 public DefaultNHttpClientConnectionFactory(final HttpParams params) { 099 this(DefaultHttpResponseFactory.INSTANCE, HeapByteBufferAllocator.INSTANCE, params); 100 } 101 102 /** 103 * @since 4.3 104 */ 105 public DefaultNHttpClientConnectionFactory( 106 final ContentLengthStrategy incomingContentStrategy, 107 final ContentLengthStrategy outgoingContentStrategy, 108 final NHttpMessageParserFactory<HttpResponse> responseParserFactory, 109 final NHttpMessageWriterFactory<HttpRequest> requestWriterFactory, 110 final ByteBufferAllocator allocator, 111 final ConnectionConfig cconfig) { 112 super(); 113 this.incomingContentStrategy = incomingContentStrategy; 114 this.outgoingContentStrategy = outgoingContentStrategy; 115 this.responseParserFactory = responseParserFactory; 116 this.requestWriterFactory = requestWriterFactory; 117 this.allocator = allocator; 118 this.cconfig = cconfig != null ? cconfig : ConnectionConfig.DEFAULT; 119 } 120 121 /** 122 * @since 4.3 123 */ 124 public DefaultNHttpClientConnectionFactory( 125 final NHttpMessageParserFactory<HttpResponse> responseParserFactory, 126 final NHttpMessageWriterFactory<HttpRequest> requestWriterFactory, 127 final ByteBufferAllocator allocator, 128 final ConnectionConfig cconfig) { 129 this(null, null, responseParserFactory, requestWriterFactory, allocator, cconfig); 130 } 131 132 /** 133 * @since 4.3 134 */ 135 public DefaultNHttpClientConnectionFactory( 136 final NHttpMessageParserFactory<HttpResponse> responseParserFactory, 137 final NHttpMessageWriterFactory<HttpRequest> requestWriterFactory, 138 final ConnectionConfig cconfig) { 139 this(null, null, responseParserFactory, requestWriterFactory, null, cconfig); 140 } 141 142 /** 143 * @since 4.3 144 */ 145 public DefaultNHttpClientConnectionFactory(final ConnectionConfig cconfig) { 146 this(null, null, null, null, null, cconfig); 147 } 148 149 /** 150 * @since 4.3 151 */ 152 public DefaultNHttpClientConnectionFactory() { 153 this(null, null, null, null, null, null); 154 } 155 156 /** 157 * @deprecated (4.3) no longer used. 158 */ 159 @Deprecated 160 protected DefaultNHttpClientConnection createConnection( 161 final IOSession session, 162 final HttpResponseFactory responseFactory, 163 final ByteBufferAllocator allocator, 164 final HttpParams params) { 165 return new DefaultNHttpClientConnection(session, responseFactory, allocator, params); 166 } 167 168 @Override 169 public DefaultNHttpClientConnection createConnection(final IOSession session) { 170 return new DefaultNHttpClientConnection( 171 session, 172 this.cconfig.getBufferSize(), 173 this.cconfig.getFragmentSizeHint(), 174 this.allocator, 175 ConnSupport.createDecoder(this.cconfig), 176 ConnSupport.createEncoder(this.cconfig), 177 this.cconfig.getMessageConstraints(), 178 this.incomingContentStrategy, 179 this.outgoingContentStrategy, 180 this.requestWriterFactory, 181 this.responseParserFactory); 182 } 183 184}