Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  * Copyright (c) 2010-2011. Axon Framework
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.
package org.axonframework.commandhandling.interceptors;
Interceptor that applies JSR303 bean validation on incoming commands. When validation on a command fails, a JSR303ViolationException is thrown, holding the constraint violations.

Allard Buijze
    private final ValidatorFactory validatorFactory;

Initializes a validation interceptor using a default ValidatorFactory (see javax.validation.Validation.buildDefaultValidatorFactory()).
    public BeanValidationInterceptor() {

Initializes a validation interceptor using the given ValidatorFactory.

validatorFactory the factory providing Validator instances for this interceptor.
    public BeanValidationInterceptor(ValidatorFactory validatorFactory) {
        this. = validatorFactory;
    public Object handle(Object commandUnitOfWork unitOfWorkInterceptorChain interceptorChainthrows Throwable {
        Validator validator = .getValidator();
        Set<ConstraintViolation<Object>> violations = validateCommand(commandvalidator);
        if (violations != null && !violations.isEmpty()) {
            throw new JSR303ViolationException("One or more JSR303 constraints were violated."violations);
        return interceptorChain.proceed();

Validate the given command using the given validator. The default implementation merely calls validator.validate(command).

Subclasses may override this method to alter the validation behavior in specific cases. Although the null is accepted as return value to indicate that there are no constraint violations, implementations are encouraged to return an empty Set instead.

command The command to validate
validator The validator provided by the validator factory
a set of Constraint Violations. May also return null.
    protected Set<ConstraintViolation<Object>> validateCommand(Object commandValidator validator) {
        return validator.validate(command);
New to GrepCode? Check out our FAQ X