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 2015/05/30 12:33:06 UTC

[1/6] camel git commit: Make json-path suppress PathNotFoundExceptions by default

Repository: camel
Updated Branches:
  refs/heads/master 36424430c -> be73f1aa8


Make json-path suppress PathNotFoundExceptions by default


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

Branch: refs/heads/master
Commit: ac67a65ade12ed829179e7106c961bb16feab010
Parents: 3642443
Author: Preben Asmussen <pr...@gmail.com>
Authored: Tue May 26 17:37:28 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sat May 30 09:41:59 2015 +0200

----------------------------------------------------------------------
 .../apache/camel/jsonpath/JsonPathEngine.java   | 22 +++++---
 .../apache/camel/jsonpath/JsonPathBeanTest.java | 59 ++++++++++++++++++++
 .../camel/jsonpath/JsonPathLanguageTest.java    | 15 ++++-
 3 files changed, 86 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/ac67a65a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java
index d1d6dae..6977108 100644
--- a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java
+++ b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java
@@ -23,7 +23,10 @@ import java.net.URL;
 import java.nio.charset.Charset;
 
 import com.jayway.jsonpath.Configuration;
+import com.jayway.jsonpath.Configuration.Defaults;
 import com.jayway.jsonpath.JsonPath;
+import com.jayway.jsonpath.Option;
+import com.jayway.jsonpath.internal.DefaultsImpl;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.InvalidPayloadException;
@@ -38,7 +41,8 @@ public class JsonPathEngine {
     private final Configuration configuration;
 
     public JsonPathEngine(String expression) {
-        this.configuration = Configuration.defaultConfiguration();
+        Defaults defaults = DefaultsImpl.INSTANCE;
+        this.configuration = Configuration.builder().jsonProvider(defaults.jsonProvider()).options(Option.SUPPRESS_EXCEPTIONS).build();
         this.path = JsonPath.compile(expression);
     }
 
@@ -52,22 +56,22 @@ public class JsonPathEngine {
                 json = ((WrappedFile<?>)json).getFile();
             }
         } else if (json instanceof WrappedFile) {
-            json = ((WrappedFile<?>) json).getFile();
+            json = ((WrappedFile<?>)json).getFile();
         }
 
         // the message body type should use the suitable read method
         if (json instanceof String) {
-            String str = (String) json;
-            return path.read(str);
+            String str = (String)json;
+            return path.read(str, configuration);
         } else if (json instanceof InputStream) {
-            InputStream is = (InputStream) json;
+            InputStream is = (InputStream)json;
             return path.read(is, Charset.defaultCharset().displayName(), configuration);
         } else if (json instanceof File) {
-            File file = (File) json;
-            return path.read(file);
+            File file = (File)json;
+            return path.read(file, configuration);
         } else if (json instanceof URL) {
-            URL url = (URL) json;
-            return path.read(url);
+            URL url = (URL)json;
+            return path.read(url, configuration);
         }
 
         // fallback as input stream

