diff --git a/src/main/java/com/alttd/webinterface/http/NotificationServer.java b/src/main/java/com/alttd/webinterface/http/NotificationServer.java index 58e5c45..d6de60e 100644 --- a/src/main/java/com/alttd/webinterface/http/NotificationServer.java +++ b/src/main/java/com/alttd/webinterface/http/NotificationServer.java @@ -7,6 +7,8 @@ import io.javalin.http.HttpStatus; import lombok.extern.slf4j.Slf4j; import java.util.Optional; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; /** @@ -36,6 +38,7 @@ public class NotificationServer { }).start(port); app.get("/notify/{file}.json", this::handleNotifyRequest); + app.get("/notify/{uuid}/{file}.json", this::handleNotifyRequestUuid); log.info("NotificationServer started on port {}", port); } catch (Exception e) { @@ -58,12 +61,45 @@ public class NotificationServer { */ private void handleNotifyRequest(Context ctx) { String uri = ctx.path(); - log.info("Received request: {} {}", ctx.method(), uri); + log.info("Received download request: {} {}", ctx.method(), uri); String fileName = ctx.pathParam("file") + ".json"; log.info("Requested file: {}", fileName); - FileDownloadService.downloadFileAsync(fileName).thenAccept(fileData -> { + CompletableFuture> optionalCompletableFuture = FileDownloadService.downloadFileAsync(fileName); + downloadFile(ctx, optionalCompletableFuture); + } + + /** + * Handles requests to the /notify/.json endpoint. + */ + private void handleNotifyRequestUuid(Context ctx) { + String uri = ctx.path(); + log.info("Received UUID request: {} {}", ctx.method(), uri); + + String stringUUID = ctx.pathParam("uuid"); + log.info("Requested uuid: {}", stringUUID); + + UUID uuid; + try { + uuid = UUID.fromString(stringUUID); + } catch (Exception e) { + log.error("Invalid UUID: {}", stringUUID, e); + ctx.status(HttpStatus.BAD_REQUEST); + ctx.contentType("text/plain"); + ctx.result("Invalid UUID format"); + return; + } + + String fileName = ctx.pathParam("file") + ".json"; + log.info("Requested uuid file: {}", fileName); + + CompletableFuture> optionalCompletableFuture = FileDownloadService.downloadFileAsync(uuid, fileName); + downloadFile(ctx, optionalCompletableFuture); + } + + private void downloadFile(Context ctx, CompletableFuture> optionalCompletableFuture) { + optionalCompletableFuture.thenAccept(fileData -> { if (fileData.isPresent()) { ctx.contentType("application/json"); ctx.result(fileData.get()); @@ -73,7 +109,7 @@ public class NotificationServer { ctx.result("File not found or download failed"); } }).exceptionally(e -> { - log.error("Error downloading file: {}", fileName, e); + log.error("Error downloading file", e); ctx.status(HttpStatus.INTERNAL_SERVER_ERROR); ctx.contentType("text/plain"); ctx.result("Failed to handle download"); diff --git a/src/main/java/com/alttd/webinterface/web_interact/FileDownloadService.java b/src/main/java/com/alttd/webinterface/web_interact/FileDownloadService.java index 289d7c4..9323a28 100644 --- a/src/main/java/com/alttd/webinterface/web_interact/FileDownloadService.java +++ b/src/main/java/com/alttd/webinterface/web_interact/FileDownloadService.java @@ -9,6 +9,7 @@ import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.util.Optional; +import java.util.UUID; import java.util.concurrent.CompletableFuture; /** @@ -31,6 +32,20 @@ public class FileDownloadService { } downloadUrl += fileName; + return downloadFileFromUrlAsync(downloadUrl); + } + + public static CompletableFuture> downloadFileAsync(UUID uuid, String fileName) { + String downloadUrl = Config.DOWNLOAD_ENDPOINT; + if (!downloadUrl.endsWith("/")) { + downloadUrl += "/"; + } + downloadUrl += uuid.toString() + "/"; + downloadUrl += fileName; + return downloadFileFromUrlAsync(downloadUrl); + } + + private static CompletableFuture> downloadFileFromUrlAsync(String downloadUrl) { log.debug("Downloading file from {}", downloadUrl); HttpClient client = HttpClient.newHttpClient(); @@ -43,15 +58,15 @@ public class FileDownloadService { return client.sendAsync(request, HttpResponse.BodyHandlers.ofByteArray()) .thenApply(response -> { if (response.statusCode() == HttpServletResponse.SC_OK) { - log.debug("Successfully downloaded file: {}", fileName); + log.debug("Successfully downloaded file: {}", downloadUrl); return Optional.of(response.body()); } else { - log.error("Failed to download file: {}. Status code: {}", fileName, response.statusCode()); + log.error("Failed to download file: {}. Status code: {}", downloadUrl, response.statusCode()); return Optional.empty(); } }) .exceptionally(e -> { - log.error("Exception occurred while downloading file: {}", fileName, e); + log.error("Exception occurred while downloading file: {}", downloadUrl, e); return Optional.empty(); }); }