aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-03-29 14:42:45 +0100
committerLinnea Gräf <nea@nea.moe>2024-03-29 14:42:45 +0100
commit1789e8d98393fa778e1690af964d14fa784fe09f (patch)
treea3a520d1cafbfb28c133189ba771d4f3d467619b
parentf9917503a384cf3b95896b7faab9bfec85ef9057 (diff)
downloadlibautoupdate-1789e8d98393fa778e1690af964d14fa784fe09f.tar.gz
libautoupdate-1789e8d98393fa778e1690af964d14fa784fe09f.tar.bz2
libautoupdate-1789e8d98393fa778e1690af964d14fa784fe09f.zip
Add connection patching support
-rw-r--r--src/main/java/moe/nea/libautoupdate/UpdateUtils.java20
1 files changed, 16 insertions, 4 deletions
diff --git a/src/main/java/moe/nea/libautoupdate/UpdateUtils.java b/src/main/java/moe/nea/libautoupdate/UpdateUtils.java
index f7319ca..13bc4eb 100644
--- a/src/main/java/moe/nea/libautoupdate/UpdateUtils.java
+++ b/src/main/java/moe/nea/libautoupdate/UpdateUtils.java
@@ -7,9 +7,7 @@ import lombok.var;
import java.io.*;
import java.lang.reflect.Type;
import java.math.BigInteger;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.net.URL;
+import java.net.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
@@ -21,6 +19,7 @@ import java.security.NoSuchAlgorithmException;
import java.util.Locale;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
+import java.util.function.Consumer;
public class UpdateUtils {
private UpdateUtils() {
@@ -88,13 +87,26 @@ public class UpdateUtils {
});
}
+ private static Consumer<URLConnection> connectionPatcher = null;
+
+ public static void patchConnection(Consumer<URLConnection> connectionPatcher) {
+ UpdateUtils.connectionPatcher = connectionPatcher;
+ }
+
public static <T> CompletableFuture<T> httpGet(String url, Gson gson, Type clazz) {
return CompletableFuture.supplyAsync(() -> {
+ URLConnection conn = null;
try {
- try (val is = new URL(url).openStream()) {
+ conn = new URL(url).openConnection();
+ if (connectionPatcher != null)
+ connectionPatcher.accept(conn);
+ try (val is = conn.getInputStream()) {
return gson.fromJson(new InputStreamReader(is, StandardCharsets.UTF_8), clazz);
}
} catch (IOException e) {
+ if (conn instanceof HttpURLConnection) {
+ ((HttpURLConnection) conn).disconnect();
+ }
throw new CompletionException(e);
}
});