You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Christian Mueller <ch...@gmail.com> on 2010/03/19 15:20:37 UTC
Proposal for a new camel-bean-validation component based on jsr 303
Currently, I'm working on a project where we have to read fixed length
records and csv records. Because we don't trust other developers ;-), we
want to validate the input.
What do you think about a bean validation component which is based on jsr
303 (http://jcp.org/en/jsr/detail?id=303) and which could be used like the
existing camel-validation component:
from(file://sampleIn.txt)
// transformation to a java object
.to("bean-validation://param1=value1")
.to("file://sampleOut.txt");
Pre defined validation rules are e. g.:
- @NotNull
- @AssertTrue
- @Size(min = 2, max = 14)
- @Min(2)
- @Max(2)
- @Valid
- @Pattern()
- ... and many more
JSR 303 also allows you to define custom validation rules. See here for more
details:
http://docs.jboss.org/hibernate/stable/validator/reference/en/html_single/#validator-customconstraints-simple
For more information, please have a look in the reference implementation:
http://docs.jboss.org/hibernate/stable/validator/reference/en/html_single/
The Hibernate Validator (the reference implementation) is licensed under the
Apache License Version 2:
http://anonsvn.jboss.org/repos/hibernate/validator/tags/v4_0_2_GA/license.txt
What do you think? Could this be an useful component for Camel?
Regards,
Christian
--
View this message in context: http://old.nabble.com/Proposal-for-a-new-camel-bean-validation-component-based-on-jsr-303-tp27950969p27950969.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: Proposal for a new camel-bean-validation component based on jsr
303
Posted by Christian Mueller <ch...@gmail.com>.
Ok, I will do this.
I opened an issue on JIRA and will track further discussions there.
https://issues.apache.org/activemq/browse/CAMEL-2565
--
View this message in context: http://old.nabble.com/Proposal-for-a-new-camel-bean-validation-component-based-on-jsr-303-tp27950969p27976253.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: Proposal for a new camel-bean-validation component based on jsr
303
Posted by Claus Ibsen <cl...@gmail.com>.
Hi
Looks promising. Nice that JSR-303 can gather all the validations
error in one go, so you got them all.
I recon for starters you try to implement this as a new
bean-validation component.
Then later we can see how to hookup with a predicate.
On Sun, Mar 21, 2010 at 12:02 PM, Christian Mueller
<ch...@gmail.com> wrote:
>
> 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 =
> Validation.buildDefaultValidatorFactory();
> Validator validator = factory.getValidator();
>
> beanValidator.setValidator(validator);
> }
> }
>
> public class BeanValidator implements Processor {
>
> private Validator validator;
>
> public void process(Exchange exchange) throws Exception {
> Object bean = exchange.getIn().getBody();
>
> Set<ConstraintViolation> constraintViolations =
> validator.validate(bean);
> 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(bean);
>
> buffer.append(" errors: [");
> for (ConstraintViolation constraintViolation : constraintViolations)
> {
> buffer.append("property: " +
> constraintViolation.getPropertyPath() + "; value: " +
> constraintViolation.getInvalidValue() + "; constraint: " +
> constraintViolation.getMessage() + "; ");
> }
> buffer.append("]");
>
> 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
> (http://docs.jboss.org/hibernate/stable/validator/reference/en/html_single/#validator-usingvalidator-validationgroups)
> - Creating custom constraints
> (http://docs.jboss.org/hibernate/stable/validator/reference/en/html_single/#validator-customconstraints)
> - XML configuration
> (http://docs.jboss.org/hibernate/stable/validator/reference/en/html_single/#validator-xmlconfiguration)
> - Configure custom ValidationProviderResolver, MessageInterpolator,
> TraversableResolver or ConstraintValidatorFactory
> (http://docs.jboss.org/hibernate/stable/validator/reference/en/html_single/#validator-bootstrapping)
>
> 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:
> http://issues.apache.org/activemq/browse/CAMEL-1276
> http://issues.apache.org/activemq/browse/CAMEL-1537
>
> You and others prefers to use Predicates/Processors like this:
>
> from("foo")
> .filter().validate(theValidationEndpointUri)
> .to("blah")
>
> or
>
> from("foo")
> .validate(nonblocking)
> .to("bar")
>
> 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
> Predicate/Processor?
>
> Regards,
> Christian
>
> --
> View this message in context: http://old.nabble.com/Proposal-for-a-new-camel-bean-validation-component-based-on-jsr-303-tp27950969p27975303.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>
--
Claus Ibsen
Apache Camel Committer
Author of Camel in Action: http://www.manning.com/ibsen/
Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus
Re: Proposal for a new camel-bean-validation component based on jsr
303
Posted by Christian Mueller <ch...@gmail.com>.
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 =
Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
beanValidator.setValidator(validator);
}
}
public class BeanValidator implements Processor {
private Validator validator;
public void process(Exchange exchange) throws Exception {
Object bean = exchange.getIn().getBody();
Set<ConstraintViolation> constraintViolations =
validator.validate(bean);
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(bean);
buffer.append(" errors: [");
for (ConstraintViolation constraintViolation : constraintViolations)
{
buffer.append("property: " +
constraintViolation.getPropertyPath() + "; value: " +
constraintViolation.getInvalidValue() + "; constraint: " +
constraintViolation.getMessage() + "; ");
}
buffer.append("]");
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
(http://docs.jboss.org/hibernate/stable/validator/reference/en/html_single/#validator-usingvalidator-validationgroups)
- Creating custom constraints
(http://docs.jboss.org/hibernate/stable/validator/reference/en/html_single/#validator-customconstraints)
- XML configuration
(http://docs.jboss.org/hibernate/stable/validator/reference/en/html_single/#validator-xmlconfiguration)
- Configure custom ValidationProviderResolver, MessageInterpolator,
TraversableResolver or ConstraintValidatorFactory
(http://docs.jboss.org/hibernate/stable/validator/reference/en/html_single/#validator-bootstrapping)
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:
http://issues.apache.org/activemq/browse/CAMEL-1276
http://issues.apache.org/activemq/browse/CAMEL-1537
You and others prefers to use Predicates/Processors like this:
from("foo")
.filter().validate(theValidationEndpointUri)
.to("blah")
or
from("foo")
.validate(nonblocking)
.to("bar")
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
Predicate/Processor?
Regards,
Christian
--
View this message in context: http://old.nabble.com/Proposal-for-a-new-camel-bean-validation-component-based-on-jsr-303-tp27950969p27975303.html
Sent from the Camel - Users mailing list archive at Nabble.com.
Re: Proposal for a new camel-bean-validation component based on jsr
303
Posted by Claus Ibsen <cl...@gmail.com>.
What would the difference be between just invoking a JSR-303 bean using
.to("bean:foo?method=validateMyFooPlease")
And just have a void method definition with the JSR-303 @ annotations?
I assume JSR-303 will throw some exception if the validation failed?
I recon I need to see a more concrete example to "get the picture" :)
But its a cool idea to leverage JSR-303 for validation.
We have a ticket in JIRA for some Predicate based validation as well.
So maybe JSR-303 can be wrapped with a Predicate so you can do CBR
routing and have valid -> foo and otherwise -> bar (invalid ones).
On Fri, Mar 19, 2010 at 3:20 PM, Christian Mueller
<ch...@gmail.com> wrote:
>
> Currently, I'm working on a project where we have to read fixed length
> records and csv records. Because we don't trust other developers ;-), we
> want to validate the input.
> What do you think about a bean validation component which is based on jsr
> 303 (http://jcp.org/en/jsr/detail?id=303) and which could be used like the
> existing camel-validation component:
>
> from(file://sampleIn.txt)
> // transformation to a java object
> .to("bean-validation://param1=value1")
> .to("file://sampleOut.txt");
>
> Pre defined validation rules are e. g.:
> - @NotNull
> - @AssertTrue
> - @Size(min = 2, max = 14)
> - @Min(2)
> - @Max(2)
> - @Valid
> - @Pattern()
> - ... and many more
>
> JSR 303 also allows you to define custom validation rules. See here for more
> details:
> http://docs.jboss.org/hibernate/stable/validator/reference/en/html_single/#validator-customconstraints-simple
>
> For more information, please have a look in the reference implementation:
> http://docs.jboss.org/hibernate/stable/validator/reference/en/html_single/
>
> The Hibernate Validator (the reference implementation) is licensed under the
> Apache License Version 2:
> http://anonsvn.jboss.org/repos/hibernate/validator/tags/v4_0_2_GA/license.txt
>
> What do you think? Could this be an useful component for Camel?
>
> Regards,
> Christian
> --
> View this message in context: http://old.nabble.com/Proposal-for-a-new-camel-bean-validation-component-based-on-jsr-303-tp27950969p27950969.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>
>
--
Claus Ibsen
Apache Camel Committer
Author of Camel in Action: http://www.manning.com/ibsen/
Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus