Skip to content

Commit

Permalink
Merge pull request #3022 from SCADA-LTS/feature/#3021_Refactoring_for…
Browse files Browse the repository at this point in the history
…_String_validation_by_Java_Bean_Validation

#3021 Refactoring for String validation by Java Bean Validation:
  • Loading branch information
Limraj authored Sep 25, 2024
2 parents 65b09ef + f62a4ed commit 30b2799
Show file tree
Hide file tree
Showing 16 changed files with 151 additions and 83 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<A extends Annotation, T> implements ConstraintValidator<A, T> {

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();
}
}
7 changes: 7 additions & 0 deletions src/org/scada_lts/web/beans/validation/ScadaValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.scada_lts.web.beans.validation;

public interface ScadaValidator<T> {

void validate(T object) throws ScadaValidatorException;

}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<CssValid, String> {

@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<String> validator = new SacCssValidator();
validator.validate(value);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<String> {

private final Parser parser;

Expand All @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/org/scada_lts/web/mvc/api/css/CssStyle.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,15 +25,15 @@ public static Collection<Object[]> data() {
}

private final String css;
public final CssValidator validator;
public final ScadaValidator<String> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,15 +25,15 @@ public static Collection<Object[]> data() {
}

private final String css;
public final CssValidator validator;
public final ScadaValidator<String> validator;

public CssValidatorTest(String css) {
this.css = css;
this.validator = new SacCssValidator();
}

@Test
public void when_isValidCss() throws CustomCssException {
public void when_isValidCss() throws ScadaValidatorException {
validator.validate(css);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
7 changes: 7 additions & 0 deletions webapp-resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
<!-- Security Logging-->
<Property name="securityLoggingLevel">ERROR</Property>

<!-- Java Bean Validation Logging-->
<Property name="validationLoggingLevel">ERROR</Property>

</Properties>

<Appenders>
Expand Down Expand Up @@ -327,6 +330,10 @@
<Logger name="org.scada_lts.web.security" level="${securityLoggingLevel}" />
<Logger name="org.springframework.security" level="${securityLoggingLevel}" />

<!-- Java Bean Validation Logging -->
<Logger name="javax.validation" level="${validationLoggingLevel}" />
<Logger name="org.scada_lts.web.beans.validation" level="${validationLoggingLevel}" />

<Root level="${rootLoggingLevel}">
<AppenderRef ref="STDOUT"/>
<AppenderRef ref="ASYNC"/>
Expand Down

0 comments on commit 30b2799

Please sign in to comment.