cancel
Showing results for 
Search instead for 
Did you mean: 

Calling an external web service from an Alfresco Web Script

nmcminn
Champ in-the-making
Champ in-the-making
Greetings all,

I am currently working on a project that will be made MUCH easier if I can figure out a way to call a REST web service from a web script in Alfresco.  It is my understanding that Alfresco web scripts are run in Rhino.  Rhino supports a few built in functions, such as readUrl, that would be ideal for my purposes.  I'm a bit new to running JavaScript in the Rhino environment, so I may be missing something obvious.

When I run my script via the Rhino console, it works as expected.  However, when I try to execute the same script in the Alfresco environment, I get the following:

org.mozilla.javascript.EcmaError - ReferenceError: "readUrl" is not defined. (workspace://SpacesStore/Company Home/Data Dictionary/Web Scripts/blah/blah/blah#linenumber)

With this failing, I went for the next obvious line of attack: using Rhino to load the java.net package and using the Java URL object to hit my service.  This too was a no-go, as the importPackage provided by Rhino led to the same ReferenceError shown above.  A little digging in the forums turned up this thread:

http://forums.alfresco.com/en/viewtopic.php?f=27&t=7134&hilit=rhino

The thread referenced above clearly shows why I couldn't load any Java packages/classes into my web scripts, this functionality is disabled for (very valid) security purposes.  However, the original poster's question, how to hit an external web service from a web script, was never really addressed.

So, my question:  What is the best way to hit a simple external RESTful web service from an Alfresco web script?  I only need support for GET operations, nothing fancy.
7 REPLIES 7

chrisb
Champ in-the-making
Champ in-the-making
Hi Nathan,

Did you manage to do this in the end? I am also looking to make use of external web services from an Alfresco webscript but am having difficulty finding any concrete examples of the best way to go about this.

The forum post you link to gives a hint on the Alfresco sanctioned approach, namely:

The correct way to call Java from JavaScript is to create a Repository action class and then call that using the 'actions' object from JavaScript. Since only a server admin can add new JARs to the repo this is a safe route.

…but doesn't really go into much more detail. Is anyone aware of any more detailed guidance on going down the Repository action class route for accessing external web services (wiki examples maybe)?

Thanks

weisinger
Champ in-the-making
Champ in-the-making
Hi Nathan,

If you're using Alfresco webscripts, you should be able to use a remote connector to call into an external service.

There's a good example of how to build a dashlet here that calls into a Yahoo service to retrieve weather information, parses the data using E4X in JS, and then displays it in Share.

http://drquyong.com/myblog/?p=28

Is that the kind of thing you're looking for?

Dick Weisinger

chrisb
Champ in-the-making
Champ in-the-making
Thats a good link - thanks for sharing it.

Do you know if that approach using the "remote" connector works for webscripts running outside of the share environment (i.e. as part of the main Alfresco repo install)?

Chris

weisinger
Champ in-the-making
Champ in-the-making
Chris,

"remote" is a root-scoped object in the Surf Javascript API.
I don't think that it is available for use from the Alfresco explorer client.

To access the alfresco repository from share/surf, you can connect using "alfresco" instead of "http".

http://wiki.alfresco.com/wiki/Surf_Platform_-_Freemarker_Template_and_JavaScript_API

Dick Weisinger

chrisb
Champ in-the-making
Champ in-the-making
Dick,

Thanks for clarifying that. Looks like I'll need to go down the Repository action class route.

Chris

a_evagelopoulos
Champ in-the-making
Champ in-the-making
and what about when the web service requires authentication?
is there any way to authenticate the user with connectors object?

dc_noze
Champ in-the-making
Champ in-the-making
Hi guys,
if someone is already interested, here http://forums.alfresco.com/en/viewtopic.php?f=36&t=27962#p101418 there is a solution on how to expose the remote object in the web client webscript api.
About authentication i have added the alf_ticket parameter to remote call.