Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | * ==================================================================== |
michael@0 | 3 | * |
michael@0 | 4 | * Licensed to the Apache Software Foundation (ASF) under one or more |
michael@0 | 5 | * contributor license agreements. See the NOTICE file distributed with |
michael@0 | 6 | * this work for additional information regarding copyright ownership. |
michael@0 | 7 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
michael@0 | 8 | * (the "License"); you may not use this file except in compliance with |
michael@0 | 9 | * the License. You may obtain a copy of the License at |
michael@0 | 10 | * |
michael@0 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 12 | * |
michael@0 | 13 | * Unless required by applicable law or agreed to in writing, software |
michael@0 | 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
michael@0 | 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
michael@0 | 16 | * See the License for the specific language governing permissions and |
michael@0 | 17 | * limitations under the License. |
michael@0 | 18 | * ==================================================================== |
michael@0 | 19 | * |
michael@0 | 20 | * This software consists of voluntary contributions made by many |
michael@0 | 21 | * individuals on behalf of the Apache Software Foundation. For more |
michael@0 | 22 | * information on the Apache Software Foundation, please see |
michael@0 | 23 | * <http://www.apache.org/>. |
michael@0 | 24 | * |
michael@0 | 25 | */ |
michael@0 | 26 | |
michael@0 | 27 | package ch.boye.httpclientandroidlib.conn.params; |
michael@0 | 28 | |
michael@0 | 29 | import java.net.InetAddress; |
michael@0 | 30 | |
michael@0 | 31 | import ch.boye.httpclientandroidlib.annotation.Immutable; |
michael@0 | 32 | |
michael@0 | 33 | import ch.boye.httpclientandroidlib.HttpHost; |
michael@0 | 34 | import ch.boye.httpclientandroidlib.params.HttpParams; |
michael@0 | 35 | import ch.boye.httpclientandroidlib.conn.routing.HttpRoute; |
michael@0 | 36 | |
michael@0 | 37 | /** |
michael@0 | 38 | * An adaptor for manipulating HTTP routing parameters |
michael@0 | 39 | * in {@link HttpParams}. |
michael@0 | 40 | * |
michael@0 | 41 | * @since 4.0 |
michael@0 | 42 | */ |
michael@0 | 43 | @Immutable |
michael@0 | 44 | public class ConnRouteParams implements ConnRoutePNames { |
michael@0 | 45 | |
michael@0 | 46 | /** |
michael@0 | 47 | * A special value indicating "no host". |
michael@0 | 48 | * This relies on a nonsense scheme name to avoid conflicts |
michael@0 | 49 | * with actual hosts. Note that this is a <i>valid</i> host. |
michael@0 | 50 | */ |
michael@0 | 51 | public static final HttpHost NO_HOST = |
michael@0 | 52 | new HttpHost("127.0.0.255", 0, "no-host"); // Immutable |
michael@0 | 53 | |
michael@0 | 54 | /** |
michael@0 | 55 | * A special value indicating "no route". |
michael@0 | 56 | * This is a route with {@link #NO_HOST} as the target. |
michael@0 | 57 | */ |
michael@0 | 58 | public static final HttpRoute NO_ROUTE = new HttpRoute(NO_HOST); // Immutable |
michael@0 | 59 | |
michael@0 | 60 | /** Disabled default constructor. */ |
michael@0 | 61 | private ConnRouteParams() { |
michael@0 | 62 | // no body |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | /** |
michael@0 | 66 | * Obtains the {@link ConnRoutePNames#DEFAULT_PROXY DEFAULT_PROXY} |
michael@0 | 67 | * parameter value. |
michael@0 | 68 | * {@link #NO_HOST} will be mapped to <code>null</code>, |
michael@0 | 69 | * to allow unsetting in a hierarchy. |
michael@0 | 70 | * |
michael@0 | 71 | * @param params the parameters in which to look up |
michael@0 | 72 | * |
michael@0 | 73 | * @return the default proxy set in the argument parameters, or |
michael@0 | 74 | * <code>null</code> if not set |
michael@0 | 75 | */ |
michael@0 | 76 | public static HttpHost getDefaultProxy(HttpParams params) { |
michael@0 | 77 | if (params == null) { |
michael@0 | 78 | throw new IllegalArgumentException("Parameters must not be null."); |
michael@0 | 79 | } |
michael@0 | 80 | HttpHost proxy = (HttpHost) |
michael@0 | 81 | params.getParameter(DEFAULT_PROXY); |
michael@0 | 82 | if ((proxy != null) && NO_HOST.equals(proxy)) { |
michael@0 | 83 | // value is explicitly unset |
michael@0 | 84 | proxy = null; |
michael@0 | 85 | } |
michael@0 | 86 | return proxy; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | /** |
michael@0 | 90 | * Sets the {@link ConnRoutePNames#DEFAULT_PROXY DEFAULT_PROXY} |
michael@0 | 91 | * parameter value. |
michael@0 | 92 | * |
michael@0 | 93 | * @param params the parameters in which to set the value |
michael@0 | 94 | * @param proxy the value to set, may be <code>null</code>. |
michael@0 | 95 | * Note that {@link #NO_HOST} will be mapped to |
michael@0 | 96 | * <code>null</code> by {@link #getDefaultProxy}, |
michael@0 | 97 | * to allow for explicit unsetting in hierarchies. |
michael@0 | 98 | */ |
michael@0 | 99 | public static void setDefaultProxy(HttpParams params, |
michael@0 | 100 | HttpHost proxy) { |
michael@0 | 101 | if (params == null) { |
michael@0 | 102 | throw new IllegalArgumentException("Parameters must not be null."); |
michael@0 | 103 | } |
michael@0 | 104 | params.setParameter(DEFAULT_PROXY, proxy); |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | /** |
michael@0 | 108 | * Obtains the {@link ConnRoutePNames#FORCED_ROUTE FORCED_ROUTE} |
michael@0 | 109 | * parameter value. |
michael@0 | 110 | * {@link #NO_ROUTE} will be mapped to <code>null</code>, |
michael@0 | 111 | * to allow unsetting in a hierarchy. |
michael@0 | 112 | * |
michael@0 | 113 | * @param params the parameters in which to look up |
michael@0 | 114 | * |
michael@0 | 115 | * @return the forced route set in the argument parameters, or |
michael@0 | 116 | * <code>null</code> if not set |
michael@0 | 117 | */ |
michael@0 | 118 | public static HttpRoute getForcedRoute(HttpParams params) { |
michael@0 | 119 | if (params == null) { |
michael@0 | 120 | throw new IllegalArgumentException("Parameters must not be null."); |
michael@0 | 121 | } |
michael@0 | 122 | HttpRoute route = (HttpRoute) |
michael@0 | 123 | params.getParameter(FORCED_ROUTE); |
michael@0 | 124 | if ((route != null) && NO_ROUTE.equals(route)) { |
michael@0 | 125 | // value is explicitly unset |
michael@0 | 126 | route = null; |
michael@0 | 127 | } |
michael@0 | 128 | return route; |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | /** |
michael@0 | 132 | * Sets the {@link ConnRoutePNames#FORCED_ROUTE FORCED_ROUTE} |
michael@0 | 133 | * parameter value. |
michael@0 | 134 | * |
michael@0 | 135 | * @param params the parameters in which to set the value |
michael@0 | 136 | * @param route the value to set, may be <code>null</code>. |
michael@0 | 137 | * Note that {@link #NO_ROUTE} will be mapped to |
michael@0 | 138 | * <code>null</code> by {@link #getForcedRoute}, |
michael@0 | 139 | * to allow for explicit unsetting in hierarchies. |
michael@0 | 140 | */ |
michael@0 | 141 | public static void setForcedRoute(HttpParams params, |
michael@0 | 142 | HttpRoute route) { |
michael@0 | 143 | if (params == null) { |
michael@0 | 144 | throw new IllegalArgumentException("Parameters must not be null."); |
michael@0 | 145 | } |
michael@0 | 146 | params.setParameter(FORCED_ROUTE, route); |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | /** |
michael@0 | 150 | * Obtains the {@link ConnRoutePNames#LOCAL_ADDRESS LOCAL_ADDRESS} |
michael@0 | 151 | * parameter value. |
michael@0 | 152 | * There is no special value that would automatically be mapped to |
michael@0 | 153 | * <code>null</code>. You can use the wildcard address (0.0.0.0 for IPv4, |
michael@0 | 154 | * :: for IPv6) to override a specific local address in a hierarchy. |
michael@0 | 155 | * |
michael@0 | 156 | * @param params the parameters in which to look up |
michael@0 | 157 | * |
michael@0 | 158 | * @return the local address set in the argument parameters, or |
michael@0 | 159 | * <code>null</code> if not set |
michael@0 | 160 | */ |
michael@0 | 161 | public static InetAddress getLocalAddress(HttpParams params) { |
michael@0 | 162 | if (params == null) { |
michael@0 | 163 | throw new IllegalArgumentException("Parameters must not be null."); |
michael@0 | 164 | } |
michael@0 | 165 | InetAddress local = (InetAddress) |
michael@0 | 166 | params.getParameter(LOCAL_ADDRESS); |
michael@0 | 167 | // no explicit unsetting |
michael@0 | 168 | return local; |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | /** |
michael@0 | 172 | * Sets the {@link ConnRoutePNames#LOCAL_ADDRESS LOCAL_ADDRESS} |
michael@0 | 173 | * parameter value. |
michael@0 | 174 | * |
michael@0 | 175 | * @param params the parameters in which to set the value |
michael@0 | 176 | * @param local the value to set, may be <code>null</code> |
michael@0 | 177 | */ |
michael@0 | 178 | public static void setLocalAddress(HttpParams params, |
michael@0 | 179 | InetAddress local) { |
michael@0 | 180 | if (params == null) { |
michael@0 | 181 | throw new IllegalArgumentException("Parameters must not be null."); |
michael@0 | 182 | } |
michael@0 | 183 | params.setParameter(LOCAL_ADDRESS, local); |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | } |
michael@0 | 187 |