ScriptContext
The JSR-223 scripting languages ScriptContext is pre configured with the following attributes all set at ENGINE_SCOPE:
Attribute |
Type |
Value |
context |
org.apache.camel.CamelContext |
The Camel Context |
exchange |
org.apache.camel.Exchange |
The current Exchange |
request |
org.apache.camel.Message |
The IN message |
response |
org.apache.camel.Message |
The OUT message |
properties |
org.apache.camel.builder.script.PropertiesFunction |
Camel 2.9: Function with a resolve method to make it easier to use Camels Properties component from scripts. See further below for example. |
Attributes
You can add your own attributes with the attribute(name, value) DSL method, such as:
In the sample below we add an attribute user that is an object we already have instantiated as myUser. This object has a getFirstName() method that we want to set as header on the message. We use the groovy language to concat the first and last name into a single string that is returned.
from("direct:in").setHeader("name").groovy("'$user.firstName $user.lastName'").attribute("user", myUser).to("seda:users");
Any scripting language
Camel can run any JSR-223 scripting languages using the script DSL method such as:
from("direct:in").setHeader("firstName").script("jaskel", "user.firstName").attribute("user", myUser).to("seda:users");
This is a bit different using the Spring DSL where you use the _expression_ element that doesn't support setting attributes (yet):
<from uri="direct:in"/>
<setHeader headerName="firstName">
<_expression_ language="jaskel">user.firstName</_expression_>
</setHeader>
<to uri="seda:users"/>
You can also use predicates e.g. in a Filter:
<filter>
<language language="beanshell">request.getHeaders().get("Foo").equals("Bar")</language>
<to uri="direct:next" />
</filter>
See Scripting Languages for the list of languages with explicit DSL support.
Some languages without specific DSL support but known to work with these generic methods include:
Language |
Implementation |
language="..." value |
BeanShell |
BeanShell 2.0b5 |
beanshell or bsh |
Additional arguments to ScriptingEngine
Available as of Camel 2.8
You can provide additional arguments to the ScriptingEngine using a header on the Camel message with the key CamelScriptArguments.
See this example:
public void testArgumentsExample() throws Exception {
if (!ScriptTestHelper.canRunTestOnThisPlatform()) {
return;
}
getMockEndpoint("mock:result").expectedMessageCount(0);
getMockEndpoint("mock:unmatched").expectedMessageCount(1);
Map<String, Object> arguments = new HashMap<String, Object>();
arguments.put("foo", "bar");
arguments.put("baz", 7);
template.sendBodyAndHeader("direct:start", "hello", ScriptBuilder.ARGUMENTS, arguments);
assertMockEndpointsSatisfied();
}
Using properties function
Available as of Camel 2.9
If you need to use the Properties component from a script to lookup property placeholders, then its a bit cumbersome to do so.
For example to set a header name myHeader with a value from a property placeholder, which key is provided in a header named "foo".
.setHeader("myHeader").groovy("context.resolvePropertyPlaceholders('{{' + request.headers.get('foo') + '}}')")
From Camel 2.9 onwards you can now use the properties function and the same example is simpler:
.setHeader("myHeader").groovy("properties.resolve(request.headers.get('foo'))")
Loading script from external resource
Available as of Camel 2.11
You can externalize the script and have Camel load it from a resource such as "classpath:", "file:", or "http:".
This is done using the following syntax: "resource:scheme:location", eg to refer to a file on the classpath you can do:
.setHeader("myHeader").groovy("resource:classpath:mygroovy.groovy")
Dependencies
To use scripting languages in your camel routes you need to add the a dependency on camel-script which integrates the JSR-223 scripting engine.
If you use maven you could just add the following to your pom.xml, substituting the version number for the latest & greatest release (see the download page for the latest versions).
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-script</artifactId>
<version>x.x.x</version>
</dependency>