What I have done so far is adding a severity elemen / attribute into an annotation as such:
Code:
public @interface AuthorityToBackdate {
< .... >
Severity severity() default Severity.ERROR;
}
I then define an enum of the severity, and within that enum class, I then obtain the severity:
Code:
import javax.validation.ConstraintViolation;
import javax.validation.Payload;
public enum Severity implements Payload {
INFO, WARNING, ERROR, FATAL;
public static Severity getSeverity(ConstraintViolation violation) {
Severity severity = (Severity) violation.getConstraintDescriptor().getAttributes().get("severity");
return severity;
}
}
Then from the custom validator, I can then change the attribute to a WARNING.
And then from the BV client, after doing the validation, I can then do something lik below:
Code:
Severity severity = Severity.getSeverity(violation);
.. to find out the severity.
However, the above means that:
1) All my custom annotations must have the attribute severity defined
2) I should modify the method Severit.getSeverity() so that it returns Severity.ERROR by default if the attribute does not exist.
Some questions though:
Since the severity is an attribute of the constraint and obtained via ConstraintDescriptor.getAttribute(), is setting any attribute for that matter within a ConstraintValidator ( e.g. to change it from default of ERROR to WARNING ) thread safe ?
Also, if after an execution of the custom validator, the attribute was changed to WARNING, will subsequent calls to the validator and / or will subsequent BV client calls see the attribute as WARNING instead of ERROR ? That is, changing the value of an attribute becomes its new current value unless otherwise changed again ?