-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3022 from SCADA-LTS/feature/#3021_Refactoring_for…
…_String_validation_by_Java_Bean_Validation #3021 Refactoring for String validation by Java Bean Validation:
- Loading branch information
Showing
16 changed files
with
151 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/org/scada_lts/web/beans/validation/AbstractConstraintValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
src/org/scada_lts/web/beans/validation/ScadaValidatorException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/org/scada_lts/web/beans/validation/css/CssConstraintValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
.../web/mvc/api/css/validation/CssValid.java → ...ts/web/beans/validation/css/CssValid.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/org/scada_lts/web/beans/validation/css/CssValidatorException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 0 additions & 39 deletions
39
src/org/scada_lts/web/mvc/api/css/validation/CssConstraintValidator.java
This file was deleted.
Oops, something went wrong.
6 changes: 0 additions & 6 deletions
6
src/org/scada_lts/web/mvc/api/css/validation/utils/CssValidator.java
This file was deleted.
Oops, something went wrong.
23 changes: 0 additions & 23 deletions
23
src/org/scada_lts/web/mvc/api/css/validation/utils/CustomCssException.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...idation/utils/CssValidatorTestsSuite.java → ...alidation/css/CssValidatorTestsSuite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters