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 2018/10/25 08:46:23 UTC

[camel] branch master updated (b1df34a -> a287357)

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git.


    from b1df34a  CAMEL-11497: Migrate FAQ - remaining questions
     new ff12e2f  CAMEL-12900: Route contract validate does not throw validation exception when validation fails
     new a287357  Fixed CS

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../camel/impl/validator/ProcessorValidator.java   |  9 ++++-
 .../main/java/org/apache/camel/spi/Validator.java  |  2 +-
 .../java/org/apache/camel/util/URISupport.java     |  2 +-
 .../validator/ValidatorXmlSchemaTest.java}         | 47 +++++++++++-----------
 .../{model/simpleRest.xml => impl/validate.xsd}    | 18 ++++-----
 5 files changed, 43 insertions(+), 35 deletions(-)
 copy camel-core/src/test/java/org/apache/camel/{processor/RouteNoAutoStartupTest.java => impl/validator/ValidatorXmlSchemaTest.java} (59%)
 copy camel-core/src/test/resources/org/apache/camel/{model/simpleRest.xml => impl/validate.xsd} (77%)


[camel] 01/02: CAMEL-12900: Route contract validate does not throw validation exception when validation fails

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit ff12e2f010ebbcb94a09c987db59485793a0d869
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Oct 25 10:43:38 2018 +0200

    CAMEL-12900: Route contract validate does not throw validation exception when validation fails
---
 .../camel/impl/validator/ProcessorValidator.java   |  9 ++-
 .../main/java/org/apache/camel/spi/Validator.java  |  2 +-
 .../impl/validator/ValidatorXmlSchemaTest.java     | 70 ++++++++++++++++++++++
 .../resources/org/apache/camel/impl/validate.xsd   | 29 +++++++++
 4 files changed, 108 insertions(+), 2 deletions(-)

diff --git a/camel-core/src/main/java/org/apache/camel/impl/validator/ProcessorValidator.java b/camel-core/src/main/java/org/apache/camel/impl/validator/ProcessorValidator.java
index fead39a..9f64619 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/validator/ProcessorValidator.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/validator/ProcessorValidator.java
@@ -55,16 +55,23 @@ public class ProcessorValidator extends Validator {
         Exchange exchange = message.getExchange();
         
         LOG.debug("Sending to validate processor '{}'", processor);
+        // create a new exchange to use during validation to avoid side-effects on original exchange
         DefaultExchange validateExchange = new DefaultExchange(exchange);
         validateExchange.setIn(message);
         validateExchange.setProperties(exchange.getProperties());
         try {
             processor.process(validateExchange);
+
+            // if the validation failed then propagate the exception
+            if (validateExchange.getException() != null) {
+                exchange.setException(validateExchange.getException());
+            }
+
         } catch (Exception e) {
             if (e instanceof ValidationException) {
                 throw (ValidationException)e;
             } else {
-                throw new ValidationException(String.format("Validation failed for '%s'", type), validateExchange, e);
+                throw new ValidationException(String.format("Validation failed for '%s'", type), exchange, e);
             }
         }
     }
diff --git a/camel-core/src/main/java/org/apache/camel/spi/Validator.java b/camel-core/src/main/java/org/apache/camel/spi/Validator.java
index 50b2d86..1429cc1 100644
--- a/camel-core/src/main/java/org/apache/camel/spi/Validator.java
+++ b/camel-core/src/main/java/org/apache/camel/spi/Validator.java
@@ -31,7 +31,7 @@ import org.apache.camel.support.ServiceSupport;
  * {@link ContractAdvice} applies Validator if input/output type is declared on
  * a route with validation enabled.
  *  
- * @see {@link ContractAdvice}
+ * @see ContractAdvice
  * {@link InputTypeDefinition} {@link OutputTypeDefinition}
  */
 public abstract class Validator extends ServiceSupport implements CamelContextAware {
diff --git a/camel-core/src/test/java/org/apache/camel/impl/validator/ValidatorXmlSchemaTest.java b/camel-core/src/test/java/org/apache/camel/impl/validator/ValidatorXmlSchemaTest.java
new file mode 100644
index 0000000..0e3b4e9
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/impl/validator/ValidatorXmlSchemaTest.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.impl.validator;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.ValidationException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+
+public class ValidatorXmlSchemaTest extends ContextTestSupport {
+
+    @Test
+    public void shouldPass() throws Exception {
+        final String body = "<user><name>Jan</name></user>";
+        MockEndpoint mock = getMockEndpoint("mock:result");
+
+        mock.expectedMessageCount(1);
+        mock.expectedBodiesReceived(body);
+
+        template.sendBody("direct:in", body);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void shouldThrowException() throws Exception {
+        final String body = "<fail/>";
+        MockEndpoint mock = getMockEndpoint("mock:result");
+
+        mock.expectedMessageCount(0);
+        try {
+            template.sendBody("direct:in", body);
+            fail("Should throw exception");
+        } catch (CamelExecutionException e) {
+            assertIsInstanceOf(ValidationException.class, e.getCause());
+        }
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                validator().type("xml").withUri("validator:org/apache/camel/impl/validate.xsd");
+
+                from("direct:in").inputTypeWithValidate("xml")
+                    .to("mock:result");
+            }
+        };
+    }
+
+}
diff --git a/camel-core/src/test/resources/org/apache/camel/impl/validate.xsd b/camel-core/src/test/resources/org/apache/camel/impl/validate.xsd
new file mode 100644
index 0000000..784dc78
--- /dev/null
+++ b/camel-core/src/test/resources/org/apache/camel/impl/validate.xsd
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
+
+  <xs:element name="user">
+    <xs:complexType>
+      <xs:sequence>
+        <xs:element name="name" type="xs:string"/>
+      </xs:sequence>
+    </xs:complexType>
+  </xs:element>
+</xs:schema>
\ No newline at end of file


[camel] 02/02: Fixed CS

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit a287357e2117528830bde1207fe15ad81bea6633
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Oct 25 10:44:54 2018 +0200

    Fixed CS
---
 camel-core/src/main/java/org/apache/camel/util/URISupport.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/camel-core/src/main/java/org/apache/camel/util/URISupport.java b/camel-core/src/main/java/org/apache/camel/util/URISupport.java
index 96f2cfa..1b13164 100644
--- a/camel-core/src/main/java/org/apache/camel/util/URISupport.java
+++ b/camel-core/src/main/java/org/apache/camel/util/URISupport.java
@@ -667,7 +667,7 @@ public final class URISupport {
         final StringBuilder joined = new StringBuilder();
 
         boolean addedLast = false;
-        for (int i = paths.length -1 ; i >= 0 ; i--) {
+        for (int i = paths.length - 1; i >= 0; i--) {
             String path = paths[i];
             if (ObjectHelper.isNotEmpty(path)) {
                 if (addedLast) {