Class ValidationUtils
The ValidationUtils class supports two primary responsibilities:
- Object validation: Validates message payloads using a
Validator, optionally applying validation groups specified viaValidateWithannotations. - Authorization enforcement: Performs role-based access checks based on annotations such as
RequiresAnyRole,ForbidsAnyRole, andNoUserRequireddeclared on classes, methods, or packages.
Validation
Validation is typically executed automatically by the ValidatingInterceptor before invoking handler methods.
The default validator implementation is loaded via ServiceLoader (e.g. Jsr380Validator).
Methods like assertValid(Object, Class[]) and checkValidity(Object, Validator, Class[])
support recursive validation of collections and custom validation groups.
Authorization
The class also performs role-based security checks. Handler methods or message payloads can declare required roles,
which are evaluated against the current User. If authorization fails, the utility throws either
UnauthenticatedException or UnauthorizedException.
Examples
ValidationUtils.assertValid(payload); // Validate a single object
boolean isValid = ValidationUtils.isValid(payload, MyGroup.class); // Validate with a group
User user = ...;
ValidationUtils.assertAuthorized(MyCommand.class, user); // Authorization check
- See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanassertAuthorized(Class<?> payloadType, User user) Verifies whether the given user is authorized to issue the given payload, based on roles declared via annotations on the payload's class or package.static booleanassertAuthorized(Class<?> target, Executable method, User user) Checks if the given user is authorized to invoke the given method on the given target.protected static booleanassertAuthorized(String action, User user, ValidationUtils.RequiredRole[] requiredRoles) static voidassertValid(Object object, Validator validator, Class<?>... groups) Asserts that the object is valid using the givenValidatorand validation groups.static voidassertValid(Object object, Class<?>... groups) Asserts that the given object is valid, using the defaultValidator.static Optional<ValidationException> checkValidity(Object object, Validator validator, Class<?>... groups) Checks whether the provided object is valid using the givenValidatorand validation groups.static Optional<ValidationException> checkValidity(Object object, Class<?>... groups) Checks whether the provided object is valid, using the defaultValidatorand validation groups.protected static ValidationUtils.RequiredRole[]getRequiredRoles(Collection<? extends Annotation> annotations) static booleanignoreSilently(Class<?> payloadType, User user) Determines whether a particular operation on a payload type should be ignored without raising an exception.static booleanignoreSilently(Class<?> target, Executable method, User user) Determines whether a specific method invocation on a target class by a given user should be ignored without raising an exception, based on the user's authorization.static booleanReturnstrueif the object is valid, using the givenValidatorand validation groups.static booleanReturnstrueif the given object is valid using the defaultValidatorand validation groups.
-
Field Details
-
defaultValidator
Returns the defaultValidatorused for message validation.This is resolved via Java's
ServiceLoadermechanism. If no customValidatoris found, a default JSR 380 (Bean Validation) implementation is used.
-
-
Constructor Details
-
ValidationUtils
public ValidationUtils()
-
-
Method Details
-
checkValidity
Checks whether the provided object is valid, using the defaultValidatorand validation groups.- Parameters:
object- the object to validategroups- optional validation groups- Returns:
- an
Optionalcontaining aValidationExceptionif validation fails, or empty if valid
-
isValid
-
assertValid
Asserts that the given object is valid, using the defaultValidator.Throws a
ValidationExceptionif the object fails validation.- Parameters:
object- the object to validategroups- optional validation groups- Throws:
ValidationException- if validation fails
-
checkValidity
public static Optional<ValidationException> checkValidity(Object object, Validator validator, Class<?>... groups) Checks whether the provided object is valid using the givenValidatorand validation groups.- Parameters:
object- the object to validatevalidator- the validator to usegroups- optional validation groups- Returns:
- an
Optionalcontaining aValidationExceptionif invalid, or empty if valid
-
isValid
Returnstrueif the object is valid, using the givenValidatorand validation groups.- Parameters:
object- the object to validatevalidator- the validator to usegroups- optional validation groups- Returns:
trueif valid,falseotherwise
-
assertValid
Asserts that the object is valid using the givenValidatorand validation groups.Throws a
ValidationExceptionif validation fails.- Parameters:
object- the object to validatevalidator- the validator to usegroups- optional validation groups- Throws:
ValidationException- if validation fails
-
assertAuthorized
public static boolean assertAuthorized(Class<?> payloadType, @Nullable User user) throws UnauthenticatedException, UnauthorizedException Verifies whether the given user is authorized to issue the given payload, based on roles declared via annotations on the payload's class or package.Returns
trueif the user is authorized.If the user is not authorized, either
falseis returned or an exception is thrown. Which happens depends on the detected annotation.- Parameters:
payloadType- the class of the payloaduser- the authenticated user (may be null)- Returns:
trueif authorized,falseif authorization should fail quietly- Throws:
UnauthenticatedException- if authentication is required but the user isnullUnauthorizedException- if the user lacks required roles
-
ignoreSilently
Determines whether a particular operation on a payload type should be ignored without raising an exception.Note: If the user is not authorized and an error occurs during the authorization check, the error is caught silently, and the method invocation is allowed to proceed.
- Parameters:
payloadType- the class of the payload to be evaluateduser- the user whose authorization is being evaluated; may be null for unauthenticated access- Returns:
trueif the operation should be ignored silently,falseotherwise
-
assertAuthorized
Checks if the given user is authorized to invoke the given method on the given target.Returns
trueif the user is authorized.If the user is not authorized, either
falseis returned or an exception is thrown. Which happens depends on the detected annotation.Role requirements may be defined via annotations on the method, class, or package.
- Parameters:
target- the class declaring the methodmethod- the method to checkuser- the user to check- Returns:
trueif authorized,falseif authorization should fail quietly- Throws:
UnauthenticatedException- if authentication is required but the user isnullUnauthorizedException- if the user lacks required roles
-
ignoreSilently
Determines whether a specific method invocation on a target class by a given user should be ignored without raising an exception, based on the user's authorization.Note: If the user is not authorized and an error occurs during the authorization check, the error is caught silently, and the method invocation is allowed to proceed.
- Parameters:
target- the class declaring the method to be checkedmethod- the executable method to be checkeduser- the user whose authorization is being evaluated- Returns:
trueif the method invocation should be ignored without raising an exception,falseotherwise
-
assertAuthorized
protected static boolean assertAuthorized(String action, @Nullable User user, ValidationUtils.RequiredRole[] requiredRoles) -
getRequiredRoles
protected static ValidationUtils.RequiredRole[] getRequiredRoles(Collection<? extends Annotation> annotations)
-