mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-11-16 01:21:16 +01:00
WatermarkController: use ExceptionUtils to throw custom Exceptions
This commit is contained in:
parent
2fb895dd3b
commit
371cd1d5c2
@ -42,11 +42,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
|
|
||||||
import stirling.software.SPDF.model.api.security.AddWatermarkRequest;
|
import stirling.software.SPDF.model.api.security.AddWatermarkRequest;
|
||||||
import stirling.software.common.service.CustomPDFDocumentFactory;
|
import stirling.software.common.service.CustomPDFDocumentFactory;
|
||||||
import stirling.software.common.util.GeneralUtils;
|
import stirling.software.common.util.*;
|
||||||
import stirling.software.common.util.PdfUtils;
|
|
||||||
import stirling.software.common.util.RegexPatternUtils;
|
|
||||||
import stirling.software.common.util.WatermarkRandomizer;
|
|
||||||
import stirling.software.common.util.WebResponseUtils;
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@RestController
|
@RestController
|
||||||
@ -77,34 +73,33 @@ public class WatermarkController {
|
|||||||
// Validate opacity bounds (0.0 - 1.0)
|
// Validate opacity bounds (0.0 - 1.0)
|
||||||
float opacity = request.getOpacity();
|
float opacity = request.getOpacity();
|
||||||
if (opacity < 0.0f || opacity > 1.0f) {
|
if (opacity < 0.0f || opacity > 1.0f) {
|
||||||
String errorMsg =
|
log.error("Opacity must be between 0.0 and 1.0, but got: {}", opacity);
|
||||||
String.format("Opacity must be between 0.0 and 1.0, but got: %.2f", opacity);
|
throw ExceptionUtils.createIllegalArgumentException(
|
||||||
log.warn("Validation failed: {}", errorMsg);
|
"error.opacityOutOfRange" , //TODO
|
||||||
throw new IllegalArgumentException(errorMsg);
|
"Opacity must be between 0.0 and 1.0, but got: {0}",
|
||||||
|
opacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate rotation range: rotationMin <= rotationMax
|
// Validate rotation range: rotationMin <= rotationMax
|
||||||
Float rotationMin = request.getRotationMin();
|
Float rotationMin = request.getRotationMin();
|
||||||
Float rotationMax = request.getRotationMax();
|
Float rotationMax = request.getRotationMax();
|
||||||
if (rotationMin != null && rotationMax != null && rotationMin > rotationMax) {
|
if (rotationMin != null && rotationMax != null && rotationMin > rotationMax) {
|
||||||
String errorMsg =
|
log.error("Rotation minimum ({}) must be less than or equal to rotation maximum ({})", rotationMin, rotationMax);
|
||||||
String.format(
|
throw ExceptionUtils.createIllegalArgumentException(
|
||||||
"Rotation minimum (%.2f) must be less than or equal to rotation maximum (%.2f)",
|
"error.rotationRangeInvalid" , //TODO
|
||||||
rotationMin, rotationMax);
|
"Rotation minimum ({0}) must be less than or equal to rotation maximum ({1})",
|
||||||
log.warn("Validation failed: {}", errorMsg);
|
rotationMin, rotationMax);
|
||||||
throw new IllegalArgumentException(errorMsg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate font size range: fontSizeMin <= fontSizeMax
|
// Validate font size range: fontSizeMin <= fontSizeMax
|
||||||
Float fontSizeMin = request.getFontSizeMin();
|
Float fontSizeMin = request.getFontSizeMin();
|
||||||
Float fontSizeMax = request.getFontSizeMax();
|
Float fontSizeMax = request.getFontSizeMax();
|
||||||
if (fontSizeMin != null && fontSizeMax != null && fontSizeMin > fontSizeMax) {
|
if (fontSizeMin != null && fontSizeMax != null && fontSizeMin > fontSizeMax) {
|
||||||
String errorMsg =
|
log.error("Font size minimum ({}) must be less than or equal to font size maximum ({})", fontSizeMin, fontSizeMax);
|
||||||
String.format(
|
throw ExceptionUtils.createIllegalArgumentException(
|
||||||
"Font size minimum (%.2f) must be less than or equal to font size maximum (%.2f)",
|
"error.fontSizeRangeInvalid" , //TODO
|
||||||
fontSizeMin, fontSizeMax);
|
"Font size minimum ({0}) must be less than or equal to font size maximum ({1})",
|
||||||
log.warn("Validation failed: {}", errorMsg);
|
fontSizeMin, fontSizeMax);
|
||||||
throw new IllegalArgumentException(errorMsg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate color format when not using random color
|
// Validate color format when not using random color
|
||||||
@ -113,12 +108,11 @@ public class WatermarkController {
|
|||||||
if (customColor != null && !Boolean.TRUE.equals(randomColor)) {
|
if (customColor != null && !Boolean.TRUE.equals(randomColor)) {
|
||||||
// Check if color is valid hex format (#RRGGBB or #RRGGBBAA)
|
// Check if color is valid hex format (#RRGGBB or #RRGGBBAA)
|
||||||
if (!customColor.matches("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$")) {
|
if (!customColor.matches("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$")) {
|
||||||
String errorMsg =
|
log.error("Invalid color format: {}. Expected hex format like #RRGGBB or #RRGGBBAA", customColor);
|
||||||
String.format(
|
throw ExceptionUtils.createIllegalArgumentException(
|
||||||
"Invalid color format: '%s'. Expected hex format like #RRGGBB or #RRGGBBAA",
|
"error.invalidColorFormat" , //TODO
|
||||||
customColor);
|
"Invalid color format: {0}. Expected hex format like #RRGGBB or #RRGGBBAA",
|
||||||
log.warn("Validation failed: {}", errorMsg);
|
customColor);
|
||||||
throw new IllegalArgumentException(errorMsg);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,12 +120,11 @@ public class WatermarkController {
|
|||||||
Float mirroringProbability = request.getMirroringProbability();
|
Float mirroringProbability = request.getMirroringProbability();
|
||||||
if (mirroringProbability != null
|
if (mirroringProbability != null
|
||||||
&& (mirroringProbability < 0.0f || mirroringProbability > 1.0f)) {
|
&& (mirroringProbability < 0.0f || mirroringProbability > 1.0f)) {
|
||||||
String errorMsg =
|
log.error("Mirroring probability must be between 0.0 and 1.0, but got: {}", mirroringProbability);
|
||||||
String.format(
|
throw ExceptionUtils.createIllegalArgumentException(
|
||||||
"Mirroring probability must be between 0.0 and 1.0, but got: %.2f",
|
"error.mirroringProbabilityOutOfRange" , //TODO
|
||||||
mirroringProbability);
|
"Mirroring probability must be between 0.0 and 1.0, but got: {0}",
|
||||||
log.warn("Validation failed: {}", errorMsg);
|
mirroringProbability);
|
||||||
throw new IllegalArgumentException(errorMsg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate watermark type
|
// Validate watermark type
|
||||||
@ -139,21 +132,21 @@ public class WatermarkController {
|
|||||||
if (watermarkType == null
|
if (watermarkType == null
|
||||||
|| (!watermarkType.equalsIgnoreCase("text")
|
|| (!watermarkType.equalsIgnoreCase("text")
|
||||||
&& !watermarkType.equalsIgnoreCase("image"))) {
|
&& !watermarkType.equalsIgnoreCase("image"))) {
|
||||||
String errorMsg =
|
log.error( "Watermark type must be 'text' or 'image', but got: {}", watermarkType);
|
||||||
String.format(
|
throw ExceptionUtils.createIllegalArgumentException(
|
||||||
"Watermark type must be 'text' or 'image', but got: '%s'",
|
"error.unsupportedWatermarkType" , //TODO
|
||||||
watermarkType);
|
"Watermark type must be 'text' or 'image', but got: {0}",
|
||||||
log.warn("Validation failed: {}", errorMsg);
|
watermarkType);
|
||||||
throw new IllegalArgumentException(errorMsg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate text watermark has text
|
// Validate text watermark has text
|
||||||
if ("text".equalsIgnoreCase(watermarkType)) {
|
if ("text".equalsIgnoreCase(watermarkType)) {
|
||||||
String watermarkText = request.getWatermarkText();
|
String watermarkText = request.getWatermarkText();
|
||||||
if (watermarkText == null || watermarkText.trim().isEmpty()) {
|
if (watermarkText == null || watermarkText.trim().isEmpty()) {
|
||||||
String errorMsg = "Watermark text is required when watermark type is 'text'";
|
log.error("Watermark text is required when watermark type is 'text'");
|
||||||
log.warn("Validation failed: {}", errorMsg);
|
throw ExceptionUtils.createIllegalArgumentException(
|
||||||
throw new IllegalArgumentException(errorMsg);
|
"error.watermarkTextRequired", //TODO
|
||||||
|
"Watermark text is required when watermark type is 'text'");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,31 +154,30 @@ public class WatermarkController {
|
|||||||
if ("image".equalsIgnoreCase(watermarkType)) {
|
if ("image".equalsIgnoreCase(watermarkType)) {
|
||||||
MultipartFile watermarkImage = request.getWatermarkImage();
|
MultipartFile watermarkImage = request.getWatermarkImage();
|
||||||
if (watermarkImage == null || watermarkImage.isEmpty()) {
|
if (watermarkImage == null || watermarkImage.isEmpty()) {
|
||||||
String errorMsg = "Watermark image is required when watermark type is 'image'";
|
log.error("Watermark image is required when watermark type is 'image'");
|
||||||
log.warn("Validation failed: {}", errorMsg);
|
throw ExceptionUtils.createIllegalArgumentException(
|
||||||
throw new IllegalArgumentException(errorMsg);
|
"error.watermarkImageRequired", //TODO
|
||||||
|
"Watermark image is required when watermark type is 'image'");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate image type - only allow common image formats
|
// Validate image type - only allow common image formats
|
||||||
String contentType = watermarkImage.getContentType();
|
String contentType = watermarkImage.getContentType();
|
||||||
String originalFilename = watermarkImage.getOriginalFilename();
|
String originalFilename = watermarkImage.getOriginalFilename();
|
||||||
if (contentType != null && !isSupportedImageType(contentType)) {
|
if (contentType != null && !isSupportedImageType(contentType)) {
|
||||||
String errorMsg =
|
log.error("Unsupported image type: {}. Supported types: PNG, JPG, JPEG, GIF, BMP", contentType);
|
||||||
String.format(
|
throw ExceptionUtils.createIllegalArgumentException(
|
||||||
"Unsupported image type: '%s'. Supported types: PNG, JPG, JPEG, GIF, BMP",
|
"error.unsupportedContentType", //TODO
|
||||||
contentType);
|
"Unsupported image type: {0}. Supported types: PNG, JPG, JPEG, GIF, BMP",
|
||||||
log.warn("Validation failed: {}", errorMsg);
|
contentType);
|
||||||
throw new IllegalArgumentException(errorMsg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Additional check based on file extension
|
// Additional check based on file extension
|
||||||
if (originalFilename != null && !hasSupportedImageExtension(originalFilename)) {
|
if (originalFilename != null && !hasSupportedImageExtension(originalFilename)) {
|
||||||
String errorMsg =
|
log.error("Unsupported image file extension in: {}. Supported extensions: .png, .jpg, .jpeg, .gif, .bmp", originalFilename);
|
||||||
String.format(
|
throw ExceptionUtils.createIllegalArgumentException(
|
||||||
"Unsupported image file extension in: '%s'. Supported extensions: .png, .jpg, .jpeg, .gif, .bmp",
|
"error.unsupportedImageFileType", //TODO
|
||||||
originalFilename);
|
"Unsupported image file extension in: {0}. Supported extensions: .png, .jpg, .jpeg, .gif, .bmp",
|
||||||
log.warn("Validation failed: {}", errorMsg);
|
originalFilename);
|
||||||
throw new IllegalArgumentException(errorMsg);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user