Using the Workflow Java API

The workflow Java API consists of the com.adobe.granite.workflow package and several sub-packages. The most significant member of the API is the com.adobe.granite.workflow.WorkflowSession class. The WorkflowSession class provides access to both design-time and runtime workflow objects:

workflow models

work items

workflow instances

workflow data

inbox items

The class also provides several methods for intervening in workflow lifecycles.

The following table provides links to the reference documentation of several key Java objects to use when interacting programmatically with workflows. The examples that follow demonstrate how to obtain and use the class objects in code.

Using the Workflow REST API

The curl command line tool enables you to use the Workflow REST API to access workflow objects and manage instance lifecycles. The examples that follow demonstrate the use of the REST API via the curl command line tool.

The Workflow console makes heavy use of the REST API; so this page describes the REST API for workflows.

The following actions are supported with the REST API:

start or stop a workflow service

create, update or delete workflow models

start, suspend, resume or terminate workflow instances

complete or delegate work items

Note:

By using Firebug, a Firefox extension for web development, it is possible to follow the HTTP traffic when the console is operated. For example you can check the parameters and the values sent to the AEM server with a POST request.

In this page it is assumed that AEM runs on localhost at port 4502 and that the installation context is "/" (root). If it is not the case of your installation, the URIs, to which the HTTP requests apply, need to be adapted accordingly.

The rendering supported for GET requests is the JSON rendering. The URLs for GET should have the .json extension, for example:

http://localhost:4502/etc/workflow.json

Managing Workflow Instances

The following HTTP request methods apply to:

http://localhost:4502/etc/workflow/instances

HTTP request method

Actions

GET

Lists the available workflow instances.

POST

Creates a new workflow instance. The parameters are:
- model: the ID (URI) of the respective workflow model
- payloadType: containing the type of the payload (for example JCR_PATH or URL).
The payload is sent as parameter payload. A 201 (CREATED) response is sent back with a location header containing the URL of the new workflow instance resource.

Managing a Workflow Instance by its State

The following HTTP request methods apply to:

http://localhost:4502/etc/workflow/instances.{state}

HTTP request method

Actions

GET

Lists the available workflow instances and their states (RUNNING, SUSPENDED, ABORTED or COMPLETED)

Managing a Workflow Instance by its ID

The following HTTP request methods apply to:

http://localhost:4502/etc/workflow/instances/{id}

HTTP request method

Actions

GET

Gets the instances data (definition and metadata) including the link to the respective workflow model.

POST

Changes the state of the instance. The new state is sent as the parameter state and must have one of the following values: RUNNING, SUSPENDED, or ABORTED.
If the new state is not reachable (for example when suspending a terminated instance) a 409 (CONFLICT) response is sent back to the client.

Managing Workflow Models

The following HTTP request methods apply to:

http://localhost:4502/etc/workflow/models

HTTP request method

Actions

GET

Lists the available workflow models.

POST

Creates a new workflow model. If the parameter title is sent, a new model is created with the specified title. Attaching a JSON model definition as parameter model creates a new workflow model according to the provided definition.
A 201 response (CREATED) is sent back with a location header containing the URL of the new workflow model resource.
The same happens when a model definition is attached as a file parameter called modelfile.
In both the cases of the model and modelfile parameters, an additional parameter called type is required to define the serialization format. New serialization formats can be integrated using the OSGI API. A standard JSON serializer is delivered with the workflow engine. Its type is JSON. See below for an example of the format.

Example: in the browser, a request tohttp://localhost:4502/etc/workflow/models.json gets a similar json file to the following one:

Updates the HEAD version of the model (creates a new version).
The complete model definition for the new version of the model must be added as a parameter called model. Additionally a type parameter is needed as when creating new models and needs to have the value JSON.

POST

Same behaviour as with PUT. Needed because AEM widgets do not support PUT operations.

DELETE

Deletes the model. In order to solve firewall/proxy issues a POST that contains an X-HTTP-Method-Override header entry with value DELETE will also be accepted as DELETE request.

Example: in the browser, a request to http://localhost:4502/etc/workflow/models/publish_example.-1.json returns a json file that is similar to the following code:

Managing (User) Inboxes

Lists the work items that are in the inbox of the user, who is identified by the HTTP authentication headers.

POST

Completes the work item whose URI is sent as the parameter item and advances the according workflow instance to the next node(s), that is defined by the parameter route or backroute in case of going a step back.
If the parameter delegatee is sent, the work item identified by the parameter item is delegated to the specified participant.

Managing a (User) Inbox by the WorkItem ID

The following HTTP request methods apply to:

http://localhost:4502/bin/workflow/inbox/{id}

HTTP request method

Actions

GET

Gets the data (definition and metadata) of the inbox WorkItem identified by its ID.

Examples

Obtaining a WorkflowSession Object

The com.adobe.granite.workflow.WorkflowSession class is adaptable from a javax.jcr.Session object or a org.apache.sling.api.resource.ResourceResolver object.

Java

In a JSP script (or Java code for a servlet class), use the HTTP request object to obtain a SlingHttpServletRequest object, which provides access to a ResourceResolver object. Adapt the ResourceResolver object to WorkflowSession.

Deleting the model sets the deleted property of the model's metaData child node to true.

Deletion does not remove the model node.

When creating a new model:

The workflow model editor requires that models use a specific node structure below /etc/workflow/models. The parent node of the model must be of the type cq:Page having a jcr:content node with the following property values:

sling:resourceType: cq/workflow/components/pages/model

cq:template: /libs/cq/workflow/templates/model

When you create a model, you must first create this cq:Page node and use its jcr:content node as the parent of the model node.

The id argument that some methods require for identifying the model is the absolute path of the model node in the repository:

Listening for Workflow Events

Use the OSGi event framework to listen for events that the com.adobe.granite.workflow.event.WorkflowEvent class defines. This class also provides several useful methods for obtaining information about the subject of the event. For example, the getWorkItem method returns the WorkItem object for the workitem that is involved in the event.

The following example code defines a service that listens to workflow events and performs tasks according to the type of event.