camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r814110 - in /camel/trunk/components/camel-velocity/src: main/java/org/apache/camel/component/velocity/VelocityEndpoint.java test/java/org/apache/camel/component/velocity/VelocitySetTemplateViaHeaderTest.java
Date Sat, 12 Sep 2009 06:09:01 GMT
Author: davsclaus
Date: Sat Sep 12 06:09:01 2009
New Revision: 814110

URL: http://svn.apache.org/viewvc?rev=814110&view=rev
Log:
CAMEL-1850: Applied patch with thanks to Stan Lewis. Velocity component now supports overriding
template resource from a message header to dynamic use other templates.

Added:
    camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocitySetTemplateViaHeaderTest.java
  (with props)
Modified:
    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/VelocityEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java?rev=814110&r1=814109&r2=814110&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 12 06:09:01 2009
@@ -101,14 +101,39 @@
         return encoding;
     }
 
+    public VelocityEndpoint findOrCreateEndpoint(String uri, String newResourceUri) {
+        String newUri = uri.replace(getResourceUri(), newResourceUri);
+        if (log.isDebugEnabled()) {
+            log.debug("Getting endpoint with URI: " + newUri);
+        }
+        return (VelocityEndpoint) getCamelContext().getEndpoint(newUri);
+    }
+
     @SuppressWarnings("unchecked")
     @Override
     protected void onExchange(Exchange exchange) throws Exception {
-        Resource resource = getResource();
-        ObjectHelper.notNull(resource, "resource");
         String path = getResourceUri();
         ObjectHelper.notNull(path, "resourceUri");
 
+        String newResourceUri = exchange.getIn().getHeader(VelocityConstants.VELOCITY_RESOURCE_URI,
String.class);
+        if (newResourceUri != null) {
+            exchange.getIn().removeHeader(VelocityConstants.VELOCITY_RESOURCE_URI);
+
+            if (log.isDebugEnabled()) {
+                log.debug(VelocityConstants.VELOCITY_RESOURCE_URI + " set to " + newResourceUri
+ " creating new endpoint to handle exchange");
+            }
+            VelocityEndpoint newEndpoint = findOrCreateEndpoint(getEndpointUri(), newResourceUri);
+            newEndpoint.onExchange(exchange);
+            return;
+        }
+
+        Resource resource = getResource();
+        ObjectHelper.notNull(resource, "resource");
+
+        if (log.isDebugEnabled()) {
+            log.debug("Using resource: " + resource + " with resourceUri: " + path + " for
endpoint " + getEndpointUri());
+        }
+
         // getResourceAsInputStream also considers the content cache
         Reader reader = encoding != null ? new InputStreamReader(getResourceAsInputStream(),
encoding) : new InputStreamReader(getResourceAsInputStream());
         StringWriter buffer = new StringWriter();
@@ -128,7 +153,7 @@
         out.setBody(buffer.toString());
         out.setHeader(VelocityConstants.VELOCITY_RESOURCE, resource);
         out.setHeader(VelocityConstants.VELOCITY_RESOURCE_URI, path);
-        Map<String, Object> headers = (Map<String, Object>)velocityContext.get("headers");
+        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/VelocitySetTemplateViaHeaderTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocitySetTemplateViaHeaderTest.java?rev=814110&view=auto
==============================================================================
--- camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocitySetTemplateViaHeaderTest.java
(added)
+++ camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocitySetTemplateViaHeaderTest.java
Sat Sep 12 06:09:01 2009
@@ -0,0 +1,66 @@
+/**
+ * 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.Message;
+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 VelocitySetTemplateViaHeaderTest extends CamelTestSupport {
+
+    private Exchange createLetter(String template) {
+        Exchange exchange = context.getEndpoint("direct:a").createExchange();
+        Message msg = exchange.getIn();
+        msg.setHeader("firstName", "Claus");
+        msg.setHeader("lastName", "Ibsen");
+        msg.setHeader("item", "Camel in Action");
+        msg.setHeader(VelocityConstants.VELOCITY_RESOURCE_URI, template);
+        msg.setBody("PS: Next beer is on me, James");
+        return exchange;
+    }
+
+    @Test
+    public void testVelocityLetter() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.expectedBodiesReceived("Dear Ibsen, Claus\n\nThanks for the order of Camel in
Action.\n\nRegards Camel Riders Bookstore\nPS: Next beer is on me, James");
+
+        template.send("direct:a", createLetter("org/apache/camel/component/velocity/letter.vm"));
+
+        mock.assertIsSatisfied();
+        
+        mock.reset();
+        
+        mock.expectedMessageCount(1);
+        mock.expectedBodiesReceived("Dear Ibsen, Claus\n\nThanks for the order of Camel in
Action.\n\nRegards Apache Camel Riders Bookstore\nPS: Next beer is on me, James");
+
+        template.send("direct:a", createLetter("org/apache/camel/component/velocity/letter2.vm"));
+
+        mock.assertIsSatisfied();
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from("direct:a").to("velocity:dummy").to("mock:result");
+            }
+        };
+    }
+}

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

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



Mime
View raw message