View Javadoc

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