Skip to content

Commit

Permalink
Merge pull request #493 from reftel/feature/follow_redirects
Browse files Browse the repository at this point in the history
Feature/follow redirects
  • Loading branch information
oleg-nenashev authored Apr 7, 2021
2 parents 6d5a483 + fe12eb5 commit 3800322
Showing 1 changed file with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
Expand Down Expand Up @@ -88,7 +90,7 @@ public void postConstruct() throws IOException {
// Explode war if necessary
String warPath = settings.warDir.getAbsolutePath();
if(FilenameUtils.getExtension(warPath).equals("war") && new File(warPath).isFile()) {
System.out.println("Exploding," + warPath + "this might take some time.");
System.out.println("Exploding " + warPath + ", this might take some time.");
settings.warDir = Util.explodeWar(warPath);
}

Expand Down Expand Up @@ -119,6 +121,15 @@ public void postConstruct() throws IOException {
}
}

private void copyURLToFile(URL url, File file) throws IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setInstanceFollowRedirects(true);
try (InputStream stream = connection.getInputStream()) {
FileUtils.copyInputStreamToFile(stream, file);
}

}

private File getJenkinsWar(final @CheckForNull String requiredVersion) throws IOException {
final String versionToUse;
if (requiredVersion == null) {
Expand All @@ -128,7 +139,7 @@ private File getJenkinsWar(final @CheckForNull String requiredVersion) throws IO
latestCore.getParentFile().mkdirs();
// Check once a day
if (!latestCore.exists() || latestCore.lastModified() < CACHE_EXPIRE) {
FileUtils.copyURLToFile(URI.create("http://updates.jenkins.io/stable/latestCore.txt").toURL(), latestCore);
copyURLToFile(URI.create("http://updates.jenkins.io/stable/latestCore.txt").toURL(), latestCore);
}
versionToUse = FileUtils.readFileToString(latestCore, StandardCharsets.US_ASCII);
} else {
Expand All @@ -142,7 +153,7 @@ private File getJenkinsWar(final @CheckForNull String requiredVersion) throws IO
war.getParentFile().mkdirs();
final URL url = new URL(getLauncherOptions().getMirrorURL(String.format("http://updates.jenkins.io/download/war/%s/jenkins.war", versionToUse)));
System.out.printf("Downloading jenkins %s...%n", versionToUse);
FileUtils.copyURLToFile(url, war);
copyURLToFile(url, war);
}

return war;
Expand Down

0 comments on commit 3800322

Please sign in to comment.