Since we're on a major migration process of this website, some component documents here are out of sync right now. In the meantime you may want to look at the asciidoc in the repository: https://github.com/apache/camel/blob/master/README.md https://github.com/apache/camel/blob/master/components/readme.adoc
LanguageAvailable as of Camel 2.5 The language component allows you to send Exchange to an endpoint which executes a script by any of the supported Languages in Camel. This component is provided out of the box in URI formatlanguage://languageName[:script][?options] And from Camel 2.11 onwards you can refer to an external resource for the script using same notation as supported by the other Languages in Camel language://languageName:resource:scheme:location][?options] URI OptionsThe component supports the following options.
Message HeadersThe following message headers can be used to affect the behavior of the component
ExamplesFor example you can use the Simple language to Message Translator a message:Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
String script = URLEncoder.encode("Hello ${body}", "UTF-8");
from("direct:start").to("language:simple:" + script).to("mock:result");
In case you want to convert the message body type you can do this as well:Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
String script = URLEncoder.encode("${mandatoryBodyAs(String)}", "UTF-8");
from("direct:start").to("language:simple:" + script).to("mock:result");
You can also use the Groovy language, such as this example where the input message will by multiplied with 2:Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
String script = URLEncoder.encode("request.body * 2", "UTF-8");
from("direct:start").to("language:groovy:" + script).to("mock:result");
You can also provide the script as a header as shown below. Here we use XPath language to extract the text from the <foo> tag.Object out = producer.requestBodyAndHeader("language:xpath", "<foo>Hello World</foo>", Exchange.LANGUAGE_SCRIPT, "/foo/text()");
assertEquals("Hello World", out);
Loading scripts from resourcesAvailable as of Camel 2.9 You can specify a resource uri for a script to load in either the endpoint uri, or in the For example to load a script from the classpath:Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
from("direct:start")
// load the script from the classpath
.to("language:simple:classpath:org/apache/camel/component/language/mysimplescript.txt")
.to("mock:result");
By default the script is 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:Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
from("direct:start")
// the script will be loaded on each message, as we disabled cache
.to("language:simple:file:target/script/myscript.txt?contentCache=false")
.to("mock:result");
From Camel 2.11 onwards you can refer to the resource similar to the other Languages in Camel by prefixing with "resource:" as shown below:Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
from("direct:start")
// load the script from the classpath
.to("language:simple:resource:classpath:org/apache/camel/component/language/mysimplescript.txt")
.to("mock:result");
See Also |
