camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject [3/5] git commit: CAMEL-7539 StringTemplateEndpoint should support set variable map from message header
Date Thu, 26 Jun 2014 06:00:59 GMT
CAMEL-7539 StringTemplateEndpoint should support set variable map from message header


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

Branch: refs/heads/master
Commit: 157ef3af08f6cef1e8a9fc3a3fce6bfefc6b4206
Parents: 676d8cb
Author: Willem Jiang <willem.jiang@gmail.com>
Authored: Wed Jun 25 23:15:11 2014 +0800
Committer: Willem Jiang <willem.jiang@gmail.com>
Committed: Thu Jun 26 12:03:12 2014 +0800

----------------------------------------------------------------------
 .../stringtemplate/StringTemplateConstants.java |  1 +
 .../stringtemplate/StringTemplateEndpoint.java  |  7 +++++-
 .../stringtemplate/StringTemplateTest.java      | 25 ++++++++++++++++++++
 3 files changed, 32 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/157ef3af/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateConstants.java
----------------------------------------------------------------------
diff --git a/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateConstants.java
b/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateConstants.java
index a01ce63..a06d90e 100644
--- a/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateConstants.java
+++ b/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateConstants.java
@@ -22,6 +22,7 @@ package org.apache.camel.component.stringtemplate;
 public final class StringTemplateConstants {
 
     public static final String STRINGTEMPLATE_RESOURCE_URI = "CamelStringTemplateResourceUri";
+    public static final String STRINGTEMPLATE_VARIABLE_MAP = "CamelStringTemplateVariableMap";
 
     private StringTemplateConstants() {
         // Utility class

http://git-wip-us.apache.org/repos/asf/camel/blob/157ef3af/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateEndpoint.java
b/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateEndpoint.java
index a5d6837..5e2d356 100644
--- a/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateEndpoint.java
+++ b/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateEndpoint.java
@@ -72,7 +72,12 @@ public class StringTemplateEndpoint extends ResourceEndpoint {
     @Override
     protected void onExchange(Exchange exchange) throws Exception {
         StringWriter buffer = new StringWriter();
-        Map<String, Object> variableMap = ExchangeHelper.createVariableMap(exchange);
+        
+        @SuppressWarnings("unchecked")
+        Map<String, Object> variableMap = exchange.getIn().getHeader(StringTemplateConstants.STRINGTEMPLATE_VARIABLE_MAP,
Map.class);
+        if (variableMap == null) {
+            variableMap = ExchangeHelper.createVariableMap(exchange);
+        }
 
         // getResourceAsInputStream also considers the content cache
         String text = exchange.getContext().getTypeConverter().mandatoryConvertTo(String.class,
getResourceAsInputStream());

http://git-wip-us.apache.org/repos/asf/camel/blob/157ef3af/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateTest.java
----------------------------------------------------------------------
diff --git a/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateTest.java
b/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateTest.java
index 56b983e..acee109 100644
--- a/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateTest.java
+++ b/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateTest.java
@@ -16,6 +16,9 @@
  */
 package org.apache.camel.component.stringtemplate;
 
+import java.util.HashMap;
+import java.util.Map;
+
 import javax.activation.DataHandler;
 
 import org.apache.camel.Exchange;
@@ -44,6 +47,28 @@ public class StringTemplateTest extends CamelTestSupport {
         assertEquals("Christian", response.getOut().getHeader("name"));
         assertSame(dataHandler, response.getOut().getAttachment("item"));
     }
+    
+    @Test
+    public void testVelocityContext() throws Exception {
+        Exchange exchange = template.request("direct:a", new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setBody("");
+                exchange.getIn().setHeader("name", "Christian");
+                Map<String, Object> variableMap = new HashMap<String, Object>();
+                Map<String, Object> headersMap = new HashMap<String, Object>();
+                headersMap.put("name", "Willem");
+                variableMap.put("headers", headersMap);
+                variableMap.put("body", "Monday");
+                variableMap.put("exchange", exchange);
+                exchange.getIn().setHeader(StringTemplateConstants.STRINGTEMPLATE_VARIABLE_MAP,
variableMap);
+                exchange.setProperty("item", "7");
+            }
+        });
+
+        assertEquals("Dear Willem. You ordered item 7 on Monday.", exchange.getOut().getBody());
+        assertEquals("Christian", exchange.getOut().getHeader("name"));
+    }
 
     protected RouteBuilder createRouteBuilder() {
         return new RouteBuilder() {


Mime
View raw message