camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [2/2] git commit: CAMEL-7859: Language component - Add support for binary content
Date Wed, 24 Sep 2014 10:17:34 GMT
CAMEL-7859: Language component - Add support for binary content


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

Branch: refs/heads/camel-2.14.x
Commit: 49f7a4d3e4a9d215274068ca3ddd6be1008a0e29
Parents: 3eb7ce3
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Wed Sep 24 12:16:45 2014 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Wed Sep 24 12:17:21 2014 +0200

----------------------------------------------------------------------
 .../component/language/LanguageEndpoint.java    |  17 +++++
 .../component/language/LanguageProducer.java    |  64 ++++++++++---------
 .../ConstantLanguageBinaryResourceTest.java     |  45 +++++++++++++
 .../test/resources/org/apache/camel/logo.jpeg   | Bin 0 -> 10249 bytes
 4 files changed, 97 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/49f7a4d3/camel-core/src/main/java/org/apache/camel/component/language/LanguageEndpoint.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/language/LanguageEndpoint.java
b/camel-core/src/main/java/org/apache/camel/component/language/LanguageEndpoint.java
index 3bd9443..71261ff 100644
--- a/camel-core/src/main/java/org/apache/camel/component/language/LanguageEndpoint.java
+++ b/camel-core/src/main/java/org/apache/camel/component/language/LanguageEndpoint.java
@@ -51,6 +51,8 @@ public class LanguageEndpoint extends ResourceEndpoint {
     @UriParam
     private boolean transform = true;
     @UriParam
+    private boolean binary;
+    @UriParam
     private boolean contentResolvedFromResource;
     @UriParam
     private boolean cacheScript;
@@ -153,6 +155,21 @@ public class LanguageEndpoint extends ResourceEndpoint {
         this.transform = transform;
     }
 
+    public boolean isBinary() {
+        return binary;
+    }
+
+    /**
+     * Whether the script is binary content or text content.
+     * <p/>
+     * By default the script is read as text content (eg <tt>java.lang.String</tt>)
+     *
+     * @param binary <tt>true</tt> to read the script as binary, instead of text
based.
+     */
+    public void setBinary(boolean binary) {
+        this.binary = binary;
+    }
+
     /**
      * Sets the name of the language to use
      *

http://git-wip-us.apache.org/repos/asf/camel/blob/49f7a4d3/camel-core/src/main/java/org/apache/camel/component/language/LanguageProducer.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/language/LanguageProducer.java
b/camel-core/src/main/java/org/apache/camel/component/language/LanguageProducer.java
index 2e6a10e..6a59314 100644
--- a/camel-core/src/main/java/org/apache/camel/component/language/LanguageProducer.java
+++ b/camel-core/src/main/java/org/apache/camel/component/language/LanguageProducer.java
@@ -39,10 +39,12 @@ public class LanguageProducer extends DefaultProducer {
     }
 
     public void process(Exchange exchange) throws Exception {
+        String script = null;
+
         // is there a custom expression in the header?
         Expression exp = exchange.getIn().getHeader(Exchange.LANGUAGE_SCRIPT, Expression.class);
         if (exp == null) {
-            String script = exchange.getIn().getHeader(Exchange.LANGUAGE_SCRIPT, String.class);
+            script = exchange.getIn().getHeader(Exchange.LANGUAGE_SCRIPT, String.class);
             if (script != null) {
                 // the script may be a file: so resolve it before using
                 script = getEndpoint().resolveScript(script);
@@ -54,59 +56,63 @@ public class LanguageProducer extends DefaultProducer {
             exp = getEndpoint().getExpression();
         }
 
+        // the script can be a resource from the endpoint,
+        // or refer to a resource itself
+        // or just be a plain string
+        InputStream is = null;
+
         // fallback and use resource uri from endpoint
         if (exp == null) {
-            String script = getEndpoint().getScript();
+            script = getEndpoint().getScript();
 
             if (script == null && getEndpoint().getResourceUri() == null) {
                 // no script to execute
                 throw new CamelExchangeException("No script to evaluate", exchange);
             }
 
-            // the script can be a resource from the endpoint,
-            // or refer to a resource itself
-            // or just be a plain string
-            InputStream is = null;
             if (script == null) {
                 is = getEndpoint().getResourceAsInputStream();
             } else if (ResourceHelper.hasScheme(script)) {
                 is = ResourceHelper.resolveMandatoryResourceAsInputStream(getEndpoint().getCamelContext().getClassResolver(),
script);
             }
-            if (is != null) {
+
+            if (is != null && !getEndpoint().isBinary()) {
                 try {
                     script = getEndpoint().getCamelContext().getTypeConverter().convertTo(String.class,
exchange, is);
                 } finally {
                     IOHelper.close(is);
                 }
             }
+        }
 
-            if (script != null) {
-                // create the expression from the script
-                exp = getEndpoint().getLanguage().createExpression(script);
-                // expression was resolved from resource
-                getEndpoint().setContentResolvedFromResource(true);
-                // if we cache then set this as expression on endpoint so we don't re-create
it again
-                if (getEndpoint().isCacheScript()) {
-                    getEndpoint().setExpression(exp);
-                }
-            } else {
-                // no script to execute
-                throw new CamelExchangeException("No script to evaluate", exchange);
+        // if we have a text based script then use and evaluate it
+        if (script != null) {
+            // create the expression from the script
+            exp = getEndpoint().getLanguage().createExpression(script);
+            // expression was resolved from resource
+            getEndpoint().setContentResolvedFromResource(true);
+            // if we cache then set this as expression on endpoint so we don't re-create
it again
+            if (getEndpoint().isCacheScript()) {
+                getEndpoint().setExpression(exp);
             }
         }
 
-        ObjectHelper.notNull(exp, "expression");
-
+        // the result is either the result of the expression or the input stream as-is because
its binary content
         Object result;
-        try {
-            result = exp.evaluate(exchange, Object.class);
-            log.debug("Evaluated expression as: {} with: {}", result, exchange);
-        } finally {
-            if (!getEndpoint().isCacheScript()) {
-                // some languages add themselves as a service which we then need to remove
if we are not cached
-                ServiceHelper.stopService(exp);
-                getEndpoint().getCamelContext().removeService(exp);
+        if (exp != null) {
+            try {
+                result = exp.evaluate(exchange, Object.class);
+                log.debug("Evaluated expression as: {} with: {}", result, exchange);
+            } finally {
+                if (!getEndpoint().isCacheScript()) {
+                    // some languages add themselves as a service which we then need to remove
if we are not cached
+                    ServiceHelper.stopService(exp);
+                    getEndpoint().getCamelContext().removeService(exp);
+                }
             }
+        } else {
+            // use the result as-is
+            result = is;
         }
 
         // set message body if transform is enabled

http://git-wip-us.apache.org/repos/asf/camel/blob/49f7a4d3/camel-core/src/test/java/org/apache/camel/language/ConstantLanguageBinaryResourceTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/language/ConstantLanguageBinaryResourceTest.java
b/camel-core/src/test/java/org/apache/camel/language/ConstantLanguageBinaryResourceTest.java
new file mode 100644
index 0000000..29f3922
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/language/ConstantLanguageBinaryResourceTest.java
@@ -0,0 +1,45 @@
+/**
+ * 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.language;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+
+public class ConstantLanguageBinaryResourceTest extends ContextTestSupport {
+
+    public void testConstantBinary() throws Exception {
+        byte[] data = template.requestBody("direct:start", "", byte[].class);
+        // should have X number of bytes
+        assertNotNull(data);
+        assertEquals(10249, data.length);
+
+        // store the logo
+        template.sendBodyAndHeader("file:target", data, Exchange.FILE_NAME, "savedlogo.jpeg");
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .to("language:constant:resource:classpath:org/apache/camel/logo.jpeg?binary=true");
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/49f7a4d3/camel-core/src/test/resources/org/apache/camel/logo.jpeg
----------------------------------------------------------------------
diff --git a/camel-core/src/test/resources/org/apache/camel/logo.jpeg b/camel-core/src/test/resources/org/apache/camel/logo.jpeg
new file mode 100644
index 0000000..b635017
Binary files /dev/null and b/camel-core/src/test/resources/org/apache/camel/logo.jpeg differ


Mime
View raw message