From 2c2dd57f723e2040c80296c79f0fb21a8b525036 Mon Sep 17 00:00:00 2001 From: Roland Weber Date: Sun, 10 Feb 2008 10:18:48 +0000 Subject: [PATCH] HTTPCLIENT-742: common interface for HttpRoute and RouteTracker git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@620254 13f79535-47bb-0310-9956-ffa450edef68 --- RELEASE_NOTES.txt | 3 + .../apache/http/conn/routing/HttpRoute.java | 114 ++-------- .../apache/http/conn/routing/RouteInfo.java | 194 ++++++++++++++++++ .../http/conn/routing/RouteTracker.java | 95 ++------- .../http/conn/routing/TestHttpRoute.java | 4 +- .../http/conn/routing/TestRouteDirector.java | 4 +- .../http/conn/routing/TestRouteTracker.java | 4 +- 7 files changed, 228 insertions(+), 190 deletions(-) create mode 100644 module-client/src/main/java/org/apache/http/conn/routing/RouteInfo.java diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt index 179834f32..41ff05dfd 100644 --- a/RELEASE_NOTES.txt +++ b/RELEASE_NOTES.txt @@ -1,6 +1,9 @@ Changes since 4.0 Alpha 2 ------------------- +* [HTTPCLIENT-742] common interface for HttpRoute and RouteTracker + Contributed by Roland Weber + * [HTTPCLIENT-741] Fixed concurrency issues in AbstractClientConnAdapter. Contributed by Oleg Kalnichevski diff --git a/module-client/src/main/java/org/apache/http/conn/routing/HttpRoute.java b/module-client/src/main/java/org/apache/http/conn/routing/HttpRoute.java index 2964e292a..e23de4568 100644 --- a/module-client/src/main/java/org/apache/http/conn/routing/HttpRoute.java +++ b/module-client/src/main/java/org/apache/http/conn/routing/HttpRoute.java @@ -48,33 +48,7 @@ import org.apache.http.HttpHost; * * @since 4.0 */ -public final class HttpRoute implements Cloneable { - - /** - * The tunnelling type of a route. - * Plain routes are established by connecting to the target or - * the first proxy. - * Tunnelled routes are established by connecting to the first proxy - * and tunnelling through all proxies to the target. - * Routes without a proxy cannot be tunnelled. - */ - public enum TunnelType { PLAIN, TUNNELLED }; - - /** - * The layering type of a route. - * Plain routes are established by connecting or tunnelling. - * Layered routes are established by layering a protocol such as TLS/SSL - * over an existing connection. - * Protocols can only be layered over a tunnel to the target, or - * or over a direct connection without proxies. - *
- * Layering a protocol - * over a direct connection makes little sense, since the connection - * could be established with the new protocol in the first place. - * But we don't want to exclude that use case. - */ - public enum LayerType { PLAIN, LAYERED }; - +public final class HttpRoute implements RouteInfo, Cloneable { /** The target host to connect to. */ private final HttpHost targetHost; @@ -279,55 +253,26 @@ public final class HttpRoute implements Cloneable { } - /** - * Obtains the target host. - * - * @return the target host - */ + + // non-JavaDoc, see interface RouteInfo public final HttpHost getTargetHost() { return this.targetHost; } - /** - * Obtains the local address to connect from. - * - * @return the local address, - * or null - */ + // non-JavaDoc, see interface RouteInfo public final InetAddress getLocalAddress() { return this.localAddress; } - /** - * Obtains the number of hops in this route. - * A direct route has one hop. A route through a proxy has two hops. - * A route through a chain of n proxies has n+1 hops. - * - * @return the number of hops in this route - */ + // non-JavaDoc, see interface RouteInfo public final int getHopCount() { return (proxyChain == null) ? 1 : (proxyChain.length+1); } - /** - * Obtains the target of a hop in this route. - * The target of the last hop is the {@link #getTargetHost target host}, - * the target of previous hops is the respective proxy in the chain. - * For a route through exactly one proxy, target of hop 0 is the proxy - * and target of hop 1 is the target host. - * - * @param hop index of the hop for which to get the target, - * 0 for first - * - * @return the target of the given hop - * - * @throws IllegalArgumentException - * if the argument is negative or not less than - * {@link #getHopCount getHopCount()} - */ + // non-JavaDoc, see interface RouteInfo public final HttpHost getHopTarget(int hop) { if (hop < 0) throw new IllegalArgumentException @@ -348,72 +293,37 @@ public final class HttpRoute implements Cloneable { } - /** - * Obtains the first proxy host. - * - * @return the first proxy in the proxy chain, or - * null if this route is direct - */ + // non-JavaDoc, see interface RouteInfo public final HttpHost getProxyHost() { return (this.proxyChain == null) ? null : this.proxyChain[0]; } - /** - * Obtains the tunnel type of this route. - * If there is a proxy chain, only end-to-end tunnels are considered. - * - * @return the tunnelling type - */ + // non-JavaDoc, see interface RouteInfo public final TunnelType getTunnelType() { return this.tunnelled; } - /** - * Checks whether this route is tunnelled through a proxy. - * If there is a proxy chain, only end-to-end tunnels are considered. - * - * @return true if tunnelled end-to-end through at least - * one proxy, - * false otherwise - */ + // non-JavaDoc, see interface RouteInfo public final boolean isTunnelled() { return (this.tunnelled == TunnelType.TUNNELLED); } - /** - * Obtains the layering type of this route. - * In the presence of proxies, only layering over an end-to-end tunnel - * is considered. - * - * @return the layering type - */ + // non-JavaDoc, see interface RouteInfo public final LayerType getLayerType() { return this.layered; } - /** - * Checks whether this route includes a layered protocol. - * In the presence of proxies, only layering over an end-to-end tunnel - * is considered. - * - * @return true if layered, - * false otherwise - */ + // non-JavaDoc, see interface RouteInfo public final boolean isLayered() { return (this.layered == LayerType.LAYERED); } - /** - * Checks whether this route is secure. - * - * @return true if secure, - * false otherwise - */ + // non-JavaDoc, see interface RouteInfo public final boolean isSecure() { return this.secure; } diff --git a/module-client/src/main/java/org/apache/http/conn/routing/RouteInfo.java b/module-client/src/main/java/org/apache/http/conn/routing/RouteInfo.java new file mode 100644 index 000000000..bb12be294 --- /dev/null +++ b/module-client/src/main/java/org/apache/http/conn/routing/RouteInfo.java @@ -0,0 +1,194 @@ +/* + * $HeadURL$ + * $Revision$ + * $Date$ + * + * ==================================================================== + * 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.conn.routing; + +import java.net.InetAddress; + +import org.apache.http.HttpHost; + + +/** + * Read-only interface for route information. + * + * @author Roland Weber + * + * + * + * @version $Revision$ + * + * @since 4.0 + */ +public interface RouteInfo { + + /** + * The tunnelling type of a route. + * Plain routes are established by connecting to the target or + * the first proxy. + * Tunnelled routes are established by connecting to the first proxy + * and tunnelling through all proxies to the target. + * Routes without a proxy cannot be tunnelled. + */ + public enum TunnelType { PLAIN, TUNNELLED }; + + /** + * The layering type of a route. + * Plain routes are established by connecting or tunnelling. + * Layered routes are established by layering a protocol such as TLS/SSL + * over an existing connection. + * Protocols can only be layered over a tunnel to the target, or + * or over a direct connection without proxies. + *
+ * Layering a protocol + * over a direct connection makes little sense, since the connection + * could be established with the new protocol in the first place. + * But we don't want to exclude that use case. + */ + public enum LayerType { PLAIN, LAYERED }; + + + + /** + * Obtains the target host. + * + * @return the target host + */ + HttpHost getTargetHost() + ; + + + /** + * Obtains the local address to connect from. + * + * @return the local address, + * or null + */ + InetAddress getLocalAddress() + ; + + + /** + * Obtains the number of hops in this route. + * A direct route has one hop. A route through a proxy has two hops. + * A route through a chain of n proxies has n+1 hops. + * + * @return the number of hops in this route + */ + int getHopCount() + ; + + + /** + * Obtains the target of a hop in this route. + * The target of the last hop is the {@link #getTargetHost target host}, + * the target of previous hops is the respective proxy in the chain. + * For a route through exactly one proxy, target of hop 0 is the proxy + * and target of hop 1 is the target host. + * + * @param hop index of the hop for which to get the target, + * 0 for first + * + * @return the target of the given hop + * + * @throws IllegalArgumentException + * if the argument is negative or not less than + * {@link #getHopCount getHopCount()} + */ + HttpHost getHopTarget(int hop) + ; + + + /** + * Obtains the first proxy host. + * + * @return the first proxy in the proxy chain, or + * null if this route is direct + */ + HttpHost getProxyHost() + ; + + + /** + * Obtains the tunnel type of this route. + * If there is a proxy chain, only end-to-end tunnels are considered. + * + * @return the tunnelling type + */ + TunnelType getTunnelType() + ; + + + /** + * Checks whether this route is tunnelled through a proxy. + * If there is a proxy chain, only end-to-end tunnels are considered. + * + * @return true if tunnelled end-to-end through at least + * one proxy, + * false otherwise + */ + boolean isTunnelled() + ; + + + /** + * Obtains the layering type of this route. + * In the presence of proxies, only layering over an end-to-end tunnel + * is considered. + * + * @return the layering type + */ + LayerType getLayerType() + ; + + + /** + * Checks whether this route includes a layered protocol. + * In the presence of proxies, only layering over an end-to-end tunnel + * is considered. + * + * @return true if layered, + * false otherwise + */ + boolean isLayered() + ; + + + /** + * Checks whether this route is secure. + * + * @return true if secure, + * false otherwise + */ + boolean isSecure() + ; + + +} // interface RouteInfo diff --git a/module-client/src/main/java/org/apache/http/conn/routing/RouteTracker.java b/module-client/src/main/java/org/apache/http/conn/routing/RouteTracker.java index 2aa364f4f..c389d9887 100644 --- a/module-client/src/main/java/org/apache/http/conn/routing/RouteTracker.java +++ b/module-client/src/main/java/org/apache/http/conn/routing/RouteTracker.java @@ -34,8 +34,6 @@ package org.apache.http.conn.routing; import java.net.InetAddress; import org.apache.http.HttpHost; -import org.apache.http.conn.routing.HttpRoute.TunnelType; -import org.apache.http.conn.routing.HttpRoute.LayerType; /** @@ -49,7 +47,7 @@ import org.apache.http.conn.routing.HttpRoute.LayerType; * * @since 4.0 */ -public final class RouteTracker implements Cloneable { +public final class RouteTracker implements RouteInfo, Cloneable { /** The target host to connect to. */ private final HttpHost targetHost; @@ -212,37 +210,20 @@ public final class RouteTracker implements Cloneable { } - /** - * Obtains the target host. - * - * @return the target host - */ + + // non-JavaDoc, see interface RouteInfo public final HttpHost getTargetHost() { return this.targetHost; } - /** - * Obtains the local address to connect from. - * - * @return the local address, - * or null - */ + // non-JavaDoc, see interface RouteInfo public final InetAddress getLocalAddress() { return this.localAddress; } - /** - * Obtains the number of tracked hops. - * An unconnected route has no hops. - * Connecting directly to the target adds one hop. - * Connecting to a proxy adds two hops, one for the proxy and - * one for the target. - * Tunnelling to a proxy in a proxy chain adds one hop. - * - * @return the number of hops in the tracked route - */ + // non-JavaDoc, see interface RouteInfo public final int getHopCount() { int hops = 0; if (this.connected) { @@ -255,18 +236,7 @@ public final class RouteTracker implements Cloneable { } - /** - * Obtains the target of a hop in this route. - * - * @param hop index of the hop for which to get the target, - * 0 for first - * - * @return the target of the given hop - * - * @throws IllegalArgumentException - * if the argument is negative or not less than - * {@link #getHopCount getHopCount()} - */ + // non-JavaDoc, see interface RouteInfo public final HttpHost getHopTarget(int hop) { if (hop < 0) throw new IllegalArgumentException @@ -288,82 +258,43 @@ public final class RouteTracker implements Cloneable { } - /** - * Obtains the first proxy host. - * - * @return the first proxy host, or null if not tracked - */ + // non-JavaDoc, see interface RouteInfo public final HttpHost getProxyHost() { return (this.proxyChain == null) ? null : this.proxyChain[0]; } - /** - * Checks whether this route is connected to it's first hop. - * - * @return true if connected, - * false otherwise - */ + // non-JavaDoc, see interface RouteInfo public final boolean isConnected() { return this.connected; } - /** - * Obtains the tunnel type of this route. - * If there is a proxy chain, only end-to-end tunnels are considered. - * - * @return the tunnelling type - */ + // non-JavaDoc, see interface RouteInfo public final TunnelType getTunnelType() { return this.tunnelled; } - /** - * Checks whether this route is tunnelled through a proxy. - * If there is a proxy chain, only end-to-end tunnels are considered. - * - * @return true if tunnelled end-to-end through at least - * one proxy, - * false otherwise - */ + // non-JavaDoc, see interface RouteInfo public final boolean isTunnelled() { return (this.tunnelled == TunnelType.TUNNELLED); } - /** - * Obtains the layering type of this route. - * In the presence of proxies, only layering over an end-to-end tunnel - * is considered. - * - * @return the layering type - */ + // non-JavaDoc, see interface RouteInfo public final LayerType getLayerType() { return this.layered; } - /** - * Checks whether this route includes a layered protocol. - * In the presence of proxies, only layering over an end-to-end tunnel - * is considered. - * - * @return true if layered, - * false otherwise - */ + // non-JavaDoc, see interface RouteInfo public final boolean isLayered() { return (this.layered == LayerType.LAYERED); } - /** - * Checks whether this route is secure. - * - * @return true if secure, - * false otherwise - */ + // non-JavaDoc, see interface RouteInfo public final boolean isSecure() { return this.secure; } diff --git a/module-client/src/test/java/org/apache/http/conn/routing/TestHttpRoute.java b/module-client/src/test/java/org/apache/http/conn/routing/TestHttpRoute.java index e24b18317..f07ba432b 100644 --- a/module-client/src/test/java/org/apache/http/conn/routing/TestHttpRoute.java +++ b/module-client/src/test/java/org/apache/http/conn/routing/TestHttpRoute.java @@ -41,8 +41,8 @@ import junit.framework.TestCase; import junit.framework.TestSuite; import org.apache.http.HttpHost; -import org.apache.http.conn.routing.HttpRoute.TunnelType; -import org.apache.http.conn.routing.HttpRoute.LayerType; +import org.apache.http.conn.routing.RouteInfo.TunnelType; +import org.apache.http.conn.routing.RouteInfo.LayerType; /** diff --git a/module-client/src/test/java/org/apache/http/conn/routing/TestRouteDirector.java b/module-client/src/test/java/org/apache/http/conn/routing/TestRouteDirector.java index 73f8f4fbd..d63af4d8b 100644 --- a/module-client/src/test/java/org/apache/http/conn/routing/TestRouteDirector.java +++ b/module-client/src/test/java/org/apache/http/conn/routing/TestRouteDirector.java @@ -38,8 +38,8 @@ import junit.framework.TestCase; import junit.framework.TestSuite; import org.apache.http.HttpHost; -import org.apache.http.conn.routing.HttpRoute.TunnelType; -import org.apache.http.conn.routing.HttpRoute.LayerType; +import org.apache.http.conn.routing.RouteInfo.TunnelType; +import org.apache.http.conn.routing.RouteInfo.LayerType; /** diff --git a/module-client/src/test/java/org/apache/http/conn/routing/TestRouteTracker.java b/module-client/src/test/java/org/apache/http/conn/routing/TestRouteTracker.java index 00fb70b3c..27d779b7a 100644 --- a/module-client/src/test/java/org/apache/http/conn/routing/TestRouteTracker.java +++ b/module-client/src/test/java/org/apache/http/conn/routing/TestRouteTracker.java @@ -40,8 +40,8 @@ import junit.framework.TestCase; import junit.framework.TestSuite; import org.apache.http.HttpHost; -import org.apache.http.conn.routing.HttpRoute.TunnelType; -import org.apache.http.conn.routing.HttpRoute.LayerType; +import org.apache.http.conn.routing.RouteInfo.TunnelType; +import org.apache.http.conn.routing.RouteInfo.LayerType; /**