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/16 16:46:07 UTC

svn commit: r815812 - in /camel/trunk/components/camel-freemarker/src: main/java/org/apache/camel/component/freemarker/FreemarkerEndpoint.java test/java/org/apache/camel/component/freemarker/FreemarkerHeaderSetTemplateTest.java

Author: davsclaus
Date: Wed Sep 16 14:46:07 2009
New Revision: 815812

URL: http://svn.apache.org/viewvc?rev=815812&view=rev
Log:
CAMEL-2004: template can be set in header. Applied patch with thanks to Stan Lewis.

Added:
    camel/trunk/components/camel-freemarker/src/test/java/org/apache/camel/component/freemarker/FreemarkerHeaderSetTemplateTest.java   (with props)
Modified:
    camel/trunk/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerEndpoint.java

Modified: camel/trunk/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerEndpoint.java?rev=815812&r1=815811&r2=815812&view=diff
==============================================================================
--- camel/trunk/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerEndpoint.java (original)
+++ camel/trunk/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerEndpoint.java Wed Sep 16 14:46:07 2009
@@ -81,6 +81,14 @@
         this.configuration = configuration;
     }
 
+    public FreemarkerEndpoint findOrCreateEndpoint(String uri, String newResourceUri) {
+        String newUri = uri.replace(getResourceUri(), newResourceUri);
+        if (log.isDebugEnabled()) {
+            log.debug("Getting endpoint with URI: " + newUri);
+        }
+        return (FreemarkerEndpoint) getCamelContext().getEndpoint(newUri);
+    }
+
     @Override
     @SuppressWarnings("unchecked")
     protected void onExchange(Exchange exchange) throws Exception {
@@ -88,6 +96,18 @@
         ObjectHelper.notNull(configuration, "configuration");
         ObjectHelper.notNull(path, "resourceUri");
 
+        String newResourceUri = exchange.getIn().getHeader(FreemarkerConstants.FREEMARKER_RESOURCE_URI, String.class);
+        if (newResourceUri != null) {
+            exchange.getIn().removeHeader(FreemarkerConstants.FREEMARKER_RESOURCE_URI);
+
+            if (log.isDebugEnabled()) {
+                log.debug(FreemarkerConstants.FREEMARKER_RESOURCE_URI + " set to " + newResourceUri + " creating new endpoint to handle exchange");
+            }
+            FreemarkerEndpoint newEndpoint = findOrCreateEndpoint(getEndpointUri(), newResourceUri);
+            newEndpoint.onExchange(exchange);
+            return;
+        }
+
         Map variableMap = ExchangeHelper.createVariableMap(exchange);
 
         if (log.isDebugEnabled()) {

Added: camel/trunk/components/camel-freemarker/src/test/java/org/apache/camel/component/freemarker/FreemarkerHeaderSetTemplateTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-freemarker/src/test/java/org/apache/camel/component/freemarker/FreemarkerHeaderSetTemplateTest.java?rev=815812&view=auto
==============================================================================
--- camel/trunk/components/camel-freemarker/src/test/java/org/apache/camel/component/freemarker/FreemarkerHeaderSetTemplateTest.java (added)
+++ camel/trunk/components/camel-freemarker/src/test/java/org/apache/camel/component/freemarker/FreemarkerHeaderSetTemplateTest.java Wed Sep 16 14:46:07 2009
@@ -0,0 +1,63 @@
+/**
+ * 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.freemarker;
+
+import org.apache.camel.Exchange;
+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;
+
+/**
+ * Freemarker unit test
+ */
+public class FreemarkerHeaderSetTemplateTest 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 Exception {
+        Exchange response = template.request("direct:a", new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                Message in = exchange.getIn();
+                in.setBody("answer");
+                in.setHeader("cheese", value);
+            }
+        });
+        assertOutMessageBodyEquals(response, expectedBody);
+    }
+
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                // START SNIPPET: example
+                from("direct:a").
+                        setHeader(FreemarkerConstants.FREEMARKER_RESOURCE_URI).constant("org/apache/camel/component/freemarker/example.ftl").
+                        to("freemarker:dummy");
+                // END SNIPPET: example
+            }
+        };
+    }
+}

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

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