From 693f0244f9c3658b17b6352555408421954fbd4c Mon Sep 17 00:00:00 2001 From: Kamil Date: Wed, 25 Sep 2024 14:06:08 +0200 Subject: [PATCH 1/3] #3021 Refactoring for String validation by Java Bean Validation: - created interface ScadaValidator and exception ScadaValidatorException, AbstractConstraintValidator, CssValidatorException; - moved css validation to org.scada_lts.web.beans.validation.css; --- build.gradle | 2 +- .../AbstractConstraintValidator.java | 47 +++++++++++++++++++ .../web/beans/validation/ScadaValidator.java | 7 +++ .../validation/ScadaValidatorException.java | 23 +++++++++ .../css/CssConstraintValidator.java | 26 ++++++++++ .../validation/css}/CssValid.java | 2 +- .../validation/css/CssValidatorException.java | 25 ++++++++++ .../validation/css}/SacCssValidator.java | 9 ++-- .../scada_lts/web/mvc/api/css/CssStyle.java | 2 +- .../validation/CssConstraintValidator.java | 39 --------------- .../css/validation/utils/CssValidator.java | 6 --- .../validation/utils/CustomCssException.java | 23 --------- .../css}/CssValidatorExceptionTest.java | 10 ++-- .../validation/css}/CssValidatorTest.java | 8 ++-- .../css}/CssValidatorTestsSuite.java | 2 +- webapp-resources/log4j2.xml | 7 +++ 16 files changed, 155 insertions(+), 83 deletions(-) create mode 100644 src/org/scada_lts/web/beans/validation/AbstractConstraintValidator.java create mode 100644 src/org/scada_lts/web/beans/validation/ScadaValidator.java create mode 100644 src/org/scada_lts/web/beans/validation/ScadaValidatorException.java create mode 100644 src/org/scada_lts/web/beans/validation/css/CssConstraintValidator.java rename src/org/scada_lts/web/{mvc/api/css/validation => beans/validation/css}/CssValid.java (90%) create mode 100644 src/org/scada_lts/web/beans/validation/css/CssValidatorException.java rename src/org/scada_lts/web/{mvc/api/css/validation/utils => beans/validation/css}/SacCssValidator.java (80%) delete mode 100644 src/org/scada_lts/web/mvc/api/css/validation/CssConstraintValidator.java delete mode 100644 src/org/scada_lts/web/mvc/api/css/validation/utils/CssValidator.java delete mode 100644 src/org/scada_lts/web/mvc/api/css/validation/utils/CustomCssException.java rename test/org/scada_lts/web/{mvc/api/css/validation/utils => beans/validation/css}/CssValidatorExceptionTest.java (72%) rename test/org/scada_lts/web/{mvc/api/css/validation/utils => beans/validation/css}/CssValidatorTest.java (75%) rename test/org/scada_lts/web/{mvc/api/css/validation/utils => beans/validation/css}/CssValidatorTestsSuite.java (80%) diff --git a/build.gradle b/build.gradle index 2962dd4b1e..352a67a2af 100644 --- a/build.gradle +++ b/build.gradle @@ -242,7 +242,7 @@ test { includeTestsMatching "org.scada_lts.utils.BlockingQueuesUtilsTest" includeTestsMatching "org.scada_lts.web.security.XssProtectHtmlEscapeUtilsTest" includeTestsMatching "org.scada_lts.web.security.XssUtilsTest" - includeTestsMatching "org.scada_lts.web.mvc.api.css.validation.utils.CssValidatorTestsSuite" + includeTestsMatching "org.scada_lts.web.mvc.api.validation.css.CssValidatorTestsSuite" } failFast = true diff --git a/src/org/scada_lts/web/beans/validation/AbstractConstraintValidator.java b/src/org/scada_lts/web/beans/validation/AbstractConstraintValidator.java new file mode 100644 index 0000000000..798349cd0f --- /dev/null +++ b/src/org/scada_lts/web/beans/validation/AbstractConstraintValidator.java @@ -0,0 +1,47 @@ +package org.scada_lts.web.beans.validation; + + +import com.serotonin.mango.util.LoggingUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; +import java.lang.annotation.Annotation; + + +public abstract class AbstractConstraintValidator implements ConstraintValidator { + + private static final Logger LOG = LogManager.getLogger(AbstractConstraintValidator.class); + + @Override + public void initialize(A constraintAnnotation) { + } + + @Override + public final boolean isValid(T value, ConstraintValidatorContext context) { + try { + beforeValidate(value); + } catch (Exception e) { + LOG.warn("ConstraintValidator: {}", LoggingUtils.causeInfo(e)); + addErrors(context, LoggingUtils.causeInfo(e)); + return false; + } + try { + validate(value); + return true; + } catch (Exception e) { + LOG.warn("ConstraintValidator: {}", LoggingUtils.causeInfo(e)); + addErrors(context, LoggingUtils.causeInfo(e)); + return false; + } + } + + public void beforeValidate(T value) throws Exception {} + public abstract void validate(T value) throws Exception; + + private static void addErrors(ConstraintValidatorContext context, String msg) { + context.disableDefaultConstraintViolation(); + context.buildConstraintViolationWithTemplate("Unexpected error: " + msg).addConstraintViolation(); + } +} diff --git a/src/org/scada_lts/web/beans/validation/ScadaValidator.java b/src/org/scada_lts/web/beans/validation/ScadaValidator.java new file mode 100644 index 0000000000..d9e40966d7 --- /dev/null +++ b/src/org/scada_lts/web/beans/validation/ScadaValidator.java @@ -0,0 +1,7 @@ +package org.scada_lts.web.beans.validation; + +public interface ScadaValidator { + + void validate(T object) throws ScadaValidatorException; + +} diff --git a/src/org/scada_lts/web/beans/validation/ScadaValidatorException.java b/src/org/scada_lts/web/beans/validation/ScadaValidatorException.java new file mode 100644 index 0000000000..0e7fde1f66 --- /dev/null +++ b/src/org/scada_lts/web/beans/validation/ScadaValidatorException.java @@ -0,0 +1,23 @@ +package org.scada_lts.web.beans.validation; + +public class ScadaValidatorException extends Exception { + + public ScadaValidatorException() { + } + + public ScadaValidatorException(String message) { + super(message); + } + + public ScadaValidatorException(String message, Throwable cause) { + super(message, cause); + } + + public ScadaValidatorException(Throwable cause) { + super(cause); + } + + public ScadaValidatorException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/org/scada_lts/web/beans/validation/css/CssConstraintValidator.java b/src/org/scada_lts/web/beans/validation/css/CssConstraintValidator.java new file mode 100644 index 0000000000..69911930ee --- /dev/null +++ b/src/org/scada_lts/web/beans/validation/css/CssConstraintValidator.java @@ -0,0 +1,26 @@ +package org.scada_lts.web.beans.validation.css; + +import org.scada_lts.serorepl.utils.StringUtils; +import org.scada_lts.web.beans.validation.AbstractConstraintValidator; +import org.scada_lts.web.beans.validation.ScadaValidator; + + +public class CssConstraintValidator extends AbstractConstraintValidator { + + @Override + public void initialize(CssValid constraintAnnotation) { + } + + @Override + public void beforeValidate(String value) throws Exception { + if(StringUtils.isEmpty(value)) { + throw new CssValidatorException("Empty"); + } + } + + @Override + public void validate(String value) throws Exception { + ScadaValidator validator = new SacCssValidator(); + validator.validate(value); + } +} diff --git a/src/org/scada_lts/web/mvc/api/css/validation/CssValid.java b/src/org/scada_lts/web/beans/validation/css/CssValid.java similarity index 90% rename from src/org/scada_lts/web/mvc/api/css/validation/CssValid.java rename to src/org/scada_lts/web/beans/validation/css/CssValid.java index f48f46fc1f..5abb9bd8a9 100644 --- a/src/org/scada_lts/web/mvc/api/css/validation/CssValid.java +++ b/src/org/scada_lts/web/beans/validation/css/CssValid.java @@ -1,4 +1,4 @@ -package org.scada_lts.web.mvc.api.css.validation; +package org.scada_lts.web.beans.validation.css; import javax.validation.Constraint; diff --git a/src/org/scada_lts/web/beans/validation/css/CssValidatorException.java b/src/org/scada_lts/web/beans/validation/css/CssValidatorException.java new file mode 100644 index 0000000000..f1d8943a93 --- /dev/null +++ b/src/org/scada_lts/web/beans/validation/css/CssValidatorException.java @@ -0,0 +1,25 @@ +package org.scada_lts.web.beans.validation.css; + +import org.scada_lts.web.beans.validation.ScadaValidatorException; + +public class CssValidatorException extends ScadaValidatorException { + + public CssValidatorException() { + } + + public CssValidatorException(String message) { + super(message); + } + + public CssValidatorException(String message, Throwable cause) { + super(message, cause); + } + + public CssValidatorException(Throwable cause) { + super(cause); + } + + public CssValidatorException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/org/scada_lts/web/mvc/api/css/validation/utils/SacCssValidator.java b/src/org/scada_lts/web/beans/validation/css/SacCssValidator.java similarity index 80% rename from src/org/scada_lts/web/mvc/api/css/validation/utils/SacCssValidator.java rename to src/org/scada_lts/web/beans/validation/css/SacCssValidator.java index ebefad4d16..f7c31f79d1 100644 --- a/src/org/scada_lts/web/mvc/api/css/validation/utils/SacCssValidator.java +++ b/src/org/scada_lts/web/beans/validation/css/SacCssValidator.java @@ -1,12 +1,13 @@ -package org.scada_lts.web.mvc.api.css.validation.utils; +package org.scada_lts.web.beans.validation.css; import com.steadystate.css.parser.SACParserCSS3; +import org.scada_lts.web.beans.validation.ScadaValidator; import org.w3c.css.sac.*; import java.io.IOException; import java.io.StringReader; -public final class SacCssValidator implements CssValidator { +public final class SacCssValidator implements ScadaValidator { private final Parser parser; @@ -15,11 +16,11 @@ public SacCssValidator() { } @Override - public void validate(String style) throws CustomCssException { + public void validate(String style) throws CssValidatorException { try { validateStyle(parser, style); } catch (Exception e) { - throw new CustomCssException(e.getMessage(), e); + throw new CssValidatorException(e.getMessage(), e); } } diff --git a/src/org/scada_lts/web/mvc/api/css/CssStyle.java b/src/org/scada_lts/web/mvc/api/css/CssStyle.java index a152ea0a39..0021b3f7f4 100644 --- a/src/org/scada_lts/web/mvc/api/css/CssStyle.java +++ b/src/org/scada_lts/web/mvc/api/css/CssStyle.java @@ -3,7 +3,7 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; -import org.scada_lts.web.mvc.api.css.validation.CssValid; +import org.scada_lts.web.beans.validation.css.CssValid; public class CssStyle { diff --git a/src/org/scada_lts/web/mvc/api/css/validation/CssConstraintValidator.java b/src/org/scada_lts/web/mvc/api/css/validation/CssConstraintValidator.java deleted file mode 100644 index a57b3af538..0000000000 --- a/src/org/scada_lts/web/mvc/api/css/validation/CssConstraintValidator.java +++ /dev/null @@ -1,39 +0,0 @@ -package org.scada_lts.web.mvc.api.css.validation; - - -import com.serotonin.mango.util.LoggingUtils; -import org.scada_lts.serorepl.utils.StringUtils; -import org.scada_lts.web.mvc.api.css.validation.utils.CssValidator; -import org.scada_lts.web.mvc.api.css.validation.utils.SacCssValidator; - -import javax.validation.ConstraintValidator; -import javax.validation.ConstraintValidatorContext; - - -public class CssConstraintValidator implements ConstraintValidator { - - @Override - public void initialize(CssValid constraintAnnotation) { - } - - @Override - public boolean isValid(String value, ConstraintValidatorContext context) { - if (StringUtils.isEmpty(value)) { - addErrors(context, "Empty"); - return false; - } - try { - CssValidator validator = new SacCssValidator(); - validator.validate(value); - return true; - }catch (Exception e) { - addErrors(context, LoggingUtils.causeInfo(e)); - return false; - } - } - - private static void addErrors(ConstraintValidatorContext context, String msg) { - context.disableDefaultConstraintViolation(); - context.buildConstraintViolationWithTemplate("Unexpected error: " + msg).addConstraintViolation(); - } -} diff --git a/src/org/scada_lts/web/mvc/api/css/validation/utils/CssValidator.java b/src/org/scada_lts/web/mvc/api/css/validation/utils/CssValidator.java deleted file mode 100644 index 8ed98f7ab3..0000000000 --- a/src/org/scada_lts/web/mvc/api/css/validation/utils/CssValidator.java +++ /dev/null @@ -1,6 +0,0 @@ -package org.scada_lts.web.mvc.api.css.validation.utils; - -public interface CssValidator { - - void validate(String style) throws CustomCssException; -} diff --git a/src/org/scada_lts/web/mvc/api/css/validation/utils/CustomCssException.java b/src/org/scada_lts/web/mvc/api/css/validation/utils/CustomCssException.java deleted file mode 100644 index c9561bc583..0000000000 --- a/src/org/scada_lts/web/mvc/api/css/validation/utils/CustomCssException.java +++ /dev/null @@ -1,23 +0,0 @@ -package org.scada_lts.web.mvc.api.css.validation.utils; - -public class CustomCssException extends Exception { - - public CustomCssException() { - } - - public CustomCssException(String message) { - super(message); - } - - public CustomCssException(String message, Throwable cause) { - super(message, cause); - } - - public CustomCssException(Throwable cause) { - super(cause); - } - - public CustomCssException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { - super(message, cause, enableSuppression, writableStackTrace); - } -} diff --git a/test/org/scada_lts/web/mvc/api/css/validation/utils/CssValidatorExceptionTest.java b/test/org/scada_lts/web/beans/validation/css/CssValidatorExceptionTest.java similarity index 72% rename from test/org/scada_lts/web/mvc/api/css/validation/utils/CssValidatorExceptionTest.java rename to test/org/scada_lts/web/beans/validation/css/CssValidatorExceptionTest.java index c15f26dab8..4d521bf59c 100644 --- a/test/org/scada_lts/web/mvc/api/css/validation/utils/CssValidatorExceptionTest.java +++ b/test/org/scada_lts/web/beans/validation/css/CssValidatorExceptionTest.java @@ -1,8 +1,10 @@ -package org.scada_lts.web.mvc.api.css.validation.utils; +package org.scada_lts.web.beans.validation.css; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; +import org.scada_lts.web.beans.validation.ScadaValidator; +import org.scada_lts.web.beans.validation.ScadaValidatorException; import java.util.Arrays; import java.util.Collection; @@ -23,15 +25,15 @@ public static Collection data() { } private final String css; - public final CssValidator validator; + public final ScadaValidator validator; public CssValidatorExceptionTest(String css) { this.css = css; this.validator = new SacCssValidator(); } - @Test(expected = CustomCssException.class) - public void when_isInvalidCss() throws CustomCssException { + @Test(expected = CssValidatorException.class) + public void when_isInvalidCss() throws ScadaValidatorException { validator.validate(css); } } diff --git a/test/org/scada_lts/web/mvc/api/css/validation/utils/CssValidatorTest.java b/test/org/scada_lts/web/beans/validation/css/CssValidatorTest.java similarity index 75% rename from test/org/scada_lts/web/mvc/api/css/validation/utils/CssValidatorTest.java rename to test/org/scada_lts/web/beans/validation/css/CssValidatorTest.java index d3658ed1ec..ff5f6efbc6 100644 --- a/test/org/scada_lts/web/mvc/api/css/validation/utils/CssValidatorTest.java +++ b/test/org/scada_lts/web/beans/validation/css/CssValidatorTest.java @@ -1,8 +1,10 @@ -package org.scada_lts.web.mvc.api.css.validation.utils; +package org.scada_lts.web.beans.validation.css; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; +import org.scada_lts.web.beans.validation.ScadaValidator; +import org.scada_lts.web.beans.validation.ScadaValidatorException; import java.util.Arrays; import java.util.Collection; @@ -23,7 +25,7 @@ public static Collection data() { } private final String css; - public final CssValidator validator; + public final ScadaValidator validator; public CssValidatorTest(String css) { this.css = css; @@ -31,7 +33,7 @@ public CssValidatorTest(String css) { } @Test - public void when_isValidCss() throws CustomCssException { + public void when_isValidCss() throws ScadaValidatorException { validator.validate(css); } } diff --git a/test/org/scada_lts/web/mvc/api/css/validation/utils/CssValidatorTestsSuite.java b/test/org/scada_lts/web/beans/validation/css/CssValidatorTestsSuite.java similarity index 80% rename from test/org/scada_lts/web/mvc/api/css/validation/utils/CssValidatorTestsSuite.java rename to test/org/scada_lts/web/beans/validation/css/CssValidatorTestsSuite.java index affe36a966..327e00eda8 100644 --- a/test/org/scada_lts/web/mvc/api/css/validation/utils/CssValidatorTestsSuite.java +++ b/test/org/scada_lts/web/beans/validation/css/CssValidatorTestsSuite.java @@ -1,4 +1,4 @@ -package org.scada_lts.web.mvc.api.css.validation.utils; +package org.scada_lts.web.beans.validation.css; import org.junit.runner.RunWith; import org.junit.runners.Suite; diff --git a/webapp-resources/log4j2.xml b/webapp-resources/log4j2.xml index 8f07ef9f6d..56c759e1b0 100644 --- a/webapp-resources/log4j2.xml +++ b/webapp-resources/log4j2.xml @@ -45,6 +45,9 @@ ERROR + + WARN + @@ -327,6 +330,10 @@ + + + + From 3e677d9d70e879312dc98fef54213ccce594c58f Mon Sep 17 00:00:00 2001 From: Kamil Date: Wed, 25 Sep 2024 14:12:47 +0200 Subject: [PATCH 2/3] #3021 Refactoring for String validation by Java Bean Validation: - CssConstraintValidator: removed initialize method; --- .../web/beans/validation/css/CssConstraintValidator.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/org/scada_lts/web/beans/validation/css/CssConstraintValidator.java b/src/org/scada_lts/web/beans/validation/css/CssConstraintValidator.java index 69911930ee..eed5566eab 100644 --- a/src/org/scada_lts/web/beans/validation/css/CssConstraintValidator.java +++ b/src/org/scada_lts/web/beans/validation/css/CssConstraintValidator.java @@ -7,10 +7,6 @@ public class CssConstraintValidator extends AbstractConstraintValidator { - @Override - public void initialize(CssValid constraintAnnotation) { - } - @Override public void beforeValidate(String value) throws Exception { if(StringUtils.isEmpty(value)) { From f62a4edf4e5cffc34723b58341504be1f3795ec4 Mon Sep 17 00:00:00 2001 From: Kamil Date: Wed, 25 Sep 2024 14:36:08 +0200 Subject: [PATCH 3/3] #3021 Refactoring for String validation by Java Bean Validation: - set ERROR logging level for validationLoggingLevel in log4j2.xml --- webapp-resources/log4j2.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webapp-resources/log4j2.xml b/webapp-resources/log4j2.xml index 56c759e1b0..996e610ff7 100644 --- a/webapp-resources/log4j2.xml +++ b/webapp-resources/log4j2.xml @@ -46,7 +46,7 @@ ERROR - WARN + ERROR