|
1 | 1 | package com.akamai.edgegrid.signer.apachehttpclient5; |
2 | 2 |
|
3 | 3 | import com.akamai.edgegrid.signer.ClientCredential; |
4 | | -import org.apache.hc.client5.http.impl.routing.SystemDefaultRoutePlanner; |
| 4 | +import org.apache.hc.client5.http.HttpRoute; |
| 5 | +import org.apache.hc.client5.http.routing.HttpRoutePlanner; |
| 6 | +import org.apache.hc.core5.annotation.Contract; |
| 7 | +import org.apache.hc.core5.annotation.ThreadingBehavior; |
| 8 | +import org.apache.hc.core5.http.HttpException; |
5 | 9 | import org.apache.hc.core5.http.HttpHost; |
6 | 10 | import org.apache.hc.core5.http.protocol.HttpContext; |
7 | 11 |
|
8 | | -import java.net.ProxySelector; |
9 | | - |
10 | | -public class ApacheHttpClient5EdgeGridRoutePlanner extends SystemDefaultRoutePlanner { |
| 12 | +/** |
| 13 | + * Apache HTTP Client binding for EdgeGrid route planner for computing {@link HttpRoute}. |
| 14 | + * |
| 15 | + */ |
| 16 | +@Contract(threading = ThreadingBehavior.STATELESS) |
| 17 | +public class ApacheHttpClient5EdgeGridRoutePlanner implements HttpRoutePlanner { |
11 | 18 |
|
12 | 19 | private final ClientCredential clientCredential; |
13 | 20 |
|
| 21 | + /** |
| 22 | + * Creates an EdgeGrid route planner using {@link ClientCredential}. |
| 23 | + * |
| 24 | + * @param clientCredential a {@link ClientCredential} |
| 25 | + */ |
14 | 26 | public ApacheHttpClient5EdgeGridRoutePlanner(ClientCredential clientCredential) { |
15 | | - super(ProxySelector.getDefault()); |
16 | 27 | this.clientCredential = clientCredential; |
17 | 28 | } |
18 | 29 |
|
19 | 30 | @Override |
20 | | - protected HttpHost determineProxy(HttpHost target, HttpContext context) { |
| 31 | + public HttpRoute determineRoute(HttpHost target, HttpContext context) throws HttpException { |
21 | 32 | var hostname = clientCredential.getHost(); |
22 | | - int port = -1; |
| 33 | + int port = 443; |
23 | 34 | final int pos = hostname.lastIndexOf(":"); |
24 | 35 | if (pos > 0) { |
25 | 36 | try { |
26 | 37 | port = Integer.parseInt(hostname.substring(pos + 1)); |
| 38 | + if (port <= 0 || port > 65535) { |
| 39 | + throw new NumberFormatException(); |
| 40 | + } |
27 | 41 | } catch (NumberFormatException ex) { |
28 | 42 | throw new IllegalArgumentException("Host contains invalid port number: " + hostname); |
29 | 43 | } |
30 | 44 | hostname = hostname.substring(0, pos); |
31 | 45 | } |
32 | | - return new HttpHost("https", hostname, port); |
| 46 | + HttpHost host = new HttpHost("https", hostname, port); |
| 47 | + return new HttpRoute(host, null, true); |
33 | 48 | } |
34 | 49 | } |
| 50 | + |
| 51 | + |
0 commit comments