forked from graalvm/mx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
URLConnectionDownload.java
179 lines (169 loc) · 7.77 KB
/
URLConnectionDownload.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.io.*;
import java.net.*;
import java.nio.file.Paths;
import java.util.*;
import java.util.regex.*;
/**
* Downloads content from a given URL to a given file.
*
* @param path where to write the content
* @param urls the URLs to try, stopping after the first successful one
*/
public class URLConnectionDownload {
/**
* Iterate over list of environment variable to find one that correctly specify an proxy.
*
* @param propPrefix indicates which proxy property to set (i.e., http or https)
* @param proxyEnvVariableNames list of environment variable
* @return a string specifying the proxy url
*/
private static String setProxy(String[] proxyEnvVariableNames, String propPrefix) {
String proxy = null;
String proxyEnvVar = "";
for (String envvar : proxyEnvVariableNames) {
proxy = System.getenv(envvar);
if (proxy != null) {
proxyEnvVar = envvar;
break;
}
}
if (proxy != null) {
Pattern p = Pattern.compile("(?:http://)?([^:]+)(:\\d+)?");
Matcher m = p.matcher(proxy);
if (m.matches()) {
String host = m.group(1);
String port = m.group(2);
System.setProperty(propPrefix + ".proxyHost", host);
if (port != null) {
port = port.substring(1); // strip ':'
System.setProperty(propPrefix + ".proxyPort", port);
}
return proxy;
} else {
System.err.println("Value of " + proxyEnvVar + " is not valid: " + proxy);
}
} else {
System.err.println("** If behind a firewall without direct internet access, use the " + proxyEnvVariableNames[0] + " environment variable (e.g. 'env " + proxyEnvVariableNames[0] +
"=proxy.company.com:80 max ...') or download manually with a web browser.");
}
return "";
}
/**
* Downloads content from a given URL to a given file.
*
* @param args
* --no-progress is an optional first arg to suppress progress meter
* arg[0] is the path where to write the content. The remainder
* of args are the URLs to try, stopping after the first
* successful one
*/
public static void main(String[] args) {
boolean verbose = true;
int firstArgIndex = 0;
if (args[0].equals("--no-progress")) {
firstArgIndex = 1;
verbose = false;
}
File path = new File(args[firstArgIndex]);
String[] urls = new String[args.length - 1 - firstArgIndex];
System.arraycopy(args, firstArgIndex + 1, urls, 0, urls.length);
File parent = path.getParentFile();
makeDirectory(parent);
// Enable use of system proxies
System.setProperty("java.net.useSystemProxies", "true");
// Set standard proxy if any
String proxy = setProxy(new String[]{"HTTP_PROXY", "http_proxy"}, "http");
// Set proxy for secure http if explicitely set, default to http proxy otherwise
String secureProxy = setProxy(new String[]{"HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy"}, "https");
String proxyMsg = "";
if (secureProxy.length() > 0 && proxy.length() > 0 && !secureProxy.equals(proxy)) {
proxyMsg = " via " + proxy + " / " + secureProxy;
} else if (proxy.length() > 0) {
proxyMsg = " via " + proxy;
} else if (secureProxy.length() > 0) {
proxyMsg = " via " + secureProxy;
}
for (String s : urls) {
try {
while (true) {
System.err.println("Downloading " + s + " to " + path + proxyMsg);
URL url = new URL(s);
URLConnection conn = url.openConnection();
// 10 second timeout to establish connection
conn.setConnectTimeout(10000);
if (conn instanceof HttpURLConnection) {
// HttpURLConnection per default follows redirections,
// but not if it changes the protocol (e.g. http ->
// https). While this is a sane default, in our
// situation it's okay to follow a protocol transition.
HttpURLConnection httpconn = (HttpURLConnection) conn;
switch (httpconn.getResponseCode()) {
case HttpURLConnection.HTTP_MOVED_PERM:
case HttpURLConnection.HTTP_MOVED_TEMP:
System.err.println("follow redirect...");
s = httpconn.getHeaderField("Location");
continue;
}
}
InputStream in = conn.getInputStream();
int size = conn.getContentLength();
FileOutputStream out = new FileOutputStream(path);
int read = 0;
byte[] buf = new byte[8192];
int n = 0;
while ((read = in.read(buf)) != -1) {
n += read;
if (verbose) {
long percent = ((long) n * 100 / size);
System.err.print("\r " + n + " bytes " + (size == -1 ? "" : " (" + percent + "%)"));
}
out.write(buf, 0, read);
}
System.err.println();
out.close();
in.close();
return;
}
} catch (MalformedURLException e) {
throw new Error("Error in URL " + s, e);
} catch (IOException e) {
System.err.println("Error reading from " + s + ": " + e);
if (e.toString().contains("the trustAnchors parameter must be non-empty") &&
System.getProperty("java.specification.version").compareTo("1.9") == 0) {
System.err.println("Possible solution: copy cacerts file from OracleJDK to " +
Paths.get(System.getProperty("java.home"), "lib", "security", "cacerts"));
System.err.println(" Source: http://mail.openjdk.java.net/pipermail/core-libs-dev/2015-September/035528.html");
}
path.delete();
}
}
throw new Error("Could not download content to " + path + " from " + Arrays.toString(urls));
}
private static void makeDirectory(File directory) {
if (!directory.exists() && !directory.mkdirs()) {
throw new Error("Could not make directory " + directory);
}
}
}