Return-Path: X-Original-To: apmail-camel-commits-archive@www.apache.org Delivered-To: apmail-camel-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 8AF8F11E2D for ; Wed, 24 Sep 2014 10:17:39 +0000 (UTC) Received: (qmail 8009 invoked by uid 500); 24 Sep 2014 10:17:34 -0000 Delivered-To: apmail-camel-commits-archive@camel.apache.org Received: (qmail 7959 invoked by uid 500); 24 Sep 2014 10:17:34 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 7849 invoked by uid 99); 24 Sep 2014 10:17:34 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 24 Sep 2014 10:17:34 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 13842838D57; Wed, 24 Sep 2014 10:17:33 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: davsclaus@apache.org To: commits@camel.apache.org Date: Wed, 24 Sep 2014 10:17:34 -0000 Message-Id: <0710ebcf37664b75832904fe5fa5d283@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [2/2] git commit: CAMEL-7859: Language component - Add support for binary content 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 Authored: Wed Sep 24 12:16:45 2014 +0200 Committer: Claus Ibsen 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. + *

+ * By default the script is read as text content (eg java.lang.String) + * + * @param binary true 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