The Nuxeo Platform is designed to be extended with additional customized capabilities and integrated with any number of other systems. As we’ve shown with many other examples, adding any type of external system connection is simply a matter of implementing the appropriate Nuxeo interface, defining the configuration in XML format and deploying to your Nuxeo instance. In my last blog I discussed content synchronization using the Nuxeo and SAP integration.

Today I will talk about how to easily use Getty Images in the Nuxeo Platform.

Getty Images API Example

In this example we’re going to integrate the Nuxeo Platform with the Getty Images repository, a stock photo service. Getty Images is a convenient image delivery service and the Nuxeo Platform is perfect for working with those images. So, automating the integration maximizes the mutual utility of the services. We will utilize their API to retrieve information about images managed by the Nuxeo Digital Asset Management software.

Getty Images logo

When a user downloads an image from Getty Image and drops the file into the Nuxeo Platform, it will automatically be updated with the caption/description and title. All this is transparent and automatic to the user.

Implementation

There will be two components in this project. First, a custom operation that will retrieve image information from Getty Images by referencing the image ID. Defining this as an operation instead of an action or listener response allows us to utilize the same code in various cases to maximize code reusability and flexibility. The second part will be a Nuxeo automation chain definition which will be called either by response to a Picture document import event or by a UI button press.

Creation of a Nuxeo operation involves implementing a Nuxeo Operation and the Nuxeo IDE pre-configures everything, leaving us to just implement the actual work.

@Operation(id=ImportGettyData.ID, category=Constants.CAT_DOCUMENT, label="ImportGettyData")
public class ImportGettyData {
    public static final String ID = "ImportGettyData";
    @OperationMethod(collector=DocumentModelCollector.class)
    public DocumentModel run(DocumentModel input) {
       BlobHolder bh=input.getAdapter(BlobHolder.class);
       String fileName = bh.getBlob().getFilename();
       String imageId = FilenameUtils.getBaseName(fileName);
       JsonNode imageData = runRestCall(imageId);
       if (imageData == null) {
           return input;
       }
       input.setPropertyValue("dc:description", "\""+imageData.get("caption").getValueAsText()+"\"");
       input.setPropertyValue("dc:title", imageData.get("title").getValueAsText());
       return input;
    }

The Getty Images API is REST based and returns data in JSON format. We will therefore utilize Jersey and Jackson libraries, already included with Nuxeo, to call the appropriate resource and parse the returned data.

private JsonNode runRestCall(String imageID){
        Client client = Client.create();
        JsonNode imageNode = null;

        WebResource gettyAPIResource = client.resource("https://api.gettyimages.com/v3/images");
        ClientResponse response = gettyAPIResource.path(imageID).header("Api-Key", API_KEY).type(MediaType.APPLICATION_JSON).get(ClientResponse.class);

        String jsonString = response.getEntity(String.class);
        ObjectMapper mapper = new ObjectMapper();
        JsonNode jsonResponse = mapper.readTree(jsonString);
        imageNode = jsonResponse.findValue("images").get(0);

        return imageNode;
}

We export the custom operation definition and register it in our Studio project to make it available for use in the visual Automation Chain designer. The operation can be part of a larger chain performing any other business activities besides the Getty Images query or in this example simply to update the current document in the context and save it. It can be run against a list of documents or a single one depending on user actions in the UI. All this is transparent and the reason why using an operation is so flexible is because Studio allows for flexible and fast development.

Automation Chain GettyImportAutomation Chain GettyImport

After defining the Automation Chain, we need to define the conditions of activation. User actions will allow a user to explicitly call this update while an Event response will automatically run the chain when a new Picture is created or imported making the whole process transparent and automatic.

Image information retrieved in the Nuxeo PlatformImage information retrieved in the Nuxeo Platform

Here’s what it will look like:


Video Content

While this is a simple use case to demonstrate the use of an external API in an Operation and Automation Chain, the same structure can be extended to make other API calls to search images, get more fields, etc. The Operation can be used in more complex Automation Chains as part of a bigger process. To learn more check out contributing an Operation and Automation Chains in Studio in Nuxeo documentation and try them out!