CAMEL-7999: More components include documentation
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/c4dc594a Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/c4dc594a Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/c4dc594a Branch: refs/heads/master Commit: c4dc594a591224a49b6bb2e5be4e0d84cb47e3e6 Parents: 5cb374b Author: Claus Ibsen <davscl...@apache.org> Authored: Tue Dec 2 09:29:24 2014 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Tue Dec 2 09:29:24 2014 +0100 ---------------------------------------------------------------------- .../component/bean/validator/BeanValidator.java | 88 ------------ .../bean/validator/BeanValidatorComponent.java | 47 ++----- .../bean/validator/BeanValidatorEndpoint.java | 135 +++++++++++++++++++ .../bean/validator/BeanValidatorProducer.java | 70 ++++++++++ .../BeanValidatorConfigurationTest.java | 30 ++--- 5 files changed, 223 insertions(+), 147 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/c4dc594a/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidator.java ---------------------------------------------------------------------- diff --git a/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidator.java b/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidator.java deleted file mode 100644 index 1deb976..0000000 --- a/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidator.java +++ /dev/null @@ -1,88 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.camel.component.bean.validator; - -import java.util.Set; - -import javax.validation.ConstraintValidatorFactory; -import javax.validation.ConstraintViolation; -import javax.validation.MessageInterpolator; -import javax.validation.TraversableResolver; -import javax.validation.Validator; -import javax.validation.ValidatorFactory; - -import org.apache.camel.Exchange; -import org.apache.camel.Processor; - -/** - * Bean validator that uses the JSR 303 reference implementation (Hibernate Validator). - * Throws {@link BeanValidationException} if constrain violations are detected. - */ -public class BeanValidator implements Processor { - - private ValidatorFactory validatorFactory; - private Validator validator; - private Class<?> group; - - public void process(Exchange exchange) throws Exception { - Object bean = exchange.getIn().getBody(); - Set<ConstraintViolation<Object>> constraintViolations = null; - - if (this.group != null) { - constraintViolations = validator.validate(bean, group); - } else { - constraintViolations = validator.validate(bean); - } - - if (!constraintViolations.isEmpty()) { - throw new BeanValidationException(exchange, constraintViolations, exchange.getIn().getBody()); - } - } - - public ValidatorFactory getValidatorFactory() { - return validatorFactory; - } - - public void setValidatorFactory(ValidatorFactory validatorFactory) { - this.validatorFactory = validatorFactory; - this.validator = this.validatorFactory.getValidator(); - } - - public Validator getValidator() { - return validator; - } - - public Class<?> getGroup() { - return group; - } - - public void setGroup(Class<?> group) { - this.group = group; - } - - public MessageInterpolator getMessageInterpolator() { - return this.validatorFactory.getMessageInterpolator(); - } - - public TraversableResolver getTraversableResolver() { - return this.validatorFactory.getTraversableResolver(); - } - - public ConstraintValidatorFactory getConstraintValidatorFactory() { - return this.validatorFactory.getConstraintValidatorFactory(); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/c4dc594a/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidatorComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidatorComponent.java b/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidatorComponent.java index a70ab2e..f95f026 100644 --- a/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidatorComponent.java +++ b/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidatorComponent.java @@ -18,52 +18,25 @@ package org.apache.camel.component.bean.validator; import java.util.Map; -import javax.validation.ConstraintValidatorFactory; -import javax.validation.MessageInterpolator; -import javax.validation.TraversableResolver; -import javax.validation.ValidationProviderResolver; -import javax.validation.ValidatorFactory; - import org.apache.camel.Endpoint; -import org.apache.camel.impl.DefaultComponent; -import org.apache.camel.impl.ProcessorEndpoint; -import org.apache.camel.util.PlatformHelper; - -import static org.apache.camel.component.bean.validator.ValidatorFactories.buildValidatorFactory; +import org.apache.camel.impl.UriEndpointComponent; /** * Bean Validator Component for validating Java beans against reference implementation of JSR 303 Validator (Hibernate * Validator). */ -public class BeanValidatorComponent extends DefaultComponent { - - @Override - protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { - BeanValidator beanValidator = new BeanValidator(); +public class BeanValidatorComponent extends UriEndpointComponent { - ValidatorFactory validatorFactory = buildValidatorFactory( - isOsgiContext(), - resolveAndRemoveReferenceParameter(parameters, "validationProviderResolver", ValidationProviderResolver.class), - resolveAndRemoveReferenceParameter(parameters, "messageInterpolator", MessageInterpolator.class), - resolveAndRemoveReferenceParameter(parameters, "traversableResolver", TraversableResolver.class), - resolveAndRemoveReferenceParameter(parameters, "constraintValidatorFactory", ConstraintValidatorFactory.class)); - beanValidator.setValidatorFactory(validatorFactory); - - String group = getAndRemoveParameter(parameters, "group", String.class); - if (group != null) { - beanValidator.setGroup(getCamelContext().getClassResolver().resolveMandatoryClass(group)); - } - - return new ProcessorEndpoint(uri, this, beanValidator); + public BeanValidatorComponent() { + super(BeanValidatorEndpoint.class); } - /** - * Recognizes if component is executed in the OSGi environment. - * - * @return true if component is executed in the OSGi environment. False otherwise. - */ - protected boolean isOsgiContext() { - return PlatformHelper.isOsgiContext(getCamelContext()); + @Override + protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { + BeanValidatorEndpoint endpoint = new BeanValidatorEndpoint(uri, this); + endpoint.setLabel(remaining); + setProperties(endpoint, parameters); + return endpoint; } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/c4dc594a/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidatorEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidatorEndpoint.java b/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidatorEndpoint.java new file mode 100644 index 0000000..6583638 --- /dev/null +++ b/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidatorEndpoint.java @@ -0,0 +1,135 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.camel.component.bean.validator; + +import javax.validation.ConstraintValidatorFactory; +import javax.validation.MessageInterpolator; +import javax.validation.TraversableResolver; +import javax.validation.ValidationProviderResolver; +import javax.validation.ValidatorFactory; + +import org.apache.camel.Component; +import org.apache.camel.Consumer; +import org.apache.camel.Processor; +import org.apache.camel.Producer; +import org.apache.camel.impl.DefaultEndpoint; +import org.apache.camel.spi.UriEndpoint; +import org.apache.camel.spi.UriParam; +import org.apache.camel.spi.UriPath; +import org.apache.camel.util.PlatformHelper; + +import static org.apache.camel.component.bean.validator.ValidatorFactories.buildValidatorFactory; + +@UriEndpoint(scheme = "bean-validator", label = "validation") +public class BeanValidatorEndpoint extends DefaultEndpoint { + + @UriPath(description = "Where label is an arbitrary text value describing the endpoint") + private String label; + @UriParam + private String group; + @UriParam + private ValidationProviderResolver validationProviderResolver; + @UriParam + private MessageInterpolator messageInterpolator; + @UriParam + private TraversableResolver traversableResolver; + @UriParam + private ConstraintValidatorFactory constraintValidatorFactory; + + public BeanValidatorEndpoint(String endpointUri, Component component) { + super(endpointUri, component); + } + + @Override + public Producer createProducer() throws Exception { + BeanValidatorProducer producer = new BeanValidatorProducer(this); + if (group != null) { + producer.setGroup(getCamelContext().getClassResolver().resolveMandatoryClass(group)); + } + ValidatorFactory validatorFactory = buildValidatorFactory(isOsgiContext(), + validationProviderResolver, messageInterpolator, traversableResolver, constraintValidatorFactory); + producer.setValidatorFactory(validatorFactory); + return producer; + } + + @Override + public Consumer createConsumer(Processor processor) throws Exception { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isSingleton() { + return true; + } + + /** + * Recognizes if component is executed in the OSGi environment. + * + * @return true if component is executed in the OSGi environment. False otherwise. + */ + protected boolean isOsgiContext() { + return PlatformHelper.isOsgiContext(getCamelContext()); + } + + public String getLabel() { + return label; + } + + public void setLabel(String label) { + this.label = label; + } + + public String getGroup() { + return group; + } + + public void setGroup(String group) { + this.group = group; + } + + public ValidationProviderResolver getValidationProviderResolver() { + return validationProviderResolver; + } + + public void setValidationProviderResolver(ValidationProviderResolver validationProviderResolver) { + this.validationProviderResolver = validationProviderResolver; + } + + public MessageInterpolator getMessageInterpolator() { + return messageInterpolator; + } + + public void setMessageInterpolator(MessageInterpolator messageInterpolator) { + this.messageInterpolator = messageInterpolator; + } + + public TraversableResolver getTraversableResolver() { + return traversableResolver; + } + + public void setTraversableResolver(TraversableResolver traversableResolver) { + this.traversableResolver = traversableResolver; + } + + public ConstraintValidatorFactory getConstraintValidatorFactory() { + return constraintValidatorFactory; + } + + public void setConstraintValidatorFactory(ConstraintValidatorFactory constraintValidatorFactory) { + this.constraintValidatorFactory = constraintValidatorFactory; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/c4dc594a/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidatorProducer.java ---------------------------------------------------------------------- diff --git a/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidatorProducer.java b/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidatorProducer.java new file mode 100644 index 0000000..7fbc29d --- /dev/null +++ b/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidatorProducer.java @@ -0,0 +1,70 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.camel.component.bean.validator; + +import java.util.Set; +import javax.validation.ConstraintViolation; +import javax.validation.ValidatorFactory; + +import org.apache.camel.Exchange; +import org.apache.camel.impl.DefaultProducer; + +/** + * Bean validator that uses the JSR 303 reference implementation (Hibernate Validator). + * Throws {@link BeanValidationException} if constrain violations are detected. + */ +public class BeanValidatorProducer extends DefaultProducer { + + private ValidatorFactory validatorFactory; + private Class<?> group; + + public BeanValidatorProducer(BeanValidatorEndpoint endpoint) { + super(endpoint); + } + + public void process(Exchange exchange) throws Exception { + Object bean = exchange.getIn().getBody(); + Set<ConstraintViolation<Object>> constraintViolations; + + if (this.group != null) { + constraintViolations = validatorFactory.getValidator().validate(bean, group); + } else { + constraintViolations = validatorFactory.getValidator().validate(bean); + } + + if (!constraintViolations.isEmpty()) { + throw new BeanValidationException(exchange, constraintViolations, exchange.getIn().getBody()); + } + } + + public ValidatorFactory getValidatorFactory() { + return validatorFactory; + } + + public void setValidatorFactory(ValidatorFactory validatorFactory) { + this.validatorFactory = validatorFactory; + } + + public Class<?> getGroup() { + return group; + } + + public void setGroup(Class<?> group) { + this.group = group; + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/c4dc594a/components/camel-bean-validator/src/test/java/org/apache/camel/component/bean/validator/BeanValidatorConfigurationTest.java ---------------------------------------------------------------------- diff --git a/components/camel-bean-validator/src/test/java/org/apache/camel/component/bean/validator/BeanValidatorConfigurationTest.java b/components/camel-bean-validator/src/test/java/org/apache/camel/component/bean/validator/BeanValidatorConfigurationTest.java index b24a417..4242d39 100644 --- a/components/camel-bean-validator/src/test/java/org/apache/camel/component/bean/validator/BeanValidatorConfigurationTest.java +++ b/components/camel-bean-validator/src/test/java/org/apache/camel/component/bean/validator/BeanValidatorConfigurationTest.java @@ -18,7 +18,6 @@ package org.apache.camel.component.bean.validator; import java.lang.annotation.ElementType; import java.util.Locale; - import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorFactory; import javax.validation.MessageInterpolator; @@ -27,12 +26,7 @@ import javax.validation.Path.Node; import javax.validation.TraversableResolver; import org.apache.camel.impl.JndiRegistry; -import org.apache.camel.impl.ProcessorEndpoint; import org.apache.camel.test.junit4.CamelTestSupport; -import org.hibernate.validator.internal.engine.ValidatorImpl; -import org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorFactoryImpl; -import org.hibernate.validator.internal.engine.resolver.DefaultTraversableResolver; -import org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator; import org.junit.Test; /** @@ -70,14 +64,8 @@ public class BeanValidatorConfigurationTest extends CamelTestSupport { return; } - ProcessorEndpoint endpoint = context.getEndpoint("bean-validator://x", ProcessorEndpoint.class); - BeanValidator processor = (BeanValidator) endpoint.getProcessor(); - - assertNull(processor.getGroup()); - assertTrue(processor.getValidator() instanceof ValidatorImpl); - assertTrue(processor.getMessageInterpolator() instanceof ResourceBundleMessageInterpolator); - assertTrue(processor.getTraversableResolver() instanceof DefaultTraversableResolver); - assertTrue(processor.getConstraintValidatorFactory() instanceof ConstraintValidatorFactoryImpl); + BeanValidatorEndpoint endpoint = context.getEndpoint("bean-validator://x", BeanValidatorEndpoint.class); + assertNull(endpoint.getGroup()); } @Test @@ -87,18 +75,16 @@ public class BeanValidatorConfigurationTest extends CamelTestSupport { return; } - ProcessorEndpoint endpoint = context.getEndpoint("bean-validator://x" + BeanValidatorEndpoint endpoint = context.getEndpoint("bean-validator://x" + "?group=org.apache.camel.component.bean.validator.OptionalChecks" + "&messageInterpolator=#myMessageInterpolator" + "&traversableResolver=#myTraversableResolver" - + "&constraintValidatorFactory=myConstraintValidatorFactory", ProcessorEndpoint.class); - BeanValidator processor = (BeanValidator) endpoint.getProcessor(); + + "&constraintValidatorFactory=#myConstraintValidatorFactory", BeanValidatorEndpoint.class); - assertEquals("org.apache.camel.component.bean.validator.OptionalChecks", processor.getGroup().getName()); - assertTrue(processor.getValidator() instanceof ValidatorImpl); - assertSame(processor.getMessageInterpolator(), this.messageInterpolator); - assertSame(processor.getTraversableResolver(), this.traversableResolver); - assertSame(processor.getConstraintValidatorFactory(), this.constraintValidatorFactory); + assertEquals("org.apache.camel.component.bean.validator.OptionalChecks", endpoint.getGroup()); + assertSame(endpoint.getMessageInterpolator(), this.messageInterpolator); + assertSame(endpoint.getTraversableResolver(), this.traversableResolver); + assertSame(endpoint.getConstraintValidatorFactory(), this.constraintValidatorFactory); } class MyMessageInterpolator implements MessageInterpolator {