Calling rest service via rule script

cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Member II

Calling rest service via rule script

Hi,

For a project I'm working on I need to be able to upload files to an alfresco repository along with a document Id (to be stored as a value of a custom model).

After a file is uploaded, I want to make a call to an external rest service (that I have built) that will be able to retrieve other useful bits of metadata from a database by using the document Id provided and update the node using the public rest api.

I had been hoping to create a script to call this rest service using the folder rules.

My current approach doesn't seem to be working as I get errors when trying to upload when the rule is active, which I imagine is due to the seemingly common "XMLHttpRequest is not defined" error (which I get when trying to run the script locally).

Is there a way I can achieve this via this approach?

If not, what is the best approach to take?

6 Replies
Highlighted
Senior Member II

Re: Calling rest service via rule script

Script which is getting executed by rule on folder, is a server side script.So the object of XMLHttpRequest  will not work in that script.

If you would like to call the rest service from that file you can follow below link for implementation of it.

java - XMLHttpRequest in Rhino? - Stack Overflow 

Another approach would be to define a policy and call 

Highlighted
Advanced II

Re: Calling rest service via rule script

The javascript executed by folder rules are not client side javascript, and this is why you can not make direct calls to rest APIs.

What you can do is to develop a custom service (java code), and then publish what you need as a Javascript root object, and then use it to call and recover the metadata you need.

You can see how it is created by reading the following blog post

A Simple Pattern for Alfresco Extensions

This will guide you on how to develop the integration, how to embed it into Alfresco, and then how to publish it to be used in the Javascript file.

Try it and let us know if you have problems trying to follow the steps.

Highlighted
Member II

Re: Calling rest service via rule script

Is there a way to do this without having access to the Alfresco code?

I am using an Alfresco instance that is running externally, but I have full access to share...

Highlighted
Advanced II

Re: Calling rest service via rule script

You don't need the access to "Alfresco code".

All you need is to use the Alfresco SDK to generate a project, create all the customizations you need (as described in the blog post I linked), package it and deploy into your Alfresco server.

For information about the development process, check this series of tutorials

https://ecmarchitect.com/alfresco-developer-series

Highlighted
Member II

Re: Calling rest service via rule script

Ok, I suppose my real question is:

Is there a way to do this in Alfresco already? - by any route, not necessarily scripting

As I don't want to patch it if I don't have to

Highlighted
Advanced II

Re: Calling rest service via rule script

Not that I know.