|
| 1 | +import java.io.*; |
| 2 | +import java.net.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +import org.apache.tools.ant.BuildException; |
| 6 | +import org.apache.tools.ant.Task; |
| 7 | + |
| 8 | + |
| 9 | +/** |
| 10 | + * Ant Task for downloading the latest JRE or JDK from Oracle. |
| 11 | + */ |
| 12 | +public class Downloader extends Task { |
| 13 | + static final String COOKIE = |
| 14 | + "gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; " + |
| 15 | + "oraclelicense=accept-securebackup-cookie"; |
| 16 | + |
| 17 | + private int version; |
| 18 | + private int update; |
| 19 | + private int build; |
| 20 | + |
| 21 | + private boolean jdk; |
| 22 | + |
| 23 | + private File baseDir; |
| 24 | + private boolean includeRecorder; |
| 25 | + |
| 26 | + |
| 27 | + public Downloader() { } |
| 28 | + |
| 29 | + |
| 30 | + public void setVersion(int version) { |
| 31 | + this.version = version; |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + public void setUpdate(int update) { |
| 36 | + this.update = update; |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | + public void setBuild(int build) { |
| 41 | + this.build = build; |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + public void setJDK(boolean jdk) { |
| 46 | + this.jdk = jdk; |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + public void execute() throws BuildException { |
| 51 | + //if (baseDir == null) { |
| 52 | + // throw new BuildException("dir parameter must be set!"); |
| 53 | + //} |
| 54 | + |
| 55 | + /* |
| 56 | + downloadJRE("linux-i586.tar.gz"); |
| 57 | + downloadJRE("linux-x64.tar.gz"); |
| 58 | + downloadJRE("windows-i586.tar.gz"); |
| 59 | + downloadJRE("windows-x64.tar.gz"); |
| 60 | + downloadJRE("windows-i586.exe"); |
| 61 | + downloadJRE("windows-x64.exe"); |
| 62 | + downloadJRE("macosx-x64.dmg"); |
| 63 | + downloadJRE("macosx-x64.tar.gz"); |
| 64 | +
|
| 65 | + downloadJDK("linux-i586.tar.gz"); |
| 66 | + downloadJDK("linux-x64.tar.gz"); |
| 67 | + downloadJDK("windows-i586.exe"); |
| 68 | + downloadJDK("windows-x64.exe"); |
| 69 | + downloadJDK("macosx-x64.dmg"); |
| 70 | + */ |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + static void download(File folder, String filename, |
| 75 | + boolean jre, String platform, |
| 76 | + int version, int update, int build) { |
| 77 | + //HttpURLConnection.setFollowRedirects(true); |
| 78 | + if (filename == null) { |
| 79 | + filename = (jre ? "jre" : "jdk") + |
| 80 | + (update == 0 ? |
| 81 | + String.format("-%d-%s", version, platform) : |
| 82 | + String.format("-%du%d-%s", version, update, platform)); |
| 83 | + } |
| 84 | + //String url = "http://download.oracle.com/otn-pub/java/jdk/" + |
| 85 | + // https://edelivery.oracle.com/otn-pub/java/jdk/7u45-b18/jre-7u45-linux-i586.tar.gz |
| 86 | + String url = "https://edelivery.oracle.com/otn-pub/java/jdk/" + |
| 87 | + //String url = "https://download.oracle.com/otn-pub/java/jdk/" + |
| 88 | + (update == 0 ? |
| 89 | + String.format("%d-b%02d/", version, build) : |
| 90 | + String.format("%du%d-b%02d/", version, update, build)) + filename; |
| 91 | + System.out.println(url); |
| 92 | + |
| 93 | + try { |
| 94 | + HttpURLConnection conn = |
| 95 | + (HttpURLConnection) new URL(url).openConnection(); |
| 96 | + //conn.setRequestProperty("Cookie", "name1=value1; name2=value2"); |
| 97 | + conn.setRequestProperty("Cookie", COOKIE); |
| 98 | + //conn.setRequestProperty("Cookie", "gpw_e24=http://www.oracle.com/"); |
| 99 | + |
| 100 | + //printHeaders(conn); |
| 101 | + //conn.connect(); |
| 102 | + if (conn.getResponseCode() == 302) { |
| 103 | + Map<String, List<String>> headers = conn.getHeaderFields(); |
| 104 | + List<String> location = headers.get("Location"); |
| 105 | + if (location.size() == 1) { |
| 106 | + url = location.get(0); |
| 107 | + } else { |
| 108 | + throw new RuntimeException("Got " + location.size() + " locations."); |
| 109 | + } |
| 110 | + List<String> cookies = headers.get("Set-Cookie"); |
| 111 | + conn = (HttpURLConnection) new URL(url).openConnection(); |
| 112 | + for (String cookie : cookies) { |
| 113 | + conn.setRequestProperty("Cookie", cookie); |
| 114 | + } |
| 115 | + conn.setRequestProperty("Cookie", COOKIE); |
| 116 | + conn.connect(); |
| 117 | + } |
| 118 | + |
| 119 | + if (conn.getResponseCode() == 200) { |
| 120 | + InputStream input = conn.getInputStream(); |
| 121 | + BufferedInputStream bis = new BufferedInputStream(input); |
| 122 | + File outputFile = new File(folder, filename); |
| 123 | + BufferedOutputStream output = |
| 124 | + new BufferedOutputStream(new FileOutputStream(outputFile)); |
| 125 | + int c = bis.read(); |
| 126 | + while (c != -1) { |
| 127 | + output.write(c); |
| 128 | + c = bis.read(); |
| 129 | + } |
| 130 | + bis.close(); |
| 131 | + output.flush(); |
| 132 | + output.close(); |
| 133 | + } else { |
| 134 | + printHeaders(conn); |
| 135 | + System.exit(1); |
| 136 | + } |
| 137 | + |
| 138 | + } catch (Exception e) { |
| 139 | + e.printStackTrace(); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + |
| 144 | + static void printHeaders(URLConnection conn) { |
| 145 | + Map<String, List<String>> headers = conn.getHeaderFields(); |
| 146 | + Set<Map.Entry<String, List<String>>> entrySet = headers.entrySet(); |
| 147 | + for (Map.Entry<String, List<String>> entry : entrySet) { |
| 148 | + String headerName = entry.getKey(); |
| 149 | + System.out.println("Header Name:" + headerName); |
| 150 | + List<String> headerValues = entry.getValue(); |
| 151 | + for (String value : headerValues) { |
| 152 | + System.out.print("Header value:" + value); |
| 153 | + } |
| 154 | + System.out.println(); |
| 155 | + System.out.println(); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments