Showing posts with label webservices. Show all posts
Showing posts with label webservices. Show all posts

Tuesday, September 22, 2009

Invoking Web services using stubs

I've been using Keith's tutorial on "Invoking Web Services from a Mashup" when testing the stub generating and use of generated stub features in WSO2 Mashup Server.

This note is an elaborated version of stubs part of that article, containing the same information in a more detail manner where a less-techy person like me can follow effortlessly.


Why do you need a stub :
Stub is used to generate the soap message for us to send the request and recieve the response over the wire.
It is a Java class that is statically bound to a service endpoint interface. A stub, or a client proxy object, defines all the methods that the service endpoint interface defines. Therefore, the client can invoke methods of a web service directly via the stub. The advantage of this is that it is simple and easy to code. The disadvantage is that the slightest change of web service definition lead to the stub being useless... and this means the stub must be regenerated. It is advisable to use the static stub technique if you know that the web service is stable and is not going to change its definition. Static stub is tied to the implementation. In other words, it is implementation-specific.



Invoking an external service in asynchronous manner
----------------------------------------------------------------------

I'll be invoking a service residing externally in http://www.webservicex.net. WSDL url of this is http://www.webservicex.net/CurrencyConvertor.asmx?WSDL.


1. Go to stubs generator in WSO2 Mashup Server 2.0.0 and select to generate an E4X stub for the above wsdl.




2. Copy and paste this stub to a note pad.

3. Deploy the following service in the Mashup Server.

system.include("currencyConvertorStub.js"); 

convertionRateUsingStub.inputTypes={"fromCurrency" : "string", "toCurrency" : "string"}; 
convertionRateUsingStub.outputType="string"; 
function convertionRateUsingStub(fromCurrency, toCurrency){ 
var rate =  CurrencyConvertor.ConversionRate(fromCurrency, toCurrency); 
return "The conversion rate is " + rate; 
}

4. When a service is deployed in the mashup server a folder with the syntax 'servicename.resources' will be created in the MASHUP_HOME/repository/scripts folder.
So if our service above was deployed as "CurrenctConversion.js", there will be a folder called "CurrenctConversion.resources".


5. Now go to the stub that you pasted in to a note pad, save it in MASHUP_HOME/repository/scripts/CurrenctConversion.resources as "currencyConvertorStub.js"

6. Access the tryit of "CurrenctConversion" javascripts service.

7. It'll be doing the currency coversion using the stub generated from the WSDL. ????/



Invoking an external service in synchronous manner
----------------------------------------------------------------------

I'll be invoking the same service using http://www.webservicex.net/CurrencyConvertor.asmx?WSDL.

1. Go to stubs generator in WSO2 Mashup Server and select to generate an E4X stub for the above service.

2. Copy and paste this stub to a note pad.


3. Deploy the following service in the Mashup Server.

system.include("currencyConvertorStub.js");

convertionRateUsingStubAsync.inputTypes={"fromCurrency" : "string", "toCurrency" : "string"};
convertionRateUsingStubAsync.outputType="string";
function convertionRateUsingStubAsync(fromCurrency, toCurrency){
CurrencyConvertor.ConversionRate.callback = success;
CurrencyConvertor.ConversionRate.onError = failure;
CurrencyConvertor.ConversionRate(fromCurrency, toCurrency);
return "Invoked the ConversionRate operation in a async manner";
}

success.visible=false;
function success(ConversionRateResponse) {
system.log("The response of ConversionRate was : " + ConversionRateResponse);
}

failure.visible=false;
function failure(error) {
system.log("Error occured while calling ConversionRate, Reason is : " + error.reason);
}


4. When a service is deployed in the mashup server a folder with the syntax 'servicename.resources' will be created in the MASHUP_HOME/repository/scripts folder.
So if our service above was deployed as "CurrenctConversion_sync.js", there will be a folder called "CurrenctConversion_sync.resources".


5. Now go to the stub that you pasted in to a note pad, save it in MASHUP_HOME/repository/scripts/CurrenctConversion.resources as "currencyConvertorStub.js"


6. Access the tryit of "CurrenctConversion_sync" javascripts service.




Invoking a service secured using Username Token authentication
-----------------------------------------------------------------------------------------

1. Sign-in to MS console.

2. Create a service (usernameTokenService.js) using the source below ;

demo.inputTypes={"firstParam" : "string" , "secondParam" : "string"};
demo.outputType="string";
function demo(firstParam, secondParam){
return "user " + request.authenticatedUser + " called the demo function with params " + firstParam + " and " + secondParam;
}

3. Secure the service with Username Token
This can be done from Manage > Services > List > [ServiceName] > Service Dashboard > Security

4. Create another service (invokeSecuredService.js) using the source below;

system.include("usernameTokenServiceStub.js");

invokeSecuredService.inputTypes={"firstParam" : "string" , "secondParam" : "string"};
invokeSecuredService.outputType="string";
function invokeSecuredService(firstParam, secondParam) {
usernameTokenService.username = "yourUsername";
usernameTokenService.password = "yourPassword";
return usernameTokenService.demo(firstParam, secondParam);
}

5. Generate the stub for "usernameTokenService" service and save it as usernameTokenServiceStub.js



6. Copy it to the .resource folder of the "invokeSecuredService" service we created in step 4

7. tryit? the second service (invokeSecuredService).

Featured

Selenium - Page Object Model and Action Methods

  How we change this code to PageObjectModel and action classes. 1 2 3 driver . findElement ( By . id ( "userEmail" )). sendKeys (...

Popular Posts