From dc3b8d47fb4b727c700f70ceab6237d67ce30e4b Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Sat, 15 Jun 2013 12:50:56 +0000 Subject: [PATCH] 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 --- .../examples/client/ClientConfiguration.java | 2 +- .../http/conn/HttpConnectionFactory.java | 4 +- .../apache/http/impl/client/ProxyClient.java | 7 +- .../BasicHttpClientConnectionManager.java | 8 +-- .../ManagedHttpClientConnectionFactory.java | 6 +- .../PoolingHttpClientConnectionManager.java | 14 ++-- .../TestBasicHttpClientConnectionManager.java | 66 ++++++++++++------- ...estManagedHttpClientConnectionFactory.java | 52 --------------- 8 files changed, 66 insertions(+), 93 deletions(-) delete mode 100644 httpclient/src/test/java/org/apache/http/impl/conn/TestManagedHttpClientConnectionFactory.java diff --git a/httpclient/src/examples/org/apache/http/examples/client/ClientConfiguration.java b/httpclient/src/examples/org/apache/http/examples/client/ClientConfiguration.java index 3ce077402..855dbc700 100644 --- a/httpclient/src/examples/org/apache/http/examples/client/ClientConfiguration.java +++ b/httpclient/src/examples/org/apache/http/examples/client/ClientConfiguration.java @@ -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 connFactory = new ManagedHttpClientConnectionFactory( + HttpConnectionFactory connFactory = new ManagedHttpClientConnectionFactory( requestWriterFactory, responseParserFactory); // Client HTTP connection objects when fully initialized can be bound to diff --git a/httpclient/src/main/java/org/apache/http/conn/HttpConnectionFactory.java b/httpclient/src/main/java/org/apache/http/conn/HttpConnectionFactory.java index 60f6be030..746c2ea4e 100644 --- a/httpclient/src/main/java/org/apache/http/conn/HttpConnectionFactory.java +++ b/httpclient/src/main/java/org/apache/http/conn/HttpConnectionFactory.java @@ -34,8 +34,8 @@ import org.apache.http.config.ConnectionConfig; * * @since 4.3 */ -public interface HttpConnectionFactory { +public interface HttpConnectionFactory { - T create(ConnectionConfig config); + C create(T route, ConnectionConfig config); } diff --git a/httpclient/src/main/java/org/apache/http/impl/client/ProxyClient.java b/httpclient/src/main/java/org/apache/http/impl/client/ProxyClient.java index 254e8c1bb..10076a812 100644 --- a/httpclient/src/main/java/org/apache/http/impl/client/ProxyClient.java +++ b/httpclient/src/main/java/org/apache/http/impl/client/ProxyClient.java @@ -79,7 +79,7 @@ import org.apache.http.util.EntityUtils; @SuppressWarnings("deprecation") public class ProxyClient { - private final HttpConnectionFactory connFactory; + private final HttpConnectionFactory 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 connFactory, + final HttpConnectionFactory 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; diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/BasicHttpClientConnectionManager.java b/httpclient/src/main/java/org/apache/http/impl/conn/BasicHttpClientConnectionManager.java index df8b1cdfe..2c5e3ce79 100644 --- a/httpclient/src/main/java/org/apache/http/impl/conn/BasicHttpClientConnectionManager.java +++ b/httpclient/src/main/java/org/apache/http/impl/conn/BasicHttpClientConnectionManager.java @@ -81,7 +81,7 @@ public class BasicHttpClientConnectionManager implements HttpClientConnectionMan private final Log log = LogFactory.getLog(getClass()); private final HttpClientConnectionOperator connectionOperator; - private final HttpConnectionFactory connFactory; + private final HttpConnectionFactory connFactory; @GuardedBy("this") private ManagedHttpClientConnection conn; @@ -119,7 +119,7 @@ public class BasicHttpClientConnectionManager implements HttpClientConnectionMan public BasicHttpClientConnectionManager( final Lookup socketFactoryRegistry, - final HttpConnectionFactory connFactory, + final HttpConnectionFactory connFactory, final SchemePortResolver schemePortResolver, final DnsResolver dnsResolver) { super(); @@ -133,7 +133,7 @@ public class BasicHttpClientConnectionManager implements HttpClientConnectionMan public BasicHttpClientConnectionManager( final Lookup socketFactoryRegistry, - final HttpConnectionFactory connFactory) { + final HttpConnectionFactory 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; diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/ManagedHttpClientConnectionFactory.java b/httpclient/src/main/java/org/apache/http/impl/conn/ManagedHttpClientConnectionFactory.java index 332449fbb..b91a6ddef 100644 --- a/httpclient/src/main/java/org/apache/http/impl/conn/ManagedHttpClientConnectionFactory.java +++ b/httpclient/src/main/java/org/apache/http/impl/conn/ManagedHttpClientConnectionFactory.java @@ -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 { +public class ManagedHttpClientConnectionFactory + implements HttpConnectionFactory { 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; diff --git a/httpclient/src/main/java/org/apache/http/impl/conn/PoolingHttpClientConnectionManager.java b/httpclient/src/main/java/org/apache/http/impl/conn/PoolingHttpClientConnectionManager.java index da2d87da3..3b5d6942e 100644 --- a/httpclient/src/main/java/org/apache/http/impl/conn/PoolingHttpClientConnectionManager.java +++ b/httpclient/src/main/java/org/apache/http/impl/conn/PoolingHttpClientConnectionManager.java @@ -120,25 +120,25 @@ public class PoolingHttpClientConnectionManager public PoolingHttpClientConnectionManager( final Registry socketFactoryRegistry, - final HttpConnectionFactory connFactory) { + final HttpConnectionFactory connFactory) { this(socketFactoryRegistry, connFactory, null); } public PoolingHttpClientConnectionManager( - final HttpConnectionFactory connFactory) { + final HttpConnectionFactory connFactory) { this(getDefaultRegistry(), connFactory, null); } public PoolingHttpClientConnectionManager( final Registry socketFactoryRegistry, - final HttpConnectionFactory connFactory, + final HttpConnectionFactory connFactory, final DnsResolver dnsResolver) { this(socketFactoryRegistry, connFactory, null, dnsResolver, -1, TimeUnit.MILLISECONDS); } public PoolingHttpClientConnectionManager( final Registry socketFactoryRegistry, - final HttpConnectionFactory connFactory, + final HttpConnectionFactory 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 { private final ConfigData configData; - private final HttpConnectionFactory connFactory; + private final HttpConnectionFactory connFactory; InternalConnectionFactory( final ConfigData configData, - final HttpConnectionFactory connFactory) { + final HttpConnectionFactory 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); } } diff --git a/httpclient/src/test/java/org/apache/http/impl/conn/TestBasicHttpClientConnectionManager.java b/httpclient/src/test/java/org/apache/http/impl/conn/TestBasicHttpClientConnectionManager.java index 1175b3085..0fdcfb401 100644 --- a/httpclient/src/test/java/org/apache/http/impl/conn/TestBasicHttpClientConnectionManager.java +++ b/httpclient/src/test/java/org/apache/http/impl/conn/TestBasicHttpClientConnectionManager.java @@ -46,7 +46,7 @@ import org.mockito.Mockito; public class TestBasicHttpClientConnectionManager { private ManagedHttpClientConnection conn; - private HttpConnectionFactory connFactory; + private HttpConnectionFactory connFactory; private Lookup 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.any())).thenReturn(conn); + Mockito.when(connFactory.create( + Mockito.eq(route), Mockito.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.any()); + Mockito.verify(connFactory, Mockito.times(2)).create( + Mockito.eq(route), Mockito.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.any())).thenReturn(conn); + Mockito.when(connFactory.create(Mockito.eq(route), Mockito.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.any()); + Mockito.verify(connFactory, Mockito.times(1)).create( + Mockito.eq(route), Mockito.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.any()); + Mockito.verify(connFactory, Mockito.times(1)).create( + Mockito.eq(route), Mockito.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.any())).thenReturn(conn); + Mockito.when(connFactory.create( + Mockito.eq(route), Mockito.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.any()); + Mockito.verify(connFactory, Mockito.times(1)).create( + Mockito.eq(route), Mockito.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.any()); + Mockito.verify(connFactory, Mockito.times(1)).create( + Mockito.eq(route), Mockito.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.any())).thenReturn(conn); + Mockito.when(connFactory.create( + Mockito.any(), Mockito.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.any()); + Mockito.verify(connFactory, Mockito.times(1)).create( + Mockito.eq(route1), Mockito.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.any()); + Mockito.verify(connFactory, Mockito.times(1)).create( + Mockito.eq(route1), Mockito.any()); + Mockito.verify(connFactory, Mockito.times(1)).create( + Mockito.eq(route2), Mockito.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.any())).thenReturn(conn); + Mockito.when(connFactory.create( + Mockito.eq(route), Mockito.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.any()); + Mockito.verify(connFactory, Mockito.times(1)).create( + Mockito.eq(route), Mockito.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.any()); + Mockito.verify(connFactory, Mockito.times(2)).create( + Mockito.eq(route), Mockito.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.any())).thenReturn(conn); + Mockito.when(connFactory.create( + Mockito.eq(route), Mockito.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.any()); + Mockito.verify(connFactory, Mockito.times(1)).create( + Mockito.eq(route), Mockito.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.any())).thenReturn(conn); + Mockito.when(connFactory.create( + Mockito.eq(route), Mockito.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.any()); + Mockito.verify(connFactory, Mockito.times(1)).create( + Mockito.eq(route), Mockito.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.any())).thenReturn(conn); + Mockito.when(connFactory.create( + Mockito.eq(route), Mockito.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.any()); + Mockito.verify(connFactory, Mockito.times(1)).create( + Mockito.eq(route), Mockito.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.any())).thenReturn(conn); + Mockito.when(connFactory.create( + Mockito.eq(route), Mockito.any())).thenReturn(conn); final ConnectionRequest connRequest1 = mgr.requestConnection(route, null); final HttpClientConnection conn1 = connRequest1.get(0, TimeUnit.MILLISECONDS); diff --git a/httpclient/src/test/java/org/apache/http/impl/conn/TestManagedHttpClientConnectionFactory.java b/httpclient/src/test/java/org/apache/http/impl/conn/TestManagedHttpClientConnectionFactory.java deleted file mode 100644 index a4fbdf517..000000000 --- a/httpclient/src/test/java/org/apache/http/impl/conn/TestManagedHttpClientConnectionFactory.java +++ /dev/null @@ -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 - * . - * - */ -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)); - } -}