From 11b15823199d7356d0ac36a8e51b40e33c6f7052 Mon Sep 17 00:00:00 2001 From: Yannick Forster Date: Thu, 7 Dec 2023 17:25:54 +0100 Subject: [PATCH] Cleanup of the Builder Methods loading Defaults for invalid inputs --- .../impl/rendering/CaptchaRenderer.java | 43 ++++++++++++++----- .../flexcaptcha/impl/token/CaptchaCipher.java | 13 ++++-- .../impl/token/CaptchaCipherBuilder.java | 4 -- 3 files changed, 43 insertions(+), 17 deletions(-) delete mode 100644 src/main/java/io/github/yaforster/flexcaptcha/impl/token/CaptchaCipherBuilder.java diff --git a/src/main/java/io/github/yaforster/flexcaptcha/impl/rendering/CaptchaRenderer.java b/src/main/java/io/github/yaforster/flexcaptcha/impl/rendering/CaptchaRenderer.java index 78ce5d2..2772935 100644 --- a/src/main/java/io/github/yaforster/flexcaptcha/impl/rendering/CaptchaRenderer.java +++ b/src/main/java/io/github/yaforster/flexcaptcha/impl/rendering/CaptchaRenderer.java @@ -35,7 +35,7 @@ public class CaptchaRenderer extends AbstractCaptchaRenderer { /** * Defines the maximum angle that can be used to rotate a single character in the captcha */ - private final Double maxrotateAngle; + private final Double maximumLetterRotationAngle; /** * String name of the font to be used when writing characters to the image */ @@ -55,16 +55,14 @@ public class CaptchaRenderer extends AbstractCaptchaRenderer { @Builder public CaptchaRenderer(int pictureHeight, int pictureWidth, List availableTextColors, String imgFileFormat - , Double maxrotateAngle, String fontName, List imageOperationsList, + , Double maximumLetterRotationAngle, String fontName, List imageOperationsList, AbstractCaptchaImageBackground imageBackground, NoiseSettings noiseSettings) { super(getPictureHeightOrDefault(pictureHeight), getPictureWidthOrDefault(pictureWidth), - (availableTextColors == null || availableTextColors.isEmpty()) ? - Collections.singletonList(Color.BLACK) : availableTextColors, - StringUtils.isBlank(imgFileFormat) ? DEFAULT_IMAGE_FORMAT : imgFileFormat); - this.maxrotateAngle = Optional.ofNullable(maxrotateAngle).orElse(DEFAULT_MAX_LETTER_ROTATION_ANGLE); - this.fontName = StringUtils.isBlank(fontName) ? DEFAULT_FONT : fontName; - this.imageOperationsList = Optional.ofNullable(imageOperationsList).orElse(Collections.emptyList()); - this.imageBackground = Optional.ofNullable(imageBackground).orElse(DEFAULT_BACKGROUND); + getAvailableTextColorsOrDefault(availableTextColors), getImageFileFormatOrDefault(imgFileFormat)); + this.maximumLetterRotationAngle = getMaximumLetterRotationAngleOrDefault(maximumLetterRotationAngle); + this.fontName = getFontNameOrDefault(fontName); + this.imageOperationsList = getImageOperationsListOrDefault(imageOperationsList); + this.imageBackground = getImageBackgroundOrDefault(imageBackground); this.noiseSettings = getNoiseSettingsOrDefault(noiseSettings); } @@ -82,6 +80,31 @@ private static int getPictureWidthOrDefault(int pictureWidth) { return pictureWidth != 0 ? pictureWidth : DEFAULT_PICTURE_WIDTH; } + private static List getAvailableTextColorsOrDefault(List availableTextColors) { + return (availableTextColors == null || availableTextColors.isEmpty()) ? + Collections.singletonList(Color.BLACK) : availableTextColors; + } + + private static String getImageFileFormatOrDefault(String imgFileFormat) { + return StringUtils.isBlank(imgFileFormat) ? DEFAULT_IMAGE_FORMAT : imgFileFormat; + } + + private static Double getMaximumLetterRotationAngleOrDefault(Double maxrotateAngle) { + return Optional.ofNullable(maxrotateAngle).orElse(DEFAULT_MAX_LETTER_ROTATION_ANGLE); + } + + private static String getFontNameOrDefault(String fontName) { + return StringUtils.isBlank(fontName) ? DEFAULT_FONT : fontName; + } + + private static List getImageOperationsListOrDefault(List imageOperationsList) { + return Optional.ofNullable(imageOperationsList).orElse(Collections.emptyList()); + } + + private static AbstractCaptchaImageBackground getImageBackgroundOrDefault(AbstractCaptchaImageBackground imageBackground) { + return Optional.ofNullable(imageBackground).orElse(DEFAULT_BACKGROUND); + } + /** * Sets a clean NoiseSetting that will not produce any noise when rendered if the given noiseSetting is null or * contains no color. @@ -254,7 +277,7 @@ private static int getHorizontalPlacementOfCharacter(TextRenderingData tRD, Inte } private double getRandomAngleWithinMaximumBounds() { - return (new SecureRandom().nextDouble() - 0.5) * maxrotateAngle; + return (new SecureRandom().nextDouble() - 0.5) * maximumLetterRotationAngle; } diff --git a/src/main/java/io/github/yaforster/flexcaptcha/impl/token/CaptchaCipher.java b/src/main/java/io/github/yaforster/flexcaptcha/impl/token/CaptchaCipher.java index 6afb9e1..3f36a1b 100644 --- a/src/main/java/io/github/yaforster/flexcaptcha/impl/token/CaptchaCipher.java +++ b/src/main/java/io/github/yaforster/flexcaptcha/impl/token/CaptchaCipher.java @@ -21,9 +21,16 @@ public class CaptchaCipher extends AbstractCaptchaCipher { @Builder private CaptchaCipher(CipherSettings cipherSettings, String encryptionPassword, ExpirationTimeSettings expirationTimeSettings) { - super(Optional.ofNullable(cipherSettings) - .orElse(CipherSettings.getDefaultCipherSettings()), Optional.ofNullable(encryptionPassword) - .orElse(StringUtils.EMPTY), expirationTimeSettings); + super(getCipherSettingsOrDefault(cipherSettings), getEncryptionPasswordOrDefault(encryptionPassword), + expirationTimeSettings); + } + + private static CipherSettings getCipherSettingsOrDefault(CipherSettings cipherSettings) { + return Optional.ofNullable(cipherSettings).orElse(CipherSettings.getDefaultCipherSettings()); + } + + private static String getEncryptionPasswordOrDefault(String encryptionPassword) { + return Optional.ofNullable(encryptionPassword).orElse(StringUtils.EMPTY); } @Override diff --git a/src/main/java/io/github/yaforster/flexcaptcha/impl/token/CaptchaCipherBuilder.java b/src/main/java/io/github/yaforster/flexcaptcha/impl/token/CaptchaCipherBuilder.java deleted file mode 100644 index f0cef89..0000000 --- a/src/main/java/io/github/yaforster/flexcaptcha/impl/token/CaptchaCipherBuilder.java +++ /dev/null @@ -1,4 +0,0 @@ -package io.github.yaforster.flexcaptcha.impl.token; - -public class CaptchaCipherBuilder { -}