michael@0: /* michael@0: * ==================================================================== michael@0: * michael@0: * Licensed to the Apache Software Foundation (ASF) under one or more michael@0: * contributor license agreements. See the NOTICE file distributed with michael@0: * this work for additional information regarding copyright ownership. michael@0: * The ASF licenses this file to You under the Apache License, Version 2.0 michael@0: * (the "License"); you may not use this file except in compliance with michael@0: * the License. You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: * ==================================================================== michael@0: * michael@0: * This software consists of voluntary contributions made by many michael@0: * individuals on behalf of the Apache Software Foundation. For more michael@0: * information on the Apache Software Foundation, please see michael@0: * . michael@0: * michael@0: */ michael@0: michael@0: package ch.boye.httpclientandroidlib.conn.params; michael@0: michael@0: import java.net.InetAddress; michael@0: michael@0: import ch.boye.httpclientandroidlib.annotation.Immutable; michael@0: michael@0: import ch.boye.httpclientandroidlib.HttpHost; michael@0: import ch.boye.httpclientandroidlib.params.HttpParams; michael@0: import ch.boye.httpclientandroidlib.conn.routing.HttpRoute; michael@0: michael@0: /** michael@0: * An adaptor for manipulating HTTP routing parameters michael@0: * in {@link HttpParams}. michael@0: * michael@0: * @since 4.0 michael@0: */ michael@0: @Immutable michael@0: public class ConnRouteParams implements ConnRoutePNames { michael@0: michael@0: /** michael@0: * A special value indicating "no host". michael@0: * This relies on a nonsense scheme name to avoid conflicts michael@0: * with actual hosts. Note that this is a valid host. michael@0: */ michael@0: public static final HttpHost NO_HOST = michael@0: new HttpHost("127.0.0.255", 0, "no-host"); // Immutable michael@0: michael@0: /** michael@0: * A special value indicating "no route". michael@0: * This is a route with {@link #NO_HOST} as the target. michael@0: */ michael@0: public static final HttpRoute NO_ROUTE = new HttpRoute(NO_HOST); // Immutable michael@0: michael@0: /** Disabled default constructor. */ michael@0: private ConnRouteParams() { michael@0: // no body michael@0: } michael@0: michael@0: /** michael@0: * Obtains the {@link ConnRoutePNames#DEFAULT_PROXY DEFAULT_PROXY} michael@0: * parameter value. michael@0: * {@link #NO_HOST} will be mapped to null, michael@0: * to allow unsetting in a hierarchy. michael@0: * michael@0: * @param params the parameters in which to look up michael@0: * michael@0: * @return the default proxy set in the argument parameters, or michael@0: * null if not set michael@0: */ michael@0: public static HttpHost getDefaultProxy(HttpParams params) { michael@0: if (params == null) { michael@0: throw new IllegalArgumentException("Parameters must not be null."); michael@0: } michael@0: HttpHost proxy = (HttpHost) michael@0: params.getParameter(DEFAULT_PROXY); michael@0: if ((proxy != null) && NO_HOST.equals(proxy)) { michael@0: // value is explicitly unset michael@0: proxy = null; michael@0: } michael@0: return proxy; michael@0: } michael@0: michael@0: /** michael@0: * Sets the {@link ConnRoutePNames#DEFAULT_PROXY DEFAULT_PROXY} michael@0: * parameter value. michael@0: * michael@0: * @param params the parameters in which to set the value michael@0: * @param proxy the value to set, may be null. michael@0: * Note that {@link #NO_HOST} will be mapped to michael@0: * null by {@link #getDefaultProxy}, michael@0: * to allow for explicit unsetting in hierarchies. michael@0: */ michael@0: public static void setDefaultProxy(HttpParams params, michael@0: HttpHost proxy) { michael@0: if (params == null) { michael@0: throw new IllegalArgumentException("Parameters must not be null."); michael@0: } michael@0: params.setParameter(DEFAULT_PROXY, proxy); michael@0: } michael@0: michael@0: /** michael@0: * Obtains the {@link ConnRoutePNames#FORCED_ROUTE FORCED_ROUTE} michael@0: * parameter value. michael@0: * {@link #NO_ROUTE} will be mapped to null, michael@0: * to allow unsetting in a hierarchy. michael@0: * michael@0: * @param params the parameters in which to look up michael@0: * michael@0: * @return the forced route set in the argument parameters, or michael@0: * null if not set michael@0: */ michael@0: public static HttpRoute getForcedRoute(HttpParams params) { michael@0: if (params == null) { michael@0: throw new IllegalArgumentException("Parameters must not be null."); michael@0: } michael@0: HttpRoute route = (HttpRoute) michael@0: params.getParameter(FORCED_ROUTE); michael@0: if ((route != null) && NO_ROUTE.equals(route)) { michael@0: // value is explicitly unset michael@0: route = null; michael@0: } michael@0: return route; michael@0: } michael@0: michael@0: /** michael@0: * Sets the {@link ConnRoutePNames#FORCED_ROUTE FORCED_ROUTE} michael@0: * parameter value. michael@0: * michael@0: * @param params the parameters in which to set the value michael@0: * @param route the value to set, may be null. michael@0: * Note that {@link #NO_ROUTE} will be mapped to michael@0: * null by {@link #getForcedRoute}, michael@0: * to allow for explicit unsetting in hierarchies. michael@0: */ michael@0: public static void setForcedRoute(HttpParams params, michael@0: HttpRoute route) { michael@0: if (params == null) { michael@0: throw new IllegalArgumentException("Parameters must not be null."); michael@0: } michael@0: params.setParameter(FORCED_ROUTE, route); michael@0: } michael@0: michael@0: /** michael@0: * Obtains the {@link ConnRoutePNames#LOCAL_ADDRESS LOCAL_ADDRESS} michael@0: * parameter value. michael@0: * There is no special value that would automatically be mapped to michael@0: * null. You can use the wildcard address (0.0.0.0 for IPv4, michael@0: * :: for IPv6) to override a specific local address in a hierarchy. michael@0: * michael@0: * @param params the parameters in which to look up michael@0: * michael@0: * @return the local address set in the argument parameters, or michael@0: * null if not set michael@0: */ michael@0: public static InetAddress getLocalAddress(HttpParams params) { michael@0: if (params == null) { michael@0: throw new IllegalArgumentException("Parameters must not be null."); michael@0: } michael@0: InetAddress local = (InetAddress) michael@0: params.getParameter(LOCAL_ADDRESS); michael@0: // no explicit unsetting michael@0: return local; michael@0: } michael@0: michael@0: /** michael@0: * Sets the {@link ConnRoutePNames#LOCAL_ADDRESS LOCAL_ADDRESS} michael@0: * parameter value. michael@0: * michael@0: * @param params the parameters in which to set the value michael@0: * @param local the value to set, may be null michael@0: */ michael@0: public static void setLocalAddress(HttpParams params, michael@0: InetAddress local) { michael@0: if (params == null) { michael@0: throw new IllegalArgumentException("Parameters must not be null."); michael@0: } michael@0: params.setParameter(LOCAL_ADDRESS, local); michael@0: } michael@0: michael@0: } michael@0: