From 558e35de62c1595ad2d0a370ba18a97510c9a72f Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com.> Date: Wed, 2 Jul 2025 14:35:26 +0100 Subject: [PATCH] other fixes --- .../software/common/util/ExceptionUtils.java | 6 ++++ .../software/common/util/FileToPdf.java | 8 +++-- .../software/common/util/PdfUtils.java | 30 ++++++++++++------- .../common/util/TranslatableIOException.java | 28 +++++++++++++++++ .../SPDF/config/GlobalExceptionHandler.java | 19 ++++++++++++ 5 files changed, 77 insertions(+), 14 deletions(-) create mode 100644 common/src/main/java/stirling/software/common/util/TranslatableIOException.java diff --git a/common/src/main/java/stirling/software/common/util/ExceptionUtils.java b/common/src/main/java/stirling/software/common/util/ExceptionUtils.java index 16ac5978e..161cdf64f 100644 --- a/common/src/main/java/stirling/software/common/util/ExceptionUtils.java +++ b/common/src/main/java/stirling/software/common/util/ExceptionUtils.java @@ -105,6 +105,9 @@ public class ExceptionUtils { public static IOException createIOException( String messageKey, String defaultMessage, Exception cause, Object... args) { String message = messageKey != null ? defaultMessage : String.format(defaultMessage, args); + if (messageKey != null) { + return new TranslatableIOException(message, messageKey, cause, args); + } return new IOException(message, cause); } @@ -120,6 +123,9 @@ public class ExceptionUtils { public static RuntimeException createRuntimeException( String messageKey, String defaultMessage, Exception cause, Object... args) { String message = messageKey != null ? defaultMessage : String.format(defaultMessage, args); + if (messageKey != null) { + return new TranslatableException(message, messageKey, args); + } return new RuntimeException(message, cause); } diff --git a/common/src/main/java/stirling/software/common/util/FileToPdf.java b/common/src/main/java/stirling/software/common/util/FileToPdf.java index 13636e2f4..c735e5287 100644 --- a/common/src/main/java/stirling/software/common/util/FileToPdf.java +++ b/common/src/main/java/stirling/software/common/util/FileToPdf.java @@ -32,16 +32,18 @@ public class FileToPdf { try (TempFile tempOutputFile = new TempFile(tempFileManager, ".pdf")) { try (TempFile tempInputFile = - new TempFile(tempFileManager, fileName.endsWith(".html") ? ".html" : ".zip")) { + new TempFile( + tempFileManager, + fileName.toLowerCase().endsWith(".html") ? ".html" : ".zip")) { - if (fileName.endsWith(".html")) { + if (fileName.toLowerCase().endsWith(".html")) { String sanitizedHtml = sanitizeHtmlContent( new String(fileBytes, StandardCharsets.UTF_8), disableSanitize); Files.write( tempInputFile.getPath(), sanitizedHtml.getBytes(StandardCharsets.UTF_8)); - } else if (fileName.endsWith(".zip")) { + } else if (fileName.toLowerCase().endsWith(".zip")) { Files.write(tempInputFile.getPath(), fileBytes); sanitizeHtmlFilesInZip( tempInputFile.getPath(), disableSanitize, tempFileManager); diff --git a/common/src/main/java/stirling/software/common/util/PdfUtils.java b/common/src/main/java/stirling/software/common/util/PdfUtils.java index 5c5bda000..ccc0d1a25 100644 --- a/common/src/main/java/stirling/software/common/util/PdfUtils.java +++ b/common/src/main/java/stirling/software/common/util/PdfUtils.java @@ -42,26 +42,34 @@ public class PdfUtils { public static PDRectangle textToPageSize(String size) { switch (size.toUpperCase()) { - case "A0": + case "A0" -> { return PDRectangle.A0; - case "A1": + } + case "A1" -> { return PDRectangle.A1; - case "A2": + } + case "A2" -> { return PDRectangle.A2; - case "A3": + } + case "A3" -> { return PDRectangle.A3; - case "A4": + } + case "A4" -> { return PDRectangle.A4; - case "A5": + } + case "A5" -> { return PDRectangle.A5; - case "A6": + } + case "A6" -> { return PDRectangle.A6; - case "LETTER": + } + case "LETTER" -> { return PDRectangle.LETTER; - case "LEGAL": + } + case "LEGAL" -> { return PDRectangle.LEGAL; - default: - throw ExceptionUtils.createInvalidPageSizeException(size); + } + default -> throw ExceptionUtils.createInvalidPageSizeException(size); } } diff --git a/common/src/main/java/stirling/software/common/util/TranslatableIOException.java b/common/src/main/java/stirling/software/common/util/TranslatableIOException.java new file mode 100644 index 000000000..079a03214 --- /dev/null +++ b/common/src/main/java/stirling/software/common/util/TranslatableIOException.java @@ -0,0 +1,28 @@ +package stirling.software.common.util; + +import java.io.IOException; + +/** + * IOException that carries translation information for frontend internationalization. The + * GlobalExceptionHandler extracts this info to create structured error responses. + */ +public class TranslatableIOException extends IOException { + + private final String translationKey; + private final Object[] translationArgs; + + public TranslatableIOException( + String message, String translationKey, Exception cause, Object... translationArgs) { + super(message, cause); + this.translationKey = translationKey; + this.translationArgs = translationArgs; + } + + public String getTranslationKey() { + return translationKey; + } + + public Object[] getTranslationArgs() { + return translationArgs; + } +} diff --git a/stirling-pdf/src/main/java/stirling/software/SPDF/config/GlobalExceptionHandler.java b/stirling-pdf/src/main/java/stirling/software/SPDF/config/GlobalExceptionHandler.java index c4bbf08c1..f8e6bc1a9 100644 --- a/stirling-pdf/src/main/java/stirling/software/SPDF/config/GlobalExceptionHandler.java +++ b/stirling-pdf/src/main/java/stirling/software/SPDF/config/GlobalExceptionHandler.java @@ -58,6 +58,25 @@ public class GlobalExceptionHandler { return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST); } + @ExceptionHandler(stirling.software.common.util.TranslatableIOException.class) + public ResponseEntity handleTranslatableIOException( + stirling.software.common.util.TranslatableIOException e) { + List translationArgs = null; + if (e.getTranslationArgs() != null) { + translationArgs = Arrays.stream(e.getTranslationArgs()).map(String::valueOf).toList(); + } + + ErrorResponse errorResponse = + new ErrorResponse( + "Bad Request", + e.getMessage(), + getStackTrace(e), + e.getTranslationKey(), + translationArgs); + + return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST); + } + @ExceptionHandler(IllegalArgumentException.class) public ResponseEntity handleIllegalArgumentException( IllegalArgumentException e) {