cancel
Showing results for 
Search instead for 
Did you mean: 

Hit External API

drozes
Champ in-the-making
Champ in-the-making
Hello All;
New to Alfresco and appreciate any help available.

Trying to make a simple server side request to a URL (ie. http://mycompany.com/apiservice?method=getDocuments)

I'll create the webscript's xml file and am able to hit the URL successfully:
<webscript>
  <shortname>Tests</shortname>
  <description>Sample Tests</description>
  <url>/sample/tests</url>
  <authentication>user</authentication>
</webscript>


I now need the controller method to execute on server side and pass the data in to my ftl.

Once again, this needs to be server side to avoid cross domain issues.

Thanks for any help.


*Addition:

currently my server side javascript is doing:

// change the name of this document
document.properties.name = "User_File-" +document.properties.name;
//Add property
document.properties["cm:status"] = "Not Started";
// save the property modifications
document.save();

//Need to hit External API - Indiciating new document.
response = hiturl(" http://mycompany.com/apiservice?method=addNewDoc")
6 REPLIES 6

mrogers
Star Contributor
Star Contributor
Take a look at the example web scripts on the wiki.   Or in Alfresco's source code.

There are examples of Java Script Controllers and Java Controllers.

Java script controllers go alongside the template with a naming convention for method/response type.

Java Controllers need to be registered with Spring,  just follow the existing patterns.

openpj
Elite Collaborator
Elite Collaborator
I published an example project that implements the Java-Backed WebScripts described in the Alfresco Wiki:
http://code.google.com/p/alfresco-java-backed-webscripts-demo/

You can take a look at this Maven module and you can try the implementation running on-the-fly an Alfresco instance with Jetty.

The project is mentioned in the Alfresco Wiki here:
http://wiki.alfresco.com/wiki/Java-backed_Web_Scripts_Samples#The_Demo_Web_Scripts

Hope this helps.

drozes
Champ in-the-making
Champ in-the-making
thank you both for your replie!  Very appreciated.

Will be running through your suggestions today.

jpotts
World-Class Innovator
World-Class Innovator
It sounds like you know how to create a web script, but your challenge is that you aren't sure how to hit a non-Alfresco service from the web script controller. Your goal is to invoke that service and then put some of that data in your web script response.

Option 1: Assuming you are staying with JavaScript

It looks like you are trying to use a JavaScript controller. The Web Script engine actually has a "remote" object that can be used to invoke remote endpoints. Typically, it is used from a Share tier web script to invoke a web script on the repository tier. But it can be used to hit any end point. If you need to do this from a Share tier web script, you should be able to configure a new end point and then invoke it using the remote object. Take a look at the slingshot-application-context.xml and find the bean with an id of "webframework.store.remote.abstract" to see how the default "alfresco" endpoint is configured.

Because the repository tier and the share tier use the same web script engine, you *might* be able to use the remote object from a repository tier web script. The problem is that the object is disabled by default (for security reasons I don't quite understand). I haven't done this since 3.0 but back then I did successfully re-enable the remote object on the repository tier so that I could invoke third-party services using the remote object in a repository tier web script. I have no idea what is involved in doing that in more recent releases, but it might be worth a look.

In any case, regardless of the tier you may find it helpful to understand how Surf defines endpoints and how it handles authentication and so on. Here is an old, but potentially interesting presentation from the Surf Code Camps we did a long time ago.

Option 2: Assuming you are open to switching to Java

If you were to use Java, you could use a class like HttpClient or similar to invoke a web script. Piergiorgio has provided you with links to Java-based web scripts because once you are in Java, the world is your oyster. Google should be able to point you to many examples of Java code invoking a service and getting JSON back.

A hybrid approach would be to write Java code that invokes the service as just described and then expose that Java class as a JavaScript root object. Then you'd still be able to use a JavaScript-based web script. This hybrid approach is common when you want to make it easy for web script developers to write scripts, but you've got some heavy lifting that Java is better suited for. There are numerous examples in the Alfresco source that wrap Java services as a JavaScript root object. Look in the org.alfresco.repo.jscript package for examples. This involves basically writing the wrapper class then telling Spring about it.

Hopefully this gets you pointed in the write direction.

Jeff

drozes
Champ in-the-making
Champ in-the-making
Thanks for the reply John, your replies always are the best.

I decided to add a library to allow remotes calls from repo tier.

Used this link as a perfect and fast guide (took 10 minutes with making webscripts to test)
http://www.unorganizedmachines.com/site/software-and-technology/34-software-development/97-calling-w...

drozes
Champ in-the-making
Champ in-the-making
Would love to understand how to expose the native 'remote' object to the repo tier.

Do i need to get a JAR for it, or can i reference the one the webclient is using?

Anyone have a guide?