From bddf3d1f3f821e7dda81675ff602cf0b935925fc Mon Sep 17 00:00:00 2001 From: Gerald Quintana Date: Wed, 19 Jun 2013 22:06:17 +0100 Subject: [PATCH] Initial Mustache component commit --- components/camel-mustache/ReadMe.wiki | 96 +++++++++ components/camel-mustache/pom.xml | 64 ++++++ .../component/mustache/MustacheComponent.java | 70 ++++++ .../component/mustache/MustacheConstants.java | 39 ++++ .../component/mustache/MustacheEndpoint.java | 180 ++++++++++++++++ .../src/main/resources/LICENSE.txt | 203 ++++++++++++++++++ .../org/apache/camel/component/mustache | 1 + .../src/main/resources/NOTICE.txt | 11 + .../mustache/MustacheComponentTest.java | 147 +++++++++++++ .../mustache/MustacheLetterTest.java | 66 ++++++ .../src/test/resources/another.mustache | 4 + .../src/test/resources/child.mustache | 4 + .../src/test/resources/included.mustache | 1 + .../src/test/resources/includer.mustache | 3 + .../src/test/resources/letter.mustache | 6 + .../src/test/resources/log4j.properties | 36 ++++ .../src/test/resources/parent.mustache | 5 + .../src/test/resources/simple.mustache | 1 + components/pom.xml | 1 + 19 files changed, 938 insertions(+) create mode 100644 components/camel-mustache/ReadMe.wiki create mode 100644 components/camel-mustache/pom.xml create mode 100644 components/camel-mustache/src/main/java/org/apache/camel/component/mustache/MustacheComponent.java create mode 100644 components/camel-mustache/src/main/java/org/apache/camel/component/mustache/MustacheConstants.java create mode 100644 components/camel-mustache/src/main/java/org/apache/camel/component/mustache/MustacheEndpoint.java create mode 100644 components/camel-mustache/src/main/resources/LICENSE.txt create mode 100644 components/camel-mustache/src/main/resources/META-INF/services/org/apache/camel/component/mustache create mode 100644 components/camel-mustache/src/main/resources/NOTICE.txt create mode 100644 components/camel-mustache/src/test/java/org/apache/camel/component/mustache/MustacheComponentTest.java create mode 100644 components/camel-mustache/src/test/java/org/apache/camel/component/mustache/MustacheLetterTest.java create mode 100644 components/camel-mustache/src/test/resources/another.mustache create mode 100644 components/camel-mustache/src/test/resources/child.mustache create mode 100644 components/camel-mustache/src/test/resources/included.mustache create mode 100644 components/camel-mustache/src/test/resources/includer.mustache create mode 100644 components/camel-mustache/src/test/resources/letter.mustache create mode 100644 components/camel-mustache/src/test/resources/log4j.properties create mode 100644 components/camel-mustache/src/test/resources/parent.mustache create mode 100644 components/camel-mustache/src/test/resources/simple.mustache diff --git a/components/camel-mustache/ReadMe.wiki b/components/camel-mustache/ReadMe.wiki new file mode 100644 index 0000000000000..0642e9bc5b391 --- /dev/null +++ b/components/camel-mustache/ReadMe.wiki @@ -0,0 +1,96 @@ +h2. Mustache + +The *mustache:* component allows for processing a message using a [Mustache|http://mustache.github.io/] template. This can be ideal when using [Templating] to generate responses for requests. + +Maven users will need to add the following dependency to their {{pom.xml}} for this component: +{code:xml} + +org.apache.camel +camel-mustache +x.x.x + +{code} + +h3. URI format + +{code} +mustache:templateName[?options] +{code} + +Where *templateName* is the classpath-local URI of the template to invoke; or the complete URL of the remote template (eg: file://folder/myfile.mustache). + +You can append query options to the URI in the following format, {{?option=value&option=value&...}} + +h3. Options +{div:class=confluenceTableSmall} +|| Option || Default || Description || +| {{encoding}} | {{null}} | Character encoding of the resource content. | +| {{startDelimiter}} | {{ | Characters used to mark template code beginning. | +| {{endDelimiter}} | }} | Characters used to mark template code end. | +{div} + +h3. Mustache Context +Camel will provide exchange information in the Mustache context (just a {{Map}}). The {{Exchange}} is transferred as: +{div:class=confluenceTableSmall} +|| key || value || +| {{exchange}} | The {{Exchange}} itself. | +| {{exchange.properties}} | The {{Exchange}} properties. | +| {{headers}} | The headers of the In message. | +| {{camelContext}} | The Camel Context. | +| {{request}} | The In message. | +| {{body}} | The In message body. | +| {{response}} | The Out message (only for InOut message exchange pattern). | +{div} + +h3. Dynamic templates + +Camel provides two headers by which you can define a different resource location for a template or the template content itself. If any of these headers is set then Camel uses this over the endpoint configured resource. This allows you to provide a dynamic template at runtime. +{div:class=confluenceTableSmall} +|| Header || Type || Description || Support Version || +| MustacheConstants.MUSTACHE_RESOURCE | org.springframework.core.io.Resource | The template resource | | +| MustacheConstants.MUSTACHE_RESOURCE_URI | String | A URI for the template resource to use instead of the endpoint configured. | | +| MustacheConstants.MUSTACHE_TEMPLATE | String | The template to use instead of the endpoint configured. | | +{div} + +h3. Samples + +For example you could use something like: + +{code} +from("activemq:My.Queue"). +to("mustache:com/acme/MyResponse.mustache"); +{code} + +To use a Mustache template to formulate a response for a message for InOut message exchanges (where there is a {{JMSReplyTo}} header). + +If you want to use InOnly and consume the message and send it to another destination you could use: + +{code} +from("activemq:My.Queue"). +to("mustache:com/acme/MyResponse.mustache"). +to("activemq:Another.Queue"); +{code} + +It's possible to specify what template the component should use dynamically via a header, so for example: +{code} +from("direct:in"). +setHeader(MustacheConstants.MUSTACHE_RESOURCE_URI).constant("path/to/my/template.mustache"). +to("mustache:dummy"); +{code} + +h3. The Email Sample +In this sample we want to use Mustache templating for an order confirmation email. The email template is laid out in Mustache as: +{code} +Dear {{headers.lastName}}}, {{headers.firstName}} + +Thanks for the order of {{headers.item}}. + +Regards Camel Riders Bookstore +{{body}} +{code} + +And the java code: + +{snippet:id=e1|lang=java|url=camel/trunk/components/camel-mustache/src/test/java/org/apache/camel/component/mustache/MustacheLetterTest.java} + +{include:Endpoint See Also} \ No newline at end of file diff --git a/components/camel-mustache/pom.xml b/components/camel-mustache/pom.xml new file mode 100644 index 0000000000000..f6be585b9a78d --- /dev/null +++ b/components/camel-mustache/pom.xml @@ -0,0 +1,64 @@ + + + + 4.0.0 + + + org.apache.camel + components + 2.12-SNAPSHOT + + camel-mustache + bundle + Camel :: Mustache + Camel Mustache support + + org.apache.camel.component.mustache.* + org.apache.camel.spi.ComponentResolver;component=mustache + + + + + + org.apache.camel + camel-core + + + com.github.spullara.mustache.java + compiler + 0.8.12 + + + + + org.apache.camel + camel-test + test + + + org.slf4j + slf4j-log4j12 + test + + + junit + junit + test + + + diff --git a/components/camel-mustache/src/main/java/org/apache/camel/component/mustache/MustacheComponent.java b/components/camel-mustache/src/main/java/org/apache/camel/component/mustache/MustacheComponent.java new file mode 100644 index 0000000000000..8777b383158ae --- /dev/null +++ b/components/camel-mustache/src/main/java/org/apache/camel/component/mustache/MustacheComponent.java @@ -0,0 +1,70 @@ +/** + * 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.mustache; + +import com.github.mustachejava.DefaultMustacheFactory; +import com.github.mustachejava.MustacheFactory; +import java.util.Map; + +import org.apache.camel.Endpoint; +import org.apache.camel.impl.DefaultComponent; +import org.apache.camel.util.ObjectHelper; + +/** + * Represents the component that manages {@link MustacheEndpoint}. + * URI pattern: {@code mustache://template_name.mustache} + * Supports parameters: + *