1919import org .apache .commons .compress .archivers .tar .TarArchiveEntry ;
2020import org .apache .commons .compress .archivers .tar .TarArchiveInputStream ;
2121import org .apache .commons .compress .compressors .gzip .GzipCompressorInputStream ;
22+ import org .apache .commons .lang .math .NumberUtils ;
2223import org .apache .http .HttpEntity ;
24+ import org .apache .http .HttpHost ;
25+ import org .apache .http .auth .AuthScope ;
26+ import org .apache .http .auth .UsernamePasswordCredentials ;
27+ import org .apache .http .client .CredentialsProvider ;
28+ import org .apache .http .client .config .RequestConfig ;
2329import org .apache .http .client .methods .CloseableHttpResponse ;
2430import org .apache .http .client .methods .HttpGet ;
31+ import org .apache .http .impl .client .BasicCredentialsProvider ;
2532import org .apache .http .impl .client .CloseableHttpClient ;
2633import org .apache .http .impl .client .HttpClientBuilder ;
34+ import org .apache .http .impl .client .HttpClients ;
2735
2836import java .io .*;
2937
@@ -48,15 +56,44 @@ public static boolean downloadFile(String remoteUrl, String localPath) throws IO
4856 File file = new File (localPath );
4957 if (!file .exists ()) {
5058 file .getParentFile ().mkdirs ();
51- HttpClientBuilder builder = HttpClientBuilder .create ();
52- CloseableHttpClient client = builder .build ();
59+
60+ String scheme = "http" ;
61+ if (remoteUrl .toLowerCase ().startsWith ("https" )){
62+ scheme = "https" ;
63+ }
64+ String proxyHost = System .getProperty (scheme + ".proxyHost" );
65+ String proxyPort = System .getProperty (scheme + ".proxyPort" );
66+ String proxyUser = System .getProperty (scheme + ".proxyUser" );
67+ String proxyPass = System .getProperty (scheme + ".proxyPassword" );
68+
69+ CloseableHttpClient client = null ;
70+ if (proxyHost == null || proxyHost .isEmpty () || !NumberUtils .isNumber (proxyPort )) {
71+ HttpClientBuilder builder = HttpClientBuilder .create ();
72+ client = builder .build ();
73+ } else {
74+ HttpHost proxy = new HttpHost (proxyHost , Integer .parseInt (proxyPort ), scheme );
75+ CredentialsProvider credsProvider = new BasicCredentialsProvider ();
76+ UsernamePasswordCredentials proxyCredentials = null ;
77+ if (proxyUser != null && proxyPass != null ){
78+ proxyCredentials = new UsernamePasswordCredentials (proxyUser , proxyPass );
79+ credsProvider .setCredentials (
80+ new AuthScope (proxy ),
81+ proxyCredentials );
82+ }
83+ RequestConfig config = RequestConfig .custom ()
84+ .setProxy (proxy )
85+ .build ();
86+ client = HttpClients .custom ()
87+ .setDefaultCredentialsProvider (credsProvider )
88+ .setDefaultRequestConfig (config )
89+ .build ();
90+ }
5391 try (CloseableHttpResponse response = client .execute (new HttpGet (remoteUrl ))) {
5492 HttpEntity entity = response .getEntity ();
5593 if (entity != null ) {
5694 try (FileOutputStream outstream = new FileOutputStream (file )) {
5795 entity .writeTo (outstream );
5896 outstream .flush ();
59- outstream .close ();
6097 }
6198 }
6299 }
0 commit comments