How to call REST API of remote service in rule executed script

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

How to call REST API of remote service in rule executed script

Hi,

I read many posts about this problem but I didn't found solution.

I know that

remote is not available

and I cannot use

XMLHttpRequest()

What can I do to call REST API in rule executed script?

Please write a snippet of code.

Thank in advance.

8 Replies
Highlighted
Established Member

Re: How to call REST API of remote service in rule executed script

In Rule Script, the remote object is not available. You have to manage by using available objects or have to write an action. 

Document, CompanyHome, siteService etc objects are available. 

document.save();

var targetNode = companyhome.childByNamePath(newtargectDir);

var siteNode = siteService.getSite(site);
site = siteNode.title;

Highlighted
Established Member

Re: How to call REST API of remote service in rule executed script

Thank you but is not usefull at all.

Please tell me how to achieve calling external REST API from rule script. Either using available object or action.

What action do you mean?

Highlighted
Professional

Re: How to call REST API of remote service in rule executed script

Which REST API are you referring to? The Alfresco REST API or some third-party REST API?

Highlighted
Active Member II

Re: How to call REST API of remote service in rule executed script

Hi Jeff, I also have the same challenge. 

For example I want to call the following RESTful API from a rule JavaScript file, how I can do that ? :

http://localhost:8080/alfresco/service/api/workflow-instances

I use Alfresco Community v 5.1 (f)

Highlighted
Professional

Re: How to call REST API of remote service in rule executed script

When you are running code in the repository tier (in the alfresco.war) and you feel like you need to call a web script from JavaScript, you should first ask yourself if there is a native API call that will get you what you need.

In this case, you want to get the workflow instances, which is available natively in the JavaScript API, see http://docs.alfresco.com/5.1/references/API-JS-WorkflowManager.html 

If that doesn't get you what you need you can write a Java class that uses the native Java API which can surely meet your needs. You can then use either a Java-backed web script to use that class or you can wrap the Java class with a BaseScopableProcessorExtension so that it is available as a root scoped JavaScript object. There are lots of examples of how to do that, but here's one.

Highlighted
Active Member II

Re: How to call REST API of remote service in rule executed script

Jeff,

I do not found the required function by the link  http://docs.alfresco.com/5.1/references/API-JS-WorkflowManager.html

and the link you have provided contains only the Java code and this is not for newbies like me.

I know how to create an Alfresco Share project by Alfresco SDK, but I still do not understand how to develop extensions of JS API.

So let me ask you, please tell me in couple of words (in step-by-step manner) what I need to do to start development process of JS API's extension or point me where I can find this information in yours book "Alfresco One 5x Developers Guide (Second edition)" (has been bought several days ago).

Thank you very much!

Regards,

Egor

Highlighted
Professional

Re: How to call REST API of remote service in rule executed script

Extending the server-side JavaScript engine by adding your own root-scoped objects will require Java.

There is an example of it in the first edition of the book, but I think it got cut from the second edition. You can still get to the source code from the first edition, however, just ignore the ant-based build.

The example in the book consolidates the "rating" logic into a common service class, then wraps that class to make the rating service available from JavaScript.

Rating Service:

The Java that implements the Ratings Service is:

https://github.com/jpotts/alfresco-developer-guide/blob/master/client-extensions/src/java/com/someco...

You can see it has methods for performing CRUD functions on nodes/ratings.

JScript wrapper:

That service is then wrapped with this small class:

https://github.com/jpotts/alfresco-developer-guide/blob/master/client-extensions/src/java/com/someco...

 

That class basically defines the API that will be exposed to JavaScript.

Spring Config:

In this file, look at the bean with an id of "ratingScript".

https://github.com/jpotts/alfresco-developer-guide/blob/master/client-extensions/config/alfresco/ext...

 

That tells Alfresco, "Here is a new object to expose to the JavaScript engine".

It includes a reference to another bean called "RatingService", which in turn points to "ratingServiceImpl", both in the same file. That bean points to the Rating Service Java class implementation and "injects" the dependencies that the RatingService needs (the NodeService and the SearchService).

Summary:

So the Rating Service centralizes all logic related to ratings and can be called by any Java class in Alfresco (rule actions, Java-based web scripts, Activiti task listeners, etc.).

Then, the JScript wrapper exposes the same service to the JavaScript engine making the same logic available to things like the JavaScript console, JavaScript-based web scripts, or Activiti in-line JavaScript code blocks.

Again, I'm linking you to this VERY OLD source code to give you an idea of how this works. Do not try to take that project and build it as it uses an old structure and an obsolete build system.

In your case, if the out-of-the-box workflow object does not meet your requirements, you'll have to use the Java API. Probably the best approach is to write a small service class, like I've done here, that uses the Java API to get information from workflows, then you'll write a wrapper to expose that service to JavaScript.

Highlighted
Established Member

Re: How to call REST API of remote service in rule executed script

Hi Jeff,

I want to call third party REST API. Flowable in my case.