org.axonframework.commandhandling.interceptors
Class BeanValidationInterceptor

java.lang.Object
  extended by org.axonframework.commandhandling.interceptors.BeanValidationInterceptor
All Implemented Interfaces:
CommandDispatchInterceptor, CommandHandlerInterceptor

public class BeanValidationInterceptor
extends Object
implements CommandHandlerInterceptor, CommandDispatchInterceptor

Interceptor that applies JSR303 bean validation on incoming commands. When validation on a command fails, a JSR303ViolationException is thrown, holding the constraint violations.

This interceptor can either be used as a CommandHandlerInterceptor or as a CommandDispatchInterceptor.

Since:
1.1
Author:
Allard Buijze

Constructor Summary
BeanValidationInterceptor()
          Initializes a validation interceptor using a default ValidatorFactory (see Validation.buildDefaultValidatorFactory()).
BeanValidationInterceptor(javax.validation.ValidatorFactory validatorFactory)
          Initializes a validation interceptor using the given ValidatorFactory.
 
Method Summary
 CommandMessage<?> handle(CommandMessage<?> command)
          Invoked each time a command is about to be dispatched on a Command Bus.
 Object handle(CommandMessage<?> command, UnitOfWork unitOfWork, InterceptorChain interceptorChain)
          The handle method is invoked each time a command is dispatched through the command bus that the CommandHandlerInterceptor is declared on.
protected  Set<javax.validation.ConstraintViolation<Object>> validateCommand(Object command, javax.validation.Validator validator)
          Validate the given command using the given validator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BeanValidationInterceptor

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


BeanValidationInterceptor

public BeanValidationInterceptor(javax.validation.ValidatorFactory validatorFactory)
Initializes a validation interceptor using the given ValidatorFactory.

Parameters:
validatorFactory - the factory providing Validator instances for this interceptor.
Method Detail

handle

public Object handle(CommandMessage<?> command,
                     UnitOfWork unitOfWork,
                     InterceptorChain interceptorChain)
              throws Throwable
Description copied from interface: CommandHandlerInterceptor
The handle method is invoked each time a command is dispatched through the command bus that the CommandHandlerInterceptor is declared on. The incoming command and contextual information can be found in the given unitOfWork.

The interceptor is responsible for the continuation of the dispatch process by invoking the InterceptorChain.proceed(CommandMessage) method on the given interceptorChain.

Any information gathered by interceptors may be attached to the unitOfWork. This information is made available to the CommandCallback provided by the dispatching component.

Interceptors are highly recommended not to change the type of the command handling result, as the dispatching component might expect a result of a specific type.

Specified by:
handle in interface CommandHandlerInterceptor
Parameters:
command - The command being dispatched
unitOfWork - The UnitOfWork in which
interceptorChain - The interceptor chain that allows this interceptor to proceed the dispatch process
Returns:
the result of the command handler. May have been modified by interceptors.
Throws:
Throwable - any exception that occurs while handling the command

handle

public CommandMessage<?> handle(CommandMessage<?> command)
Description copied from interface: CommandDispatchInterceptor
Invoked each time a command is about to be dispatched on a Command Bus. The given commandMessage represents the command being dispatched.

Specified by:
handle in interface CommandDispatchInterceptor
Parameters:
command - The command message intended to be dispatched on the Command Bus
Returns:
the command message to dispatch on the Command Bus

validateCommand

protected Set<javax.validation.ConstraintViolation<Object>> validateCommand(Object command,
                                                                            javax.validation.Validator validator)
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.


Copyright © 2010-2016. All Rights Reserved.