Exploring Nuxeo APIs: Content Automation


Fri 27 January 2012 By Laurent Doguin

The range of APIs offered by the Nuxeo Platform

One of the main assets of the Nuxeo Platform is the richness of its APIs: CMIS, REST, WebServices, WSS, Content Automation. Content Automation is a subset of our REST API and is probably the most powerful and simplest means to access Nuxeo. Its strength and richness lies in its ability to provide access to all the services offered within the Nuxeo Platform, but also because of how it integrates with other Nuxeo tools, such as Nuxeo Studio for customizing it, or Nuxeo IDE for extending.

From our wiki:

Content Automation is a Nuxeo service that exposes commons actions you do on a Nuxeo application as atomic operations so that one can assemble them to create complex business rules and logic, without writing any Java code.

In other words, content automation provides a high level API over Nuxeo services - an API made up of operations that can be assembled into complex operation chains (or macro operations). These operations are also exposed remotely through a REST API.

The main goal of automation is to enable end users to rapidly build complex business logic without writing any Java code. Instead, just by assembling the built-in set of atomic operations into complex chains and then plugging these chains inside Nuxeo as UI actions, event handlers, REST bindings, etc., they are able to attain the same results.

You can also create new atomic operations (write a Java class that defines an operation) and contribute them to the set of built-in operations. To define an operation chain, you just need to write an XML contribution that describes the chain by listing each operation in the chain along with the parameter values that will be used to execute the operation. If you need to define dynamic operation parameters (whose value will be computed at runtime when the operation is executed) you can use scripting (e.g. EL syntax) to fetch the actual parameter value at execution time.

So here is a short screencast showing you how to write and deploy an operation on a Nuxeo server using Nuxeo IDE, and how this operation can be called from a simple python script. You will also witness the awesome hot-reload feature of the IDE :-)




Here is the python script:

[python]

#!/usr/bin/env python
import urllib2, base64, json, sys

def count_words(document, field, server='localhost', port=8080,
user='Administrator', password='Administrator'):
"""Call the CountWords operation on a text field of a document"""

url = "http://%s:%d/nuxeo/site/automation/CountWords" % (
    server, port)
auth = 'Basic %s' % base64.b64encode(user + ":" + password).strip()
request_headers = {
    "Content-Type": "application/json+nxrequest",
    "Accept": "application/json+nxentity, */*",
    "Authorization": auth,
}
request_body = {"params": {"document": document, "field": field}}
request_body = json.dumps(request_body)

request = urllib2.Request(url, request_body, request_headers)
response = urllib2.urlopen(request)
return int(json.loads(response.read()).get(u'value', 0))

if name == 'main':
print count_words(sys.argv[1], sys.argv[2])

[/python]

And this is the java code:

[java]
package org.nuxeo.sample;

import org.nuxeo.ecm.automation.core.Constants;
import org.nuxeo.ecm.automation.core.annotations.Operation;
import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
import org.nuxeo.ecm.automation.core.annotations.Param;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.model.PropertyException;

/**

  • @author ogrisel
    */
    @Operation(id = CountWords.ID, category = Constants.CAT_DOCUMENT, label = "CountWords", description = "")
    public class CountWords {

    public static final String ID = "CountWords";

    @Param(name = "document")
    DocumentModel doc;

    @Param(name = "field")
    String fieldName;

    @OperationMethod
    public Object run() throws PropertyException, ClientException {

     String text = doc.getProperty(fieldName).getValue(String.class).trim();
     return text.isEmpty() ? 0 : text.split("s+").length;
    

    }

}
[/java]


Category: Product & Development
Tagged: API, CMIS, Java, Nuxeo IDE