Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions components/camel-mustache/ReadMe.wiki
Original file line number Diff line number Diff line change
@@ -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}
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-mustache</artifactId>
<version>x.x.x</version> <!-- use the same version as your Camel core version -->
</dependency>
{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}
64 changes: 64 additions & 0 deletions components/camel-mustache/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.camel</groupId>
<artifactId>components</artifactId>
<version>2.12-SNAPSHOT</version>
</parent>
<artifactId>camel-mustache</artifactId>
<packaging>bundle</packaging>
<name>Camel :: Mustache</name>
<description>Camel Mustache support</description>
<properties>
<camel.osgi.export.pkg>org.apache.camel.component.mustache.*</camel.osgi.export.pkg>
<camel.osgi.export.service>org.apache.camel.spi.ComponentResolver;component=mustache</camel.osgi.export.service>
</properties>

<dependencies>

<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
</dependency>
<dependency>
<groupId>com.github.spullara.mustache.java</groupId>
<artifactId>compiler</artifactId>
<version>0.8.12</version>
</dependency>

<!-- testing -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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:
* <ul>
* <li>encoding: default platform one </li>
* <li>startDelimiter: default "{{" </li>
* <li>endDelimiter: default "}}" </li>
* </li>
*/
public class MustacheComponent extends DefaultComponent {
/**
* Mustache factory
*/
private MustacheFactory mustacheFactory = new DefaultMustacheFactory();
@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
MustacheEndpoint endpoint = new MustacheEndpoint(uri, this, remaining);
endpoint.setMustacheFactory(getMustacheFactory());
String encoding = getAndRemoveParameter(parameters, "encoding", String.class);
if (ObjectHelper.isNotEmpty(encoding)) {
endpoint.setEncoding(encoding);
}
String startDelimiter = getAndRemoveParameter(parameters, "startDelimiter", String.class);
if (ObjectHelper.isNotEmpty(startDelimiter)) {
endpoint.setStartDelimiter(startDelimiter);
}
String endDelimiter = getAndRemoveParameter(parameters, "endDelimiter", String.class);
if (ObjectHelper.isNotEmpty(endDelimiter)) {
endpoint.setEndDelimiter(endDelimiter);
}
setProperties(endpoint, parameters);
return endpoint;
}

public MustacheFactory getMustacheFactory() {
return mustacheFactory;
}

public void setMustacheFactory(MustacheFactory mustacheFactory) {
this.mustacheFactory = mustacheFactory;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* 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;

/**
* Mustache component constants
*/
public final class MustacheConstants {
/**
* Header containing a Mustache template location
*/
public static final String MUSTACHE_RESOURCE_URI = "MustacheResourceUri";
/**
* Header containing the Mustache template code
*/
public static final String MUSTACHE_TEMPLATE = "MustacheTemplate";
/**
* Mustache endpoint URI prefix
*/
public static final String MUSTACHE_ENDPOINT_URI_PREFIX="mustache:";
private MustacheConstants() {
// Utility class
}

}
Loading