You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2015/07/29 09:15:45 UTC

[1/2] camel git commit: add VELOCITY_SUPPLEMENTAL_CONTEXT header

Repository: camel
Updated Branches:
  refs/heads/master d49b9c2f2 -> b671ac5d7


add VELOCITY_SUPPLEMENTAL_CONTEXT header

Add a header to specify a Map of supplementary variables to the
default velocity context entries.  Entries in this map will override
the default pre-defined values when using the same key, e.g. "body".

This provides a simpler way to pre-defined variables than providing
a custom context that completely replaces the default, while still
allowing values to be "returned" from the template via headers.


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

Branch: refs/heads/master
Commit: 6b0b4f1c50d82ba22b1f49df60235a6dd89e4693
Parents: d49b9c2
Author: Chris Pimlott <ch...@spartansoftwareinc.com>
Authored: Tue Jul 28 17:54:00 2015 -0700
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed Jul 29 09:18:02 2015 +0200

----------------------------------------------------------------------
 .../component/velocity/VelocityConstants.java   |  4 +-
 .../component/velocity/VelocityEndpoint.java    | 11 +++-
 .../VelocitySupplementalContextTest.java        | 55 ++++++++++++++++++++
 3 files changed, 67 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/6b0b4f1c/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityConstants.java
