camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cmuel...@apache.org
Subject [1/2] git commit: CAMEL-6317: Camel-validator not able to resolve schema when using useSharedSchema=false Thanks David J. M. Karlsen for reporting it
Date Thu, 20 Jun 2013 20:15:56 GMT
Updated Branches:
  refs/heads/camel-2.11.x a1c4835ea -> 270962a54


CAMEL-6317: Camel-validator not able to resolve schema when using useSharedSchema=false
Thanks David J. M. Karlsen for reporting it


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

Branch: refs/heads/camel-2.11.x
Commit: 612f95bba302228a0a900f7334d8a571c7a0a74e
Parents: a1c4835
Author: cmueller <cmueller@apache.org>
Authored: Thu Jun 20 22:07:31 2013 +0200
Committer: cmueller <cmueller@apache.org>
Committed: Thu Jun 20 22:15:15 2013 +0200

----------------------------------------------------------------------
 .../component/validator/ValidatorComponent.java | 19 +++++++++++--------
 .../validation/ValidatingProcessor.java         | 20 +++++++++++++++++++-
 .../component/validator/ValidatorRouteTest.java | 15 ++++++++++++---
 3 files changed, 42 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/612f95bb/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 58de3e7..f69a5b9 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
@@ -19,11 +19,8 @@ package org.apache.camel.component.validator;
 import java.io.InputStream;
 import java.util.Map;
 
-import javax.xml.transform.stream.StreamSource;
-
-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;
@@ -31,6 +28,7 @@ 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;
 
 /**
  * The <a href="http://camel.apache.org/validation.html">Validator Component</a>
@@ -43,18 +41,23 @@ public class ValidatorComponent extends DefaultComponent {
     protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object>
parameters) throws Exception {
         final String resourceUri = remaining;
         InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext().getClassResolver(),
resourceUri);
-        StreamSource source = new StreamSource(is);
+        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.setSchemaSource(source);
+        validator.setSchemaAsByteArray(bytes);
+        //validator.setSchemaSource(source);
         LOG.debug("{} using schema resource: {}", this, resourceUri);
         configureValidator(validator, uri, remaining, parameters);
 
         // force loading of schema at create time otherwise concurrent
         // processing could cause thread safe issues for the javax.xml.validation.SchemaFactory
         validator.loadSchema();
-        // and make sure to close the input stream after the schema has been loaded
-        IOHelper.close(is);
 
         return new ProcessorEndpoint(uri, this, validator);
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/612f95bb/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 40995f1..6c325d9 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
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.processor.validation;
 
+import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
@@ -65,6 +66,7 @@ public class ValidatingProcessor implements Processor {
     private SchemaFactory schemaFactory;
     private URL schemaUrl;
     private File schemaFile;
+    private byte[] schemaAsByteArray;
     private ValidatorErrorHandler errorHandler = new DefaultValidationErrorHandler();
     private boolean useDom;
     private boolean useSharedSchema = true;
@@ -218,6 +220,14 @@ public class ValidatingProcessor implements Processor {
         this.schemaFile = schemaFile;
     }
 
+    public byte[] getSchemaAsByteArray() {
+        return schemaAsByteArray;
+    }
+
+    public void setSchemaAsByteArray(byte[] schemaAsByteArray) {
+        this.schemaAsByteArray = schemaAsByteArray;
+    }
+
     public SchemaFactory getSchemaFactory() {
         if (schemaFactory == null) {
             schemaFactory = createSchemaFactory();
@@ -314,11 +324,19 @@ public class ValidatingProcessor implements Processor {
         if (url != null) {
             return factory.newSchema(url);
         }
+
         File file = getSchemaFile();
         if (file != null) {
             return factory.newSchema(file);
         }
-        return factory.newSchema(getSchemaSource());
+
+        byte[] bytes = getSchemaAsByteArray();
+        if (bytes != null) {
+            return factory.newSchema(new StreamSource(new ByteArrayInputStream(schemaAsByteArray)));
+        }
+
+        Source source = getSchemaSource();
+        return factory.newSchema(source);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/camel/blob/612f95bb/camel-core/src/test/java/org/apache/camel/component/validator/ValidatorRouteTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/validator/ValidatorRouteTest.java
b/camel-core/src/test/java/org/apache/camel/component/validator/ValidatorRouteTest.java
index c3d1880..036704f 100644
--- a/camel-core/src/test/java/org/apache/camel/component/validator/ValidatorRouteTest.java
+++ b/camel-core/src/test/java/org/apache/camel/component/validator/ValidatorRouteTest.java
@@ -24,9 +24,6 @@ import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.processor.validation.NoXmlHeaderValidationException;
 
-/**
- *
- */
 public class ValidatorRouteTest extends ContextTestSupport {
 
     protected MockEndpoint validEndpoint;
@@ -125,6 +122,14 @@ public class ValidatorRouteTest extends ContextTestSupport {
         MockEndpoint.assertIsSatisfied(validEndpoint, invalidEndpoint, finallyEndpoint);
     }
 
+    public void testUseNotASharedSchema() throws Exception {
+        validEndpoint.expectedMessageCount(1);
+
+        template.sendBody("direct:useNotASharedSchema", "<mail xmlns='http://foo.com/bar'><subject>Hey</subject><body>Hello
world!</body></mail>");
+
+        MockEndpoint.assertIsSatisfied(validEndpoint, invalidEndpoint, finallyEndpoint);
+    }
+
     @Override
     protected void setUp() throws Exception {
         super.setUp();
@@ -166,6 +171,10 @@ public class ValidatorRouteTest extends ContextTestSupport {
                 from("direct:startNullHeaderNoFail")
                         .to("validator:org/apache/camel/component/validator/schema.xsd?headerName=headerToValidate&failOnNullHeader=false")
                         .to("mock:valid");
+
+                from("direct:useNotASharedSchema")
+                    .to("validator:org/apache/camel/component/validator/schema.xsd?useSharedSchema=false")
+                    .to("mock:valid");
             }
         };
     }


Mime
View raw message