Skip to content

Commit

Permalink
fix program website link; update check handles redirects #713
Browse files Browse the repository at this point in the history
  • Loading branch information
afdia committed Oct 11, 2023
1 parent a235c33 commit 591875e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
Expand Up @@ -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";
Expand Down
39 changes: 38 additions & 1 deletion umlet-swing/src/main/java/com/baselet/gui/BrowserLauncher.java
Expand Up @@ -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;
Expand Down Expand Up @@ -67,10 +72,42 @@ else if (SystemInfo.OS == Os.WINDOWS) {
}

public static String readURL(String url) throws IOException {
Map<String, Integer> visited = new HashMap<String, Integer>();
int times;
HttpURLConnection con;
while (true) { // follow redirects (see https://stackoverflow.com/q/1884230)
times = visited.compute(url, new BiFunction<String, Integer, Integer>() {
@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");
}
Expand Down

0 comments on commit 591875e

Please sign in to comment.