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