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
  *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * 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.

Author(s):
Allard Buijze
Since:
1.1
    private final ValidatorFactory validatorFactory;

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

    
Initializes a validation interceptor using the given ValidatorFactory.

Parameters:
validatorFactory the factory providing Validator instances for this interceptor.
    public BeanValidationInterceptor(ValidatorFactory validatorFactory) {
        this. = validatorFactory;
    }
    @Override
    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.

Parameters:
command The command to validate
validator The validator provided by the validator factory
Returns:
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