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: camel-jsonpath component.
Date Sun, 06 Oct 2013 14:28:52 GMT
Updated Branches:
  refs/heads/master 969d88146 -> 7817d1434


CAMEL-6238: camel-jsonpath component.


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

Branch: refs/heads/master
Commit: 7817d14343e2e7e6a19f3ad82492726224f3e492
Parents: 969d881
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Sun Oct 6 16:28:46 2013 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Sun Oct 6 16:28:46 2013 +0200

----------------------------------------------------------------------
 .../apache/camel/jsonpath/JSonPathEngine.java   | 37 ++++++++++++++++++--
 .../apache/camel/jsonpath/JsonPathLanguage.java | 20 ++++++-----
 2 files changed, 46 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/7817d143/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 48bde5c..745ce0e 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
@@ -16,22 +16,55 @@
  */
 package org.apache.camel.jsonpath;
 
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.URL;
 
+import com.jayway.jsonpath.Configuration;
 import com.jayway.jsonpath.JsonPath;
+import org.apache.camel.Exchange;
+import org.apache.camel.InvalidPayloadException;
+import org.apache.camel.WrappedFile;
 
 public class JSonPathEngine {
 
     private final String expression;
     private final JsonPath path;
+    private final Configuration configuration;
 
     public JSonPathEngine(String expression) {
         this.expression = expression;
+        this.configuration = Configuration.defaultConfiguration();
         this.path = JsonPath.compile(expression);
     }
 
-    public Object read(InputStream json) throws IOException {
-        return path.read(json);
+    public Object read(Exchange exchange) throws IOException, InvalidPayloadException {
+        Object json = exchange.getIn().getBody();
+
+        if (json instanceof WrappedFile) {
+            json = ((WrappedFile) json).getFile();
+        }
+
+        // the message body type should use the suitable read method
+        if (configuration.getProvider().isContainer(json)) {
+            return path.read(json);
+        } else if (json instanceof String) {
+            String str = (String) json;
+            return path.read(str);
+        } else if (json instanceof InputStream) {
+            InputStream is = (InputStream) json;
+            return path.read(is);
+        } else if (json instanceof File) {
+            File file = (File) json;
+            return path.read(file);
+        } else if (json instanceof URL) {
+            URL url = (URL) json;
+            return path.read(url);
+        }
+
+        // fallback as input stream
+        InputStream is = exchange.getIn().getMandatoryBody(InputStream.class);
+        return path.read(is);
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/7817d143/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 9776f77..b53ce36 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,8 +16,6 @@
  */
 package org.apache.camel.jsonpath;
 
-import java.io.InputStream;
-
 import org.apache.camel.Exchange;
 import org.apache.camel.Expression;
 import org.apache.camel.ExpressionEvaluationException;
@@ -25,7 +23,6 @@ import org.apache.camel.ExpressionIllegalSyntaxException;
 import org.apache.camel.Predicate;
 import org.apache.camel.support.ExpressionAdapter;
 import org.apache.camel.support.LanguageSupport;
-import org.apache.camel.util.IOHelper;
 
 public class JsonPathLanguage extends LanguageSupport {
 
@@ -47,6 +44,11 @@ public class JsonPathLanguage extends LanguageSupport {
                     throw new ExpressionEvaluationException(this, exchange, e);
                 }
             }
+
+            @Override
+            public String toString() {
+                return "jsonpath[" + predicate + "]";
+            }
         };
     }
 
@@ -68,16 +70,16 @@ public class JsonPathLanguage extends LanguageSupport {
                     throw new ExpressionEvaluationException(this, exchange, e);
                 }
             }
+
+            @Override
+            public String toString() {
+                return "jsonpath[" + expression + "]";
+            }
         };
     }
 
     private Object evaluateJsonPath(Exchange exchange, JSonPathEngine engine) throws Exception
{
-        InputStream is = exchange.getIn().getMandatoryBody(InputStream.class);
-        try {
-            return engine.read(is);
-        } finally {
-            IOHelper.close(is);
-        }
+        return engine.read(exchange);
     }
 
 }


Mime
View raw message