----------------------------------------------------------------------
diff --git a/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityConstants.java b/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityConstants.java
index 0590cb2..4994008 100644
--- a/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityConstants.java
+++ b/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityConstants.java
@@ -24,9 +24,11 @@ public final class VelocityConstants {
     public static final String VELOCITY_RESOURCE_URI = "CamelVelocityResourceUri";
 
     public static final String VELOCITY_TEMPLATE = "CamelVelocityTemplate";
-    
+
     public static final String VELOCITY_CONTEXT = "CamelVelocityContext";
 
+    public static final String VELOCITY_SUPPLEMENTAL_CONTEXT = "CamelVelocitySupplementalContext";
+
     private VelocityConstants() {
         // Utility class
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/6b0b4f1c/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java b/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java
index 82d0c65..d36da1f 100644
--- a/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java
+++ b/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java
@@ -42,7 +42,7 @@ import org.apache.velocity.runtime.log.CommonsLogLogChute;
 
 @UriEndpoint(scheme = "velocity", title = "Velocity", syntax = "velocity:resourceUri", producerOnly = true, label = "transformation")
 public class VelocityEndpoint extends ResourceEndpoint {
-    
+
     private VelocityEngine velocityEngine;
 
     @UriParam(defaultValue = "true")
@@ -102,7 +102,7 @@ public class VelocityEndpoint extends ResourceEndpoint {
             }
 
             log.debug("Initializing VelocityEngine with properties {}", properties);
-            // help the velocityEngine to load the CamelVelocityClasspathResourceLoader 
+            // help the velocityEngine to load the CamelVelocityClasspathResourceLoader
             ClassLoader old = Thread.currentThread().getContextClassLoader();
             try {
                 ClassLoader delegate = new CamelVelocityDelegateClassLoader(old);
@@ -196,6 +196,13 @@ public class VelocityEndpoint extends ResourceEndpoint {
         Context velocityContext = exchange.getIn().getHeader(VelocityConstants.VELOCITY_CONTEXT, Context.class);
         if (velocityContext == null) {
             Map<String, Object> variableMap = ExchangeHelper.createVariableMap(exchange);
+
+            @SuppressWarnings("unchecked")
+            Map<String, Object> supplementalMap = exchange.getIn().getHeader(VelocityConstants.VELOCITY_SUPPLEMENTAL_CONTEXT, Map.class);
+            if (supplementalMap != null) {
+                variableMap.putAll(supplementalMap);
+            }
+
             velocityContext = new VelocityContext(variableMap);
         }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/6b0b4f1c/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocitySupplementalContextTest.java
----------------------------------------------------------------------
diff --git a/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocitySupplementalContextTest.java b/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocitySupplementalContextTest.java
new file mode 100644
index 0000000..db7fdb5
--- /dev/null
+++ b/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocitySupplementalContextTest.java
@@ -0,0 +1,55 @@
+package org.apache.camel.component.velocity;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Produce;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class VelocitySupplementalContextTest extends CamelTestSupport {
+
+    @Produce(uri = "direct:input")
+    protected ProducerTemplate inputEndpoint;
+
+    @EndpointInject(uri = "mock:results")
+    protected MockEndpoint outputEndpoint;
+
+    @Test
+    public void testCamelRoute() throws Exception {
+        outputEndpoint.expectedMessageCount(1);
+        outputEndpoint.expectedHeaderReceived("body", "new_body");
+        outputEndpoint.expectedHeaderReceived("in.body", "old_body");
+        outputEndpoint.expectedBodiesReceived("bar");
+
+        Map<String, Object> headers = new HashMap<String, Object>();
+        headers.put(VelocityConstants.VELOCITY_TEMPLATE,
+                "#set( $headers.body = ${body} )\n" +
+                "#set( $headers['in.body'] = $in.body )\n" + "bar");
+        inputEndpoint.sendBodyAndHeaders("old_body", headers);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+
+        final Map<String, Object> supplementalContext = new HashMap<>();
+        supplementalContext.put("body", "new_body");
+
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:input")
+                .setHeader(VelocityConstants.VELOCITY_SUPPLEMENTAL_CONTEXT).constant(supplementalContext)
+                .to("velocity:template-in-header")
+                .to("mock:results");
+            }
+        };
+    }
+
+}


[2/2] camel git commit: Fixed CS

Posted by da...@apache.org.
Fixed CS


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

Branch: refs/heads/master
Commit: b671ac5d79c7db7ebdf05886541825435369534a
Parents: 6b0b4f1
Author: Claus Ibsen <da...@apache.org>
Authored: Wed Jul 29 09:22:52 2015 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed Jul 29 09:22:52 2015 +0200

----------------------------------------------------------------------
 .../VelocitySupplementalContextTest.java        | 25 ++++++++++++++++----
 1 file changed, 20 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/b671ac5d/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocitySupplementalContextTest.java
----------------------------------------------------------------------
diff --git a/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocitySupplementalContextTest.java b/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocitySupplementalContextTest.java
index db7fdb5..22ae92f 100644
--- a/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocitySupplementalContextTest.java
+++ b/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocitySupplementalContextTest.java
@@ -1,3 +1,19 @@
+/**
+ * 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.component.velocity;
 
 import java.util.HashMap;
@@ -28,8 +44,7 @@ public class VelocitySupplementalContextTest extends CamelTestSupport {
 
         Map<String, Object> headers = new HashMap<String, Object>();
         headers.put(VelocityConstants.VELOCITY_TEMPLATE,
-                "#set( $headers.body = ${body} )\n" +
-                "#set( $headers['in.body'] = $in.body )\n" + "bar");
+                "#set( $headers.body = ${body} )\n#set( $headers['in.body'] = $in.body )\n" + "bar");
         inputEndpoint.sendBodyAndHeaders("old_body", headers);
 
         assertMockEndpointsSatisfied();
@@ -45,9 +60,9 @@ public class VelocitySupplementalContextTest extends CamelTestSupport {
             @Override
             public void configure() throws Exception {
                 from("direct:input")
-                .setHeader(VelocityConstants.VELOCITY_SUPPLEMENTAL_CONTEXT).constant(supplementalContext)
-                .to("velocity:template-in-header")
-                .to("mock:results");
+                    .setHeader(VelocityConstants.VELOCITY_SUPPLEMENTAL_CONTEXT).constant(supplementalContext)
+                    .to("velocity:template-in-header")
+                    .to("mock:results");
             }
         };
     }