From 6963674cd6a5ee8fcbb360d15c11c4efcba180c1 Mon Sep 17 00:00:00 2001 From: Roland Weber Date: Sun, 3 Feb 2008 23:14:50 +0000 Subject: [PATCH] HTTPCLIENT-735: explicit unsetting of DEFAULT_PROXY and FORCED_ROUTE git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@618119 13f79535-47bb-0310-9956-ffa450edef68 --- .../http/conn/params/HttpRouteParams.java | 202 ++++++++++++++++++ .../impl/conn/DefaultHttpRoutePlanner.java | 17 +- .../impl/conn/ProxySelectorRoutePlanner.java | 13 +- 3 files changed, 218 insertions(+), 14 deletions(-) create mode 100644 module-client/src/main/java/org/apache/http/conn/params/HttpRouteParams.java diff --git a/module-client/src/main/java/org/apache/http/conn/params/HttpRouteParams.java b/module-client/src/main/java/org/apache/http/conn/params/HttpRouteParams.java new file mode 100644 index 000000000..bab18d4a8 --- /dev/null +++ b/module-client/src/main/java/org/apache/http/conn/params/HttpRouteParams.java @@ -0,0 +1,202 @@ +/* + * $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.params; + + +import java.net.InetAddress; + +import org.apache.http.HttpHost; +import org.apache.http.params.HttpParams; +import org.apache.http.conn.routing.HttpRoute; + + + +/** + * An adaptor for accessing route related parameters in {@link HttpParams}. + * See {@link ConnRoutePNames} for parameter name definitions. + * + * @author Oleg Kalnichevski + * @author Roland Weber + * + * @version $Revision$ + * + * @since 4.0 + */ +public class HttpRouteParams { + + /** + * A special value indicating "no host". + * This relies on a nonsense scheme name to avoid conflicts + * with actual hosts. + */ + public static final HttpHost NO_HOST = + new HttpHost("127.0.0.255", -32768, "$_"); + + /** + * A special value indicating "no route". + * This is a route with {@link #NO_HOST} as the target. + */ + public static final HttpRoute NO_ROUTE = new HttpRoute(NO_HOST); + + + + /** Disabled default constructor. */ + private HttpRouteParams() { + // no body + } + + + /** + * Obtains the {@link ConnRoutePNames#DEFAULT_PROXY DEFAULT_PROXY} + * parameter value. + * {@link #NO_HOST} will be mapped to null, + * to allow unsetting in a hierarchy. + * + * @param params the parameters in which to look up + * + * @return the default proxy set in the argument parameters, or + * null if not set + */ + public final static HttpHost getDefaultProxy(HttpParams params) { + if (params == null) { + throw new IllegalArgumentException("Parameters must not be null."); + } + HttpHost proxy = (HttpHost) + params.getParameter(ConnRoutePNames.DEFAULT_PROXY); + if ((proxy != null) && NO_HOST.equals(proxy)) { + // value is explicitly unset + proxy = null; + } + return proxy; + } + + + /** + * Sets the {@link ConnRoutePNames#DEFAULT_PROXY DEFAULT_PROXY} + * parameter value. + * + * @param params the parameters in which to set the value + * @param proxy the value to set, may be null. + * Note that {@link #NO_HOST} will be mapped to + * null by {@link #getDefaultProxy}, + * to allow for explicit unsetting in hierarchies. + */ + public final static void setDefaultProxy(HttpParams params, + HttpHost proxy) { + if (params == null) { + throw new IllegalArgumentException("Parameters must not be null."); + } + params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); + } + + + /** + * Obtains the {@link ConnRoutePNames#FORCED_ROUTE FORCED_ROUTE} + * parameter value. + * {@link #NO_ROUTE} will be mapped to null, + * to allow unsetting in a hierarchy. + * + * @param params the parameters in which to look up + * + * @return the forced route set in the argument parameters, or + * null if not set + */ + public final static HttpRoute getForcedRoute(HttpParams params) { + if (params == null) { + throw new IllegalArgumentException("Parameters must not be null."); + } + HttpRoute route = (HttpRoute) + params.getParameter(ConnRoutePNames.FORCED_ROUTE); + if ((route != null) && NO_ROUTE.equals(route)) { + // value is explicitly unset + route = null; + } + return route; + } + + + /** + * Sets the {@link ConnRoutePNames#FORCED_ROUTE FORCED_ROUTE} + * parameter value. + * + * @param params the parameters in which to set the value + * @param route the value to set, may be null. + * Note that {@link #NO_ROUTE} will be mapped to + * null by {@link #getForcedRoute}, + * to allow for explicit unsetting in hierarchies. + */ + public final static void setForcedRoute(HttpParams params, + HttpRoute route) { + if (params == null) { + throw new IllegalArgumentException("Parameters must not be null."); + } + params.setParameter(ConnRoutePNames.FORCED_ROUTE, route); + } + + + /** + * Obtains the {@link ConnRoutePNames#LOCAL_ADDRESS LOCAL_ADDRESS} + * parameter value. + * {@link #NO_HOST} will be mapped to null, + * to allow unsetting in a hierarchy. + * + * @param params the parameters in which to look up + * + * @return the default proxy set in the argument parameters, or + * null if not set + */ + public final static InetAddress getLocalAddress(HttpParams params) { + if (params == null) { + throw new IllegalArgumentException("Parameters must not be null."); + } + InetAddress local = (InetAddress) + params.getParameter(ConnRoutePNames.LOCAL_ADDRESS); + // currently no explicit unsetting + return local; + } + + + /** + * Sets the {@link ConnRoutePNames#LOCAL_ADDRESS LOCAL_ADDRESS} + * parameter value. + * + * @param params the parameters in which to set the value + * @param local the value to set, may be null. + */ + public final static void setLocalAddress(HttpParams params, + InetAddress local) { + if (params == null) { + throw new IllegalArgumentException("Parameters must not be null."); + } + params.setParameter(ConnRoutePNames.LOCAL_ADDRESS, local); + } +} + diff --git a/module-client/src/main/java/org/apache/http/impl/conn/DefaultHttpRoutePlanner.java b/module-client/src/main/java/org/apache/http/impl/conn/DefaultHttpRoutePlanner.java index a5a8e71c8..0699623ab 100644 --- a/module-client/src/main/java/org/apache/http/impl/conn/DefaultHttpRoutePlanner.java +++ b/module-client/src/main/java/org/apache/http/impl/conn/DefaultHttpRoutePlanner.java @@ -44,12 +44,13 @@ import org.apache.http.conn.SchemeRegistry; import org.apache.http.conn.routing.HttpRoute; import org.apache.http.conn.routing.HttpRoutePlanner; -import org.apache.http.conn.params.ConnRoutePNames; +import org.apache.http.conn.params.HttpRouteParams; /** * Default implementation of an {@link HttpRoutePlanner}. - * This implementation is based on {@link ConnRoutePNames parameters}. + * This implementation is based on + * {@link org.apache.http.conn.params.ConnRoutePNames parameters}. * It will not make use of any Java system properties, * nor of system or browser proxy settings. */ @@ -85,8 +86,8 @@ public class DefaultHttpRoutePlanner implements HttpRoutePlanner { } // If we have a forced route, we can do without a target. - HttpRoute route = (HttpRoute) - request.getParams().getParameter(ConnRoutePNames.FORCED_ROUTE); + HttpRoute route = + HttpRouteParams.getForcedRoute(request.getParams()); if (route != null) return route; @@ -98,10 +99,10 @@ public class DefaultHttpRoutePlanner implements HttpRoutePlanner { ("Target host must not be null."); } - final InetAddress local = (InetAddress) - request.getParams().getParameter(ConnRoutePNames.LOCAL_ADDRESS); - final HttpHost proxy = (HttpHost) - request.getParams().getParameter(ConnRoutePNames.DEFAULT_PROXY); + final InetAddress local = + HttpRouteParams.getLocalAddress(request.getParams()); + final HttpHost proxy = + HttpRouteParams.getDefaultProxy(request.getParams()); final Scheme schm = schemeRegistry.getScheme(target.getSchemeName()); // as it is typically used for TLS/SSL, we assume that diff --git a/module-client/src/main/java/org/apache/http/impl/conn/ProxySelectorRoutePlanner.java b/module-client/src/main/java/org/apache/http/impl/conn/ProxySelectorRoutePlanner.java index 98d13037e..493283241 100644 --- a/module-client/src/main/java/org/apache/http/impl/conn/ProxySelectorRoutePlanner.java +++ b/module-client/src/main/java/org/apache/http/impl/conn/ProxySelectorRoutePlanner.java @@ -50,7 +50,7 @@ import org.apache.http.conn.routing.HttpRoutePlanner; import org.apache.http.conn.Scheme; import org.apache.http.conn.SchemeRegistry; -import org.apache.http.conn.params.ConnRoutePNames; +import org.apache.http.conn.params.HttpRouteParams; /** @@ -58,7 +58,8 @@ import org.apache.http.conn.params.ConnRoutePNames; * This implementation is based on {@link java.net.ProxySelector}. * By default, it will pick up the proxy settings of the JVM, either * from system properties or from the browser running the application. - * Additionally, it interprets some {@link ConnRoutePNames parameters}, + * Additionally, it interprets some + * {@link org.apache.http.conn.params.ConnRoutePNames parameters}, * though not the {@link ConnRoutePNames#DEFAULT_PROXY DEFAULT_PROXY}. */ public class ProxySelectorRoutePlanner implements HttpRoutePlanner { @@ -123,8 +124,8 @@ public class ProxySelectorRoutePlanner implements HttpRoutePlanner { } // If we have a forced route, we can do without a target. - HttpRoute route = (HttpRoute) - request.getParams().getParameter(ConnRoutePNames.FORCED_ROUTE); + HttpRoute route = + HttpRouteParams.getForcedRoute(request.getParams()); if (route != null) return route; @@ -136,8 +137,8 @@ public class ProxySelectorRoutePlanner implements HttpRoutePlanner { ("Target host must not be null."); } - final InetAddress local = (InetAddress) - request.getParams().getParameter(ConnRoutePNames.LOCAL_ADDRESS); + final InetAddress local = + HttpRouteParams.getLocalAddress(request.getParams()); final HttpHost proxy = determineProxy(target, request, context); final Scheme schm =