camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject git commit: CAMEL-6238: Added resultType to jsonpath languagae.
Date Sun, 24 Nov 2013 09:49:55 GMT
Updated Branches:
  refs/heads/master d06f3cf90 -> 114ec60f7


CAMEL-6238: Added resultType to jsonpath languagae.


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

Branch: refs/heads/master
Commit: 114ec60f704450e63f47636aa051d28f6a3445fe
Parents: d06f3cf
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Sun Nov 24 10:51:51 2013 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Sun Nov 24 10:51:51 2013 +0100

----------------------------------------------------------------------
 .../apache/camel/builder/ExpressionClause.java  | 13 ++++
 .../camel/builder/ExpressionClauseSupport.java  | 15 +++++
 .../model/language/JsonPathExpression.java      | 58 ++++++++++++++++
 .../camel/jsonpath/JsonPathExpression.java      | 70 ++++++++++++++++++++
 .../apache/camel/jsonpath/JsonPathLanguage.java | 54 +--------------
 .../JsonPathTransformResultTypeTest.java        | 60 +++++++++++++++++
 .../SpringJsonPathTransformResultTypeTest.java  | 49 ++++++++++++++
 .../SpringJsonPathTransformResultTypeTest.xml   | 41 ++++++++++++
 8 files changed, 308 insertions(+), 52 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/114ec60f/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 81fd32f..787ff78 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
