HTTPCLIENT-1374: Added route parameter to HttpConnectionFactory

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1493350 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2013-06-15 12:50:56 +00:00
parent e208df7fc7
commit dc3b8d47fb
8 changed files with 66 additions and 93 deletions

View File

@ -130,7 +130,7 @@ public class ClientConfiguration {
// initialization of outgoing HTTP connections. Beside standard connection
// configuration parameters HTTP connection factory can define message
// parser / writer routines to be employed by individual connections.
HttpConnectionFactory<ManagedHttpClientConnection> connFactory = new ManagedHttpClientConnectionFactory(
HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory = new ManagedHttpClientConnectionFactory(
requestWriterFactory, responseParserFactory);
// Client HTTP connection objects when fully initialized can be bound to

View File

@ -34,8 +34,8 @@ import org.apache.http.config.ConnectionConfig;
*
* @since 4.3
*/
public interface HttpConnectionFactory<T extends HttpConnection> {
public interface HttpConnectionFactory<T, C extends HttpConnection> {
T create(ConnectionConfig config);
C create(T route, ConnectionConfig config);
}

View File

@ -79,7 +79,7 @@ import org.apache.http.util.EntityUtils;
@SuppressWarnings("deprecation")
public class ProxyClient {
private final HttpConnectionFactory<ManagedHttpClientConnection> connFactory;
private final HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory;
private final ConnectionConfig connectionConfig;
private final RequestConfig requestConfig;
private final HttpProcessor httpProcessor;
@ -94,7 +94,7 @@ public class ProxyClient {
* @since 4.3
*/
public ProxyClient(
final HttpConnectionFactory<ManagedHttpClientConnection> connFactory,
final HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory,
final ConnectionConfig connectionConfig,
final RequestConfig requestConfig) {
super();
@ -172,7 +172,8 @@ public class ProxyClient {
this.requestConfig.getLocalAddress(),
proxy, false, TunnelType.TUNNELLED, LayerType.PLAIN);
final ManagedHttpClientConnection conn = this.connFactory.create(this.connectionConfig);
final ManagedHttpClientConnection conn = this.connFactory.create(
route, this.connectionConfig);
final HttpContext context = new BasicHttpContext();
HttpResponse response = null;

View File

@ -81,7 +81,7 @@ public class BasicHttpClientConnectionManager implements HttpClientConnectionMan
private final Log log = LogFactory.getLog(getClass());
private final HttpClientConnectionOperator connectionOperator;
private final HttpConnectionFactory<ManagedHttpClientConnection> connFactory;
private final HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory;
@GuardedBy("this")
private ManagedHttpClientConnection conn;
@ -119,7 +119,7 @@ public class BasicHttpClientConnectionManager implements HttpClientConnectionMan
public BasicHttpClientConnectionManager(
final Lookup<ConnectionSocketFactory> socketFactoryRegistry,
final HttpConnectionFactory<ManagedHttpClientConnection> connFactory,
final HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory,
final SchemePortResolver schemePortResolver,
final DnsResolver dnsResolver) {
super();
@ -133,7 +133,7 @@ public class BasicHttpClientConnectionManager implements HttpClientConnectionMan
public BasicHttpClientConnectionManager(
final Lookup<ConnectionSocketFactory> socketFactoryRegistry,
final HttpConnectionFactory<ManagedHttpClientConnection> connFactory) {
final HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory) {
this(socketFactoryRegistry, connFactory, null, null);
}
@ -252,7 +252,7 @@ public class BasicHttpClientConnectionManager implements HttpClientConnectionMan
this.state = state;
checkExpiry();
if (this.conn == null) {
this.conn = this.connFactory.create(this.connConfig);
this.conn = this.connFactory.create(route, this.connConfig);
}
this.leased = true;
return this.conn;

View File

@ -41,6 +41,7 @@ import org.apache.http.annotation.Immutable;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.conn.HttpConnectionFactory;
import org.apache.http.conn.ManagedHttpClientConnection;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.impl.io.DefaultHttpRequestWriterFactory;
import org.apache.http.io.HttpMessageParserFactory;
import org.apache.http.io.HttpMessageWriterFactory;
@ -49,7 +50,8 @@ import org.apache.http.io.HttpMessageWriterFactory;
* @since 4.3
*/
@Immutable
public class ManagedHttpClientConnectionFactory implements HttpConnectionFactory<ManagedHttpClientConnection> {
public class ManagedHttpClientConnectionFactory
implements HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> {
private static final AtomicLong COUNTER = new AtomicLong();
@ -81,7 +83,7 @@ public class ManagedHttpClientConnectionFactory implements HttpConnectionFactory
this(null, null);
}
public ManagedHttpClientConnection create(final ConnectionConfig config) {
public ManagedHttpClientConnection create(final HttpRoute route, final ConnectionConfig config) {
final ConnectionConfig cconfig = config != null ? config : ConnectionConfig.DEFAULT;
CharsetDecoder chardecoder = null;
CharsetEncoder charencoder = null;

View File

@ -120,25 +120,25 @@ public class PoolingHttpClientConnectionManager
public PoolingHttpClientConnectionManager(
final Registry<ConnectionSocketFactory> socketFactoryRegistry,
final HttpConnectionFactory<ManagedHttpClientConnection> connFactory) {
final HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory) {
this(socketFactoryRegistry, connFactory, null);
}
public PoolingHttpClientConnectionManager(
final HttpConnectionFactory<ManagedHttpClientConnection> connFactory) {
final HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory) {
this(getDefaultRegistry(), connFactory, null);
}
public PoolingHttpClientConnectionManager(
final Registry<ConnectionSocketFactory> socketFactoryRegistry,
final HttpConnectionFactory<ManagedHttpClientConnection> connFactory,
final HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory,
final DnsResolver dnsResolver) {
this(socketFactoryRegistry, connFactory, null, dnsResolver, -1, TimeUnit.MILLISECONDS);
}
public PoolingHttpClientConnectionManager(
final Registry<ConnectionSocketFactory> socketFactoryRegistry,
final HttpConnectionFactory<ManagedHttpClientConnection> connFactory,
final HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory,
final SchemePortResolver schemePortResolver,
final DnsResolver dnsResolver,
final long timeToLive, final TimeUnit tunit) {
@ -477,11 +477,11 @@ public class PoolingHttpClientConnectionManager
static class InternalConnectionFactory implements ConnFactory<HttpRoute, ManagedHttpClientConnection> {
private final ConfigData configData;
private final HttpConnectionFactory<ManagedHttpClientConnection> connFactory;
private final HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory;
InternalConnectionFactory(
final ConfigData configData,
final HttpConnectionFactory<ManagedHttpClientConnection> connFactory) {
final HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory) {
super();
this.configData = configData != null ? configData : new ConfigData();
this.connFactory = connFactory != null ? connFactory :
@ -502,7 +502,7 @@ public class PoolingHttpClientConnectionManager
if (config == null) {
config = ConnectionConfig.DEFAULT;
}
return this.connFactory.create(config);
return this.connFactory.create(route, config);
}
}

View File

@ -46,7 +46,7 @@ import org.mockito.Mockito;
public class TestBasicHttpClientConnectionManager {
private ManagedHttpClientConnection conn;
private HttpConnectionFactory<ManagedHttpClientConnection> connFactory;
private HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory;
private Lookup<ConnectionSocketFactory> socketFactoryRegistry;
private BasicHttpClientConnectionManager mgr;
@ -64,7 +64,8 @@ public class TestBasicHttpClientConnectionManager {
final HttpHost target = new HttpHost("localhost");
final HttpRoute route = new HttpRoute(target);
Mockito.when(connFactory.create(Mockito.<ConnectionConfig>any())).thenReturn(conn);
Mockito.when(connFactory.create(
Mockito.eq(route), Mockito.<ConnectionConfig>any())).thenReturn(conn);
final ConnectionRequest connRequest1 = mgr.requestConnection(route, null);
final HttpClientConnection conn1 = connRequest1.get(0, TimeUnit.MILLISECONDS);
@ -81,7 +82,8 @@ public class TestBasicHttpClientConnectionManager {
Assert.assertNotNull(conn2);
Assert.assertFalse(conn2.isOpen());
Mockito.verify(connFactory, Mockito.times(2)).create(Mockito.<ConnectionConfig>any());
Mockito.verify(connFactory, Mockito.times(2)).create(
Mockito.eq(route), Mockito.<ConnectionConfig>any());
}
@Test
@ -89,13 +91,14 @@ public class TestBasicHttpClientConnectionManager {
final HttpHost target = new HttpHost("somehost");
final HttpRoute route = new HttpRoute(target);
Mockito.when(connFactory.create(Mockito.<ConnectionConfig>any())).thenReturn(conn);
Mockito.when(connFactory.create(Mockito.eq(route), Mockito.<ConnectionConfig>any())).thenReturn(conn);
final ConnectionRequest connRequest1 = mgr.requestConnection(route, null);
final HttpClientConnection conn1 = connRequest1.get(0, TimeUnit.MILLISECONDS);
Assert.assertNotNull(conn1);
Mockito.verify(connFactory, Mockito.times(1)).create(Mockito.<ConnectionConfig>any());
Mockito.verify(connFactory, Mockito.times(1)).create(
Mockito.eq(route), Mockito.<ConnectionConfig>any());
Mockito.when(conn.isOpen()).thenReturn(Boolean.TRUE);
@ -109,7 +112,8 @@ public class TestBasicHttpClientConnectionManager {
Assert.assertNotNull(conn2);
Assert.assertTrue(conn2.isOpen());
Mockito.verify(connFactory, Mockito.times(1)).create(Mockito.<ConnectionConfig>any());
Mockito.verify(connFactory, Mockito.times(1)).create(
Mockito.eq(route), Mockito.<ConnectionConfig>any());
}
@Test
@ -117,13 +121,15 @@ public class TestBasicHttpClientConnectionManager {
final HttpHost target = new HttpHost("somehost");
final HttpRoute route = new HttpRoute(target);
Mockito.when(connFactory.create(Mockito.<ConnectionConfig>any())).thenReturn(conn);
Mockito.when(connFactory.create(
Mockito.eq(route), Mockito.<ConnectionConfig>any())).thenReturn(conn);
final ConnectionRequest connRequest1 = mgr.requestConnection(route, "some state");
final HttpClientConnection conn1 = connRequest1.get(0, TimeUnit.MILLISECONDS);
Assert.assertNotNull(conn1);
Mockito.verify(connFactory, Mockito.times(1)).create(Mockito.<ConnectionConfig>any());
Mockito.verify(connFactory, Mockito.times(1)).create(
Mockito.eq(route), Mockito.<ConnectionConfig>any());
Mockito.when(conn.isOpen()).thenReturn(Boolean.TRUE);
@ -137,7 +143,8 @@ public class TestBasicHttpClientConnectionManager {
Assert.assertNotNull(conn2);
Assert.assertTrue(conn2.isOpen());
Mockito.verify(connFactory, Mockito.times(1)).create(Mockito.<ConnectionConfig>any());
Mockito.verify(connFactory, Mockito.times(1)).create(
Mockito.eq(route), Mockito.<ConnectionConfig>any());
}
@Test
@ -145,13 +152,15 @@ public class TestBasicHttpClientConnectionManager {
final HttpHost target1 = new HttpHost("somehost");
final HttpRoute route1 = new HttpRoute(target1);
Mockito.when(connFactory.create(Mockito.<ConnectionConfig>any())).thenReturn(conn);
Mockito.when(connFactory.create(
Mockito.<HttpRoute>any(), Mockito.<ConnectionConfig>any())).thenReturn(conn);
final ConnectionRequest connRequest1 = mgr.requestConnection(route1, null);
final HttpClientConnection conn1 = connRequest1.get(0, TimeUnit.MILLISECONDS);
Assert.assertNotNull(conn1);
Mockito.verify(connFactory, Mockito.times(1)).create(Mockito.<ConnectionConfig>any());
Mockito.verify(connFactory, Mockito.times(1)).create(
Mockito.eq(route1), Mockito.<ConnectionConfig>any());
Mockito.when(conn.isOpen()).thenReturn(Boolean.TRUE, Boolean.FALSE);
@ -168,7 +177,10 @@ public class TestBasicHttpClientConnectionManager {
Assert.assertFalse(conn2.isOpen());
Mockito.verify(conn).close();
Mockito.verify(connFactory, Mockito.times(2)).create(Mockito.<ConnectionConfig>any());
Mockito.verify(connFactory, Mockito.times(1)).create(
Mockito.eq(route1), Mockito.<ConnectionConfig>any());
Mockito.verify(connFactory, Mockito.times(1)).create(
Mockito.eq(route2), Mockito.<ConnectionConfig>any());
}
@Test
@ -176,13 +188,15 @@ public class TestBasicHttpClientConnectionManager {
final HttpHost target = new HttpHost("somehost");
final HttpRoute route = new HttpRoute(target);
Mockito.when(connFactory.create(Mockito.<ConnectionConfig>any())).thenReturn(conn);
Mockito.when(connFactory.create(
Mockito.eq(route), Mockito.<ConnectionConfig>any())).thenReturn(conn);
final ConnectionRequest connRequest1 = mgr.requestConnection(route, null);
final HttpClientConnection conn1 = connRequest1.get(0, TimeUnit.MILLISECONDS);
Assert.assertNotNull(conn1);
Mockito.verify(connFactory, Mockito.times(1)).create(Mockito.<ConnectionConfig>any());
Mockito.verify(connFactory, Mockito.times(1)).create(
Mockito.eq(route), Mockito.<ConnectionConfig>any());
Mockito.when(conn.isOpen()).thenReturn(Boolean.TRUE, Boolean.FALSE);
@ -199,7 +213,8 @@ public class TestBasicHttpClientConnectionManager {
Assert.assertFalse(conn2.isOpen());
Mockito.verify(conn).close();
Mockito.verify(connFactory, Mockito.times(2)).create(Mockito.<ConnectionConfig>any());
Mockito.verify(connFactory, Mockito.times(2)).create(
Mockito.eq(route), Mockito.<ConnectionConfig>any());
}
@Test(expected=IllegalArgumentException.class)
@ -223,13 +238,15 @@ public class TestBasicHttpClientConnectionManager {
final HttpHost target = new HttpHost("somehost");
final HttpRoute route = new HttpRoute(target);
Mockito.when(connFactory.create(Mockito.<ConnectionConfig>any())).thenReturn(conn);
Mockito.when(connFactory.create(
Mockito.eq(route), Mockito.<ConnectionConfig>any())).thenReturn(conn);
final ConnectionRequest connRequest1 = mgr.requestConnection(route, null);
final HttpClientConnection conn1 = connRequest1.get(0, TimeUnit.MILLISECONDS);
Assert.assertNotNull(conn1);
Mockito.verify(connFactory, Mockito.times(1)).create(Mockito.<ConnectionConfig>any());
Mockito.verify(connFactory, Mockito.times(1)).create(
Mockito.eq(route), Mockito.<ConnectionConfig>any());
Mockito.when(conn.isOpen()).thenReturn(Boolean.TRUE);
@ -252,13 +269,15 @@ public class TestBasicHttpClientConnectionManager {
final HttpHost target = new HttpHost("somehost");
final HttpRoute route = new HttpRoute(target);
Mockito.when(connFactory.create(Mockito.<ConnectionConfig>any())).thenReturn(conn);
Mockito.when(connFactory.create(
Mockito.eq(route), Mockito.<ConnectionConfig>any())).thenReturn(conn);
final ConnectionRequest connRequest1 = mgr.requestConnection(route, null);
final HttpClientConnection conn1 = connRequest1.get(0, TimeUnit.MILLISECONDS);
Assert.assertNotNull(conn1);
Mockito.verify(connFactory, Mockito.times(1)).create(Mockito.<ConnectionConfig>any());
Mockito.verify(connFactory, Mockito.times(1)).create(
Mockito.eq(route), Mockito.<ConnectionConfig>any());
Mockito.when(conn.isOpen()).thenReturn(Boolean.TRUE, Boolean.FALSE);
@ -279,13 +298,15 @@ public class TestBasicHttpClientConnectionManager {
final HttpHost target = new HttpHost("somehost");
final HttpRoute route = new HttpRoute(target);
Mockito.when(connFactory.create(Mockito.<ConnectionConfig>any())).thenReturn(conn);
Mockito.when(connFactory.create(
Mockito.eq(route), Mockito.<ConnectionConfig>any())).thenReturn(conn);
final ConnectionRequest connRequest1 = mgr.requestConnection(route, null);
final HttpClientConnection conn1 = connRequest1.get(0, TimeUnit.MILLISECONDS);
Assert.assertNotNull(conn1);
Mockito.verify(connFactory, Mockito.times(1)).create(Mockito.<ConnectionConfig>any());
Mockito.verify(connFactory, Mockito.times(1)).create(
Mockito.eq(route), Mockito.<ConnectionConfig>any());
Mockito.when(conn.isOpen()).thenReturn(Boolean.TRUE, Boolean.FALSE);
@ -306,7 +327,8 @@ public class TestBasicHttpClientConnectionManager {
final HttpHost target = new HttpHost("somehost");
final HttpRoute route = new HttpRoute(target);
Mockito.when(connFactory.create(Mockito.<ConnectionConfig>any())).thenReturn(conn);
Mockito.when(connFactory.create(
Mockito.eq(route), Mockito.<ConnectionConfig>any())).thenReturn(conn);
final ConnectionRequest connRequest1 = mgr.requestConnection(route, null);
final HttpClientConnection conn1 = connRequest1.get(0, TimeUnit.MILLISECONDS);

View File

@ -1,52 +0,0 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.impl.conn;
import junit.framework.Assert;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.conn.ManagedHttpClientConnection;
import org.junit.Test;
public class TestManagedHttpClientConnectionFactory {
@Test
public void testDefaultConnectionConfig() throws Exception {
validateDefault(ManagedHttpClientConnectionFactory.INSTANCE.create(ConnectionConfig.DEFAULT));
}
private void validateDefault(final ManagedHttpClientConnection managedHttpClientConnection) {
// Nothing really interesting to check here
Assert.assertNotNull(managedHttpClientConnection.getId());
}
@Test
public void testNullConnectionConfig() throws Exception {
// Should not NPE
validateDefault(ManagedHttpClientConnectionFactory.INSTANCE.create(null));
}
}