Question

I'm trying to create a mashup in WSO2 AS 5.1.0. I have managed to succesfully create a simple HelloWorld service, however when I try to integrate other service into it, I get an error.

This is my HelloWorld service:

this.documentation = "This is a test Hello World service";
system.include("HelloStub.js");

hello.documentation = "say hello"
hello.inputTypes = {"user": "string"}
hello.outputType = "string";
function hello(user){
    try{
        var response = services["admin/testmashup"].operations["sayMyName"](user);
    }catch(e){
        return "Danger, Robinson! " + e.toString()
    }
    return "Hello, there! " + response;
}

function whoAreYou(){
    try{
        var response = services["admin/testmashup"].operations["toString"]();
    }catch(e){
        return "Danger, Robinson! " + e.toString()
    }
    return "Hello! " + response;    
}

And this is the admin/testmashup service

this.serviceName = "testmashup";
this.documentation = "Test mashup service" ;

toString.documentation = "say something" ;
toString.inputTypes = { /* no arguments */ };
toString.outputType = "string"; 
function toString()
{
   return "Hi, my name is testmashup";
}

sayMyName.documentation = "Make me feel happy";
sayMyName.inputTypes = {"myName":"string"};
sayMyName.outputType = "string";
function sayMyName(myName){
    return "Your very beautiful name is " + myName;
}

I must note that when I call the admin/testmashup service, it works as expected.

The file HelloStub.js is a Javascript (E4X) stub, generated by the WSO2 Applicanton Server.

When I test the operation whoAreYou, which has no arguments, I get the following response:

<ws:whoAreYouResponse xmlns:ws="http://services.mashup.wso2.org/helloWorld?xsd">
   <return xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:js="http://www.wso2.org/ns/jstype" js:type="string" xsi:type="xs:string">Hello! &lt;ws:toStringResponse xmlns:ws="http://services.mashup.wso2.org/testmashup?xsd"&gt;&lt;return&gt;Hi, my name is testmashup&lt;/return&gt;&lt;/ws:toStringResponse&gt;</return>
</ws:whoAreYouResponse>

I can see the text Hi, my name is testmashup within the encoded response. But when I try to call hello, with the following xml:

<body>
   <p:hello xmlns:p="http://services.mashup.wso2.org/helloWorld?xsd">
      <!--Exactly 1 occurrence-->
      <user>John</user>
   </p:hello>
</body>

I get the following error:

<ws:helloResponse xmlns:ws="http://services.mashup.wso2.org/helloWorld?xsd">
   <return>Danger, Robinson! org.wso2.carbon.CarbonException: Invalid input for the payload in WSRequest Hostobject : John</return>
</ws:helloResponse>

I have tried to make this work for the last couple of days and have searched all over the place for an answer, but I can't seem to find it. The official documentation does not provide an example using stubs of external webservices that have operations with one or more arguments.

Also, if it is possible, I would like to know how to consume REST-JSON services from a javascript mashup.

Any ideas?

Was it helpful?

Solution

It turns out there is an important bunch of information missing in the official documentation. In particular, regading my issue, it is important to know that internally the generated stub uses BagderFish notation to perform the conversion between XML and JSON. I went into the stub and dived a bit and foud this piece of code, concerning to my sayMyName method:

service.operations['sayMyName'] = function (request) {
    var isAsync, response, resultValue;
    var operation = service.operations['sayMyName'];
    request = typeof request === "string" ? request : utils.bf2xml(request);
    service.$._options = new Array();
    .
    .
    .

The function bf2xml converts JSON in BadgerFish notation to XML. The function is defined later in the stub, and can be found here too. Information on BadgerFish notation is widely available also.

In order to make the HelloWorld service work properly, you can pass either a string with the XML (in that case the bf2xml function will not be called), or a JSON object with the correct notation, i.e, BadgerFish.

This will work:

var response = services["admin/testmashup"].operations["sayMyName"]({request : {myName:{$:user}}});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top