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..eed5566eab --- /dev/null +++ b/src/org/scada_lts/web/beans/validation/css/CssConstraintValidator.java @@ -0,0 +1,22 @@ +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 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..996e610ff7 100644 --- a/webapp-resources/log4j2.xml +++ b/webapp-resources/log4j2.xml @@ -45,6 +45,9 @@ ERROR + + ERROR + @@ -327,6 +330,10 @@ + + + +