Server

Returning a Custom Result with Automation

Updated: December 2, 2024

As automatic marshalling is not implemented into Automation server and client, only Document(s) and Blob(s) can be manipulated. Therefore, the way to return a custom type is to encapsulate the value in a Blob.

Below is an example, based on the results returned by the method QueryAndFetch.

  • Operation code

    package org.nuxeo.support;
    
    import java.io.ByteArrayInputStream;
    import java.io.Serializable;
    import java.util.Iterator;
    import java.util.Map;
    
    import net.sf.json.JSONArray;
    import net.sf.json.JSONObject;
    
    import org.nuxeo.ecm.automation.core.Constants;
    import org.nuxeo.ecm.automation.core.annotations.Context;
    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.Blob;
    import org.nuxeo.ecm.core.api.CoreSession;
    import org.nuxeo.ecm.core.api.IterableQueryResult;
    import org.nuxeo.ecm.core.api.impl.blob.StringBlob;
    import org.nuxeo.ecm.core.query.sql.NXQL;
    
    @Operation(id = QueryAndFetch.ID, category = Constants.CAT_FETCH, label = "QueryAndFetch", description = "Sample to show how to return a blob for any result type.")
    public class QueryAndFetch {
    
        public static final String ID = "Test.QueryAndFetch";
    
        @Context
        protected CoreSession session;
    
        @Param(name = "query")
        protected String query;
    
        protected String lang = NXQL.NXQL;
    
        @OperationMethod
        public Blob run() throws Exception {
            IterableQueryResult result = session.queryAndFetch(query, lang);
            Iterator<Map<String, Serializable>> it = result.iterator();
    
            JSONArray array = new JSONArray();
            while (it.hasNext()) {
                Map<String, Serializable> item = it.next();
                JSONObject object = new JSONObject();
                object.accumulateAll(item);
                array.add(object);
            }
    
            return new StringBlob(array.toString(), "application/json");
    
        }
    
    }
    
    
  • Registering this operation

    <?xml version="1.0"?>
    <component name="org.nuxeo.support.operations">
    
      <extension target="org.nuxeo.ecm.core.operation.OperationServiceComponent"
        point="operations">
        <operation class="org.nuxeo.support.QueryAndFetch" />
      </extension>
    </component>
    
  • Sample code to use the result from the operation

    HttpAutomationClient client = new HttpAutomationClient("http://localhost:8080/nuxeo/site/automation");
    
    Session session = client.getSession(ADMINISTRATOR, ADMINISTRATOR);
    Blob response = (Blob) session.newRequest(QueryAndFetch.ID).set("query", "select ecm:uuid, dc:title, common:icon from Document").execute();
    String json = FileUtils.read(response.getStream());
    JSONArray array = JSONArray.fromObject(json);
    System.out.println("Objects received : " + array.size());