Is it possible to set multiple posts using oval AbstractAnnotationCheck?

I am using the Oval validation framework to validate fields that HTML fields cannot contain malicious javascript code. To detect malicious code, I use an external structure that returns me a list of errors that I would like to use as error messages in this field. The problem I am facing is that I can set setMessage in a validation implementation, while I would rather do something like setMessages (List). Therefore, while I'm currently just joining semicolon errors, I would rather pass them as a list.

annotation

@Target({ ElementType.METHOD, ElementType.FIELD}) @Retention( RetentionPolicy.RUNTIME) @Constraint(checkWith = HtmlFieldValidator.class) public @interface HtmlField { String message() default "HTML could not be validated"; } 

Check

 public class HtmlFieldValidator extends AbstractAnnotationCheck<HtmlDefaultValue> { public boolean isSatisfied( Object o, Object o1, OValContext oValContext, Validator validator ) throws OValException { if (o1 == null) { return true; } else { CleanResults cleanResults = UIowaAntiSamy.cleanHtml((String) o1); if (cleanResults.getErrorMessages().size() > 0) { String errors = StringUtils.join(cleanResults.getErrorMessages(), ", "); this.setMessage(errors); return false; } else { return true; } } } } 

Model class

 class Foo { @HtmlField public String bar; } 

Controller code

 Validator validator = new Validator(); // use the OVal validator Foo foo = new Foo(); foo.bar = "<script>hack()</script>"; List<ConstraintViolation> violations = validator.validate(bo); if (violations.size() > 0) { // inform the user that I cannot accept the string because // it contains invalid html, using error messages from OVal } 
+4
source share
2 answers

If setMessage(String message) is a method created by the superclass, you can override it, and as soon as it receives the data, just split the string into a list and call the second function, in which you really place your code. On the other hand, I would also recommend changing the separator line to something more unique, since the error message itself may contain a comma.

Your question doesn’t really make much sense. If you “pass them back” to the method implemented in the superclass, this excludes the whole point of your question, since the superclass will process the data.

I'm going to assume that the setError methods is a simple installer that sets the String variable to store the error message that you plan to receive after checking the data. Since you want to have data in your preferred type, just create a new array of strings in your class and ignore the superclass. You can even use both options if you want.

 public class HtmlFieldValidator extends AbstractAnnotationCheck<HtmlDefaultValue> { public String[] errorMessages = null; public void setErrorMessages(String[] s) { this.errorMessages = s; } public boolean isSatisfied( Object o, Object o1, OValContext oValContext, Validator validator ) throws OValException { if (o1 == null) { return true; } else { CleanResults cleanResults = UIowaAntiSamy.cleanHtml((String) o1); if (cleanResults.getErrorMessages().size() > 0) { //String errors = StringUtils.join(cleanResults.getErrorMessages(), ", "); //this.setMessage(errors); this.setErrorMessages(cleanResults.getErrorMessages()); return false; } else { return true; } } } } 

In the other place:

 HtmlFieldValidator<DefaultValue> hfv = new HtmlFieldValidator<DefaultValue>(); boolean satisfied = hfv.isSatisfied(params); if (!satisfied) { String[] errorMessages = hfv.errorMessages; //instead of using their error message satisfy(errorMessages);//or whatever you want to do } 

EDIT:

After updating the code, I understand what you mean. Although I think this is kind of overdoing, and it would be much easier to just convert the string to an array later, you could do this by creating a new class that extends Validator its setMessage method. In the method, you call super.setMethod , and also split and save the string as an array in your class.

 class ValidatorWithArray extends Validator { public String[] errors; public final static String SPLIT_REGEX = ";&spLit;";// Something unique so you wont accidentally have it in the error public void setMessage(String error) { super.setMessage(error); this.errors = String.split(error, SPLIT_REGEX); } } 

In the HtmlFieldValidator :

 public boolean isSatisfied( Object o, Object o1, OValContext oValContext, Validator validator ) throws OValException { if (o1 == null) { return true; } else { CleanResults cleanResults = UIowaAntiSamy.cleanHtml((String) o1); if (cleanResults.getErrorMessages().size() > 0) { String errors = StringUtils.join(cleanResults.getErrorMessages(), ValidatorWithArray.SPLIT_REGEX); this.setMessage(errors); return false; } else { return true; } } } 

Now just use ValidatorWithArray instead of Validator

+4
source

The situation in which I want to achieve this is different from yours, however, what I found was best in my case - to create an annotation for each error (instead of having one that would return several errors). I think it depends on how many mistakes you are likely to make in my case, there were only two or three.

This method also makes your code very easy to reuse, as you can simply add annotations if they need you and combine them as you wish.

0
source

Source: https://habr.com/ru/post/1383914/


All Articles