The Scripting Language filter uses Java Specification Request (JSR) 223 to embed a scripting environment in the API Gateway's core engine. This enables you to write bespoke JavaScript or Groovy code to interact with the message as it is processed by the API Gateway. You can get, set, and evaluate specific message attributes with this filter.
Because the scripting environment is embedded in the API Gateway engine, it has access
to all Java classes on the API Gateway's classpath, including all JRE classes. If you
wish to invoke a Java object, you must place its corresponding class file on the API Gateway
classpath. The recommended way to add classes to the API Gateway classpath is to place
them (or the JAR files that contain them) in the INSTALL_DIR/ext/lib
folder. For more details, see the readme.txt
in this folder.
Some typical uses of the Scripting Language filter include the following:
-
Check the value of a specific message attribute
-
Set the value of a message attribute
-
Remove a message attribute
-
DOM processing on the XML request or response
To write a script filter, you must implement the invoke()
method.
This method takes a com.vordel.circuit.Message
object as a
parameter and returns a boolean result.
The API Gateway provides a Script Library that contains a number of
pre-written invoke()
methods to manipulate specific message attributes.
For example, there are invoke()
methods to check the value of the
SOAPAction
header, remove a specific message attribute, manipulate the message
using the DOM, and assign a particular role to a user.
You can access the script examples provided in the Script library by clicking the Show script library button on the filter's main configuration screen. For a complete list of available message attributes, see the Message Attribute Reference.
Important | |
---|---|
When writing the JavaScript or Groovy code, you should note the following:
|
Use Local Variables
The API Gateway is a multi-threaded environment, therefore, at any one time
multiple threads can be executing code in a script. When writing JavaScript
or Groovy code, always declare variables locally using var
.
Otherwise, the variables are global, and global variables can be updated by
multiple threads.
For example, always use the following approach:
var myString = new java.lang.String("hello word"); for (var i = 100; i < 100; i++) { java.lang.System.out.println(myString + java.lang.Integer.toString(i)); }
Do not use the following approach:
myString = new java.lang.String("hello word"); for (i = 100; i < 100; i++) { java.lang.System.out.println(myString + java.lang.Integer.toString(i)); }
Using the second example under load, you cannot guarantee which value is output
because both of the variables (myString
and i
) are global.
You can write or edit the JavaScript or Groovy code in the text area on the Script tab. A JavaScript function skeleton is displayed by default. Use this skeleton code as the basis for your JavaScript code. You can also load an existing JavaScript or Groovy script from the Script library by clicking the Show script library button.
On the Script library dialog, click any of the Configured scripts in the table to display the script in the text area on the right. You can edit a script directly in this text area. Make sure to click the Update button to store the updated script to the Script library.
You can add a new script to the library by clicking the Add button, which displays the Script Details dialog. Enter a Name and a Description for the new script in the fields provided. By default, the Language field is set to JavaScript, but you can also select Groovy from the drop-down list. You can then write the script in the Script text area.