ScriptEvalValve

Valve for evaluating scripts using the Oracle Nashorn scripting engine included in JDK 11.

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

Name Description Default value Mandatory Supports property expansion
mime_type Mime type of script. Used for looking up compatible script engine. Yes No
script Inline script. No No
script_path Path to script. No No

Script execution

The following object are in context during script execution:

  • flow
  • log
  • itemFactory (helper for creating items)
  • request
  • attributes
  • session

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);"
     }
}

Example Configuration (inline)

This example will pick the value of the item property filename, get the value after the "." and add that value to a new item property fileExt.

If the item property filename contains the value mydocument.docx, the item property fileExt will be set to docx after the execution of this valve.

{
				"name": "ScriptEvalValve",
				"config": {
					"mime_type": "application/javascript",
					"script": "var array = flow.items().get(0).getPropertyValue('filename').split('.');var fileExt = array.pop();flow.items().get(0).replaceProperty('filext', fileExt);"
				}
			}

 

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!

NB! Remove all the comments before adding the script to the configuration.

Script comments

The script should be added to the configuration without comments (//) to avoid runtime issues.