Skip to content

Commit e8d420c

Browse files
author
Andrey Hihlovskiy
committed
Merge branch 'master' of https://github.com/akhikhl/unpuzzle
2 parents 64ecd16 + c00558d commit e8d420c

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

libs/unpuzzle-utils/src/main/groovy/org/akhikhl/unpuzzle/utils/Downloader.groovy

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ final class Downloader {
6666

6767
void downloadFile(URL url, File file) throws IOException {
6868
Long remoteContentLength
69+
def connection = openConnection(url)
6970
try {
70-
remoteContentLength = Long.parseLong(url.openConnection().getHeaderField('Content-Length'))
71+
remoteContentLength = Long.parseLong(connection.getHeaderField('Content-Length'))
7172
} catch(NumberFormatException e) {
7273
// no header, download anyway
7374
}
@@ -76,15 +77,30 @@ final class Downloader {
7677
return
7778
}
7879
console.startProgress("Downloading file: ${url.toExternalForm()}")
80+
InputStream inStream = connection.getInputStream();
7981
try {
8082
file.parentFile.mkdirs()
81-
url.openStream().withStream { is ->
83+
inStream.withStream { is ->
8284
file.withOutputStream { os ->
8385
IOUtils.copy(is, new DownloadCountingOutputStream(os))
8486
}
8587
}
8688
} finally {
89+
inStream.close()
8790
console.endProgress()
8891
}
8992
}
93+
94+
URLConnection openConnection(URL url){
95+
String protocol = url.getProtocol()
96+
String user = System.getProperty("${protocol}.proxyUser")
97+
String pw = System.getProperty("${protocol}.proxyPassword")
98+
def connection = url.openConnection();
99+
if(user != null && pw != null ){
100+
connection.setRequestProperty(
101+
"Proxy-Authorization",
102+
"Basic " + Base64.getEncoder().encodeToString("${user}:${pw}".getBytes()));
103+
}
104+
return connection
105+
}
90106
}

0 commit comments

Comments
 (0)