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.http.impl.conn;
29
30 import java.net.InetAddress;
31
32 import org.apache.http.HttpException;
33 import org.apache.http.HttpHost;
34 import org.apache.http.HttpRequest;
35 import org.apache.http.ProtocolException;
36 import org.apache.http.annotation.Contract;
37 import org.apache.http.annotation.ThreadingBehavior;
38 import org.apache.http.client.config.RequestConfig;
39 import org.apache.http.client.protocol.HttpClientContext;
40 import org.apache.http.conn.SchemePortResolver;
41 import org.apache.http.conn.UnsupportedSchemeException;
42 import org.apache.http.conn.routing.HttpRoute;
43 import org.apache.http.conn.routing.HttpRoutePlanner;
44 import org.apache.http.protocol.HttpContext;
45 import org.apache.http.util.Args;
46
47
48
49
50
51
52
53 @Contract(threading = ThreadingBehavior.IMMUTABLE_CONDITIONAL)
54 public class DefaultRoutePlanner implements HttpRoutePlanner {
55
56 private final SchemePortResolver schemePortResolver;
57
58 public DefaultRoutePlanner(final SchemePortResolver schemePortResolver) {
59 super();
60 this.schemePortResolver = schemePortResolver != null ? schemePortResolver :
61 DefaultSchemePortResolver.INSTANCE;
62 }
63
64 @Override
65 public HttpRoute determineRoute(
66 final HttpHost host,
67 final HttpRequest request,
68 final HttpContext context) throws HttpException {
69 Args.notNull(request, "Request");
70 if (host == null) {
71 throw new ProtocolException("Target host is not specified");
72 }
73 final HttpClientContext clientContext = HttpClientContext.adapt(context);
74 final RequestConfig config = clientContext.getRequestConfig();
75 final InetAddress local = config.getLocalAddress();
76 HttpHost proxy = config.getProxy();
77 if (proxy == null) {
78 proxy = determineProxy(host, request, context);
79 }
80
81 final HttpHost target;
82 if (host.getPort() <= 0) {
83 try {
84 target = new HttpHost(
85 host.getHostName(),
86 this.schemePortResolver.resolve(host),
87 host.getSchemeName());
88 } catch (final UnsupportedSchemeException ex) {
89 throw new HttpException(ex.getMessage());
90 }
91 } else {
92 target = host;
93 }
94 final boolean secure = target.getSchemeName().equalsIgnoreCase("https");
95 return proxy == null
96 ? new HttpRoute(target, local, secure)
97 : new HttpRoute(target, local, proxy, secure);
98 }
99
100
101
102
103
104
105 protected HttpHost determineProxy(
106 final HttpHost target,
107 final HttpRequest request,
108 final HttpContext context) throws HttpException {
109 return null;
110 }
111
112 }