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  
28  package org.apache.hc.client5.http;
29  
30  import java.net.InetAddress;
31  import java.net.InetSocketAddress;
32  import java.util.ArrayList;
33  import java.util.Arrays;
34  import java.util.Collections;
35  import java.util.List;
36  
37  import org.apache.hc.core5.annotation.Contract;
38  import org.apache.hc.core5.annotation.ThreadingBehavior;
39  import org.apache.hc.core5.http.HttpHost;
40  import org.apache.hc.core5.util.Args;
41  import org.apache.hc.core5.util.LangUtils;
42  
43  /**
44   * Connection route definition for HTTP requests.
45   *
46   * @since 4.0
47   */
48  @Contract(threading = ThreadingBehavior.IMMUTABLE)
49  public final class HttpRoute implements RouteInfo, Cloneable {
50  
51      /** The target host to connect to. */
52      private final HttpHost targetHost;
53  
54      /**
55       * The local address to connect from.
56       * {@code null} indicates that the default should be used.
57       */
58      private final InetAddress localAddress;
59  
60      /** The proxy servers, if any. Never null. */
61      private final List<HttpHost> proxyChain;
62  
63      /** Whether the the route is tunnelled through the proxy. */
64      private final TunnelType tunnelled;
65  
66      /** Whether the route is layered. */
67      private final LayerType layered;
68  
69      /** Whether the route is (supposed to be) secure. */
70      private final boolean secure;
71  
72      private HttpRoute(final HttpHost targetHost, final InetAddress local, final List<HttpHost> proxies,
73                       final boolean secure, final TunnelType tunnelled, final LayerType layered) {
74          Args.notNull(targetHost, "Target host");
75          Args.notNegative(targetHost.getPort(), "Target port");
76          this.targetHost = targetHost;
77          this.localAddress = local;
78          if (proxies != null && !proxies.isEmpty()) {
79              this.proxyChain = new ArrayList<>(proxies);
80          } else {
81              this.proxyChain = null;
82          }
83          if (tunnelled == TunnelType.TUNNELLED) {
84              Args.check(this.proxyChain != null, "Proxy required if tunnelled");
85          }
86          this.secure       = secure;
87          this.tunnelled    = tunnelled != null ? tunnelled : TunnelType.PLAIN;
88          this.layered      = layered != null ? layered : LayerType.PLAIN;
89      }
90  
91      /**
92       * Creates a new route with all attributes specified explicitly.
93       *
94       * @param target    the host to which to route
95       * @param local     the local address to route from, or
96       *                  {@code null} for the default
97       * @param proxies   the proxy chain to use, or
98       *                  {@code null} for a direct route
99       * @param secure    {@code true} if the route is (to be) secure,
100      *                  {@code false} otherwise
101      * @param tunnelled the tunnel type of this route
102      * @param layered   the layering type of this route
103      */
104     public HttpRoute(final HttpHost target, final InetAddress local, final HttpHost[] proxies,
105                      final boolean secure, final TunnelType tunnelled, final LayerType layered) {
106         this(target, local, proxies != null ? Arrays.asList(proxies) : null,
107                 secure, tunnelled, layered);
108     }
109 
110     /**
111      * Creates a new route with at most one proxy.
112      *
113      * @param target    the host to which to route
114      * @param local     the local address to route from, or
115      *                  {@code null} for the default
116      * @param proxy     the proxy to use, or
117      *                  {@code null} for a direct route
118      * @param secure    {@code true} if the route is (to be) secure,
119      *                  {@code false} otherwise
120      * @param tunnelled {@code true} if the route is (to be) tunnelled
121      *                  via the proxy,
122      *                  {@code false} otherwise
123      * @param layered   {@code true} if the route includes a
124      *                  layered protocol,
125      *                  {@code false} otherwise
126      */
127     public HttpRoute(final HttpHost target, final InetAddress local, final HttpHost proxy,
128                      final boolean secure, final TunnelType tunnelled, final LayerType layered) {
129         this(target, local, proxy != null ? Collections.singletonList(proxy) : null,
130                 secure, tunnelled, layered);
131     }
132 
133     /**
134      * Creates a new direct route.
135      * That is a route without a proxy.
136      *
137      * @param target    the host to which to route
138      * @param local     the local address to route from, or
139      *                  {@code null} for the default
140      * @param secure    {@code true} if the route is (to be) secure,
141      *                  {@code false} otherwise
142      */
143     public HttpRoute(final HttpHost target, final InetAddress local, final boolean secure) {
144         this(target, local, Collections.emptyList(), secure, TunnelType.PLAIN, LayerType.PLAIN);
145     }
146 
147     /**
148      * Creates a new direct insecure route.
149      *
150      * @param target    the host to which to route
151      */
152     public HttpRoute(final HttpHost target) {
153         this(target, null, Collections.emptyList(), false, TunnelType.PLAIN, LayerType.PLAIN);
154     }
155 
156     /**
157      * Creates a new route through a proxy.
158      * When using this constructor, the {@code proxy} MUST be given.
159      * For convenience, it is assumed that a secure connection will be
160      * layered over a tunnel through the proxy.
161      *
162      * @param target    the host to which to route
163      * @param local     the local address to route from, or
164      *                  {@code null} for the default
165      * @param proxy     the proxy to use
166      * @param secure    {@code true} if the route is (to be) secure,
167      *                  {@code false} otherwise
168      */
169     public HttpRoute(final HttpHost target, final InetAddress local, final HttpHost proxy,
170                      final boolean secure) {
171         this(target, local, Collections.singletonList(Args.notNull(proxy, "Proxy host")), secure,
172              secure ? TunnelType.TUNNELLED : TunnelType.PLAIN,
173              secure ? LayerType.LAYERED    : LayerType.PLAIN);
174     }
175 
176     /**
177      * Creates a new plain route through a proxy.
178      *
179      * @param target    the host to which to route
180      * @param proxy     the proxy to use
181      *
182      * @since 4.3
183      */
184     public HttpRoute(final HttpHost target, final HttpHost proxy) {
185         this(target, null, proxy, false);
186     }
187 
188     @Override
189     public HttpHost getTargetHost() {
190         return this.targetHost;
191     }
192 
193     @Override
194     public InetAddress getLocalAddress() {
195         return this.localAddress;
196     }
197 
198     public InetSocketAddress getLocalSocketAddress() {
199         return this.localAddress != null ? new InetSocketAddress(this.localAddress, 0) : null;
200     }
201 
202     @Override
203     public int getHopCount() {
204         return proxyChain != null ? proxyChain.size() + 1 : 1;
205     }
206 
207     @Override
208     public HttpHost getHopTarget(final int hop) {
209         Args.notNegative(hop, "Hop index");
210         final int hopcount = getHopCount();
211         Args.check(hop < hopcount, "Hop index exceeds tracked route length");
212         if (hop < hopcount - 1) {
213             return this.proxyChain.get(hop);
214         }
215         return this.targetHost;
216     }
217 
218     @Override
219     public HttpHost getProxyHost() {
220         return proxyChain != null && !this.proxyChain.isEmpty() ? this.proxyChain.get(0) : null;
221     }
222 
223     @Override
224     public TunnelType getTunnelType() {
225         return this.tunnelled;
226     }
227 
228     @Override
229     public boolean isTunnelled() {
230         return (this.tunnelled == TunnelType.TUNNELLED);
231     }
232 
233     @Override
234     public LayerType getLayerType() {
235         return this.layered;
236     }
237 
238     @Override
239     public boolean isLayered() {
240         return (this.layered == LayerType.LAYERED);
241     }
242 
243     @Override
244     public boolean isSecure() {
245         return this.secure;
246     }
247 
248     /**
249      * Compares this route to another.
250      *
251      * @param obj         the object to compare with
252      *
253      * @return  {@code true} if the argument is the same route,
254      *          {@code false}
255      */
256     @Override
257     public boolean equals(final Object obj) {
258         if (this == obj) {
259             return true;
260         }
261         if (obj instanceof HttpRoute) {
262             final HttpRoute that = (HttpRoute) obj;
263             return
264                 // Do the cheapest tests first
265                 (this.secure    == that.secure) &&
266                 (this.tunnelled == that.tunnelled) &&
267                 (this.layered   == that.layered) &&
268                 LangUtils.equals(this.targetHost, that.targetHost) &&
269                 LangUtils.equals(this.localAddress, that.localAddress) &&
270                 LangUtils.equals(this.proxyChain, that.proxyChain);
271         }
272         return false;
273     }
274 
275 
276     /**
277      * Generates a hash code for this route.
278      *
279      * @return  the hash code
280      */
281     @Override
282     public int hashCode() {
283         int hash = LangUtils.HASH_SEED;
284         hash = LangUtils.hashCode(hash, this.targetHost);
285         hash = LangUtils.hashCode(hash, this.localAddress);
286         if (this.proxyChain != null) {
287             for (final HttpHost element : this.proxyChain) {
288                 hash = LangUtils.hashCode(hash, element);
289             }
290         }
291         hash = LangUtils.hashCode(hash, this.secure);
292         hash = LangUtils.hashCode(hash, this.tunnelled);
293         hash = LangUtils.hashCode(hash, this.layered);
294         return hash;
295     }
296 
297     /**
298      * Obtains a description of this route.
299      *
300      * @return  a human-readable representation of this route
301      */
302     @Override
303     public String toString() {
304         final StringBuilder cab = new StringBuilder(50 + getHopCount()*30);
305         if (this.localAddress != null) {
306             cab.append(this.localAddress);
307             cab.append("->");
308         }
309         cab.append('{');
310         if (this.tunnelled == TunnelType.TUNNELLED) {
311             cab.append('t');
312         }
313         if (this.layered == LayerType.LAYERED) {
314             cab.append('l');
315         }
316         if (this.secure) {
317             cab.append('s');
318         }
319         cab.append("}->");
320         if (this.proxyChain != null) {
321             for (final HttpHost aProxyChain : this.proxyChain) {
322                 cab.append(aProxyChain);
323                 cab.append("->");
324             }
325         }
326         cab.append(this.targetHost);
327         return cab.toString();
328     }
329 
330     // default implementation of clone() is sufficient
331     @Override
332     public Object clone() throws CloneNotSupportedException {
333         return super.clone();
334     }
335 
336 }