other fixes

This commit is contained in:
Anthony Stirling 2025-07-02 14:35:26 +01:00
parent f4e8bbf7a3
commit 558e35de62
5 changed files with 77 additions and 14 deletions

View File

@ -105,6 +105,9 @@ public class ExceptionUtils {
public static IOException createIOException( public static IOException createIOException(
String messageKey, String defaultMessage, Exception cause, Object... args) { String messageKey, String defaultMessage, Exception cause, Object... args) {
String message = messageKey != null ? defaultMessage : String.format(defaultMessage, 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); return new IOException(message, cause);
} }
@ -120,6 +123,9 @@ public class ExceptionUtils {
public static RuntimeException createRuntimeException( public static RuntimeException createRuntimeException(
String messageKey, String defaultMessage, Exception cause, Object... args) { String messageKey, String defaultMessage, Exception cause, Object... args) {
String message = messageKey != null ? defaultMessage : String.format(defaultMessage, args); String message = messageKey != null ? defaultMessage : String.format(defaultMessage, args);
if (messageKey != null) {
return new TranslatableException(message, messageKey, args);
}
return new RuntimeException(message, cause); return new RuntimeException(message, cause);
} }

View File

@ -32,16 +32,18 @@ public class FileToPdf {
try (TempFile tempOutputFile = new TempFile(tempFileManager, ".pdf")) { try (TempFile tempOutputFile = new TempFile(tempFileManager, ".pdf")) {
try (TempFile tempInputFile = 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 = String sanitizedHtml =
sanitizeHtmlContent( sanitizeHtmlContent(
new String(fileBytes, StandardCharsets.UTF_8), disableSanitize); new String(fileBytes, StandardCharsets.UTF_8), disableSanitize);
Files.write( Files.write(
tempInputFile.getPath(), tempInputFile.getPath(),
sanitizedHtml.getBytes(StandardCharsets.UTF_8)); sanitizedHtml.getBytes(StandardCharsets.UTF_8));
} else if (fileName.endsWith(".zip")) { } else if (fileName.toLowerCase().endsWith(".zip")) {
Files.write(tempInputFile.getPath(), fileBytes); Files.write(tempInputFile.getPath(), fileBytes);
sanitizeHtmlFilesInZip( sanitizeHtmlFilesInZip(
tempInputFile.getPath(), disableSanitize, tempFileManager); tempInputFile.getPath(), disableSanitize, tempFileManager);

View File

@ -42,26 +42,34 @@ public class PdfUtils {
public static PDRectangle textToPageSize(String size) { public static PDRectangle textToPageSize(String size) {
switch (size.toUpperCase()) { switch (size.toUpperCase()) {
case "A0": case "A0" -> {
return PDRectangle.A0; return PDRectangle.A0;
case "A1": }
case "A1" -> {
return PDRectangle.A1; return PDRectangle.A1;
case "A2": }
case "A2" -> {
return PDRectangle.A2; return PDRectangle.A2;
case "A3": }
case "A3" -> {
return PDRectangle.A3; return PDRectangle.A3;
case "A4": }
case "A4" -> {
return PDRectangle.A4; return PDRectangle.A4;
case "A5": }
case "A5" -> {
return PDRectangle.A5; return PDRectangle.A5;
case "A6": }
case "A6" -> {
return PDRectangle.A6; return PDRectangle.A6;
case "LETTER": }
case "LETTER" -> {
return PDRectangle.LETTER; return PDRectangle.LETTER;
case "LEGAL": }
case "LEGAL" -> {
return PDRectangle.LEGAL; return PDRectangle.LEGAL;
default: }
throw ExceptionUtils.createInvalidPageSizeException(size); default -> throw ExceptionUtils.createInvalidPageSizeException(size);
} }
} }

View File

@ -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;
}
}

View File

@ -58,6 +58,25 @@ public class GlobalExceptionHandler {
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST); return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
} }
@ExceptionHandler(stirling.software.common.util.TranslatableIOException.class)
public ResponseEntity<ErrorResponse> handleTranslatableIOException(
stirling.software.common.util.TranslatableIOException e) {
List<String> 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) @ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<ErrorResponse> handleIllegalArgumentException( public ResponseEntity<ErrorResponse> handleIllegalArgumentException(
IllegalArgumentException e) { IllegalArgumentException e) {