View Javadoc

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