http://git-wip-us.apache.org/repos/asf/camel/blob/ac67a65a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathBeanTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathBeanTest.java b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathBeanTest.java
new file mode 100644
index 0000000..fb43643
--- /dev/null
+++ b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathBeanTest.java
@@ -0,0 +1,59 @@
+/**
+ * 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.jsonpath;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class JsonPathBeanTest extends CamelTestSupport {
+
+    @Test
+    public void testFullName() throws Exception {
+        String json = "{\"person\" : {\"firstname\" : \"foo\", \"middlename\" : \"foo2\", \"lastname\" : \"bar\"}}";
+        getMockEndpoint("mock:result").expectedBodiesReceived("foo foo2 bar");
+        template.sendBody("direct:start", json);
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testFirstAndLastName() throws Exception {
+        String json = "{\"person\" : {\"firstname\" : \"foo\", \"lastname\" : \"bar\"}}";
+        getMockEndpoint("mock:result").expectedBodiesReceived("foo bar");
+        template.sendBody("direct:start", json);
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:start").bean(FullnameBean.class).to("mock:result");
+            }
+        };
+    }
+
+    protected static class FullnameBean {
+        public static String getName(@JsonPath("person.firstname") String first, @JsonPath("person.middlename") String middle, @JsonPath("person.lastname") String last) {
+            if (middle != null) {
+                return first + " " + middle + " " + last;
+            }
+            return first + " " + last;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/ac67a65a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathLanguageTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathLanguageTest.java b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathLanguageTest.java
index 1522bc6..66db4a8 100644
--- a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathLanguageTest.java
+++ b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathLanguageTest.java
@@ -48,7 +48,7 @@ public class JsonPathLanguageTest extends CamelTestSupport {
         assertEquals(2, authors.size());
         assertEquals("Nigel Rees", authors.get(0));
         assertEquals("Evelyn Waugh", authors.get(1));
-        
+
         exp = lan.createExpression("$.store.bicycle.price");
         String price = exp.evaluate(exchange, String.class);
         assertEquals("Got a wrong result", "19.95", price);
@@ -87,4 +87,17 @@ public class JsonPathLanguageTest extends CamelTestSupport {
         boolean expensive = pre.matches(exchange);
         assertFalse("Should not have expensive books", expensive);
     }
+
+    @Test
+    public void testSuppressException() throws Exception {
+        Exchange exchange = new DefaultExchange(context);
+        exchange.getIn().setBody(new File("src/test/resources/type.json"));
+
+        Language lan = context.resolveLanguage("jsonpath");
+        Expression exp = lan.createExpression("$.foo");
+        String nofoo = exp.evaluate(exchange, String.class);
+
+        assertNull(nofoo);
+    }
+
 }


[2/6] camel git commit: CAMEL-8799: Make it possible for JsonPath to suppress PathNotFoundException

Posted by da...@apache.org.
CAMEL-8799: Make it possible for JsonPath to suppress PathNotFoundException


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

Branch: refs/heads/master
Commit: e6fb2d5d8f0111292bfa318b20165677032568ad
Parents: ac67a65
Author: Claus Ibsen <da...@apache.org>
Authored: Sat May 30 10:04:54 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sat May 30 10:06:07 2015 +0200

----------------------------------------------------------------------
 .../org/apache/camel/jsonpath/JsonPath.java     |  9 ++-
 .../JsonPathAnnotationExpressionFactory.java    | 50 ++++++++++++++++
 .../apache/camel/jsonpath/JsonPathEngine.java   | 23 ++++---
 .../camel/jsonpath/JsonPathExpression.java      | 18 +++++-
 .../apache/camel/jsonpath/JsonPathLanguage.java |  8 ++-
 .../camel/jsonpath/JsonPathBeanOptionTest.java  | 63 ++++++++++++++++++++
 .../apache/camel/jsonpath/JsonPathBeanTest.java | 10 +++-
 7 files changed, 164 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/e6fb2d5d/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPath.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPath.java b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPath.java
index 354875c..eae308d 100644
--- a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPath.java
+++ b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPath.java
@@ -22,6 +22,7 @@ import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
 
+import com.jayway.jsonpath.Option;
 import org.apache.camel.language.LanguageAnnotation;
 
 /**
@@ -34,7 +35,13 @@ import org.apache.camel.language.LanguageAnnotation;
 @Retention(RetentionPolicy.RUNTIME)
 @Documented
 @Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER })
-@LanguageAnnotation(language = "jsonpath")
+@LanguageAnnotation(language = "jsonpath", factory = JsonPathAnnotationExpressionFactory.class)
 public @interface JsonPath {
+
     String value();
+
+    /**
+     * To configure the json path options to use
+     */
+    Option[] options() default {};
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/e6fb2d5d/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathAnnotationExpressionFactory.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathAnnotationExpressionFactory.java b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathAnnotationExpressionFactory.java
new file mode 100644
index 0000000..b56257a
--- /dev/null
+++ b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathAnnotationExpressionFactory.java
@@ -0,0 +1,50 @@
+/**
+ * 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.jsonpath;
+
+import java.lang.annotation.Annotation;
+
+import com.jayway.jsonpath.Option;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Expression;
+import org.apache.camel.component.bean.DefaultAnnotationExpressionFactory;
+import org.apache.camel.language.LanguageAnnotation;
+
+public class JsonPathAnnotationExpressionFactory extends DefaultAnnotationExpressionFactory {
+
+    @Override
+    public Expression createExpression(CamelContext camelContext, Annotation annotation,
+                                       LanguageAnnotation languageAnnotation, Class<?> expressionReturnType) {
+
+        String expression = getExpressionFromAnnotation(annotation);
+        JsonPathExpression answer = new JsonPathExpression(expression);
+
+        if (expressionReturnType != null) {
+            answer.setResultType(expressionReturnType);
+        }
+
+        if (annotation instanceof JsonPath) {
+            JsonPath jsonPathAnnotation = (JsonPath) annotation;
+            Option[] options = jsonPathAnnotation.options();
+            answer.setOptions(options);
+        }
+
+        answer.init();
+        return answer;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/e6fb2d5d/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java
index 6977108..5ed84b4 100644
--- a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java
+++ b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java
@@ -27,7 +27,6 @@ import com.jayway.jsonpath.Configuration.Defaults;
 import com.jayway.jsonpath.JsonPath;
 import com.jayway.jsonpath.Option;
 import com.jayway.jsonpath.internal.DefaultsImpl;
-
 import org.apache.camel.Exchange;
 import org.apache.camel.InvalidPayloadException;
 import org.apache.camel.NoTypeConversionAvailableException;
@@ -42,7 +41,13 @@ public class JsonPathEngine {
 
     public JsonPathEngine(String expression) {
         Defaults defaults = DefaultsImpl.INSTANCE;
-        this.configuration = Configuration.builder().jsonProvider(defaults.jsonProvider()).options(Option.SUPPRESS_EXCEPTIONS).build();
+        this.configuration = Configuration.builder().jsonProvider(defaults.jsonProvider()).build();
+        this.path = JsonPath.compile(expression);
+    }
+
+    public JsonPathEngine(String expression, Option[] options) {
+        Defaults defaults = DefaultsImpl.INSTANCE;
+        this.configuration = Configuration.builder().jsonProvider(defaults.jsonProvider()).options(options).build();
         this.path = JsonPath.compile(expression);
     }
 
@@ -51,26 +56,26 @@ public class JsonPathEngine {
 
         if (json instanceof GenericFile) {
             try {
-                json = GenericFileConverter.genericFileToInputStream((GenericFile<?>)json, exchange);
+                json = GenericFileConverter.genericFileToInputStream((GenericFile<?>) json, exchange);
             } catch (NoTypeConversionAvailableException e) {
-                json = ((WrappedFile<?>)json).getFile();
+                json = ((WrappedFile<?>) json).getFile();
             }
         } else if (json instanceof WrappedFile) {
-            json = ((WrappedFile<?>)json).getFile();
+            json = ((WrappedFile<?>) json).getFile();
         }
 
         // the message body type should use the suitable read method
         if (json instanceof String) {
-            String str = (String)json;
+            String str = (String) json;
             return path.read(str, configuration);
         } else if (json instanceof InputStream) {
-            InputStream is = (InputStream)json;
+            InputStream is = (InputStream) json;
             return path.read(is, Charset.defaultCharset().displayName(), configuration);
         } else if (json instanceof File) {
-            File file = (File)json;
+            File file = (File) json;
             return path.read(file, configuration);
         } else if (json instanceof URL) {
-            URL url = (URL)json;
+            URL url = (URL) json;
             return path.read(url, configuration);
         }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/e6fb2d5d/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathExpression.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathExpression.java b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathExpression.java
index 5a8ee70..d4fa8b3 100644
--- a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathExpression.java
+++ b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathExpression.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.jsonpath;
 
+import com.jayway.jsonpath.Option;
 import org.apache.camel.Exchange;
 import org.apache.camel.ExpressionEvaluationException;
 import org.apache.camel.ExpressionIllegalSyntaxException;
@@ -24,13 +25,18 @@ import org.apache.camel.support.ExpressionAdapter;
 public class JsonPathExpression extends ExpressionAdapter {
 
     private final String expression;
+    private JsonPathEngine engine;
+
     private Class<?> resultType;
-    private final JsonPathEngine engine;
+    private Option[] options;
 
     public JsonPathExpression(String expression) {
         this.expression = expression;
+    }
+
+    public void init() {
         try {
-            engine = new JsonPathEngine(expression);
+            engine = new JsonPathEngine(expression, options);
         } catch (Exception e) {
             throw new ExpressionIllegalSyntaxException(expression, e);
         }
@@ -44,6 +50,14 @@ public class JsonPathExpression extends ExpressionAdapter {
         this.resultType = resultType;
     }
 
+    public Option[] getOptions() {
+        return options;
+    }
+
+    public void setOptions(Option[] options) {
+        this.options = options;
+    }
+
     @Override
     public Object evaluate(Exchange exchange) {
         try {

http://git-wip-us.apache.org/repos/asf/camel/blob/e6fb2d5d/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathLanguage.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathLanguage.java b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathLanguage.java
index e30ad8e..8eb78db 100644
--- a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathLanguage.java
+++ b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathLanguage.java
@@ -24,12 +24,16 @@ public class JsonPathLanguage extends LanguageSupport {
 
     @Override
     public Predicate createPredicate(final String predicate) {
-        return new JsonPathExpression(predicate);
+        JsonPathExpression answer = new JsonPathExpression(predicate);
+        answer.init();
+        return answer;
     }
 
     @Override
     public Expression createExpression(final String expression) {
-        return new JsonPathExpression(expression);
+        JsonPathExpression answer = new JsonPathExpression(expression);
+        answer.init();
+        return answer;
     }
 
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/e6fb2d5d/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathBeanOptionTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathBeanOptionTest.java b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathBeanOptionTest.java
new file mode 100644
index 0000000..b8e761a
--- /dev/null
+++ b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathBeanOptionTest.java
@@ -0,0 +1,63 @@
+/**
+ * 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.jsonpath;
+
+import com.jayway.jsonpath.Option;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class JsonPathBeanOptionTest extends CamelTestSupport {
+
+    @Test
+    public void testFullName() throws Exception {
+        String json = "{\"person\" : {\"firstname\" : \"foo\", \"middlename\" : \"foo2\", \"lastname\" : \"bar\"}}";
+        getMockEndpoint("mock:result").expectedBodiesReceived("foo foo2 bar");
+        template.sendBody("direct:start", json);
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testFirstAndLastName() throws Exception {
+        String json = "{\"person\" : {\"firstname\" : \"foo\", \"lastname\" : \"bar\"}}";
+        getMockEndpoint("mock:result").expectedBodiesReceived("foo bar");
+        template.sendBody("direct:start", json);
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:start").bean(FullNameBean.class).to("mock:result");
+            }
+        };
+    }
+
+    protected static class FullNameBean {
+        // middle name is optional
+        public static String getName(@JsonPath("person.firstname") String first,
+                                     @JsonPath(value = "person.middlename", options = Option.SUPPRESS_EXCEPTIONS) String middle,
+                                     @JsonPath("person.lastname") String last) {
+            if (middle != null) {
+                return first + " " + middle + " " + last;
+            }
+            return first + " " + last;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/e6fb2d5d/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathBeanTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathBeanTest.java b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathBeanTest.java
index fb43643..f53d858 100644
--- a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathBeanTest.java
+++ b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathBeanTest.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.jsonpath;
 
+import com.jayway.jsonpath.Option;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.test.junit4.CamelTestSupport;
 import org.junit.Test;
@@ -43,13 +44,16 @@ public class JsonPathBeanTest extends CamelTestSupport {
         return new RouteBuilder() {
             @Override
             public void configure() {
-                from("direct:start").bean(FullnameBean.class).to("mock:result");
+                from("direct:start").bean(FullNameBean.class).to("mock:result");
             }
         };
     }
 
-    protected static class FullnameBean {
-        public static String getName(@JsonPath("person.firstname") String first, @JsonPath("person.middlename") String middle, @JsonPath("person.lastname") String last) {
+    protected static class FullNameBean {
+        // middle name is optional
+        public static String getName(@JsonPath("person.firstname") String first,
+                                     @JsonPath(value = "person.middlename", options = Option.SUPPRESS_EXCEPTIONS) String middle,
+                                     @JsonPath("person.lastname") String last) {
             if (middle != null) {
                 return first + " " + middle + " " + last;
             }


[5/6] camel git commit: CAMEL-8799: Make it possible for JsonPath to suppress PathNotFoundException. CAMEL-8820: Allow to do post logic when creating expression/predicate from routes

Posted by da...@apache.org.
CAMEL-8799: Make it possible for JsonPath to suppress PathNotFoundException. CAMEL-8820: Allow to do post logic when creating expression/predicate from routes


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

Branch: refs/heads/master
Commit: 2a2852117c094f2bc2778f5e9a8a404e2e395e6f
Parents: 9732938
Author: Claus Ibsen <da...@apache.org>
Authored: Sat May 30 11:27:19 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sat May 30 11:27:19 2015 +0200

----------------------------------------------------------------------
 .../apache/camel/AfterPropertiesConfigured.java | 32 ++++++++++
 .../apache/camel/builder/ExpressionClause.java  | 27 +++++++++
 .../model/language/ExpressionDefinition.java    | 13 ++++
 .../language/NamespaceAwareExpression.java      |  2 +
 .../model/language/XMLTokenizerExpression.java  |  4 +-
 .../camel/model/language/XPathExpression.java   |  1 -
 .../camel/model/language/XQueryExpression.java  |  4 +-
 .../camel/jsonpath/JsonPathExpression.java      | 26 ++++----
 .../apache/camel/jsonpath/JsonPathLanguage.java | 15 ++++-
 .../camel/jsonpath/JsonPathSuppressTest.java    | 63 ++++++++++++++++++++
 .../jsonpath/SpringJsonPathSuppressTest.java    | 55 +++++++++++++++++
 .../jsonpath/SpringJsonPathSuppressTest.xml     | 39 ++++++++++++
 12 files changed, 264 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/2a285211/camel-core/src/main/java/org/apache/camel/AfterPropertiesConfigured.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/AfterPropertiesConfigured.java b/camel-core/src/main/java/org/apache/camel/AfterPropertiesConfigured.java
new file mode 100644
index 0000000..5b95ceb
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/AfterPropertiesConfigured.java
@@ -0,0 +1,32 @@
+/**
+ * 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;
+
+/**
+ * To perform optional initialization on an element after its properties has been configured.
+ */
+public interface AfterPropertiesConfigured {
+
+    /**
+     * Callback invoked after the element have configured its properties.
+     * <p/>
+     * This allows to perform any post init work.
+     *
+     * @param camelContext  the Camel Context
+     */
+    void afterPropertiesConfigured(CamelContext camelContext);
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/2a285211/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java b/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java
index 2fc59bc..85edb58 100644
--- a/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java
+++ b/camel-core/src/main/java/org/apache/camel/builder/ExpressionClause.java
@@ -304,6 +304,19 @@ public class ExpressionClause<T> extends ExpressionDefinition {
      * expression</a>
      *
      * @param text the expression to be evaluated
+     * @param suppressExceptions whether to suppress exceptions such as PathNotFoundException
+     * @return the builder to continue processing the DSL
+     */
+    public T jsonpath(String text, boolean suppressExceptions) {
+        return delegate.jsonpath(text, suppressExceptions);
+    }
+
+    /**
+     * Evaluates a <a
+     * href="http://camel.apache.org/jsonpath.html">Json Path
+     * expression</a>
+     *
+     * @param text the expression to be evaluated
      * @param resultType the return type expected by the expression
      * @return the builder to continue processing the DSL
      */
@@ -312,6 +325,20 @@ public class ExpressionClause<T> extends ExpressionDefinition {
     }
 
     /**
+     * Evaluates a <a
+     * href="http://camel.apache.org/jsonpath.html">Json Path
+     * expression</a>
+     *
+     * @param text the expression to be evaluated
+     * @param suppressExceptions whether to suppress exceptions such as PathNotFoundException
+     * @param resultType the return type expected by the expression
+     * @return the builder to continue processing the DSL
+     */
+    public T jsonpath(String text, boolean suppressExceptions, Class<?> resultType) {
+        return delegate.jsonpath(text, suppressExceptions, resultType);
+    }
+
+    /**
      * Evaluates a <a href="http://commons.apache.org/jxpath/">JXPath expression</a>
      * 
      * @param text the expression to be evaluated

http://git-wip-us.apache.org/repos/asf/camel/blob/2a285211/camel-core/src/main/java/org/apache/camel/model/language/ExpressionDefinition.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/language/ExpressionDefinition.java b/camel-core/src/main/java/org/apache/camel/model/language/ExpressionDefinition.java
index 2a0b294..86ea52e 100644
--- a/camel-core/src/main/java/org/apache/camel/model/language/ExpressionDefinition.java
+++ b/camel-core/src/main/java/org/apache/camel/model/language/ExpressionDefinition.java
@@ -28,6 +28,7 @@ import javax.xml.bind.annotation.XmlValue;
 import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
 
+import org.apache.camel.AfterPropertiesConfigured;
 import org.apache.camel.CamelContext;
 import org.apache.camel.Exchange;
 import org.apache.camel.Expression;
@@ -265,10 +266,22 @@ public class ExpressionDefinition implements Expression, Predicate {
         this.expressionType = expressionType;
     }
 
+    @SuppressWarnings("unchecked")
     protected void configurePredicate(CamelContext camelContext, Predicate predicate) {
+        // allows to perform additional logic after the properties has been configured which may be needed
+        // in the various camel components outside camel-core
+        if (predicate instanceof AfterPropertiesConfigured) {
+            ((AfterPropertiesConfigured) predicate).afterPropertiesConfigured(camelContext);
+        }
     }
 
+    @SuppressWarnings("unchecked")
     protected void configureExpression(CamelContext camelContext, Expression expression) {
+        // allows to perform additional logic after the properties has been configured which may be needed
+        // in the various camel components outside camel-core
+        if (expression instanceof AfterPropertiesConfigured) {
+            ((AfterPropertiesConfigured) expression).afterPropertiesConfigured(camelContext);
+        }
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/camel/blob/2a285211/camel-core/src/main/java/org/apache/camel/model/language/NamespaceAwareExpression.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/language/NamespaceAwareExpression.java b/camel-core/src/main/java/org/apache/camel/model/language/NamespaceAwareExpression.java
index f3d9eb6..c549e16 100644
--- a/camel-core/src/main/java/org/apache/camel/model/language/NamespaceAwareExpression.java
+++ b/camel-core/src/main/java/org/apache/camel/model/language/NamespaceAwareExpression.java
@@ -61,11 +61,13 @@ public abstract class NamespaceAwareExpression extends ExpressionDefinition impl
     @Override
     protected void configureExpression(CamelContext camelContext, Expression expression) {
         configureNamespaceAware(expression);
+        super.configureExpression(camelContext, expression);
     }
 
     @Override
     protected void configurePredicate(CamelContext camelContext, Predicate predicate) {
         configureNamespaceAware(predicate);
+        super.configurePredicate(camelContext, predicate);
     }
 
     protected void configureNamespaceAware(Object builder) {

http://git-wip-us.apache.org/repos/asf/camel/blob/2a285211/camel-core/src/main/java/org/apache/camel/model/language/XMLTokenizerExpression.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/language/XMLTokenizerExpression.java b/camel-core/src/main/java/org/apache/camel/model/language/XMLTokenizerExpression.java
index d85e79d..8d9c266 100644
--- a/camel-core/src/main/java/org/apache/camel/model/language/XMLTokenizerExpression.java
+++ b/camel-core/src/main/java/org/apache/camel/model/language/XMLTokenizerExpression.java
@@ -96,7 +96,6 @@ public class XMLTokenizerExpression extends NamespaceAwareExpression {
 
     @Override
     protected void configureExpression(CamelContext camelContext, Expression expression) {
-        super.configureExpression(camelContext, expression);
         if (headerName != null) {
             setProperty(expression, "headerName", headerName);
         }
@@ -106,11 +105,11 @@ public class XMLTokenizerExpression extends NamespaceAwareExpression {
         if (group != null) {
             setProperty(expression, "group", group);
         }
+        super.configureExpression(camelContext, expression);
     }
 
     @Override
     protected void configurePredicate(CamelContext camelContext, Predicate predicate) {
-        super.configurePredicate(camelContext, predicate);
         if (headerName != null) {
             setProperty(predicate, "headerName", headerName);
         }
@@ -120,6 +119,7 @@ public class XMLTokenizerExpression extends NamespaceAwareExpression {
         if (group != null) {
             setProperty(predicate, "group", group);
         }
+        super.configurePredicate(camelContext, predicate);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/camel/blob/2a285211/camel-core/src/main/java/org/apache/camel/model/language/XPathExpression.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/language/XPathExpression.java b/camel-core/src/main/java/org/apache/camel/model/language/XPathExpression.java
index 4a01417..609a9e4 100644
--- a/camel-core/src/main/java/org/apache/camel/model/language/XPathExpression.java
+++ b/camel-core/src/main/java/org/apache/camel/model/language/XPathExpression.java
@@ -234,7 +234,6 @@ public class XPathExpression extends NamespaceAwareExpression {
         }
         // moved the super configuration to the bottom so that the namespace init picks up the newly set XPath Factory
         super.configureExpression(camelContext, expression);
-
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/camel/blob/2a285211/camel-core/src/main/java/org/apache/camel/model/language/XQueryExpression.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/language/XQueryExpression.java b/camel-core/src/main/java/org/apache/camel/model/language/XQueryExpression.java
index 763dfc8..0513d8e 100644
--- a/camel-core/src/main/java/org/apache/camel/model/language/XQueryExpression.java
+++ b/camel-core/src/main/java/org/apache/camel/model/language/XQueryExpression.java
@@ -107,24 +107,24 @@ public class XQueryExpression extends NamespaceAwareExpression {
 
     @Override
     protected void configureExpression(CamelContext camelContext, Expression expression) {
-        super.configureExpression(camelContext, expression);
         if (resultType != null) {
             setProperty(expression, "resultType", resultType);
         }
         if (ObjectHelper.isNotEmpty(getHeaderName())) {
             setProperty(expression, "headerName", getHeaderName());
         }
+        super.configureExpression(camelContext, expression);
     }
 
     @Override
     protected void configurePredicate(CamelContext camelContext, Predicate predicate) {
-        super.configurePredicate(camelContext, predicate);
         if (resultType != null) {
             setProperty(predicate, "resultType", resultType);
         }
         if (ObjectHelper.isNotEmpty(getHeaderName())) {
             setProperty(predicate, "headerName", getHeaderName());
         }
+        super.configurePredicate(camelContext, predicate);
     }
 
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/2a285211/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathExpression.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathExpression.java b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathExpression.java
index 3f84938..5a8bbe6 100644
--- a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathExpression.java
+++ b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathExpression.java
@@ -17,12 +17,14 @@
 package org.apache.camel.jsonpath;
 
 import com.jayway.jsonpath.Option;
+import org.apache.camel.AfterPropertiesConfigured;
+import org.apache.camel.CamelContext;
 import org.apache.camel.Exchange;
 import org.apache.camel.ExpressionEvaluationException;
 import org.apache.camel.ExpressionIllegalSyntaxException;
 import org.apache.camel.support.ExpressionAdapter;
 
-public class JsonPathExpression extends ExpressionAdapter {
+public class JsonPathExpression extends ExpressionAdapter implements AfterPropertiesConfigured {
 
     private final String expression;
     private JsonPathEngine engine;
@@ -35,14 +37,6 @@ public class JsonPathExpression extends ExpressionAdapter {
         this.expression = expression;
     }
 
-    public void init() {
-        try {
-            engine = new JsonPathEngine(expression, suppressExceptions, options);
-        } catch (Exception e) {
-            throw new ExpressionIllegalSyntaxException(expression, e);
-        }
-    }
-
     public Class<?> getResultType() {
         return resultType;
     }
@@ -91,6 +85,19 @@ public class JsonPathExpression extends ExpressionAdapter {
     }
 
     @Override
+    public void afterPropertiesConfigured(CamelContext camelContext) {
+        init();
+    }
+
+    public void init() {
+        try {
+            engine = new JsonPathEngine(expression, suppressExceptions, options);
+        } catch (Exception e) {
+            throw new ExpressionIllegalSyntaxException(expression, e);
+        }
+    }
+
+    @Override
     public String toString() {
         return "jsonpath[" + expression + "]";
     }
@@ -98,5 +105,4 @@ public class JsonPathExpression extends ExpressionAdapter {
     private Object evaluateJsonPath(Exchange exchange, JsonPathEngine engine) throws Exception {
         return engine.read(exchange);
     }
-
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/2a285211/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathLanguage.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathLanguage.java b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathLanguage.java
index 7f25e70..297823b 100644
--- a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathLanguage.java
+++ b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathLanguage.java
@@ -24,6 +24,7 @@ import org.apache.camel.support.LanguageSupport;
 public class JsonPathLanguage extends LanguageSupport {
 
     private Class<?> resultType;
+    private boolean suppressExceptions;
     private Option[] options;
 
     public Class<?> getResultType() {
@@ -34,6 +35,14 @@ public class JsonPathLanguage extends LanguageSupport {
         this.resultType = resultType;
     }
 
+    public boolean isSuppressExceptions() {
+        return suppressExceptions;
+    }
+
+    public void setSuppressExceptions(boolean suppressExceptions) {
+        this.suppressExceptions = suppressExceptions;
+    }
+
     public Option[] getOptions() {
         return options;
     }
@@ -50,8 +59,9 @@ public class JsonPathLanguage extends LanguageSupport {
     public Predicate createPredicate(final String predicate) {
         JsonPathExpression answer = new JsonPathExpression(predicate);
         answer.setResultType(resultType);
+        answer.setSuppressExceptions(suppressExceptions);
         answer.setOptions(options);
-        answer.init();
+        answer.afterPropertiesConfigured(getCamelContext());
         return answer;
     }
 
@@ -59,8 +69,9 @@ public class JsonPathLanguage extends LanguageSupport {
     public Expression createExpression(final String expression) {
         JsonPathExpression answer = new JsonPathExpression(expression);
         answer.setResultType(resultType);
+        answer.setSuppressExceptions(suppressExceptions);
         answer.setOptions(options);
-        answer.init();
+        answer.afterPropertiesConfigured(getCamelContext());
         return answer;
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/2a285211/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathSuppressTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathSuppressTest.java b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathSuppressTest.java
new file mode 100644
index 0000000..8a7d9fb
--- /dev/null
+++ b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathSuppressTest.java
@@ -0,0 +1,63 @@
+/**
+ * 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.jsonpath;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class JsonPathSuppressTest extends CamelTestSupport {
+
+    @Test
+    public void testMiddle() throws Exception {
+        String json = "{\"person\" : {\"firstname\" : \"foo\", \"middlename\" : \"foo2\", \"lastname\" : \"bar\"}}";
+
+        getMockEndpoint("mock:middle").expectedMessageCount(1);
+        getMockEndpoint("mock:other").expectedMessageCount(0);
+
+        template.sendBody("direct:start", json);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testNoMiddle() throws Exception {
+        String json = "{\"person\" : {\"firstname\" : \"foo\", \"lastname\" : \"bar\"}}";
+
+        getMockEndpoint("mock:middle").expectedMessageCount(0);
+        getMockEndpoint("mock:other").expectedMessageCount(1);
+
+        template.sendBody("direct:start", json);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .choice()
+                        .when().jsonpath("person.middlename", true)
+                            .to("mock:middle")
+                        .otherwise()
+                            .to("mock:other");
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/2a285211/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/SpringJsonPathSuppressTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/SpringJsonPathSuppressTest.java b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/SpringJsonPathSuppressTest.java
new file mode 100644
index 0000000..4ca789d
--- /dev/null
+++ b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/SpringJsonPathSuppressTest.java
@@ -0,0 +1,55 @@
+/**
+ * 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.jsonpath;
+
+import org.apache.camel.test.spring.CamelSpringTestSupport;
+import org.junit.Test;
+import org.springframework.context.support.AbstractApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+public class SpringJsonPathSuppressTest extends CamelSpringTestSupport {
+
+    @Override
+    protected AbstractApplicationContext createApplicationContext() {
+        return new ClassPathXmlApplicationContext("org/apache/camel/jsonpath/SpringJsonPathSuppressTest.xml");
+    }
+
+    @Test
+    public void testMiddle() throws Exception {
+        String json = "{\"person\" : {\"firstname\" : \"foo\", \"middlename\" : \"foo2\", \"lastname\" : \"bar\"}}";
+
+        getMockEndpoint("mock:middle").expectedMessageCount(1);
+        getMockEndpoint("mock:other").expectedMessageCount(0);
+
+        template.sendBody("direct:start", json);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testNoMiddle() throws Exception {
+        String json = "{\"person\" : {\"firstname\" : \"foo\", \"lastname\" : \"bar\"}}";
+
+        getMockEndpoint("mock:middle").expectedMessageCount(0);
+        getMockEndpoint("mock:other").expectedMessageCount(1);
+
+        template.sendBody("direct:start", json);
+
+        assertMockEndpointsSatisfied();
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/2a285211/components/camel-jsonpath/src/test/resources/org/apache/camel/jsonpath/SpringJsonPathSuppressTest.xml
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/test/resources/org/apache/camel/jsonpath/SpringJsonPathSuppressTest.xml b/components/camel-jsonpath/src/test/resources/org/apache/camel/jsonpath/SpringJsonPathSuppressTest.xml
new file mode 100644
index 0000000..be89973
--- /dev/null
+++ b/components/camel-jsonpath/src/test/resources/org/apache/camel/jsonpath/SpringJsonPathSuppressTest.xml
@@ -0,0 +1,39 @@
+<?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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="
+            http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
+            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
+
+  <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
+    <route>
+      <from uri="direct:start"/>
+      <choice>
+        <when>
+          <jsonpath suppressExceptions="true">person.middlename</jsonpath>
+          <to uri="mock:middle"/>
+        </when>
+        <otherwise>
+          <to uri="mock:other"/>
+        </otherwise>
+      </choice>
+    </route>
+  </camelContext>
+
+</beans>
\ No newline at end of file


[3/6] camel git commit: CAMEL-8799: Make it possible for JsonPath to suppress PathNotFoundException

Posted by da...@apache.org.
CAMEL-8799: Make it possible for JsonPath to suppress PathNotFoundException


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

Branch: refs/heads/master
Commit: 0654622f2a3e3d2e41fec9af4c2edeb0b5c34465
Parents: e6fb2d5
Author: Claus Ibsen <da...@apache.org>
Authored: Sat May 30 10:20:22 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sat May 30 10:20:22 2015 +0200

----------------------------------------------------------------------
 .../apache/camel/jsonpath/JsonPathEngine.java   | 10 +++---
 .../apache/camel/jsonpath/JsonPathLanguage.java | 33 ++++++++++++++++++++
 .../apache/camel/jsonpath/JsonPathBeanTest.java | 10 ++++++
 .../apache/camel/jsonpath/JsonPathCBRTest.java  |  1 -
 .../camel/jsonpath/JsonPathLanguageTest.java    |  5 ++-
 5 files changed, 53 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/0654622f/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java
index 5ed84b4..f73315e 100644
--- a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java
+++ b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java
@@ -40,14 +40,16 @@ public class JsonPathEngine {
     private final Configuration configuration;
 
     public JsonPathEngine(String expression) {
-        Defaults defaults = DefaultsImpl.INSTANCE;
-        this.configuration = Configuration.builder().jsonProvider(defaults.jsonProvider()).build();
-        this.path = JsonPath.compile(expression);
+        this(expression, null);
     }
 
     public JsonPathEngine(String expression, Option[] options) {
         Defaults defaults = DefaultsImpl.INSTANCE;
-        this.configuration = Configuration.builder().jsonProvider(defaults.jsonProvider()).options(options).build();
+        if (options != null) {
+            this.configuration = Configuration.builder().jsonProvider(defaults.jsonProvider()).options(options).build();
+        } else {
+            this.configuration = Configuration.builder().jsonProvider(defaults.jsonProvider()).build();
+        }
         this.path = JsonPath.compile(expression);
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/0654622f/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathLanguage.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathLanguage.java b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathLanguage.java
index 8eb78db..7f25e70 100644
--- a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathLanguage.java
+++ b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathLanguage.java
@@ -16,15 +16,41 @@
  */
 package org.apache.camel.jsonpath;
 
+import com.jayway.jsonpath.Option;
 import org.apache.camel.Expression;
 import org.apache.camel.Predicate;
 import org.apache.camel.support.LanguageSupport;
 
 public class JsonPathLanguage extends LanguageSupport {
 
+    private Class<?> resultType;
+    private Option[] options;
+
+    public Class<?> getResultType() {
+        return resultType;
+    }
+
+    public void setResultType(Class<?> resultType) {
+        this.resultType = resultType;
+    }
+
+    public Option[] getOptions() {
+        return options;
+    }
+
+    public void setOption(Option option) {
+        this.options = new Option[]{option};
+    }
+
+    public void setOptions(Option[] options) {
+        this.options = options;
+    }
+
     @Override
     public Predicate createPredicate(final String predicate) {
         JsonPathExpression answer = new JsonPathExpression(predicate);
+        answer.setResultType(resultType);
+        answer.setOptions(options);
         answer.init();
         return answer;
     }
@@ -32,8 +58,15 @@ public class JsonPathLanguage extends LanguageSupport {
     @Override
     public Expression createExpression(final String expression) {
         JsonPathExpression answer = new JsonPathExpression(expression);
+        answer.setResultType(resultType);
+        answer.setOptions(options);
         answer.init();
         return answer;
     }
 
+    @Override
+    public boolean isSingleton() {
+        // cannot be singleton due options
+        return false;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/0654622f/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathBeanTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathBeanTest.java b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathBeanTest.java
index f53d858..2ef033f 100644
--- a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathBeanTest.java
+++ b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathBeanTest.java
@@ -32,6 +32,16 @@ public class JsonPathBeanTest extends CamelTestSupport {
     }
 
     @Test
+    public void testFullNameTwo() throws Exception {
+        String json = "{\"person\" : {\"firstname\" : \"foo\", \"middlename\" : \"foo2\", \"lastname\" : \"bar\"}}";
+        String json2 = "{\"person\" : {\"firstname\" : \"bar\", \"middlename\" : \"bar2\", \"lastname\" : \"foo\"}}";
+        getMockEndpoint("mock:result").expectedBodiesReceived("foo foo2 bar", "bar bar2 foo");
+        template.sendBody("direct:start", json);
+        template.sendBody("direct:start", json2);
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
     public void testFirstAndLastName() throws Exception {
         String json = "{\"person\" : {\"firstname\" : \"foo\", \"lastname\" : \"bar\"}}";
         getMockEndpoint("mock:result").expectedBodiesReceived("foo bar");

http://git-wip-us.apache.org/repos/asf/camel/blob/0654622f/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathCBRTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathCBRTest.java b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathCBRTest.java
index 2e869a6..4e85260 100644
--- a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathCBRTest.java
+++ b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathCBRTest.java
@@ -68,7 +68,6 @@ public class JsonPathCBRTest extends CamelTestSupport {
         sendMessageToBicycleRoute("direct:bicycle");
         resetMocks();
         sendMessageToBicycleRoute("direct:bicycle2");
-        
     }
     
     private void sendMessageToBicycleRoute(String startPoint) throws Exception {

http://git-wip-us.apache.org/repos/asf/camel/blob/0654622f/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathLanguageTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathLanguageTest.java b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathLanguageTest.java
index 66db4a8..0f10864 100644
--- a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathLanguageTest.java
+++ b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathLanguageTest.java
@@ -19,6 +19,7 @@ package org.apache.camel.jsonpath;
 import java.io.File;
 import java.util.List;
 
+import com.jayway.jsonpath.Option;
 import org.apache.camel.Exchange;
 import org.apache.camel.Expression;
 import org.apache.camel.Predicate;
@@ -93,7 +94,9 @@ public class JsonPathLanguageTest extends CamelTestSupport {
         Exchange exchange = new DefaultExchange(context);
         exchange.getIn().setBody(new File("src/test/resources/type.json"));
 
-        Language lan = context.resolveLanguage("jsonpath");
+        JsonPathLanguage lan = (JsonPathLanguage) context.resolveLanguage("jsonpath");
+        lan.setOption(Option.SUPPRESS_EXCEPTIONS);
+
         Expression exp = lan.createExpression("$.foo");
         String nofoo = exp.evaluate(exchange, String.class);
 


[6/6] camel git commit: CAMEL-8820: Add note its currently only for languages

Posted by da...@apache.org.
CAMEL-8820: Add note its currently only for languages


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

Branch: refs/heads/master
Commit: be73f1aa893c3a86c6f3c875bd43a98d5405dc01
Parents: 2a28521
Author: Claus Ibsen <da...@apache.org>
Authored: Sat May 30 11:52:16 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sat May 30 11:52:16 2015 +0200

----------------------------------------------------------------------
 .../src/main/java/org/apache/camel/AfterPropertiesConfigured.java  | 2 ++
 1 file changed, 2 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/be73f1aa/camel-core/src/main/java/org/apache/camel/AfterPropertiesConfigured.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/AfterPropertiesConfigured.java b/camel-core/src/main/java/org/apache/camel/AfterPropertiesConfigured.java
index 5b95ceb..9db353c 100644
--- a/camel-core/src/main/java/org/apache/camel/AfterPropertiesConfigured.java
+++ b/camel-core/src/main/java/org/apache/camel/AfterPropertiesConfigured.java
@@ -18,6 +18,8 @@ package org.apache.camel;
 
 /**
  * To perform optional initialization on an element after its properties has been configured.
+ * <p/>
+ * Currently only languages is supported using this callback.
  */
 public interface AfterPropertiesConfigured {
 


[4/6] camel git commit: CAMEL-8799: Make it possible for JsonPath to suppress PathNotFoundException

Posted by da...@apache.org.
CAMEL-8799: Make it possible for JsonPath to suppress PathNotFoundException


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

Branch: refs/heads/master
Commit: 9732938301d96bc4e06101a7b95860d9446e03cc
Parents: 0654622
Author: Claus Ibsen <da...@apache.org>
Authored: Sat May 30 10:39:33 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Sat May 30 10:39:33 2015 +0200

----------------------------------------------------------------------
 .../camel/builder/ExpressionClauseSupport.java  | 33 +++++++++-
 .../model/language/JsonPathExpression.java      | 19 ++++++
 .../org/apache/camel/jsonpath/JsonPath.java     |  5 ++
 .../JsonPathAnnotationExpressionFactory.java    |  3 +
 .../apache/camel/jsonpath/JsonPathEngine.java   | 16 +++--
 .../camel/jsonpath/JsonPathExpression.java      | 20 ++++++-
 .../camel/jsonpath/JsonPathBeanOptionTest.java  | 63 --------------------
 .../JsonPathBeanSuppressExceptionsTest.java     | 62 +++++++++++++++++++
 8 files changed, 152 insertions(+), 69 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/97329383/camel-core/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java b/camel-core/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java
index d0fa176..a40b932 100644
--- a/camel-core/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java
+++ b/camel-core/src/main/java/org/apache/camel/builder/ExpressionClauseSupport.java
@@ -333,7 +333,21 @@ public class ExpressionClauseSupport<T> {
      * @return the builder to continue processing the DSL
      */
     public T jsonpath(String text) {
-        return expression(new JsonPathExpression(text));
+        return jsonpath(text, false);
+    }
+
+    /**
+     * Evaluates a <a href="http://camel.apache.org/jsonpath.html">Json Path
+     * expression</a>
+     *
+     * @param text the expression to be evaluated
+     * @param suppressExceptions whether to suppress exceptions such as PathNotFoundException
+     * @return the builder to continue processing the DSL
+     */
+    public T jsonpath(String text, boolean suppressExceptions) {
+        JsonPathExpression expression = new JsonPathExpression(text);
+        expression.setSuppressExceptions(suppressExceptions);
+        return expression(expression);
     }
 
     /**
@@ -352,6 +366,23 @@ public class ExpressionClauseSupport<T> {
     }
 
     /**
+     * Evaluates a <a href="http://camel.apache.org/jsonpath.html">Json Path
+     * expression</a>
+     *
+     * @param text the expression to be evaluated
+     * @param suppressExceptions whether to suppress exceptions such as PathNotFoundException
+     * @param resultType the return type expected by the expression
+     * @return the builder to continue processing the DSL
+     */
+    public T jsonpath(String text, boolean suppressExceptions, Class<?> resultType) {
+        JsonPathExpression expression = new JsonPathExpression(text);
+        expression.setSuppressExceptions(suppressExceptions);
+        expression.setResultType(resultType);
+        setExpressionType(expression);
+        return result;
+    }
+
+    /**
      * Evaluates a <a href="http://commons.apache.org/jxpath/">JXPath expression</a>
      *
      * @param text the expression to be evaluated

http://git-wip-us.apache.org/repos/asf/camel/blob/97329383/camel-core/src/main/java/org/apache/camel/model/language/JsonPathExpression.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/model/language/JsonPathExpression.java b/camel-core/src/main/java/org/apache/camel/model/language/JsonPathExpression.java
index 3d37b92..f5a7dd7 100644
--- a/camel-core/src/main/java/org/apache/camel/model/language/JsonPathExpression.java
+++ b/camel-core/src/main/java/org/apache/camel/model/language/JsonPathExpression.java
@@ -42,6 +42,8 @@ public class JsonPathExpression extends ExpressionDefinition {
     private String resultTypeName;
     @XmlTransient
     private Class<?> resultType;
+    @XmlAttribute @Metadata(defaultValue = "false")
+    private Boolean suppressExceptions;
 
     public JsonPathExpression() {
     }
@@ -72,6 +74,17 @@ public class JsonPathExpression extends ExpressionDefinition {
         this.resultType = resultType;
     }
 
+    public Boolean getSuppressExceptions() {
+        return suppressExceptions;
+    }
+
+    /**
+     * Whether to suppress exceptions such as PathNotFoundException.
+     */
+    public void setSuppressExceptions(Boolean suppressExceptions) {
+        this.suppressExceptions = suppressExceptions;
+    }
+
     public String getLanguage() {
         return "jsonpath";
     }
@@ -93,6 +106,9 @@ public class JsonPathExpression extends ExpressionDefinition {
         if (resultType != null) {
             setProperty(expression, "resultType", resultType);
         }
+        if (suppressExceptions != null) {
+            setProperty(expression, "suppressExceptions", suppressExceptions);
+        }
         super.configureExpression(camelContext, expression);
     }
 
@@ -101,6 +117,9 @@ public class JsonPathExpression extends ExpressionDefinition {
         if (resultType != null) {
             setProperty(predicate, "resultType", resultType);
         }
+        if (suppressExceptions != null) {
+            setProperty(predicate, "suppressExceptions", suppressExceptions);
+        }
         super.configurePredicate(camelContext, predicate);
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/97329383/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPath.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPath.java b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPath.java
index eae308d..0bca294 100644
--- a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPath.java
+++ b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPath.java
@@ -41,6 +41,11 @@ public @interface JsonPath {
     String value();
 
     /**
+     * Whether to suppress exceptions such as PathNotFoundException
+     */
+    boolean suppressExceptions() default false;
+
+    /**
      * To configure the json path options to use
      */
     Option[] options() default {};

http://git-wip-us.apache.org/repos/asf/camel/blob/97329383/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathAnnotationExpressionFactory.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathAnnotationExpressionFactory.java b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathAnnotationExpressionFactory.java
index b56257a..8c314c5 100644
--- a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathAnnotationExpressionFactory.java
+++ b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathAnnotationExpressionFactory.java
@@ -39,6 +39,9 @@ public class JsonPathAnnotationExpressionFactory extends DefaultAnnotationExpres
 
         if (annotation instanceof JsonPath) {
             JsonPath jsonPathAnnotation = (JsonPath) annotation;
+
+            answer.setSuppressExceptions(jsonPathAnnotation.suppressExceptions());
+
             Option[] options = jsonPathAnnotation.options();
             answer.setOptions(options);
         }

http://git-wip-us.apache.org/repos/asf/camel/blob/97329383/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java
index f73315e..363492d 100644
--- a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java
+++ b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java
@@ -40,15 +40,23 @@ public class JsonPathEngine {
     private final Configuration configuration;
 
     public JsonPathEngine(String expression) {
-        this(expression, null);
+        this(expression, false, null);
     }
 
-    public JsonPathEngine(String expression, Option[] options) {
+    public JsonPathEngine(String expression, boolean suppressExceptions, Option[] options) {
         Defaults defaults = DefaultsImpl.INSTANCE;
         if (options != null) {
-            this.configuration = Configuration.builder().jsonProvider(defaults.jsonProvider()).options(options).build();
+            Configuration.ConfigurationBuilder builder = Configuration.builder().jsonProvider(defaults.jsonProvider()).options(options);
+            if (suppressExceptions) {
+                builder.options(Option.SUPPRESS_EXCEPTIONS);
+            }
+            this.configuration = builder.build();
         } else {
-            this.configuration = Configuration.builder().jsonProvider(defaults.jsonProvider()).build();
+            Configuration.ConfigurationBuilder builder = Configuration.builder().jsonProvider(defaults.jsonProvider());
+            if (suppressExceptions) {
+                builder.options(Option.SUPPRESS_EXCEPTIONS);
+            }
+            this.configuration = builder.build();
         }
         this.path = JsonPath.compile(expression);
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/97329383/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathExpression.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathExpression.java b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathExpression.java
index d4fa8b3..3f84938 100644
--- a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathExpression.java
+++ b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathExpression.java
@@ -28,6 +28,7 @@ public class JsonPathExpression extends ExpressionAdapter {
     private JsonPathEngine engine;
 
     private Class<?> resultType;
+    private boolean suppressExceptions;
     private Option[] options;
 
     public JsonPathExpression(String expression) {
@@ -36,7 +37,7 @@ public class JsonPathExpression extends ExpressionAdapter {
 
     public void init() {
         try {
-            engine = new JsonPathEngine(expression, options);
+            engine = new JsonPathEngine(expression, suppressExceptions, options);
         } catch (Exception e) {
             throw new ExpressionIllegalSyntaxException(expression, e);
         }
@@ -46,14 +47,31 @@ public class JsonPathExpression extends ExpressionAdapter {
         return resultType;
     }
 
+    /**
+     * To configure the result type to use
+     */
     public void setResultType(Class<?> resultType) {
         this.resultType = resultType;
     }
 
+    public boolean isSuppressExceptions() {
+        return suppressExceptions;
+    }
+
+    /**
+     * Whether to suppress exceptions such as PathNotFoundException
+     */
+    public void setSuppressExceptions(boolean suppressExceptions) {
+        this.suppressExceptions = suppressExceptions;
+    }
+
     public Option[] getOptions() {
         return options;
     }
 
+    /**
+     * To configure the json path options to use
+     */
     public void setOptions(Option[] options) {
         this.options = options;
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/97329383/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathBeanOptionTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathBeanOptionTest.java b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathBeanOptionTest.java
deleted file mode 100644
index b8e761a..0000000
--- a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathBeanOptionTest.java
+++ /dev/null
@@ -1,63 +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.jsonpath;
-
-import com.jayway.jsonpath.Option;
-import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.test.junit4.CamelTestSupport;
-import org.junit.Test;
-
-public class JsonPathBeanOptionTest extends CamelTestSupport {
-
-    @Test
-    public void testFullName() throws Exception {
-        String json = "{\"person\" : {\"firstname\" : \"foo\", \"middlename\" : \"foo2\", \"lastname\" : \"bar\"}}";
-        getMockEndpoint("mock:result").expectedBodiesReceived("foo foo2 bar");
-        template.sendBody("direct:start", json);
-        assertMockEndpointsSatisfied();
-    }
-
-    @Test
-    public void testFirstAndLastName() throws Exception {
-        String json = "{\"person\" : {\"firstname\" : \"foo\", \"lastname\" : \"bar\"}}";
-        getMockEndpoint("mock:result").expectedBodiesReceived("foo bar");
-        template.sendBody("direct:start", json);
-        assertMockEndpointsSatisfied();
-    }
-
-    @Override
-    protected RouteBuilder createRouteBuilder() {
-        return new RouteBuilder() {
-            @Override
-            public void configure() {
-                from("direct:start").bean(FullNameBean.class).to("mock:result");
-            }
-        };
-    }
-
-    protected static class FullNameBean {
-        // middle name is optional
-        public static String getName(@JsonPath("person.firstname") String first,
-                                     @JsonPath(value = "person.middlename", options = Option.SUPPRESS_EXCEPTIONS) String middle,
-                                     @JsonPath("person.lastname") String last) {
-            if (middle != null) {
-                return first + " " + middle + " " + last;
-            }
-            return first + " " + last;
-        }
-    }
-}

http://git-wip-us.apache.org/repos/asf/camel/blob/97329383/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathBeanSuppressExceptionsTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathBeanSuppressExceptionsTest.java b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathBeanSuppressExceptionsTest.java
new file mode 100644
index 0000000..20541a2
--- /dev/null
+++ b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathBeanSuppressExceptionsTest.java
@@ -0,0 +1,62 @@
+/**
+ * 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.jsonpath;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class JsonPathBeanSuppressExceptionsTest extends CamelTestSupport {
+
+    @Test
+    public void testFullName() throws Exception {
+        String json = "{\"person\" : {\"firstname\" : \"foo\", \"middlename\" : \"foo2\", \"lastname\" : \"bar\"}}";
+        getMockEndpoint("mock:result").expectedBodiesReceived("foo foo2 bar");
+        template.sendBody("direct:start", json);
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testFirstAndLastName() throws Exception {
+        String json = "{\"person\" : {\"firstname\" : \"foo\", \"lastname\" : \"bar\"}}";
+        getMockEndpoint("mock:result").expectedBodiesReceived("foo bar");
+        template.sendBody("direct:start", json);
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:start").bean(FullNameBean.class).to("mock:result");
+            }
+        };
+    }
+
+    protected static class FullNameBean {
+        // middle name is optional
+        public static String getName(@JsonPath("person.firstname") String first,
+                                     @JsonPath(value = "person.middlename", suppressExceptions = true) String middle,
+                                     @JsonPath("person.lastname") String last) {
+            if (middle != null) {
+                return first + " " + middle + " " + last;
+            }
+            return first + " " + last;
+        }
+    }
+}