Mercurial > hg > truffle
annotate mxtool/URLConnectionDownload.java @ 18670:8aeee4179e22
Fixing checkstyle issues.
author | Stefan Anzinger <stefan.anzinger@oracle.com> |
---|---|
date | Fri, 12 Dec 2014 20:54:59 +0100 |
parents | 07462ba5a12a |
children |
rev | line source |
---|---|
3723
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
1 /* |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
4 * |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
5 * This code is free software; you can redistribute it and/or modify it |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
6 * under the terms of the GNU General Public License version 2 only, as |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
7 * published by the Free Software Foundation. |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
8 * |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
13 * accompanied this code). |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
14 * |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
15 * You should have received a copy of the GNU General Public License version |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
18 * |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
21 * questions. |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
22 */ |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
23 import java.io.*; |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
24 import java.net.*; |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
25 import java.util.*; |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
26 import java.util.regex.*; |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
27 |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
28 /** |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
29 * Downloads content from a given URL to a given file. |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
30 * |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
31 * @param path where to write the content |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
32 * @param urls the URLs to try, stopping after the first successful one |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
33 */ |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
34 public class URLConnectionDownload { |
17385
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
35 /** |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
36 * Iterate over list of environment variable to find one that correctly specify an proxy. |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
37 * |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
38 * @param propPrefix indicates which proxy property to set (i.e., http or https) |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
39 * @param proxyEnvVariableNames list of environment variable |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
40 * @return a string specifying the proxy url |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
41 */ |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
42 private static String setProxy(String[] proxyEnvVariableNames, String propPrefix) { |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
43 String proxy = null; |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
44 String proxyEnvVar = ""; |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
45 for (String envvar : proxyEnvVariableNames) { |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
46 proxy = System.getenv(envvar); |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
47 if (proxy != null) { |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
48 proxyEnvVar = envvar; |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
49 break; |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
50 } |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
51 } |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
52 if (proxy != null) { |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
53 Pattern p = Pattern.compile("(?:http://)?([^:]+)(:\\d+)?"); |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
54 Matcher m = p.matcher(proxy); |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
55 if (m.matches()) { |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
56 String host = m.group(1); |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
57 String port = m.group(2); |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
58 System.setProperty(propPrefix + ".proxyHost", host); |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
59 if (port != null) { |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
60 port = port.substring(1); // strip ':' |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
61 System.setProperty(propPrefix + ".proxyPort", port); |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
62 } |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
63 return proxy; |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
64 } else { |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
65 System.err.println("Value of " + proxyEnvVar + " is not valid: " + proxy); |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
66 } |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
67 } else { |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
68 System.err.println("** If behind a firewall without direct internet access, use the " + proxyEnvVariableNames[0] + " environment variable (e.g. 'env " + proxyEnvVariableNames[0] + |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
69 "=proxy.company.com:80 max ...') or download manually with a web browser."); |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
70 } |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
71 return ""; |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
72 } |
3723
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
73 |
17385
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
74 /** |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
75 * Downloads content from a given URL to a given file. |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
76 * |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
77 * @param args arg[0] is the path where to write the content. The remainder of args are the URLs |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
78 * to try, stopping after the first successful one |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
79 */ |
3723
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
80 public static void main(String[] args) { |
15922
2022366b513c
mx: add verbose mode to download helper
Bernhard Urban <bernhard.urban@jku.at>
parents:
5023
diff
changeset
|
81 File path = new File(args[0]); |
2022366b513c
mx: add verbose mode to download helper
Bernhard Urban <bernhard.urban@jku.at>
parents:
5023
diff
changeset
|
82 boolean verbose = args[1].equals("-v"); |
2022366b513c
mx: add verbose mode to download helper
Bernhard Urban <bernhard.urban@jku.at>
parents:
5023
diff
changeset
|
83 int offset = verbose ? 2 : 1; |
2022366b513c
mx: add verbose mode to download helper
Bernhard Urban <bernhard.urban@jku.at>
parents:
5023
diff
changeset
|
84 String[] urls = new String[args.length - offset]; |
2022366b513c
mx: add verbose mode to download helper
Bernhard Urban <bernhard.urban@jku.at>
parents:
5023
diff
changeset
|
85 System.arraycopy(args, offset, urls, 0, urls.length); |
3723
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
86 |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
87 File parent = path.getParentFile(); |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
88 makeDirectory(parent); |
17385
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
89 |
3723
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
90 // Enable use of system proxies |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
91 System.setProperty("java.net.useSystemProxies", "true"); |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
92 |
17385
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
93 // Set standard proxy if any |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
94 String proxy = setProxy(new String[]{"HTTP_PROXY", "http_proxy"}, "http"); |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
95 // Set proxy for secure http if explicitely set, default to http proxy otherwise |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
96 String secureProxy = setProxy(new String[]{"HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy"}, "https"); |
3723
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
97 String proxyMsg = ""; |
17385
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
98 if (secureProxy.length() > 0 && proxy.length() > 0 && !secureProxy.equals(proxy)) { |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
99 proxyMsg = " via " + proxy + " / " + secureProxy; |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
100 } else if (proxy.length() > 0) { |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
101 proxyMsg = " via " + proxy; |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
102 } else if (secureProxy.length() > 0) { |
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
103 proxyMsg = " via " + secureProxy; |
3723
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
104 } |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
105 |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
106 for (String s : urls) { |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
107 try { |
17345
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
108 while (true) { |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
109 System.err.println("Downloading " + s + " to " + path + proxyMsg); |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
110 URL url = new URL(s); |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
111 URLConnection conn = url.openConnection(); |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
112 // 10 second timeout to establish connection |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
113 conn.setConnectTimeout(10000); |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
114 |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
115 if (conn instanceof HttpURLConnection) { |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
116 // HttpURLConnection per default follows redirections, |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
117 // but not if it changes the protocol (e.g. http -> |
17385
07462ba5a12a
mxtools: automatically set system property https proxy
Paul Woegerer <paul.woegerer@oracle.com>
parents:
17345
diff
changeset
|
118 // https). While this is a sane default, in our |
17345
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
119 // situation it's okay to follow a protocol transition. |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
120 HttpURLConnection httpconn = (HttpURLConnection) conn; |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
121 switch (httpconn.getResponseCode()) { |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
122 case HttpURLConnection.HTTP_MOVED_PERM: |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
123 case HttpURLConnection.HTTP_MOVED_TEMP: |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
124 System.err.println("follow redirect..."); |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
125 s = httpconn.getHeaderField("Location"); |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
126 continue; |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
127 } |
15922
2022366b513c
mx: add verbose mode to download helper
Bernhard Urban <bernhard.urban@jku.at>
parents:
5023
diff
changeset
|
128 } |
17345
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
129 InputStream in = conn.getInputStream(); |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
130 int size = conn.getContentLength(); |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
131 FileOutputStream out = new FileOutputStream(path); |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
132 int read = 0; |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
133 byte[] buf = new byte[8192]; |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
134 int n = 0; |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
135 while ((read = in.read(buf)) != -1) { |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
136 n += read; |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
137 if (verbose) { |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
138 long percent = ((long) n * 100 / size); |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
139 System.err.print("\r " + n + " bytes " + (size == -1 ? "" : " (" + percent + "%)")); |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
140 } |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
141 out.write(buf, 0, read); |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
142 } |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
143 System.err.println(); |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
144 out.close(); |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
145 in.close(); |
3152f72f5cda
mx: follow redirects in URLConnectionDownload util
Bernhard Urban <bernhard.urban@jku.at>
parents:
15922
diff
changeset
|
146 return; |
3723
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
147 } |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
148 } catch (MalformedURLException e) { |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
149 throw new Error("Error in URL " + s, e); |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
150 } catch (IOException e) { |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
151 System.err.println("Error reading from " + s + ": " + e); |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
152 path.delete(); |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
153 } |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
154 } |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
155 throw new Error("Could not download content to " + path + " from " + Arrays.toString(urls)); |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
156 } |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
157 |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
158 private static void makeDirectory(File directory) { |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
159 if (!directory.exists() && !directory.mkdirs()) { |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
160 throw new Error("Could not make directory " + directory); |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
161 } |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
162 } |
6c5f528c7aac
Added a copy of the mxtool to repo.
Doug Simon <doug.simon@oracle.com>
parents:
diff
changeset
|
163 } |