Automation

Automation Helpers

Updated: July 17, 2023

Automation Helpers are stateless tool functions used in MVEL or Javascript Automation scripts.

Default Automation Helpers

Two helpers are set by default into Nuxeo Platform: "Fn" and "HTTP".

  • Fn: Functions of Fn helper can be retrieved on this class. (Fn.getVocabularyLabel(...), Fn.getNextId(), Fn.getEmails(...).....)

  • HTTP: Functions of HTTP helper can be used to make remote REST Calls.

HTTP Examples

GET REST Call from Nuxeo to another Nuxeo repository

import org.nuxeo.ecm.core.api.Blob;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.automation.AutomationService;
import org.nuxeo.ecm.automation.OperationContext;
import org.nuxeo.ecm.automation.OperationException;

Map<String, Object> params = new HashMap<>();

// Defining MVEL expression and store it in Operation Context under 'script' key parameter
params.put("script","Context.result = HTTP.call(\"Administrator\",\"Administrator\",\"GET\", \"http://localhost:18090/api/v1/path/default-domain\");");

OperationContext ctx = new OperationContext(session);

// Running the operation 'RunScript' which is evaluate a MVEL expression 'script'
automationService.run(ctx, "RunScript", params);

// 'result' will be the JSON Payload definition of the document 'default-domain'
String result = ((Blob) ctx.get("result")).getString();

POST REST Call from Nuxeo to another Nuxeo repository

import org.nuxeo.ecm.core.api.Blob;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.automation.AutomationService;
import org.nuxeo.ecm.automation.OperationContext;
import org.nuxeo.ecm.automation.OperationException;

// Defining the new document to create in JSON Format
String data = "{\"entity-type\": \"document\",\"type\": \"Workspace\",\"name\":\"newName\",\"properties\": {\"dc:title\":\"My title\",\"dc:description\":\" \"}}";

Map<String, String> headers = new HashMap<>();
headers.put("Content-type", "application/json+nxentity");
Map<String, Object> params = new HashMap<>();
OperationContext ctx = new OperationContext(session);

// Store headers and JSON Format in Operation Context
ctx.put("data", data);
ctx.put("headers", headers);

// Set the MVEL Expression to assess in RunScript operation
params.put("script", "Context.result = HTTP.call(\"Administrator\",\"Administrator\",\"POST\", \"http://localhost:18090/api/v1/path/default-domain\", Context.data, Context.headers);");

// RunScript
automationService.run(ctx, "RunScript", params);

// 'result' will be the current document JSON Payload definition which has been created under 'default-domain'
String result = ((Blob) ctx.get("result")).getString();

Contributions

Automation Helpers can be contributed to the Nuxeo Platform easily through extension point:

<extension-point name="contextHelpers">
  <documentation>
    <code>
      <!-- 'id' is the prefix to use in MVEL or Javascript scripts to get access to all embedded functions -->
      <contextHelper id="platformFunctions" class="org.nuxeo.ecm.automation.features.PlatformFunctions"/>
    </code>
  </documentation>
  <object class="org.nuxeo.ecm.automation.context.ContextHelperDescriptor"/>
</extension-point>

Default Contribution

<extension target="org.nuxeo.ecm.core.operation.OperationServiceComponent"
           point="contextHelpers">
  <contextHelper id="Fn" class="org.nuxeo.ecm.automation.features.PlatformFunctions"/>
  <contextHelper id="HTTP" class="org.nuxeo.ecm.automation.features.HTTPHelper"/>
</extension>

Custom Contribution

In order to contribute custom Helpers contribution, you have to create your own POJO extending the interface 'org.nuxeo.ecm.automation.context.ContextHelper':

XML Contribution

<extension target="org.nuxeo.ecm.core.operation.OperationServiceComponent"
           point="contextHelpers">
  <contextHelper id="dummy" class="org.test.DummyHelper"/>
</extension>

POJO

package org.test;

import org.nuxeo.ecm.automation.context.ContextHelper;

public class DummyHelper implements ContextHelper {

    public String helper1() {
        return "hello";
    }
}