You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2014/11/11 10:56:22 UTC

[3/6] camel git commit: CAMEL-8028: Validator component - Make it like the others

CAMEL-8028: Validator component - Make it like the others


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/9df72cc6
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/9df72cc6
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/9df72cc6

Branch: refs/heads/master
Commit: 9df72cc682f9df46d0fd6709a16efd8525af5d8d
Parents: d1c06a4
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Nov 11 07:58:54 2014 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Nov 11 10:55:36 2014 +0100

----------------------------------------------------------------------
 .../camel/component/ResourceEndpoint.java       |   2 +-
 .../component/validator/ValidatorComponent.java |  55 ++---
 .../component/validator/ValidatorEndpoint.java  | 203 +++++++++++++++++++
 .../component/validator/ValidatorProducer.java  |  57 ++++++
 .../validation/ValidatingProcessor.java         |   3 +-
 .../component/validator/msv/MsvComponent.java   |  35 ++--
 .../component/validator/msv/MsvEndpoint.java    |  52 +++++
 7 files changed, 343 insertions(+), 64 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/9df72cc6/camel-core/src/main/java/org/apache/camel/component/ResourceEndpoint.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/ResourceEndpoint.java b/camel-core/src/main/java/org/apache/camel/component/ResourceEndpoint.java
index 2dea113..103ee4b 100644
--- a/camel-core/src/main/java/org/apache/camel/component/ResourceEndpoint.java
+++ b/camel-core/src/main/java/org/apache/camel/component/ResourceEndpoint.java
@@ -43,7 +43,7 @@ public abstract class ResourceEndpoint extends ProcessorEndpoint implements Mana
     private volatile byte[] buffer;
     @UriParam
     private String resourceUri;
