String progressLabel = "Download " + distribution;
IProgressListener progress = runtime.getProgressListener();
progress.start(progressLabel);
// First get PHPSESSID cookie from download URL
ITimeoutConfig timeoutConfig = runtime.getTimeoutConfig();
URL url = new URL(getDownloadUrl(runtime, distribution));
logger.fine("Distro URL: " + url);
HttpURLConnection openConnection = (HttpURLConnection) url.openConnection();
openConnection.setRequestProperty("User-Agent", runtime.getUserAgent());
openConnection.setConnectTimeout(timeoutConfig.getConnectionTimeout());
openConnection.setReadTimeout(runtime.getTimeoutConfig().getReadTimeout());
openConnection.setInstanceFollowRedirects(false);
openConnection.getContent(); // we only care about the cookies, not the redirect page contents
List<HttpCookie> cookies = HttpCookie.parse(openConnection.getHeaderField("Set-Cookie"));
logger.fine("Cookies fetched from distro URL (before redirects): " + cookies);
while (("" + openConnection.getResponseCode()).startsWith("30")) {
url = new URL(url, openConnection.getHeaderField("Location"));
logger.finest("following redirect to: " + url);
openConnection = (HttpURLConnection) url.openConnection();
openConnection.setRequestProperty("User-Agent", runtime.getUserAgent());
openConnection.setConnectTimeout(timeoutConfig.getConnectionTimeout());
openConnection.setReadTimeout(runtime.getTimeoutConfig().getReadTimeout());
openConnection.setInstanceFollowRedirects(false);
openConnection.getContent();
}
logger.finest("Headers returned from distro URL (after following redirects): " + openConnection.getHeaderFields());
String refresh = openConnection.getHeaderField("Refresh");
openConnection.getInputStream().close();
// Then fetch actual file, using the received cookies
File ret = Files.createTempFile(PropertyOrPlatformTempDir.defaultInstance(),
runtime.getFileNaming().nameFor(runtime.getDownloadPrefix(), "." + runtime.getPackageResolver().getArchiveType(distribution)));
logger.fine("Saving distro to " + ret.getAbsolutePath());
if (ret.canWrite()) {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(ret));
URL url2 = new URL("http://www.tokutek.com/download.php?df=1");
if (refresh != null && !refresh.isEmpty()) {
Matcher m = REFRESH_HEADER_PATTERN.matcher(refresh);
if (m.matches()) {
url2 = new URL(url, m.group(1));
}
}
logger.fine("Ultimate download URL: " + url2);
openConnection = (HttpURLConnection) url2.openConnection();
openConnection.setRequestProperty("User-Agent",runtime.getUserAgent());
openConnection.setConnectTimeout(timeoutConfig.getConnectionTimeout());
openConnection.setReadTimeout(timeoutConfig.getReadTimeout());
if (!cookies.isEmpty()) {
StringBuilder cookie = new StringBuilder();
for (HttpCookie c : cookies) {
if (cookie.length() > 0) {
cookie.append("; ");