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.Contract;
33  import org.apache.http.annotation.ThreadingBehavior;
34  import org.apache.http.conn.routing.HttpRoute;
35  import org.apache.http.params.HttpParams;
36  import org.apache.http.util.Args;
37  
38  /**
39   * An adaptor for manipulating HTTP routing parameters
40   * in {@link HttpParams}.
41   *
42   * @since 4.0
43   *
44   * @deprecated (4.3) use {@link org.apache.http.client.config.RequestConfig}.
45   */
46  @Deprecated
47  @Contract(threading = ThreadingBehavior.IMMUTABLE)
48  public class ConnRouteParams implements ConnRoutePNames {
49  
50      /**
51       * A special value indicating "no host".
52       * This relies on a nonsense scheme name to avoid conflicts
53       * with actual hosts. Note that this is a <i>valid</i> host.
54       */
55      public static final HttpHost NO_HOST =
56          new HttpHost("127.0.0.255", 0, "no-host"); // Immutable
57  
58      /**
59       * A special value indicating "no route".
60       * This is a route with {@link #NO_HOST} as the target.
61       */
62      public static final HttpRoute NO_ROUTE = new HttpRoute(NO_HOST); // Immutable
63  
64      /** Disabled default constructor. */
65      private ConnRouteParams() {
66          // no body
67      }
68  
69      /**
70       * Obtains the {@link ConnRoutePNames#DEFAULT_PROXY DEFAULT_PROXY}
71       * parameter value.
72       * {@link #NO_HOST} will be mapped to {@code null},
73       * to allow unsetting in a hierarchy.
74       *
75       * @param params    the parameters in which to look up
76       *
77       * @return  the default proxy set in the argument parameters, or
78       *          {@code null} if not set
79       */
80      public static HttpHost getDefaultProxy(final HttpParams params) {
81          Args.notNull(params, "Parameters");
82          HttpHost proxy = (HttpHost)
83              params.getParameter(DEFAULT_PROXY);
84          if ((proxy != null) && NO_HOST.equals(proxy)) {
85              // value is explicitly unset
86              proxy = null;
87          }
88          return proxy;
89      }
90  
91      /**
92       * Sets the {@link ConnRoutePNames#DEFAULT_PROXY DEFAULT_PROXY}
93       * parameter value.
94       *
95       * @param params    the parameters in which to set the value
96       * @param proxy     the value to set, may be {@code null}.
97       *                  Note that {@link #NO_HOST} will be mapped to
98       *                  {@code null} by {@link #getDefaultProxy},
99       *                  to allow for explicit unsetting in hierarchies.
100      */
101     public static void setDefaultProxy(final HttpParams params,
102                                              final HttpHost proxy) {
103         Args.notNull(params, "Parameters");
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},
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} if not set
117      */
118     public static HttpRoute getForcedRoute(final HttpParams params) {
119         Args.notNull(params, "Parameters");
120         HttpRoute route = (HttpRoute)
121             params.getParameter(FORCED_ROUTE);
122         if ((route != null) && NO_ROUTE.equals(route)) {
123             // value is explicitly unset
124             route = null;
125         }
126         return route;
127     }
128 
129     /**
130      * Sets the {@link ConnRoutePNames#FORCED_ROUTE FORCED_ROUTE}
131      * parameter value.
132      *
133      * @param params    the parameters in which to set the value
134      * @param route     the value to set, may be {@code null}.
135      *                  Note that {@link #NO_ROUTE} will be mapped to
136      *                  {@code null} by {@link #getForcedRoute},
137      *                  to allow for explicit unsetting in hierarchies.
138      */
139     public static void setForcedRoute(final HttpParams params,
140                                             final HttpRoute route) {
141         Args.notNull(params, "Parameters");
142         params.setParameter(FORCED_ROUTE, route);
143     }
144 
145     /**
146      * Obtains the {@link ConnRoutePNames#LOCAL_ADDRESS LOCAL_ADDRESS}
147      * parameter value.
148      * There is no special value that would automatically be mapped to
149      * {@code null}. You can use the wildcard address (0.0.0.0 for IPv4,
150      * :: for IPv6) to override a specific local address in a hierarchy.
151      *
152      * @param params    the parameters in which to look up
153      *
154      * @return  the local address set in the argument parameters, or
155      *          {@code null} if not set
156      */
157     public static InetAddress getLocalAddress(final HttpParams params) {
158         Args.notNull(params, "Parameters");
159         final InetAddress local = (InetAddress)
160             params.getParameter(LOCAL_ADDRESS);
161         // no explicit unsetting
162         return local;
163     }
164 
165     /**
166      * Sets the {@link ConnRoutePNames#LOCAL_ADDRESS LOCAL_ADDRESS}
167      * parameter value.
168      *
169      * @param params    the parameters in which to set the value
170      * @param local     the value to set, may be {@code null}
171      */
172     public static void setLocalAddress(final HttpParams params,
173                                              final InetAddress local) {
174         Args.notNull(params, "Parameters");
175         params.setParameter(LOCAL_ADDRESS, local);
176     }
177 
178 }
179