camel-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Claus Ibsen <claus.ib...@gmail.com>
Subject Re: [2/5] git commit: CAMEL-7538 FreemarkerEndpoint should support to set data model from message header
Date Thu, 26 Jun 2014 06:18:06 GMT
Hi

Maybe a better name for the key is MODEL instead of MODLE.


On Thu, Jun 26, 2014 at 8:00 AM,  <ningjiang@apache.org> wrote:
> CAMEL-7538 FreemarkerEndpoint should support to set data model from message header
>
>
> Project: http://git-wip-us.apache.org/repos/asf/camel/repo
> Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/e0c7ac45
> Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/e0c7ac45
> Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/e0c7ac45
>
> Branch: refs/heads/master
> Commit: e0c7ac456ebd19d5d18235dc93821297f46969d9
> Parents: 157ef3a
> Author: Willem Jiang <willem.jiang@gmail.com>
> Authored: Wed Jun 25 23:15:52 2014 +0800
> Committer: Willem Jiang <willem.jiang@gmail.com>
> Committed: Thu Jun 26 12:03:12 2014 +0800
>
> ----------------------------------------------------------------------
>  .../freemarker/FreemarkerConstants.java         |  2 ++
>  .../freemarker/FreemarkerEndpoint.java          | 16 ++++++-----
>  .../component/freemarker/FreemarkerTest.java    | 28 +++++++++++++++++++-
>  3 files changed, 39 insertions(+), 7 deletions(-)
> ----------------------------------------------------------------------
>
>
> http://git-wip-us.apache.org/repos/asf/camel/blob/e0c7ac45/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerConstants.java
> ----------------------------------------------------------------------
> diff --git a/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerConstants.java
b/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerConstants.java
> index a8bf41f..65d41e2 100644
> --- a/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerConstants.java
> +++ b/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerConstants.java
> @@ -24,6 +24,8 @@ public final class FreemarkerConstants {
>      public static final String FREEMARKER_RESOURCE_URI = "CamelFreemarkerResourceUri";
>
>      public static final String FREEMARKER_TEMPLATE = "CamelFreemarkerTemplate";
> +
> +    public static final String FREEMARKER_DATA_MODLE = "CamelFreemarkerDataModle";
>
>      private FreemarkerConstants() {
>          // Utility class
>
> http://git-wip-us.apache.org/repos/asf/camel/blob/e0c7ac45/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerEndpoint.java
> ----------------------------------------------------------------------
> diff --git a/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerEndpoint.java
b/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerEndpoint.java
> index f2c1849..c865b2e 100644
> --- a/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerEndpoint.java
> +++ b/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerEndpoint.java
> @@ -16,13 +16,15 @@
>   */
>  package org.apache.camel.component.freemarker;
>
> +
> +
>  import java.io.Reader;
>  import java.io.StringReader;
>  import java.io.StringWriter;
> -import java.util.Map;
>
>  import freemarker.template.Configuration;
>  import freemarker.template.Template;
> +
>  import org.apache.camel.Component;
>  import org.apache.camel.Exchange;
>  import org.apache.camel.ExchangePattern;
> @@ -118,16 +120,18 @@ public class FreemarkerEndpoint extends ResourceEndpoint {
>              // remove the header to avoid it being propagated in the routing
>              exchange.getIn().removeHeader(FreemarkerConstants.FREEMARKER_TEMPLATE);
>          }
> -
> -        Map<String, Object> variableMap = ExchangeHelper.createVariableMap(exchange);
> +        Object dataModle = exchange.getIn().getHeader(FreemarkerConstants.FREEMARKER_DATA_MODLE,
Object.class);
> +        if (dataModle == null) {
> +            dataModle = ExchangeHelper.createVariableMap(exchange);
> +        }
>          // let freemarker parse and generate the result in buffer
>          Template template;
>
>          if (reader != null) {
> -            log.debug("Freemarker is evaluating template read from header {} using context:
{}", FreemarkerConstants.FREEMARKER_TEMPLATE, variableMap);
> +            log.debug("Freemarker is evaluating template read from header {} using context:
{}", FreemarkerConstants.FREEMARKER_TEMPLATE, dataModle);
>              template = new Template("temp", reader, new Configuration());
>          } else {
> -            log.debug("Freemarker is evaluating {} using context: {}", path, variableMap);
> +            log.debug("Freemarker is evaluating {} using context: {}", path, dataModle);
>              if (getEncoding() != null) {
>                  template = configuration.getTemplate(path, getEncoding());
>              } else {
> @@ -135,7 +139,7 @@ public class FreemarkerEndpoint extends ResourceEndpoint {
>              }
>          }
>          StringWriter buffer = new StringWriter();
> -        template.process(variableMap, buffer);
> +        template.process(dataModle, buffer);
>          buffer.flush();
>
>          // now lets output the results to the exchange
>
> http://git-wip-us.apache.org/repos/asf/camel/blob/e0c7ac45/components/camel-freemarker/src/test/java/org/apache/camel/component/freemarker/FreemarkerTest.java
> ----------------------------------------------------------------------
> diff --git a/components/camel-freemarker/src/test/java/org/apache/camel/component/freemarker/FreemarkerTest.java
b/components/camel-freemarker/src/test/java/org/apache/camel/component/freemarker/FreemarkerTest.java
> index 3ebea65..bce8b6b 100644
> --- a/components/camel-freemarker/src/test/java/org/apache/camel/component/freemarker/FreemarkerTest.java
> +++ b/components/camel-freemarker/src/test/java/org/apache/camel/component/freemarker/FreemarkerTest.java
> @@ -16,6 +16,9 @@
>   */
>  package org.apache.camel.component.freemarker;
>
> +import java.util.HashMap;
> +import java.util.Map;
> +
>  import javax.activation.DataHandler;
>
>  import org.apache.camel.Exchange;
> @@ -30,7 +33,7 @@ import org.junit.Test;
>  public class FreemarkerTest extends CamelTestSupport {
>
>      @Test
> -    public void testVelocityLetter() throws Exception {
> +    public void testFreemarkerLetter() throws Exception {
>          final DataHandler dataHandler = new DataHandler("my attachment", "text/plain");
>          Exchange exchange = template.request("direct:a", new Processor() {
>              @Override
> @@ -46,7 +49,30 @@ public class FreemarkerTest extends CamelTestSupport {
>          assertEquals("Christian", exchange.getOut().getHeader("name"));
>          assertSame(dataHandler, exchange.getOut().getAttachment("item"));
>      }
> +
> +
> +    @Test
> +    public void testFreemarkerDataModel() throws Exception {
> +        Exchange exchange = template.request("direct:a", new Processor() {
> +            @Override
> +            public void process(Exchange exchange) throws Exception {
> +                exchange.getIn().setBody("");
> +                exchange.getIn().setHeader("name", "Christian");
> +                Map<String, Object> variableMap = new HashMap<String, Object>();
> +                Map<String, Object> headersMap = new HashMap<String, Object>();
> +                headersMap.put("name", "Willem");
> +                variableMap.put("headers", headersMap);
> +                variableMap.put("body", "Monday");
> +                variableMap.put("exchange", exchange);
> +                exchange.getIn().setHeader(FreemarkerConstants.FREEMARKER_DATA_MODLE,
variableMap);
> +                exchange.setProperty("item", "7");
> +            }
> +        });
>
> +        assertEquals("Dear Willem. You ordered item 7 on Monday.", exchange.getOut().getBody());
> +        assertEquals("Christian", exchange.getOut().getHeader("name"));
> +    }
> +
>      protected RouteBuilder createRouteBuilder() {
>          return new RouteBuilder() {
>              public void configure() {
>



-- 
Claus Ibsen
-----------------
Red Hat, Inc.
Email: cibsen@redhat.com
Twitter: davsclaus
Blog: http://davsclaus.com
Author of Camel in Action: http://www.manning.com/ibsen
hawtio: http://hawt.io/
fabric8: http://fabric8.io/

Mime
View raw message