From 591875e939f2ea1c3b97c39be78e43484c6d32ca Mon Sep 17 00:00:00 2001 From: Andreas Date: Wed, 11 Oct 2023 20:04:05 +0200 Subject: [PATCH] fix program website link; update check handles redirects #713 --- .../com/baselet/control/enums/Program.java | 2 +- .../java/com/baselet/gui/BrowserLauncher.java | 39 ++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/umlet-elements/src/main/java/com/baselet/control/enums/Program.java b/umlet-elements/src/main/java/com/baselet/control/enums/Program.java index 0064b078f..744df548e 100644 --- a/umlet-elements/src/main/java/com/baselet/control/enums/Program.java +++ b/umlet-elements/src/main/java/com/baselet/control/enums/Program.java @@ -40,7 +40,7 @@ private Program(String version, RuntimeType runtimeType) { this.runtimeType = runtimeType; programName = "UMLet"; extension = "uxf"; - website = "http://www." + getProgramName().toLowerCase() + ".com"; + website = "https://www." + getProgramName().toLowerCase() + ".com"; if (runtimeType == RuntimeType.ECLIPSE_PLUGIN) { configName = getProgramName().toLowerCase() + "plugin.cfg"; diff --git a/umlet-swing/src/main/java/com/baselet/gui/BrowserLauncher.java b/umlet-swing/src/main/java/com/baselet/gui/BrowserLauncher.java index 16d54d6ff..bf85c0901 100644 --- a/umlet-swing/src/main/java/com/baselet/gui/BrowserLauncher.java +++ b/umlet-swing/src/main/java/com/baselet/gui/BrowserLauncher.java @@ -3,9 +3,14 @@ import java.awt.Desktop; import java.io.IOException; import java.lang.reflect.Method; +import java.net.HttpURLConnection; import java.net.URI; import java.net.URL; +import java.net.URLDecoder; +import java.util.HashMap; +import java.util.Map; import java.util.Scanner; +import java.util.function.BiFunction; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -67,10 +72,42 @@ else if (SystemInfo.OS == Os.WINDOWS) { } public static String readURL(String url) throws IOException { + Map visited = new HashMap(); + int times; + HttpURLConnection con; + while (true) { // follow redirects (see https://stackoverflow.com/q/1884230) + times = visited.compute(url, new BiFunction() { + @Override + public Integer apply(String key, Integer count) { + return count == null ? 1 : count + 1; + } + }); + if (times > 3) { + throw new IOException("Stuck in redirect loop"); + } + URL resourceUrl = new URL(url); + con = (HttpURLConnection) resourceUrl.openConnection(); + con.setConnectTimeout(15000); + con.setReadTimeout(15000); + con.setInstanceFollowRedirects(false); // Make the logic below easier to detect redirections + + switch (con.getResponseCode()) { + case HttpURLConnection.HTTP_MOVED_PERM: + case HttpURLConnection.HTTP_MOVED_TEMP: + String location = con.getHeaderField("Location"); + location = URLDecoder.decode(location, "UTF-8"); + URL base = new URL(url); + URL next = new URL(base, location); // Deal with relative URLs + url = next.toExternalForm(); + continue; + } + break; + } + StringBuilder sb = new StringBuilder(""); Scanner sc = null; try { - sc = new Scanner(new URL(url).openStream()); + sc = new Scanner(con.getInputStream()); while (sc.hasNextLine()) { sb.append(sc.nextLine()).append("\n"); }