camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christian Mueller <>
Subject Re: Proposal for a new camel-bean-validation component based on jsr 303
Date Sun, 21 Mar 2010 11:02:09 GMT

Hello Claus!

My first and easiest implementation of the bean-validator looks like (it is
inspired from the schema validation component):

public class BeanValidatorComponent extends DefaultComponent {

    protected Endpoint createEndpoint(String uri, String remaining,
Map<String, Object> parameters) throws Exception {
        BeanValidator beanValidator = new BeanValidator();
        configureValidator(beanValidator, uri, remaining, parameters);

        return new ProcessorEndpoint(uri, this, beanValidator);

    protected void configureValidator(BeanValidator beanValidator, String
uri, String remaining, Map<String, Object> parameters) throws Exception {
        ValidatorFactory factory =
        Validator validator = factory.getValidator();


public class BeanValidator implements Processor {
    private Validator validator;
    public void process(Exchange exchange) throws Exception {
        Object bean = exchange.getIn().getBody();
        Set<ConstraintViolation> constraintViolations =
        handleErrors(exchange, constraintViolations);
    public void handleErrors(Exchange exchange, Set<ConstraintViolation>
constraintViolations) throws ValidationException {
        if (!constraintViolations.isEmpty()) {
            throw new BeanValidationException(exchange,
constraintViolations, exchange.getIn().getBody());

    public Validator getValidator() {
        return validator;

    public void setValidator(Validator validator) {
        this.validator = validator;

public class BeanValidationException extends ValidationException {

    private Set<ConstraintViolation> constraintViolations;

    public BeanValidationException(Exchange exchange,
Set<ConstraintViolation> constraintViolations, Object bean) {
        super(exchange, buildMessage(constraintViolations, bean));
        this.constraintViolations = constraintViolations;

    protected static String buildMessage(Set<ConstraintViolation>
constraintViolations, Object bean) {
        StringBuffer buffer = new StringBuffer("Validation failed for: ");

        buffer.append(" errors: [");
        for (ConstraintViolation constraintViolation : constraintViolations)
            buffer.append("property: " +
constraintViolation.getPropertyPath() + "; value: " +
constraintViolation.getInvalidValue() + "; constraint: " +
constraintViolation.getMessage() + "; ");

        return buffer.toString();
    public Set<ConstraintViolation> getConstraintViolations() {
        return constraintViolations;

I think this shows how the Validation API works. The Validator will not
throw any exception, if the validation fails. It returns a Set with
ConstraintViolations. Some features of the Validator API are not shown, but
interesting for the users:
- Validating groups
- Creating custom constraints
- XML configuration
- Configure custom ValidationProviderResolver, MessageInterpolator,
TraversableResolver or ConstraintValidatorFactory

Because the validate method of the Validator doesn't throw an exception but
returns a Set with ConstraintViolations, I'm afraid we could not use the
bean component.

I found the following two still open issues for this topic:

You and others prefers to use Predicates/Processors like this:




Now, I think also this is the better solution to implements these
requirements. But I'm not so familiar with implementing
Predicates/Processors and extend the Java/XML DSL. Could you give me a hint,
where I could find a good sample in the Camel code to implement a


View this message in context:
Sent from the Camel - Users mailing list archive at

View raw message