cancel
Showing results for 
Search instead for 
Did you mean: 

Calling Web Service From Javascript

ghernando
Champ in-the-making
Champ in-the-making
I've read how with Surf webscript controllers can call web services from Javascript code using remote.connect().
I want to be able to call an external non-alfresco REST web service from a rule applied to a folder that is based on a Javascript action.
I'm not sure, but it seems like using the Surf framework remote.connect() will not work with rules, since rules work with both the Explorer and Share clients.  Is that right?
If Surf remote.connect() isn't available, then what is a good approach for calling Web services from Javascript action scripts?

Thanks
3 REPLIES 3

jpotts
World-Class Innovator
World-Class Innovator
Old thread, but thought I'd reply anyway…

Surf and web scripts are completely unrelated to rules other than the fact that both can have their logic expressed in JavaScript.

Server-side JavaScript can wrap Alfresco classes. So one way to do what you are talking about is to call the external service from Java and then expose that class and one or more methods to JavaScript much in the same way that the "search" root object (and many others) in JavaScript is just a thin wrapper around a Java-based service.

Jeff

paulo_emerique
Champ in-the-making
Champ in-the-making
Hi, we are sucessfully calling web services from JS, using this: http://www.unorganizedmachines.com/site/software-and-technology/34-software-development/97-calling-w...

What worked for us is "second option" in sample code, that is: using XMLHttpRequest works ok, SimpleHttpConnection didn't work.

Cheers,

Paulo Cesar Emerique
eSense Tecnologia
Brazil

abarisone
Star Contributor
Star Contributor
Hi all,
I think another possible approach is to expose a new Javascript object as suggested by jpotts:

<bean id="yourService" parent="baseJavaScriptExtension"
    class="com.yourcompany.alfresco.services.ScriptableYourServiceWrapper">
        <property name="extensionName">
            <value>yourService</value>
        </property>
        <property name="yourService" ref="externalServicesClient" />
</bean>
and then, as in my case, use the Apache CXF framework http://cxf.apache.org/ which is built on Spring and provides you more flexibility.

The ScriptableYourServiceWrapper is nothing but this:
public class ScriptableYourServiceWrapper extends BaseScopableProcessorExtension implements YourService{

   private yourService yourServiceClient;
   … your methods

}
and the YourServiceManager is something like this:
public class yourServiceManager implements YourService{

private YourService yourServiceServiceImpl;
   
   public void setYourService(YourService yourServiceClient) {
        this.yourServiceServiceImpl = yourServiceClient;
    }
}

The context for the external service is:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws"

    xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd
         http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

    <context:annotation-config />

    <jaxws:client id="yourServiceCxfClient"
        serviceClass="yourServiceWebServiceSEIClass"
        address="${yourServiceWebServiceURL.wsdl.url}" />

    <bean id="yourServiceService"
        class="yourServiceServiceImpl">
        <property name="cxfYourServiceClient" ref="yourServiceCxfClient" />
    </bean>

    <bean id="externalServicesClient"
        class="yourServiceManager">
        <property name="yourService" ref="yourServiceService" />
    </bean>

</beans>
The stubs for the service are obtained form the WSDL using a WSDL2Java command which gives you the Service Endpoint Interface to set as service class in the serviceClass property into the jaxws xml fragment.
Once the context is created, from javascript you only have to invoke:
yourService.yourMethod()
Hope this helps.
Andrea