Author: davsclaus
Date: Tue Sep 22 06:17:29 2009
New Revision: 817510
URL: http://svn.apache.org/viewvc?rev=817510&view=rev
Log:
CAMEL-2028: Applied patch with thanks to Stan Lewis. Freemarker now also
supports using template from header.
Added:
camel/trunk/components/camel-freemarker/src/test/java/org/apache/camel/component/freemarker/FreemarkerTemplateInHeaderTest.java
(with props)
Modified:
camel/trunk/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerComponent.java
camel/trunk/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerConstants.java
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/FreemarkerComponent.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerComponent.java?rev=817510&r1=817509&r2=817510&view=diff
==============================================================================
---
camel/trunk/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerComponent.java
(original)
+++
camel/trunk/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerComponent.java
Tue Sep 22 06:17:29 2009
@@ -29,7 +29,7 @@
*/
public class FreemarkerComponent extends DefaultComponent {
- private Configuration configuraiton;
+ private Configuration configuration;
private Configuration noCacheConfiguration;
protected Endpoint createEndpoint(String uri, String remaining, Map
parameters) throws Exception {
@@ -49,16 +49,16 @@
}
public synchronized Configuration getConfiguraiton() {
- if (configuraiton == null) {
- configuraiton = new Configuration();
+ if (configuration == null) {
+ configuration = new Configuration();
// use class template loader using Spring Resource class and / as
root in classpath
- configuraiton.setTemplateLoader(new
ClassTemplateLoader(Resource.class, "/"));
+ configuration.setTemplateLoader(new
ClassTemplateLoader(Resource.class, "/"));
}
- return (Configuration) configuraiton.clone();
+ return (Configuration) configuration.clone();
}
- public void setConfiguraiton(Configuration configuraiton) {
- this.configuraiton = configuraiton;
+ public void setConfiguraiton(Configuration configuration) {
+ this.configuration = configuration;
}
private synchronized Configuration getNoCacheConfiguration() {
Modified:
camel/trunk/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerConstants.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerConstants.java?rev=817510&r1=817509&r2=817510&view=diff
==============================================================================
---
camel/trunk/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerConstants.java
(original)
+++
camel/trunk/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerConstants.java
Tue Sep 22 06:17:29 2009
@@ -25,6 +25,8 @@
public static final String FREEMARKER_RESOURCE_URI =
"CamelFreemarkerResourceUri";
+ public static final String FREEMARKER_TEMPLATE = "CamelFreemarkerTemplate";
+
private FreemarkerConstants() {
// Utility class
}
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=817510&r1=817509&r2=817510&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
Tue Sep 22 06:17:29 2009
@@ -16,6 +16,8 @@
*/
package org.apache.camel.component.freemarker;
+import java.io.Reader;
+import java.io.StringReader;
import java.io.StringWriter;
import java.util.Map;
@@ -108,18 +110,33 @@
return;
}
- Map variableMap = ExchangeHelper.createVariableMap(exchange);
-
- if (log.isDebugEnabled()) {
- log.debug("Freemarker is evaluating " + path + " using context: "
+ variableMap);
+ Reader reader = null;
+ String content =
exchange.getIn().getHeader(FreemarkerConstants.FREEMARKER_TEMPLATE,
String.class);
+ if (content != null) {
+ // use content from header
+ reader = new StringReader(content);
+ // remove the header to avoid it being propagated in the routing
+
exchange.getIn().removeHeader(FreemarkerConstants.FREEMARKER_TEMPLATE);
}
+ Map variableMap = ExchangeHelper.createVariableMap(exchange);
// let freemarker parse and generate the result in buffer
Template template;
- if (encoding != null) {
- template = configuration.getTemplate(path, encoding);
+
+ if (reader != null) {
+ if (log.isDebugEnabled()) {
+ log.debug("Freemarker is evaluating template read from header
" + FreemarkerConstants.FREEMARKER_TEMPLATE + " using context: " + variableMap);
+ }
+ template = new Template("temp", reader, new Configuration());
} else {
- template = configuration.getTemplate(path);
+ if (log.isDebugEnabled()) {
+ log.debug("Freemarker is evaluating " + path + " using
context: " + variableMap);
+ }
+ if (encoding != null) {
+ template = configuration.getTemplate(path, encoding);
+ } else {
+ template = configuration.getTemplate(path);
+ }
}
StringWriter buffer = new StringWriter();
template.process(variableMap, buffer);
Added:
camel/trunk/components/camel-freemarker/src/test/java/org/apache/camel/component/freemarker/FreemarkerTemplateInHeaderTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-freemarker/src/test/java/org/apache/camel/component/freemarker/FreemarkerTemplateInHeaderTest.java?rev=817510&view=auto
==============================================================================
---
camel/trunk/components/camel-freemarker/src/test/java/org/apache/camel/component/freemarker/FreemarkerTemplateInHeaderTest.java
(added)
+++
camel/trunk/components/camel-freemarker/src/test/java/org/apache/camel/component/freemarker/FreemarkerTemplateInHeaderTest.java
Tue Sep 22 06:17:29 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.freemarker;
+
+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 FreemarkerTemplateInHeaderTest 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(FreemarkerConstants.FREEMARKER_TEMPLATE,
"<hello>${headers.cheese}</hello>");
+ in.setHeader("cheese", value);
+ }
+ });
+ assertOutMessageBodyEquals(response, expectedBody);
+
+ Object template =
response.getOut().getHeader(FreemarkerConstants.FREEMARKER_TEMPLATE);
+ assertNull("Template header should have been removed", template);
+ }
+
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ public void configure() {
+ from("direct:a").to("freemarker://dummy");
+ }
+ };
+ }
+
+}
Propchange:
camel/trunk/components/camel-freemarker/src/test/java/org/apache/camel/component/freemarker/FreemarkerTemplateInHeaderTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
camel/trunk/components/camel-freemarker/src/test/java/org/apache/camel/component/freemarker/FreemarkerTemplateInHeaderTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date