Language
Since Camel 2.5
Only producer is supported
The Language component allows you to send Exchange to an endpoint which executes a script by any of the supported Languages in Camel.
By having a component to execute language scripts, it allows more dynamic routing capabilities. For example, by using the Routing Slip or Dynamic Router EIPs you can send messages to language endpoints where the script is dynamically defined as well.
You only have to include additional Camel components if the language of choice mandates it, such as using Groovy or JavaScript languages.
URI format
language://languageName[:script][?options]
You can refer to an external resource for the script using the same notation as supported by the other Languages in Camel
language://languageName:resource:scheme:location][?options]
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 Language component supports the following options which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
The default of the allowTemplateFromHeader option of the endpoints: whether the script can be given in the CamelLanguageScript message header, which then takes precedence over the script of the endpoint. Enable this only when the header cannot come from an untrusted sender, as the sender can then run any script of the language and load any resource. | false | boolean | |
The default of the contentCache option of the endpoints: whether the content of a resource given with the resource: prefix is read once and kept in memory, instead of being read for every message. | 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 Language endpoint is configured using URI syntax:
language:languageName:resourceUri
With the following path and query parameters:
Path Parameters
| Name | Description | Default | Type |
|---|---|---|---|
Required The name of the language that evaluates the script, such as simple, groovy, jsonpath or xpath. The language is resolved by this name when the endpoint is created, so the JAR of the language (for example camel-groovy) must be on the classpath. Enum values:
| String | ||
The script, given in the URI after the language name, which is URL decoded as UTF-8. With the resource: prefix it is instead the URI of a resource that holds the script, for example resource:classpath:script.txt or resource:file:script.txt (a resource without a scheme is loaded from the classpath). The content of such a resource is read once and kept in memory when contentCache is enabled, and the script option takes precedence over it. Without the prefix, a script that starts with classpath:, file:, http:, https:, ref: or bean: is loaded from that resource, and #name looks it up in the Registry, on every message unless cacheScript is enabled. | 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 script can be given in the CamelLanguageScript message header. The header, when present, takes precedence over the script of the endpoint, is used for that message only, and is removed after use. It holds an org.apache.camel.Expression or the script text; text that starts with resource:, classpath:, file:, http:, https:, ref: or bean: is loaded from that resource, and #name looks it up in the Registry. Enable this only when the header cannot come from an untrusted sender, as the sender can then run any script of the language and load any resource. | false | boolean | |
Sets whether to use resource content cache or not. | true | boolean | |
The fully qualified class name of the type of the result, such as java.lang.String. The language is given this type when it compiles the script, and the result is converted to it. Binary content is not converted. | String | ||
The script to evaluate, as an alternative to giving it in the URI after the language name. It takes precedence over a resource given with the resource: prefix. A script that starts with classpath:, file:, http:, https:, ref: or bean: is loaded from that resource, and #name looks it up in the Registry, on every message unless cacheScript is enabled. | String | ||
Whether the result of the script replaces the message body. When false, the script is still evaluated, but its result is discarded and the message body is unchanged. | 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 the content of the resource is binary data instead of a script. When true, the content is not evaluated, and the result is the content as a java.io.InputStream. Applies only when the script is loaded from a resource (with the resource: prefix, a scheme such as classpath: or file:, or #name). An inline script, or a script in the CamelLanguageScript header, is evaluated as usual. | false | boolean | |
Whether to compile the script once and reuse the compiled expression for every message, instead of compiling it for each message. An inline script, or a script loaded from classpath:, file:, http:, https:, ref: or bean: (which is then loaded only once), is compiled before the first message. A script from a resource with the resource: prefix is compiled at the first message, and is kept only while contentCache is enabled. Reusing the expression can carry state of the language from one exchange to the next. A script in the CamelLanguageScript header is never cached. | false | boolean |
Message Headers
The Language component supports the following message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
CamelLanguageScript (producer) Constant: | The script to evaluate for this message, as the script text or an org.apache.camel.Expression. Read only when allowTemplateFromHeader is enabled. It takes precedence over the script of the endpoint, is never cached as the script of the endpoint (also with cacheScript=true), and the header is removed after use. Text that starts with resource:, classpath:, file:, http:, https:, ref: or bean: is loaded from that resource, and #name looks it up in the Registry. | Expression |
Message Body
The message body at each side of the Language 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 body as the language of the script reads it. The script is in the uri, the script option or a resource, or in the CamelLanguageScript header with allowTemplateFromHeader=true, and never in the body. |
Producer output |
| The result of the script, as the language gives it. Unchanged with transform=false. With binary=true the content of the resource as a stream, which is not evaluated. With
Not known when |
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.
Examples
For example, you can use the Simple language as Message Translator EIP:
-
Java
-
XML
-
YAML
from("direct:hello")
.to("language:simple:Hello ${body}"); <route>
<from uri="direct:hello"/>
<to uri="language:simple:Hello ${body}"/>
</route> - route:
from:
uri: direct:hello
steps:
- to:
uri: "language:simple:Hello ${body}" In case you want to convert the message body type, you can do this as well. However, it is better to use Convert Body To:
-
Java
-
XML
-
YAML
from("direct:toString")
.to("language:simple:${bodyAs(String.class)}"); <route>
<from uri="direct:toString"/>
<to uri="language:simple:${bodyAs(String.class)}"/>
</route> - route:
from:
uri: direct:toString
steps:
- to:
uri: "language:simple:${bodyAs(String.class)}" You can also use the Groovy language, such as this example where the input message will be multiplied with 2:
-
Java
-
XML
-
YAML
from("direct:double")
.to("language:groovy:body * 2"); <route>
<from uri="direct:double"/>
<to uri="language:groovy:body * 2"/>
</route> - route:
from:
uri: direct:double
steps:
- to:
uri: "language:groovy:body * 2" You can also provide the script as a header as shown below, when the endpoint has allowTemplateFromHeader=true. Here we use XPath language to extract the text from the <foo> tag.
Object out = producer.requestBodyAndHeader("language:xpath?allowTemplateFromHeader=true", "<foo>Hello World</foo>", Exchange.LANGUAGE_SCRIPT, "/foo/text()");
assertEquals("Hello World", out); A script from the header is used for that message only: it is never cached as the script of the endpoint, also with cacheScript=true, and the header is removed after use, so a later language endpoint runs its own script.
Loading scripts from resources
You can specify a resource uri for a script to load in either the endpoint uri, or in the Exchange.LANGUAGE_SCRIPT header (with allowTemplateFromHeader=true). The uri must start with one of the following schemes: file:, classpath:, http:, https:, ref: or bean:
-
Java
-
XML
-
YAML
from("direct:start")
.to("language:simple:resource:classpath:org/apache/camel/component/language/mysimplescript.txt")
.to("mock:result"); <route>
<from uri="direct:start"/>
<to uri="language:simple:resource:classpath:org/apache/camel/component/language/mysimplescript.txt"/>
<to uri="mock:result"/>
</route> - route:
from:
uri: direct:start
steps:
- to:
uri: language:simple:resource:classpath:org/apache/camel/component/language/mysimplescript.txt
- to:
uri: mock:result When the resource is given with the resource: prefix (see below), its content is by default loaded once and cached. However, you can disable the contentCache option and have the script loaded on each evaluation. For example, if the file myscript.txt is changed on disk, then the updated script is used:
-
Java
-
XML
-
YAML
from("direct:start")
.to("language:simple:resource:file:myscript.txt?contentCache=false")
.to("mock:result"); <route>
<from uri="direct:start"/>
<to uri="language:simple:resource:file:myscript.txt?contentCache=false"/>
<to uri="mock:result"/>
</route> - route:
from:
uri: direct:start
steps:
- to:
uri: language:simple:resource:file:myscript.txt
parameters:
contentCache: false
- to:
uri: mock:result You can also refer to the script as a resource similar to how all the other Languages in Camel functions, by prefixing with resource: as shown below:
-
Java
-
XML
-
YAML
from("direct:start")
.to("language:constant:resource:classpath:org/apache/camel/component/language/hello.txt")
.to("mock:result"); <route>
<from uri="direct:start"/>
<to uri="language:constant:resource:classpath:org/apache/camel/component/language/hello.txt"/>
<to uri="mock:result"/>
</route> - route:
from:
uri: direct:start
steps:
- to:
uri: language:constant:resource:classpath:org/apache/camel/component/language/hello.txt
- to:
uri: mock:result Binary content
With binary=true, the content of the resource is not evaluated as a script, but is the result as a stream (java.io.InputStream). This works with and without the resource: prefix, for example to return an image:
-
Java
-
XML
-
YAML
from("direct:start")
.to("language:constant:classpath:images/logo.png?binary=true")
.to("mock:result"); <route>
<from uri="direct:start"/>
<to uri="language:constant:classpath:images/logo.png?binary=true"/>
<to uri="mock:result"/>
</route> - route:
from:
uri: direct:start
steps:
- to:
uri: language:constant:classpath:images/logo.png
parameters:
binary: true
- to:
uri: mock:result