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 2009/09/19 08:53:48 UTC

svn commit: r816861 - in /camel/trunk/components/camel-velocity/src: main/java/org/apache/camel/component/velocity/ test/java/org/apache/camel/component/velocity/

Author: davsclaus
Date: Sat Sep 19 06:53:47 2009
New Revision: 816861

URL: http://svn.apache.org/viewvc?rev=816861&view=rev
Log:
CAMEL-2023: Velocity template can be provided in a header that overrides endpoint configured template resource.

Added:
    camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityTemplateInHeaderTest.java   (with props)
Modified:
    camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityConstants.java
    camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java

Modified: camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityConstants.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityConstants.java?rev=816861&r1=816860&r2=816861&view=diff
==============================================================================
--- camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityConstants.java (original)
+++ camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityConstants.java Sat Sep 19 06:53:47 2009
@@ -25,6 +25,8 @@
 
     public static final String VELOCITY_RESOURCE_URI = "CamelVelocityResourceUri";
 
+    public static final String VELOCITY_TEMPLATE = "CamelVelocityTemplate";
+
     private VelocityConstants() {
         // Utility class
     }

Modified: camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java?rev=816861&r1=816860&r2=816861&view=diff
==============================================================================
--- camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java (original)
+++ camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java Sat Sep 19 06:53:47 2009
@@ -18,6 +18,7 @@
 
 import java.io.InputStreamReader;
 import java.io.Reader;
+import java.io.StringReader;
 import java.io.StringWriter;
 import java.util.Map;
 
@@ -127,15 +128,28 @@
             return;
         }
 
-        Resource resource = getResource();
-        ObjectHelper.notNull(resource, "resource");
-
-        if (log.isDebugEnabled()) {
-            log.debug("Using resource: " + resource + " with resourceUri: " + path + " for endpoint " + getEndpointUri());
+        Resource resource = null;
+        Reader reader;
+        String content = exchange.getIn().getHeader(VelocityConstants.VELOCITY_TEMPLATE, String.class);
+        if (content != null) {
+            // use content from header
+            reader = new StringReader(content);
+            if (log.isDebugEnabled()) {
+                log.debug("Velocity content read from header " + VelocityConstants.VELOCITY_TEMPLATE + " for endpoint " + getEndpointUri());
+            }
+            // remove the header to avoid it being propagated in the routing
+            exchange.getIn().removeHeader(VelocityConstants.VELOCITY_TEMPLATE);
+        } else {
+            // use resource from endpoint configuration
+            resource = getResource();
+            ObjectHelper.notNull(resource, "resource");
+            if (log.isDebugEnabled()) {
+                log.debug("Velocity content read from resource " + resource + " with resourceUri: " + path + " for endpoint " + getEndpointUri());
+            }
+            reader = encoding != null ? new InputStreamReader(getResourceAsInputStream(), encoding) : new InputStreamReader(getResourceAsInputStream());
         }
 
         // getResourceAsInputStream also considers the content cache
-        Reader reader = encoding != null ? new InputStreamReader(getResourceAsInputStream(), encoding) : new InputStreamReader(getResourceAsInputStream());
         StringWriter buffer = new StringWriter();
         String logTag = getClass().getName();
         Map variableMap = ExchangeHelper.createVariableMap(exchange);
@@ -151,8 +165,10 @@
         // now lets output the results to the exchange
         Message out = exchange.getOut();
         out.setBody(buffer.toString());
-        out.setHeader(VelocityConstants.VELOCITY_RESOURCE, resource);
-        out.setHeader(VelocityConstants.VELOCITY_RESOURCE_URI, path);
+        if (resource != null) {
+            out.setHeader(VelocityConstants.VELOCITY_RESOURCE, resource);
+            out.setHeader(VelocityConstants.VELOCITY_RESOURCE_URI, path);
+        }
         Map<String, Object> headers = (Map<String, Object>) velocityContext.get("headers");
         for (String key : headers.keySet()) {
             out.setHeader(key, headers.get(key));

Added: camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityTemplateInHeaderTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityTemplateInHeaderTest.java?rev=816861&view=auto
==============================================================================
--- camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityTemplateInHeaderTest.java (added)
+++ camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityTemplateInHeaderTest.java Sat Sep 19 06:53:47 2009
@@ -0,0 +1,64 @@
+/**
+ * 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 org.apache.camel.Exchange;
+import org.apache.camel.InvalidPayloadException;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+/**
+ * @version $Revision$
+ */
+public class VelocityTemplateInHeaderTest extends CamelTestSupport {
+
+    @Test
+    public void testReceivesFooResponse() throws Exception {
+        assertRespondsWith("foo", "<hello>foo</hello>");
+    }
+
+    @Test
+    public void testReceivesBarResponse() throws Exception {
+        assertRespondsWith("bar", "<hello>bar</hello>");
+    }
+
+    protected void assertRespondsWith(final String value, String expectedBody) throws InvalidPayloadException {
+        Exchange response = template.request("direct:a", new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                Message in = exchange.getIn();
+                in.setHeader(VelocityConstants.VELOCITY_TEMPLATE, "<hello>${headers.cheese}</hello>");
+                in.setHeader("cheese", value);
+            }
+        });
+        assertOutMessageBodyEquals(response, expectedBody);
+
+        Object template = response.getOut().getHeader(VelocityConstants.VELOCITY_TEMPLATE);
+        assertNull("Template header should have been removed", template);
+    }
+
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:a").to("velocity://dummy");
+            }
+        };
+    }
+
+}

Propchange: camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityTemplateInHeaderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityTemplateInHeaderTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date