Script component
The Script component allows you to modify messages using scripts written in JavaScript or Groovy. The scripts can either be provided directly or uploaded as files. Scripts are able to read and modify the body, headers, and properties of messages that pass through the component.
To simplify development of scripts, the Script component offers the ability to evaluate scripts given a set of test values for the body, headers, and properties of a message.
The type of a message's body depends on the component that produced the message.
If you want to process the body of a message as a string, you may have to convert it, depending on the flow in which you're using the Script component.
The expression request.getBody(java.lang.String.class)
returns the body as a string.
The attributes that can be accessed and modified by the Script component are described in the documentation of Apache's Camel project.
Configuration
The Script component has the following configuration options:
Property | Description |
---|---|
Script | The script that should be evaluated for each message passing through the component. |
File Upload | The script that should be evaluated for each message in the form of a file. |
Language | The language used for the script. |
In Headers | A list of comma-separated name-value pairs representing headers for testing purposes. The names and values should be quoted. |
In Properties | A list of comma-separated name-value pairs representing properties for testing purposes. The names and values should be quoted. |
In Body | A body for testing purposes |
Out Headers | The list of headers after evaluating the script. |
Out Properties | The list of properties after evaluating the script. |
Out Body | The body of the message after evaluation. |
Import third party libraries in JS scripts
By using the load
function third party libraries can be imported and used in
Javascript scripts:
load(
"https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js",
"https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/hmac-sha256.min.js",
"https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/enc-base64.min.js"
);
Example
The image below shows the result of evaluating a script for a given body, set of headers and set of properties.
Remarks
- The JavaScript that you can use in the script component is not the same
implementation as in web browsers. It is an open source implementation of
JavaScript for Java. We advice that you use standard JavaScript syntax also known
as
ES5
. Newer syntax and other features are not supported at the moment. - The Groovy version that is used in the script component is
2.4.15
. - Use
result = request.body
at the end of the script to keep the original message body when you only change some headers in the script.