19
19
import org .apache .commons .compress .archivers .tar .TarArchiveEntry ;
20
20
import org .apache .commons .compress .archivers .tar .TarArchiveInputStream ;
21
21
import org .apache .commons .compress .compressors .gzip .GzipCompressorInputStream ;
22
+ import org .apache .commons .lang .math .NumberUtils ;
22
23
import 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 ;
23
29
import org .apache .http .client .methods .CloseableHttpResponse ;
24
30
import org .apache .http .client .methods .HttpGet ;
31
+ import org .apache .http .impl .client .BasicCredentialsProvider ;
25
32
import org .apache .http .impl .client .CloseableHttpClient ;
26
33
import org .apache .http .impl .client .HttpClientBuilder ;
34
+ import org .apache .http .impl .client .HttpClients ;
27
35
28
36
import java .io .*;
29
37
@@ -48,15 +56,44 @@ public static boolean downloadFile(String remoteUrl, String localPath) throws IO
48
56
File file = new File (localPath );
49
57
if (!file .exists ()) {
50
58
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
+ }
53
91
try (CloseableHttpResponse response = client .execute (new HttpGet (remoteUrl ))) {
54
92
HttpEntity entity = response .getEntity ();
55
93
if (entity != null ) {
56
94
try (FileOutputStream outstream = new FileOutputStream (file )) {
57
95
entity .writeTo (outstream );
58
96
outstream .flush ();
59
- outstream .close ();
60
97
}
61
98
}
62
99
}
0 commit comments