|
72 | 72 | import java.lang.reflect.Field; |
73 | 73 | import java.lang.reflect.Method; |
74 | 74 | import java.lang.reflect.Type; |
75 | | -import java.net.URLConnection; |
76 | | -import java.net.URLEncoder; |
| 75 | +import java.net.*; |
77 | 76 | import java.nio.file.Files; |
78 | 77 | import java.nio.file.Paths; |
79 | 78 | import java.security.GeneralSecurityException; |
@@ -141,18 +140,19 @@ public class ApiClient extends BaseClient{ |
141 | 140 | private Boolean useDualStack; |
142 | 141 |
|
143 | 142 | private boolean autoRetry = DefaultRetryerSetting.DEFAULT_AUTO_RETRY_ENABLED; |
144 | | - |
145 | 143 | private final Retryer retryer = DefaultRetryerSetting.DEFAULT_RETRYER; |
146 | 144 |
|
| 145 | + private String httpProxy; |
| 146 | + private String httpsProxy; |
| 147 | + private String noProxy; |
| 148 | + |
147 | 149 | /* |
148 | 150 | * Constructor for ApiClient |
149 | 151 | */ |
150 | 152 | public ApiClient() { |
151 | 153 | ConnectionPool connectionPool = new ConnectionPool(maxIdleConns, keepAliveDurationMs); |
152 | 154 | httpClient = new OkHttpClient(); |
153 | 155 | httpClient.setConnectionPool(connectionPool); |
154 | | - |
155 | | - |
156 | 156 | verifyingSsl = true; |
157 | 157 |
|
158 | 158 | json = new JSON(); |
@@ -591,25 +591,130 @@ public ApiClient setCustomBootstrapRegion(Set<String> customBootstrapRegion) { |
591 | 591 | } |
592 | 592 |
|
593 | 593 | /** |
594 | | - * Get the use dual stack flag. |
| 594 | + * Get the http proxy. |
| 595 | + * |
| 596 | + * @return http proxy |
| 597 | + */ |
| 598 | + public String getHttpProxy() { |
| 599 | + return this.httpProxy; |
| 600 | + } |
| 601 | + |
| 602 | + /** |
| 603 | + * Set the http proxy. |
| 604 | + * |
| 605 | + * @return Api client |
| 606 | + */ |
| 607 | + public ApiClient setHttpProxy(String httpProxy) { |
| 608 | + this.httpProxy = httpProxy; |
| 609 | + updateClientProxy(); |
| 610 | + return this; |
| 611 | + } |
| 612 | + |
| 613 | + /** |
| 614 | + * Get the https proxy. |
| 615 | + * |
| 616 | + * @return https proxy |
| 617 | + */ |
| 618 | + public String getHttpsProxy() { |
| 619 | + return this.httpsProxy; |
| 620 | + } |
| 621 | + |
| 622 | + /** |
| 623 | + * Set the https proxy. |
| 624 | + * |
| 625 | + * @return Api client |
| 626 | + */ |
| 627 | + public ApiClient setHttpsProxy(String httpsProxy) { |
| 628 | + this.httpsProxy = httpsProxy; |
| 629 | + updateClientProxy(); |
| 630 | + return this; |
| 631 | + } |
| 632 | + |
| 633 | + /** |
| 634 | + * Get the no proxy. |
595 | 635 | * |
596 | | - * @return use dual stack flag |
| 636 | + * @return no proxy |
597 | 637 | */ |
598 | | - public Boolean getUseDualStack() { |
599 | | - return this.useDualStack; |
| 638 | + public String getNoProxy() { |
| 639 | + return this.noProxy; |
600 | 640 | } |
601 | 641 |
|
602 | 642 | /** |
603 | | - * Set the use dual stack flag. |
| 643 | + * Set the no proxy. |
604 | 644 | * |
605 | | - * @param useDualStack boolean |
606 | 645 | * @return Api client |
607 | 646 | */ |
608 | | - public ApiClient setUseDualStack(boolean useDualStack) { |
609 | | - this.useDualStack = useDualStack; |
| 647 | + public ApiClient setNoProxy(String noProxy) { |
| 648 | + this.noProxy = noProxy; |
| 649 | + updateClientProxy(); |
610 | 650 | return this; |
611 | 651 | } |
612 | 652 |
|
| 653 | + private void updateClientProxy() { |
| 654 | + httpClient.setProxySelector(new ProxySelector() { |
| 655 | + @Override |
| 656 | + public List<Proxy> select(URI uri) { |
| 657 | + String targetHost = uri.getHost(); |
| 658 | + |
| 659 | + String envNoProxy = System.getenv("NO_PROXY"); |
| 660 | + if (StringUtils.isEmpty(envNoProxy)) { |
| 661 | + envNoProxy = System.getenv("no_proxy"); |
| 662 | + } |
| 663 | + |
| 664 | + boolean noProxyFlag = false; |
| 665 | + String noProxyList = (StringUtils.isEmpty(noProxy) ? envNoProxy : noProxy); |
| 666 | + if (!StringUtils.isEmpty(noProxyList)) { |
| 667 | + String[] noProxyArr = noProxyList.split(","); |
| 668 | + for (String noProxyHost : noProxyArr) { |
| 669 | + if (noProxyHost.equals(targetHost)) { |
| 670 | + noProxyFlag = true; |
| 671 | + break; |
| 672 | + } |
| 673 | + } |
| 674 | + } |
| 675 | + |
| 676 | + List<Proxy> proxies = new ArrayList<>(); |
| 677 | + if (noProxyFlag) { |
| 678 | + proxies.add(Proxy.NO_PROXY); |
| 679 | + return proxies; |
| 680 | + } |
| 681 | + |
| 682 | + addProxy(proxies, httpProxy, "HTTP_PROXY"); |
| 683 | + addProxy(proxies, httpsProxy, "HTTPS_PROXY"); |
| 684 | + |
| 685 | + return proxies; |
| 686 | + } |
| 687 | + |
| 688 | + @Override |
| 689 | + public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { |
| 690 | + } |
| 691 | + }); |
| 692 | + } |
| 693 | + |
| 694 | + private void addProxy(List<Proxy> proxies, String proxy, String env) { |
| 695 | + if (!StringUtils.isEmpty(proxy)) { |
| 696 | + try { |
| 697 | + URI u = new URI(proxy); |
| 698 | + proxies.add(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(u.getHost(), u.getPort()))); |
| 699 | + } catch (URISyntaxException e) { |
| 700 | + throw new RuntimeException(e); |
| 701 | + } |
| 702 | + } else if (!StringUtils.isEmpty(env)) { |
| 703 | + String envProxy = System.getenv(env.toUpperCase()); |
| 704 | + if (StringUtils.isEmpty(envProxy)) { |
| 705 | + envProxy = System.getenv(env.toLowerCase()); |
| 706 | + } |
| 707 | + if (!StringUtils.isEmpty(envProxy)) { |
| 708 | + try { |
| 709 | + URI u = new URI(envProxy); |
| 710 | + proxies.add(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(u.getHost(), u.getPort()))); |
| 711 | + } catch (URISyntaxException e) { |
| 712 | + throw new RuntimeException(e); |
| 713 | + } |
| 714 | + } |
| 715 | + } |
| 716 | + } |
| 717 | + |
613 | 718 | /** |
614 | 719 | * Format the given parameter object into string. |
615 | 720 | * |
|
0 commit comments