1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.client5.http.impl.routing;
29
30 import java.net.InetAddress;
31
32 import org.apache.hc.client5.http.HttpRoute;
33 import org.apache.hc.client5.http.SchemePortResolver;
34 import org.apache.hc.client5.http.config.RequestConfig;
35 import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
36 import org.apache.hc.client5.http.protocol.HttpClientContext;
37 import org.apache.hc.client5.http.routing.HttpRoutePlanner;
38 import org.apache.hc.client5.http.routing.RoutingSupport;
39 import org.apache.hc.core5.annotation.Contract;
40 import org.apache.hc.core5.annotation.ThreadingBehavior;
41 import org.apache.hc.core5.http.HttpException;
42 import org.apache.hc.core5.http.HttpHost;
43 import org.apache.hc.core5.http.ProtocolException;
44 import org.apache.hc.core5.http.protocol.HttpContext;
45
46
47
48
49
50
51
52 @Contract(threading = ThreadingBehavior.STATELESS)
53 public class DefaultRoutePlanner implements HttpRoutePlanner {
54
55 private final SchemePortResolver schemePortResolver;
56
57 public DefaultRoutePlanner(final SchemePortResolver schemePortResolver) {
58 super();
59 this.schemePortResolver = schemePortResolver != null ? schemePortResolver : DefaultSchemePortResolver.INSTANCE;
60 }
61
62 @Override
63 public final HttpRoute determineRoute(final HttpHost host, final HttpContext context) throws HttpException {
64 if (host == null) {
65 throw new ProtocolException("Target host is not specified");
66 }
67 final HttpClientContext clientContext = HttpClientContext.adapt(context);
68 final RequestConfig config = clientContext.getRequestConfig();
69 HttpHost proxy = config.getProxy();
70 if (proxy == null) {
71 proxy = determineProxy(host, context);
72 }
73 final HttpHost target = RoutingSupport.normalize(host, schemePortResolver);
74 if (target.getPort() < 0) {
75 throw new ProtocolException("Unroutable protocol scheme: " + target);
76 }
77 final boolean secure = target.getSchemeName().equalsIgnoreCase("https");
78 if (proxy == null) {
79 return new HttpRoute(target, determineLocalAddress(target, context), secure);
80 }
81 return new HttpRoute(target, determineLocalAddress(proxy, context), proxy, secure);
82 }
83
84
85
86
87
88
89 protected HttpHost determineProxy(
90 final HttpHost target,
91 final HttpContext context) throws HttpException {
92 return null;
93 }
94
95
96
97
98
99
100 protected InetAddress determineLocalAddress(
101 final HttpHost firstHop,
102 final HttpContext context) throws HttpException {
103 return null;
104 }
105
106 }