ScriptEvalValve
Valve for evaluating scripts using the Oracle Nashorn scripting engine included in JDK 8.
Supported scripting languages depends on the configuration and version of the Java Runtime.
The following languages are supported on all platforms:
- JavaScript (application/javascript)
- ECMA
Valve operates on the Current Item Set and will process all items surviving the common item filtering rules.
Properties
Script execution
The following object are in context during script execution:
- flow
- log
- itemFactory (helper for creating items)
- request
- attributes
- session
Example Configuration (Simple inline)
{
"name": "ScriptEvalValve",
"enabled": "true",
"config": {
"mime_type": "application/javascript",
"script": "console.log('From inline script');"
}
}
Example Configuration (path)
{
"name": "ScriptEvalValve",
"enabled": "true",
"config": {
"mime_type": "application/javascript",
"script_path": "src/js/test.js"
}
}
Example Configuration (script)
/*
* JavaScript that creates an LDAP compatible "user item" based on
* an input parameter called ‘id’.
*/
// Get input parameter
var id = flow.getParameter('id');
// Create DN for item (to be used as item.id)
var dn = 'cn=test_' + id + '_cn,ou=test,dc=example,dc=com';
// Create item using factory
var item = itemFactory.createItem(dn);
// Set item properties - all based on the supplied id
item.addProperty('objectClass', 'inetOrgPerson');
item.addProperty('cn', 'test_' + id + '_cn');
item.addProperty('sn', 'test_' + id + '_sn');
item.addProperty('mail', 'test_' + id + '@example.com');
item.addProperty('uid', 'test_' + id);
item.addProperty('userPassword', 'password' + id);
item.addProperty('description', "Auto generated user with password 'password" + id + "'");
// Add item to flow
flow.addItem(item);
// Done!
Example Configuration (inline)
This example will increment the numeric value of the item property adminDescription.
{
"name": "ScriptEvalValve",
"config": {
"mime_type": "application/javascript",
"script": "var newValue = parseInt(flow.items().get(0).getPropertyValue('adminDescription')) + 1;flow.items().get(0).replaceProperty('adminDescription', newValue);"
}
}