Search code examples
javascriptrestalfresco

Where do I have to define connectors in Alfresco? Can I use them from a ".bpmn" file?


Good afternoon! I am trying to communicate from Alfresco to a RESTful ws from a workflow. Somebody told me that it will be a good idea to use connector to acomplish that. I am creating a wf in ACS as a .bpmn file, so 3 questions:

  1. In what file do I have to define the connector?, I want to do the same as this js script:
var url = "https://google.com";

var xhr = new XMLHttpRequest();
xhr.open("GET", url);

xhr.onreadystatechange = function () {
   if (xhr.readyState === 4) {
      console.log(xhr.status);
      console.log(xhr.responseText);
   }};

xhr.send();
  1. Can I use the connector directly from the .bpmn file? Could you give me an example on how to use it?

  2. Could you give me an example to make a GET and a POST call?

Thanks in advance!


Solution

  • Connectors are part of the web script framework. There is a web script framework in the Alfresco Share tier and there is a web script framework in the repository tier.

    In web scripts, you use the "remote" object to make connections to remote RESTful end points. For example, the code to fetch some project information from an API via its project ID might look like this:

        var connector = remote.connect("someapp");
        var resp = connector.get("/someapp/projects/" + projectId);
        if (resp.status == 200) {
            obj = JSON.parse(resp);
            model.result = obj;
            model.startDateFormatted = getFormattedDate(model.result.StartDate);
            model.endDateFormatted = getFormattedDate(model.result.EndDate);
        }
    

    So where is that "someapp" connector defined? You can put it in an Alfresco config file such as the share custom config. In an Alfresco SDK based project that file lives in the share module under ./src/main/resources/META-INF/share-config-custom.xml:

    <config evaluator="string-compare" condition="Remote">
        <remote>
            <endpoint>
                <id>someapp</id>
                <name>Some Remote App</name>
                <connector-id>http</connector-id>
                <endpoint-url>https://someapp.example.org</endpoint-url>
            </endpoint>
        </remote>
    </config>
    

    The challenge for you is that you are running in an Activiti workflow, not a web script. And, aside from that, the remote object is disabled on the repo tier, which is also where you are running. It can be re-enabled, but I don't have those steps handy and it doesn't help you anyway.

    So, rather than use any of the above, my advice is to use a Java delegate in your workflow. From the Java delegate you can do anything you want, including leveraging something like the Apache HTTP Client to make a connection to the API you need to call.

    You might have a delegate class like:

    public class RemoteApiInvocationDelegate implements JavaDelegate {
        private Logger logger = LoggerFactory.getLogger(RemoteApiInvocationDelegate.class);
    
        public void execute(DelegateExecution execution) throws Exception {
            HttpClient httpClient = Utils.noCertClient();
    
            HttpGet httpGet = new HttpGet("http://someapp.example.org/someapp/projects/12345");
            HttpResponse response = httpClient.execute(httpGet);
            int status = response.getStatusLine().getStatusCode();
            logger.info("Response Status Code: " + status);
            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = rd.readLine()) != null) {
                sb.append(line);
            }
            logger.info(sb.toString());
        }
    }
    

    What you do with the response, like parse it, extract some data, and set it on a process variable, for example, is up to you.