-    @UriParam
+    @UriParam(defaultValue = "false")
     private boolean contentCache;
 
     public ResourceEndpoint() {

http://git-wip-us.apache.org/repos/asf/camel/blob/9df72cc6/camel-core/src/main/java/org/apache/camel/component/validator/ValidatorComponent.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/validator/ValidatorComponent.java b/camel-core/src/main/java/org/apache/camel/component/validator/ValidatorComponent.java
index 272c1f5..acde232 100644
--- a/camel-core/src/main/java/org/apache/camel/component/validator/ValidatorComponent.java
+++ b/camel-core/src/main/java/org/apache/camel/component/validator/ValidatorComponent.java
@@ -16,60 +16,29 @@
  */
 package org.apache.camel.component.validator;
 
-import java.io.InputStream;
 import java.util.Map;
 
-import org.w3c.dom.ls.LSResourceResolver;
-
 import org.apache.camel.Endpoint;
-import org.apache.camel.converter.IOConverter;
-import org.apache.camel.impl.DefaultComponent;
-import org.apache.camel.impl.ProcessorEndpoint;
-import org.apache.camel.processor.validation.ValidatingProcessor;
-import org.apache.camel.util.IOHelper;
-import org.apache.camel.util.ResourceHelper;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.apache.camel.impl.UriEndpointComponent;
 
 /**
  * The <a href="http://camel.apache.org/validation.html">Validator Component</a>
  * for validating XML against some schema
  */
-public class ValidatorComponent extends DefaultComponent {
-
-    private static final Logger LOG = LoggerFactory.getLogger(ValidatorComponent.class);
-
-    protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
-        final String resourceUri = remaining;
-        InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext().getClassResolver(), resourceUri);
-        byte[] bytes = null;
-        try {
-            bytes = IOConverter.toBytes(is);
-        } finally {
-            // and make sure to close the input stream after the schema has been loaded
-            IOHelper.close(is);
-        }
-
-        ValidatingProcessor validator = new ValidatingProcessor();
-        validator.setSchemaAsByteArray(bytes);
-        LOG.debug("{} using schema resource: {}", this, resourceUri);
-        configureValidator(validator, uri, remaining, parameters);
+public class ValidatorComponent extends UriEndpointComponent {
 
-        // force loading of schema at create time otherwise concurrent
-        // processing could cause thread safe issues for the javax.xml.validation.SchemaFactory
-        validator.loadSchema();
-
-        return new ProcessorEndpoint(uri, this, validator);
+    public ValidatorComponent() {
+        this(ValidatorEndpoint.class);
     }
 
-    protected void configureValidator(ValidatingProcessor validator, String uri, String remaining, Map<String, Object> parameters) throws Exception {
-        LSResourceResolver resourceResolver = resolveAndRemoveReferenceParameter(parameters, "resourceResolver", LSResourceResolver.class);
-        if (resourceResolver != null) {
-            validator.setResourceResolver(resourceResolver);
-        } else {
-            validator.setResourceResolver(new DefaultLSResourceResolver(getCamelContext(), remaining));
-        }
+    public ValidatorComponent(Class<? extends Endpoint> endpointClass) {
+        super(endpointClass);
+    }
 
-        setProperties(validator, parameters);
+    protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
+        ValidatorEndpoint endpoint = new ValidatorEndpoint(uri, this, remaining);
+        setProperties(endpoint, parameters);
+        return endpoint;
     }
+
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/9df72cc6/camel-core/src/main/java/org/apache/camel/component/validator/ValidatorEndpoint.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/validator/ValidatorEndpoint.java b/camel-core/src/main/java/org/apache/camel/component/validator/ValidatorEndpoint.java
new file mode 100644
index 0000000..1afb0e3
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/component/validator/ValidatorEndpoint.java
@@ -0,0 +1,203 @@
+/**
+ * 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.validator;
+
+import java.io.InputStream;
+import javax.xml.XMLConstants;
+import javax.xml.validation.SchemaFactory;
+
+import org.apache.camel.Component;
+import org.apache.camel.Consumer;
+import org.apache.camel.Processor;
+import org.apache.camel.Producer;
+import org.apache.camel.converter.IOConverter;
+import org.apache.camel.impl.DefaultEndpoint;
+import org.apache.camel.processor.validation.DefaultValidationErrorHandler;
+import org.apache.camel.processor.validation.ValidatingProcessor;
+import org.apache.camel.processor.validation.ValidatorErrorHandler;
+import org.apache.camel.spi.UriEndpoint;
+import org.apache.camel.spi.UriParam;
+import org.apache.camel.util.IOHelper;
+import org.apache.camel.util.ResourceHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.w3c.dom.ls.LSResourceResolver;
+
+@UriEndpoint(scheme = "validator")
+public class ValidatorEndpoint extends DefaultEndpoint {
+
+    private static final Logger LOG = LoggerFactory.getLogger(ValidatorEndpoint.class);
+
+    @UriParam
+    private String resourceUri;
+    @UriParam
+    private String schemaLanguage = XMLConstants.W3C_XML_SCHEMA_NS_URI;
+    @UriParam
+    private SchemaFactory schemaFactory;
+    @UriParam
+    private ValidatorErrorHandler errorHandler = new DefaultValidationErrorHandler();
+    @UriParam(defaultValue = "false")
+    private boolean useDom;
+    @UriParam(defaultValue = "true")
+    private boolean useSharedSchema = true;
+    @UriParam
+    private LSResourceResolver resourceResolver;
+    @UriParam(defaultValue = "true")
+    private boolean failOnNullBody = true;
+    @UriParam(defaultValue = "true")
+    private boolean failOnNullHeader = true;
+    @UriParam
+    private String headerName;
+
+    public ValidatorEndpoint() {
+    }
+
+    public ValidatorEndpoint(String endpointUri, Component component, String resourceUri) {
+        super(endpointUri, component);
+        this.resourceUri = resourceUri;
+    }
+
+    @Override
+    public Producer createProducer() throws Exception {
+        ValidatingProcessor validator = new ValidatingProcessor();
+
+        InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext().getClassResolver(), resourceUri);
+        byte[] bytes = null;
+        try {
+            bytes = IOConverter.toBytes(is);
+        } finally {
+            // and make sure to close the input stream after the schema has been loaded
+            IOHelper.close(is);
+        }
+
+        validator.setSchemaAsByteArray(bytes);
+        LOG.debug("{} using schema resource: {}", this, resourceUri);
+        configureValidator(validator);
+
+        // force loading of schema at create time otherwise concurrent
+        // processing could cause thread safe issues for the javax.xml.validation.SchemaFactory
+        validator.loadSchema();
+
+        return new ValidatorProducer(this, validator);
+    }
+
+    @Override
+    public Consumer createConsumer(Processor processor) throws Exception {
+        throw new UnsupportedOperationException("Cannot consume from validator");
+    }
+
+    @Override
+    public boolean isSingleton() {
+        return true;
+    }
+
+    protected void configureValidator(ValidatingProcessor validator) throws Exception {
+        if (resourceResolver != null) {
+            validator.setResourceResolver(resourceResolver);
+        } else {
+            validator.setResourceResolver(new DefaultLSResourceResolver(getCamelContext(), resourceUri));
+        }
+        validator.setSchemaLanguage(getSchemaLanguage());
+        validator.setSchemaFactory(getSchemaFactory());
+        validator.setErrorHandler(getErrorHandler());
+        validator.setUseDom(isUseDom());
+        validator.setUseSharedSchema(isUseSharedSchema());
+        validator.setFailOnNullBody(isFailOnNullBody());
+        validator.setFailOnNullHeader(isFailOnNullHeader());
+        validator.setHeaderName(getHeaderName());
+    }
+
+    public String getResourceUri() {
+        return resourceUri;
+    }
+
+    public void setResourceUri(String resourceUri) {
+        this.resourceUri = resourceUri;
+    }
+
+    public String getSchemaLanguage() {
+        return schemaLanguage;
+    }
+
+    public void setSchemaLanguage(String schemaLanguage) {
+        this.schemaLanguage = schemaLanguage;
+    }
+
+    public SchemaFactory getSchemaFactory() {
+        return schemaFactory;
+    }
+
+    public void setSchemaFactory(SchemaFactory schemaFactory) {
+        this.schemaFactory = schemaFactory;
+    }
+
+    public ValidatorErrorHandler getErrorHandler() {
+        return errorHandler;
+    }
+
+    public void setErrorHandler(ValidatorErrorHandler errorHandler) {
+        this.errorHandler = errorHandler;
+    }
+
+    public boolean isUseDom() {
+        return useDom;
+    }
+
+    public void setUseDom(boolean useDom) {
+        this.useDom = useDom;
+    }
+
+    public boolean isUseSharedSchema() {
+        return useSharedSchema;
+    }
+
+    public void setUseSharedSchema(boolean useSharedSchema) {
+        this.useSharedSchema = useSharedSchema;
+    }
+
+    public LSResourceResolver getResourceResolver() {
+        return resourceResolver;
+    }
+
+    public void setResourceResolver(LSResourceResolver resourceResolver) {
+        this.resourceResolver = resourceResolver;
+    }
+
+    public boolean isFailOnNullBody() {
+        return failOnNullBody;
+    }
+
+    public void setFailOnNullBody(boolean failOnNullBody) {
+        this.failOnNullBody = failOnNullBody;
+    }
+
+    public boolean isFailOnNullHeader() {
+        return failOnNullHeader;
+    }
+
+    public void setFailOnNullHeader(boolean failOnNullHeader) {
+        this.failOnNullHeader = failOnNullHeader;
+    }
+
+    public String getHeaderName() {
+        return headerName;
+    }
+
+    public void setHeaderName(String headerName) {
+        this.headerName = headerName;
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/9df72cc6/camel-core/src/main/java/org/apache/camel/component/validator/ValidatorProducer.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/validator/ValidatorProducer.java b/camel-core/src/main/java/org/apache/camel/component/validator/ValidatorProducer.java
new file mode 100644
index 0000000..41bc06c
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/component/validator/ValidatorProducer.java
@@ -0,0 +1,57 @@
+/**
+ * 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.validator;
+
+import org.apache.camel.AsyncCallback;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.impl.DefaultAsyncProducer;
+import org.apache.camel.processor.validation.ValidatingProcessor;
+import org.apache.camel.util.ServiceHelper;
+
+public class ValidatorProducer extends DefaultAsyncProducer {
+
+    private final ValidatingProcessor validatingProcessor;
+
+    public ValidatorProducer(Endpoint endpoint, ValidatingProcessor validatingProcessor) {
+        super(endpoint);
+        this.validatingProcessor = validatingProcessor;
+    }
+
+    @Override
+    public boolean process(Exchange exchange, AsyncCallback callback) {
+        return validatingProcessor.process(exchange, callback);
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+        ServiceHelper.startService(validatingProcessor);
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        super.doStop();
+        ServiceHelper.stopService(validatingProcessor);
+    }
+
+    @Override
+    protected void doShutdown() throws Exception {
+        super.doStop();
+        ServiceHelper.stopAndShutdownService(validatingProcessor);
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/9df72cc6/camel-core/src/main/java/org/apache/camel/processor/validation/ValidatingProcessor.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/processor/validation/ValidatingProcessor.java b/camel-core/src/main/java/org/apache/camel/processor/validation/ValidatingProcessor.java
index 2d9fa8e..359ca0d 100644
--- a/camel-core/src/main/java/org/apache/camel/processor/validation/ValidatingProcessor.java
+++ b/camel-core/src/main/java/org/apache/camel/processor/validation/ValidatingProcessor.java
@@ -55,14 +55,13 @@ import org.apache.camel.util.IOHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-
 /**
  * A processor which validates the XML version of the inbound message body
  * against some schema either in XSD or RelaxNG
  */
 public class ValidatingProcessor implements AsyncProcessor {
     private static final Logger LOG = LoggerFactory.getLogger(ValidatingProcessor.class);
-    private XmlConverter converter = new XmlConverter();
+    private final XmlConverter converter = new XmlConverter();
     private String schemaLanguage = XMLConstants.W3C_XML_SCHEMA_NS_URI;
     private volatile Schema schema;
     private Source schemaSource;

http://git-wip-us.apache.org/repos/asf/camel/blob/9df72cc6/components/camel-msv/src/main/java/org/apache/camel/component/validator/msv/MsvComponent.java
----------------------------------------------------------------------
diff --git a/components/camel-msv/src/main/java/org/apache/camel/component/validator/msv/MsvComponent.java b/components/camel-msv/src/main/java/org/apache/camel/component/validator/msv/MsvComponent.java
index 6412854..03b3c00 100644
--- a/components/camel-msv/src/main/java/org/apache/camel/component/validator/msv/MsvComponent.java
+++ b/components/camel-msv/src/main/java/org/apache/camel/component/validator/msv/MsvComponent.java
@@ -17,12 +17,10 @@
 package org.apache.camel.component.validator.msv;
 
 import java.util.Map;
-import javax.xml.XMLConstants;
+import javax.xml.validation.SchemaFactory;
 
+import org.apache.camel.Endpoint;
 import org.apache.camel.component.validator.ValidatorComponent;
-import org.apache.camel.processor.validation.ValidatingProcessor;
-import org.iso_relax.verifier.VerifierConfigurationException;
-import org.iso_relax.verifier.jaxp.validation.RELAXNGSchemaFactoryImpl;
 
 /**
  * The <a href="http://camel.apache.org/msv.html">MSV Component</a> uses the
@@ -31,25 +29,26 @@ import org.iso_relax.verifier.jaxp.validation.RELAXNGSchemaFactoryImpl;
  * @version 
  */
 public class MsvComponent extends ValidatorComponent {
-    private RELAXNGSchemaFactoryImpl schemaFactory;
+    private SchemaFactory schemaFactory;
 
-    public RELAXNGSchemaFactoryImpl getSchemaFactory() throws VerifierConfigurationException {
-        if (schemaFactory == null) {
-            schemaFactory = new RELAXNGSchemaFactoryImpl();
-        }
+    public MsvComponent() {
+        super(MsvEndpoint.class);
+    }
+
+    @Override
+    protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
+        MsvEndpoint endpoint = new MsvEndpoint(uri, this, remaining);
+        endpoint.setSchemaFactory(getSchemaFactory());
+        setProperties(endpoint, parameters);
+        return endpoint;
+    }
+
+    public SchemaFactory getSchemaFactory() {
         return schemaFactory;
     }
 
-    public void setSchemaFactory(RELAXNGSchemaFactoryImpl schemaFactory) {
+    public void setSchemaFactory(SchemaFactory schemaFactory) {
         this.schemaFactory = schemaFactory;
     }
 
-    @Override
-    protected void configureValidator(ValidatingProcessor validator, String uri, String remaining, Map<String, Object> parameters) throws Exception {
-        validator.setSchemaLanguage(XMLConstants.RELAXNG_NS_URI);
-        validator.setSchemaFactory(getSchemaFactory());
-        // must use Dom for Msv to work
-        validator.setUseDom(true);
-        super.configureValidator(validator, uri, remaining, parameters);
-    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/9df72cc6/components/camel-msv/src/main/java/org/apache/camel/component/validator/msv/MsvEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-msv/src/main/java/org/apache/camel/component/validator/msv/MsvEndpoint.java b/components/camel-msv/src/main/java/org/apache/camel/component/validator/msv/MsvEndpoint.java
new file mode 100644
index 0000000..dbc3c16
--- /dev/null
+++ b/components/camel-msv/src/main/java/org/apache/camel/component/validator/msv/MsvEndpoint.java
@@ -0,0 +1,52 @@
+/**
+ * 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.validator.msv;
+
+import javax.xml.XMLConstants;
+
+import org.apache.camel.Component;
+import org.apache.camel.component.validator.ValidatorEndpoint;
+import org.apache.camel.processor.validation.ValidatingProcessor;
+import org.apache.camel.spi.UriEndpoint;
+import org.iso_relax.verifier.jaxp.validation.RELAXNGSchemaFactoryImpl;
+
+@UriEndpoint(scheme = "msv")
+public class MsvEndpoint extends ValidatorEndpoint {
+
+    public MsvEndpoint(String endpointUri, Component component, String resourceUri) {
+        super(endpointUri, component, resourceUri);
+    }
+
+    @Override
+    protected void configureValidator(ValidatingProcessor validator) throws Exception {
+        super.configureValidator(validator);
+        validator.setSchemaLanguage(XMLConstants.RELAXNG_NS_URI);
+        // must use Dom for Msv to work
+        validator.setUseDom(true);
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+
+        // use relax schema factory by default
+        if (getSchemaFactory() == null) {
+            setSchemaFactory(new RELAXNGSchemaFactoryImpl());
+        }
+    }
+
+}