@@ -289,6 +289,19 @@ 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 resultType the return type expected by the expression
+     * @return the builder to continue processing the DSL
+     */
+    public T jsonpath(String text, Class<?> resultType) {
+        return delegate.jsonpath(text, 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/114ec60f/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 763b8e2..db3acfd 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
@@ -324,6 +324,21 @@ 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 resultType the return type expected by the expression
+     * @return the builder to continue processing the DSL
+     */
+    public T jsonpath(String text, Class<?> resultType) {
+        JsonPathExpression expression = new JsonPathExpression(text);
+        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/114ec60f/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 31c32e2..6d6eb0a 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
@@ -18,7 +18,14 @@ package org.apache.camel.model.language;
 
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlTransient;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Expression;
+import org.apache.camel.Predicate;
+import org.apache.camel.util.ObjectHelper;
 
 /**
  * For JSonPath expressions and predicates
@@ -29,6 +36,12 @@ import javax.xml.bind.annotation.XmlRootElement;
 @XmlAccessorType(XmlAccessType.FIELD)
 public class JsonPathExpression extends ExpressionDefinition {
 
+    @XmlAttribute(name = "resultType")
+    private String resultTypeName;
+
+    @XmlTransient
+    private Class<?> resultType;
+
     public JsonPathExpression() {
     }
 
@@ -36,7 +49,52 @@ public class JsonPathExpression extends ExpressionDefinition {
         super(expression);
     }
 
+    public String getResultTypeName() {
+        return resultTypeName;
+    }
+
+    public void setResultTypeName(String resultTypeName) {
+        this.resultTypeName = resultTypeName;
+    }
+
+    public Class<?> getResultType() {
+        return resultType;
+    }
+
+    public void setResultType(Class<?> resultType) {
+        this.resultType = resultType;
+    }
+
     public String getLanguage() {
         return "jsonpath";
     }
+
+    @Override
+    public Expression createExpression(CamelContext camelContext) {
+        if (resultType == null && resultTypeName != null) {
+            try {
+                resultType = camelContext.getClassResolver().resolveMandatoryClass(resultTypeName);
+            } catch (ClassNotFoundException e) {
+                throw ObjectHelper.wrapRuntimeCamelException(e);
+            }
+        }
+        return super.createExpression(camelContext);
+    }
+
+    @Override
+    protected void configureExpression(CamelContext camelContext, Expression expression)
{
+        if (resultType != null) {
+            setProperty(expression, "resultType", resultType);
+        }
+        super.configureExpression(camelContext, expression);
+    }
+
+    @Override
+    protected void configurePredicate(CamelContext camelContext, Predicate predicate) {
+        if (resultType != null) {
+            setProperty(predicate, "resultType", resultType);
+        }
+        super.configurePredicate(camelContext, predicate);
+    }
+
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/114ec60f/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
new file mode 100644
index 0000000..5a8ee70
--- /dev/null
+++ b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathExpression.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.jsonpath;
+
+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 {
+
+    private final String expression;
+    private Class<?> resultType;
+    private final JsonPathEngine engine;
+
+    public JsonPathExpression(String expression) {
+        this.expression = expression;
+        try {
+            engine = new JsonPathEngine(expression);
+        } catch (Exception e) {
+            throw new ExpressionIllegalSyntaxException(expression, e);
+        }
+    }
+
+    public Class<?> getResultType() {
+        return resultType;
+    }
+
+    public void setResultType(Class<?> resultType) {
+        this.resultType = resultType;
+    }
+
+    @Override
+    public Object evaluate(Exchange exchange) {
+        try {
+            Object result = evaluateJsonPath(exchange, engine);
+            if (resultType != null) {
+                return exchange.getContext().getTypeConverter().convertTo(resultType, exchange,
result);
+            } else {
+                return result;
+            }
+        } catch (Exception e) {
+            throw new ExpressionEvaluationException(this, exchange, e);
+        }
+    }
+
+    @Override
+    public String toString() {
+        return "jsonpath[" + expression + "]";
+    }
+
+    private Object evaluateJsonPath(Exchange exchange, JsonPathEngine engine) throws Exception
{
+        return engine.read(exchange);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/114ec60f/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 1b313e7..e30ad8e 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,70 +16,20 @@
  */
 package org.apache.camel.jsonpath;
 
-import org.apache.camel.Exchange;
 import org.apache.camel.Expression;
-import org.apache.camel.ExpressionEvaluationException;
-import org.apache.camel.ExpressionIllegalSyntaxException;
 import org.apache.camel.Predicate;
-import org.apache.camel.support.ExpressionAdapter;
 import org.apache.camel.support.LanguageSupport;
 
 public class JsonPathLanguage extends LanguageSupport {
 
     @Override
     public Predicate createPredicate(final String predicate) {
-        final JsonPathEngine engine;
-        try {
-            engine = new JsonPathEngine(predicate);
-        } catch (Exception e) {
-            throw new ExpressionIllegalSyntaxException(predicate, e);
-        }
-
-        return new ExpressionAdapter() {
-            @Override
-            public Object evaluate(Exchange exchange) {
-                try {
-                    return evaluateJsonPath(exchange, engine);
-                } catch (Exception e) {
-                    throw new ExpressionEvaluationException(this, exchange, e);
-                }
-            }
-
-            @Override
-            public String toString() {
-                return "jsonpath[" + predicate + "]";
-            }
-        };
+        return new JsonPathExpression(predicate);
     }
 
     @Override
     public Expression createExpression(final String expression) {
-        final JsonPathEngine engine;
-        try {
-            engine = new JsonPathEngine(expression);
-        } catch (Exception e) {
-            throw new ExpressionIllegalSyntaxException(expression, e);
-        }
-
-        return new ExpressionAdapter() {
-            @Override
-            public Object evaluate(Exchange exchange) {
-                try {
-                    return evaluateJsonPath(exchange, engine);
-                } catch (Exception e) {
-                    throw new ExpressionEvaluationException(this, exchange, e);
-                }
-            }
-
-            @Override
-            public String toString() {
-                return "jsonpath[" + expression + "]";
-            }
-        };
-    }
-
-    private Object evaluateJsonPath(Exchange exchange, JsonPathEngine engine) throws Exception
{
-        return engine.read(exchange);
+        return new JsonPathExpression(expression);
     }
 
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/114ec60f/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathTransformResultTypeTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathTransformResultTypeTest.java
b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathTransformResultTypeTest.java
new file mode 100644
index 0000000..b734bb8
--- /dev/null
+++ b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/JsonPathTransformResultTypeTest.java
@@ -0,0 +1,60 @@
+/**
+ * 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.io.File;
+import java.util.List;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class JsonPathTransformResultTypeTest extends CamelTestSupport {
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .transform().jsonpath("$.store.book[0].price", Float.class)
+                    .to("mock:price");
+
+                from("direct:second")
+                    .transform().jsonpath("$.store.book[0].price", Double.class)
+                    .to("mock:price");
+            }
+        };
+    }
+
+    @Test
+    public void testPriceResultType() throws Exception {
+        getMockEndpoint("mock:price").expectedMessageCount(4);
+        getMockEndpoint("mock:price").message(0).body().isInstanceOf(Float.class);
+        getMockEndpoint("mock:price").message(1).body().isInstanceOf(Double.class);
+        getMockEndpoint("mock:price").message(2).body().isInstanceOf(Float.class);
+        getMockEndpoint("mock:price").message(3).body().isInstanceOf(Double.class);
+
+        template.sendBody("direct:start", new File("src/test/resources/books.json"));
+        template.sendBody("direct:second", new File("src/test/resources/books.json"));
+        template.sendBody("direct:start", new File("src/test/resources/books.json"));
+        template.sendBody("direct:second", new File("src/test/resources/books.json"));
+
+        assertMockEndpointsSatisfied();
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/114ec60f/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/SpringJsonPathTransformResultTypeTest.java
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/SpringJsonPathTransformResultTypeTest.java
b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/SpringJsonPathTransformResultTypeTest.java
new file mode 100644
index 0000000..2a6b562
--- /dev/null
+++ b/components/camel-jsonpath/src/test/java/org/apache/camel/jsonpath/SpringJsonPathTransformResultTypeTest.java
@@ -0,0 +1,49 @@
+/**
+ * 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.io.File;
+
+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 SpringJsonPathTransformResultTypeTest extends CamelSpringTestSupport {
+
+    @Override
+    protected AbstractApplicationContext createApplicationContext() {
+        return new ClassPathXmlApplicationContext("org/apache/camel/jsonpath/SpringJsonPathTransformResultTypeTest.xml");
+    }
+
+    @Test
+    public void testPriceResultType() throws Exception {
+        getMockEndpoint("mock:price").expectedMessageCount(4);
+        getMockEndpoint("mock:price").message(0).body().isInstanceOf(Float.class);
+        getMockEndpoint("mock:price").message(1).body().isInstanceOf(Double.class);
+        getMockEndpoint("mock:price").message(2).body().isInstanceOf(Float.class);
+        getMockEndpoint("mock:price").message(3).body().isInstanceOf(Double.class);
+
+        template.sendBody("direct:start", new File("src/test/resources/books.json"));
+        template.sendBody("direct:second", new File("src/test/resources/books.json"));
+        template.sendBody("direct:start", new File("src/test/resources/books.json"));
+        template.sendBody("direct:second", new File("src/test/resources/books.json"));
+
+        assertMockEndpointsSatisfied();
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/114ec60f/components/camel-jsonpath/src/test/resources/org/apache/camel/jsonpath/SpringJsonPathTransformResultTypeTest.xml
----------------------------------------------------------------------
diff --git a/components/camel-jsonpath/src/test/resources/org/apache/camel/jsonpath/SpringJsonPathTransformResultTypeTest.xml
b/components/camel-jsonpath/src/test/resources/org/apache/camel/jsonpath/SpringJsonPathTransformResultTypeTest.xml
new file mode 100644
index 0000000..0758f25
--- /dev/null
+++ b/components/camel-jsonpath/src/test/resources/org/apache/camel/jsonpath/SpringJsonPathTransformResultTypeTest.xml
@@ -0,0 +1,41 @@
+<?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"/>
+      <transform>
+        <jsonpath resultType="Float">$.store.book[0].price</jsonpath>
+      </transform>
+      <to uri="mock:price"/>
+    </route>
+    <route>
+      <from uri="direct:second"/>
+      <transform>
+        <jsonpath resultType="Double">$.store.book[0].price</jsonpath>
+      </transform>
+      <to uri="mock:price"/>
+    </route>
+  </camelContext>
+
+</beans>
\ No newline at end of file


Mime
View raw message