String Template
Since Camel 1.2
Only producer is supported
The String Template component allows you to process a message using a String 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:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-stringtemplate</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency> URI format
string-template:templateName[?options]
Where templateName is the classpath-local URI of the template to invoke; or the complete URL of the remote template.
Configuring Options
Camel components are configured on two separate levels:
-
component level
-
endpoint level
Configuring Component Options
At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.
For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
You can configure components using:
-
the Component DSL.
-
in a configuration file (
application.properties,*.yamlfiles, etc). -
directly in the Java code.
Configuring Endpoint Options
You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.
A good practice when configuring options is to use Property Placeholders.
Property placeholders provide a few benefits:
-
They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.
-
They allow externalizing the configuration from the code.
-
They help the code to become more flexible and reusable.
The following two sections list all the options, firstly for the component followed by the endpoint.
Component Options
The String Template component supports the following options which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
Whether the attributes that the producer adds to the template from the exchange also include exchange, in, request, out, response, exchangeProperty, exchangeProperties and camelContext. If false, the template gets only the body, header, headers, variable, variables and exception attributes. Enabling it lets the template read the properties (getters and public fields) of the whole Exchange and CamelContext. The default for the endpoints of the component. | false | boolean | |
Whether the producer reads the CamelStringTemplateResourceUri, CamelStringTemplateTemplate and CamelStringTemplateVariableMap headers. If false, the producer ignores all three headers. Enable it only when the route sets these headers, not a message sender. The default for the endpoints of the component. | false | boolean | |
Whether the endpoints of the component cache their template, unless the contentCache option of the endpoint says otherwise. The producer then reads the template of resourceUri once and keeps its text, so it does not see changes to the resource until the cache is cleared, for example with the clearContentCache JMX operation of the endpoint. If false, the producer reads the resource on each exchange. The template text is compiled into an ST on each exchange in both cases. | true | boolean | |
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. | true | boolean |
Endpoint Options
The String Template endpoint is configured using URI syntax:
string-template:resourceUri
With the following path and query parameters:
Path Parameters
| Name | Description | Default | Type |
|---|---|---|---|
Required Path to the resource. You can prefix with: classpath, file, http, ref, or bean. classpath, file and http loads the resource using these protocols (classpath is default). ref will lookup the resource in the registry. bean will call a method on a bean to be used as the resource. For bean you can specify the method name after dot, eg bean:myBean.myMethod. | String |
Query Parameters
| Name | Description | Default | Type |
|---|---|---|---|
Sets whether the context map should allow access to all details. By default only the message body and headers can be accessed. This option can be enabled for full access to the current Exchange and CamelContext. Doing so impose a potential security risk as this opens access to the full power of CamelContext API. | false | boolean | |
Whether the producer reads the CamelStringTemplateResourceUri, CamelStringTemplateTemplate and CamelStringTemplateVariableMap headers. If false, the producer ignores all three headers. Enable it only when the route sets these headers, not a message sender. | false | boolean | |
Sets whether to use resource content cache or not. | true | boolean | |
The character that starts an expression in the template, by default the less-than sign. The producer compiles the template with an STGroup that has this start delimiter and the delimiterStop character. Set both, for example to $ and $ to write $body$, when the template text has less-than and greater-than signs of its own, such as XML or HTML. | < | char | |
The character that ends an expression in the template, by default the greater-than sign. Used together with delimiterStart. | > | char | |
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean |
Message Headers
The String Template component supports the following message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
CamelStringTemplateResourceUri (producer) Constant: | The URI of a template that the producer renders in place of the template of resourceUri, in the same form as resourceUri. Only used when allowTemplateFromHeader is true. The producer removes the header and hands the exchange to the string-template endpoint of that URI, with the options of this endpoint URI, which renders the text of a CamelStringTemplateTemplate header instead if one is set. After rendering, the producer sets this header to the resourceUri of the endpoint that rendered the message. | String | |
CamelStringTemplateVariableMap (producer) Constant: | A Map whose entries the producer adds to the template as its attributes, in place of the attributes that it builds from the exchange (body, headers and the other entries). The key of an entry is the name of the attribute, and must not contain a dot. Only used when allowTemplateFromHeader is true. | Map | |
CamelStringTemplateTemplate (producer) Constant: | The text of a template that the producer renders in place of the template of resourceUri, with the delimiters of delimiterStart and delimiterStop. Only used when allowTemplateFromHeader is true. The producer removes the header after reading it. | String |
Message Body
The message body at each side of the String Template component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.
| Side | Body | Description |
|---|---|---|
Producer input |
| The template gets the body as body, as it is: text, or a Map, a List or a POJO to read from; convert bytes or a stream to a String first. The template is the resource, or the CamelStringTemplateTemplate or CamelStringTemplateResourceUri header with allowTemplateFromHeader=true, and never the body. |
Producer output |
| The text the template writes, also when it is JSON or XML: unmarshal it to read its fields. The headers are kept, and the CamelStringTemplateResourceUri header is set to the uri of the resource. |
The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.
Usage
Headers
Camel sets the uri of the template resource in the message header CamelStringTemplateResourceUri.
Attributes from the exchange
Camel adds data of the exchange to the template as StringTemplate attributes. The body, header, headers, variable, variables and exception attributes are always there; the others only when the endpoint has allowContextMapAll=true.
| attribute | value |
|---|---|
| The message body. |
| The message headers. |
| The variables of the exchange. |
| The exception of the exchange, if any. |
| The |
| The exchange properties. |
| The message. |
| The message. |
| The Camel Context. |
Hot reloading
The template resource is by default read only once, as contentCache is true by default, so changes to the resource are not picked up. If you set contentCache=false, Camel reads the resource on each exchange, which makes file and classpath resources (expanded jar) hot-reloadable.
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, and the endpoint has allowTemplateFromHeader=true, then Camel uses this over the endpoint configured resource. This allows you to provide a dynamic template at runtime.
StringTemplate Attributes
You can define the custom context map by setting the message header "CamelStringTemplateVariableMap" just like the below code. The endpoint must have allowTemplateFromHeader=true, and the entries of the map replace the attributes that Camel builds from the exchange.
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("CamelStringTemplateVariableMap", variableMap); Examples
For example, you could use a string template as follows in order to formulate a response to a message:
-
Java
-
XML
-
YAML
from("activemq:My.Queue")
.to("string-template:com/acme/MyResponse.tm"); <route>
<from uri="activemq:My.Queue"/>
<to uri="string-template:com/acme/MyResponse.tm"/>
</route> - route:
from:
uri: activemq:My.Queue
steps:
- to:
uri: string-template:com/acme/MyResponse.tm