BeanShell

Overview

BeanShell is a Java-based scripting language that allows quick parsing of object. The BeanShell support is part of the camel-script module.

[Important]Important

You must use BeanShell 2.0b5 or greater.

Adding the script module

To use BeanShell in your routes you need to add a dependency on camel-script to your project as shown in Example 1, “Adding the camel-script dependency”.

Example 1. Adding the camel-script dependency

<!-- Maven POM File -->
<properties>
  <camel-version>2.8.0-fuse-00-05</camel-version>
  ...
</properties>

<dependencies>
  ...
  <dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-script</artifactId>
    <version>${camel-version}</version>
  </dependency>
  ...
</dependencies>

Built-in attributes

Table 2, “BeanShell attributes” lists the built-in attributes that are accessible when using BeanShell.

Table 2. BeanShell attributes

AttributeTypeValue
contextorg.apache.camel.CamelContextThe Camel Context
exchangeorg.apache.camel.ExchangeThe current Exchange
requestorg.apache.camel.MessageThe IN message
responseorg.apache.camel.MessageThe OUT message

The attributes all set at ENGINE_SCOPE.

Example

Example 2, “Routes using BeanShell” shows two routes that use BeanShell scripts.

Example 2. Routes using BeanShell

<camelContext>
  <route>
    <from uri=""mock:mock0" />
    <filter>
      <language language="beanshell">request.getHeaders().get("Foo") == null</language>
      <to uri="mock:mock1" />
    </filter>
  </route>
  <route>
    <from uri="direct:in"/>
    <setHeader headerName="firstName">
      <expression language="beanshell">user.firstName</expression>
    </setHeader>
    <to uri="seda:users"/>
  </route>
</camelContext>