diff --git a/.idea/misc.xml b/.idea/misc.xml index b3cf2e0..fc01513 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -8,5 +8,5 @@ - + \ No newline at end of file diff --git a/src/main/java/eu/m724/jarupdater/download/SimpleDownloader.java b/src/main/java/eu/m724/jarupdater/download/SimpleDownloader.java index f76b36b..52a29a1 100644 --- a/src/main/java/eu/m724/jarupdater/download/SimpleDownloader.java +++ b/src/main/java/eu/m724/jarupdater/download/SimpleDownloader.java @@ -17,7 +17,6 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SignatureException; import java.util.Arrays; -import java.util.HexFormat; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; @@ -59,12 +58,12 @@ public class SimpleDownloader implements Downloader { } } - if (!Arrays.equals(messageDigest.digest(), HexFormat.of().parseHex(sha256hex))) + if (!Arrays.equals(messageDigest.digest(), hexStringToByteArray(sha256hex))) throw new SignatureException(); // This is not outside try because impact is too small to justify more code return downloadedFile; - } catch (IOException | NoSuchAlgorithmException | SignatureException e) { // TODO nosuchalgo should not be thrown + } catch (IOException | NoSuchAlgorithmException | SignatureException e) { throw new CompletionException(e); } @@ -85,4 +84,13 @@ public class SimpleDownloader implements Downloader { }); } + private static byte[] hexStringToByteArray(String s) { + int len = s.length(); + byte[] data = new byte[len / 2]; + for (int i = 0; i < len; i += 2) { + data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + + Character.digit(s.charAt(i + 1), 16)); + } + return data; + } } diff --git a/src/main/java/eu/m724/jarupdater/live/GiteaMetadataDAO.java b/src/main/java/eu/m724/jarupdater/live/GiteaMetadataDAO.java index c0325a4..cc33343 100644 --- a/src/main/java/eu/m724/jarupdater/live/GiteaMetadataDAO.java +++ b/src/main/java/eu/m724/jarupdater/live/GiteaMetadataDAO.java @@ -1,20 +1,18 @@ package eu.m724.jarupdater.live; +import com.google.gson.Gson; +import eu.m724.jarupdater.object.Version; + import java.io.IOException; import java.net.ProxySelector; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; -import java.net.http.HttpClient.Redirect; -import java.net.http.HttpResponse.BodyHandlers; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; - -import com.google.gson.Gson; - -import eu.m724.jarupdater.object.Version; +import java.util.stream.Collectors; public class GiteaMetadataDAO implements MetadataDAO { private final String url; @@ -32,9 +30,9 @@ public class GiteaMetadataDAO implements MetadataDAO { return makeRequest(url).thenApply(response -> { if (response.statusCode() != 200) - throw new CompletionException(new IOException("Server returned status code %d".formatted(response.statusCode()))); + throw new CompletionException(new IOException("Server returned status code " + response.statusCode())); - return response.body().lines().toList(); + return response.body().lines().collect(Collectors.toList()); }); } @@ -46,7 +44,7 @@ public class GiteaMetadataDAO implements MetadataDAO { return makeRequest(url).thenApply(response -> { if (response.statusCode() != 200) - throw new CompletionException(new IOException("Server returned status code %d".formatted(response.statusCode()))); + throw new CompletionException(new IOException("Server returned status code " + response.statusCode())); // not throwing nosuchversionexecpion because it's not possible to tell if the server is broken or it really doesn't exist return new Gson().fromJson(response.body(), Version.class); @@ -61,12 +59,11 @@ public class GiteaMetadataDAO implements MetadataDAO { .build(); HttpClient.Builder httpClientBuilder = HttpClient.newBuilder() - .followRedirects(Redirect.NORMAL) + .followRedirects(HttpClient.Redirect.NORMAL) .proxy(ProxySelector.getDefault()); - try (HttpClient httpClient = httpClientBuilder.build()) { - return httpClient.sendAsync(request, BodyHandlers.ofString()); - } + HttpClient httpClient = httpClientBuilder.build(); + return httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString()); } private String getFileUrl(String... paths) { diff --git a/src/main/java/eu/m724/jarupdater/object/NoSuchVersionException.java b/src/main/java/eu/m724/jarupdater/object/NoSuchVersionException.java index fe466d6..ae432db 100644 --- a/src/main/java/eu/m724/jarupdater/object/NoSuchVersionException.java +++ b/src/main/java/eu/m724/jarupdater/object/NoSuchVersionException.java @@ -1,13 +1,8 @@ package eu.m724.jarupdater.object; import java.io.IOException; -import java.io.Serial; public class NoSuchVersionException extends IOException { - - @Serial - private static final long serialVersionUID = 8435964987348892266L; - public NoSuchVersionException(String version) { super(version); }