SOAP in Gecko-based Browsers

  • Revision slug: SOAP_in_Gecko-based_Browsers
  • Revision title: SOAP in Gecko-based Browsers
  • Revision id: 129081
  • Created:
  • Creator: Dria
  • Is current revision? No
  • Comment

Revision Content

Migrating from devedge page here. Not yet complete.

This article shows how to access web services using the SOAP and JavaScript capabilities available in recent Gecko-based browsers.

Introduction

Simple Object Access Protocol (SOAP) is the basis on which web services are built. It is an XML-based protocol used to communicate and interoperate with web services. With Mozilla 1.0 (upon which Netscape 7.0x is built), it is now possible for the browser to directly communicate with web services using its low-level SOAP implementation through JavaScript.

Gecko's JavaScript interface for establishing SOAP calls is a low-level API which requires one to build the SOAP envelope using several SOAP-specific JavaScript objects. This article will cover the basic SOAP operations, for a more detailed look at the low-level SOAP API in Gecko click here.(Find the equivalent to "soap scripts in mozilla")

Using JavaScript to talk to web services is subject to the same security policies as other scripts in terms of cross domain access. Therefore, accessing web services on a server other than the one where the JavaScript lives will violate the cross-domain policy. This article will show how to temporarily circumvent this for testing purposes.

Setting Up a SOAP Call

The most basic object is SOAPCall, which is used to initiate and invoke a SOAP call.

Figure 1 : Basic setup and invocation of a SOAP Call

var mySOAPCall = new SOAPCall();
mySOAPCall.transportURI = "http-based service URI"
mySOAPCall.encode(0, "method name ", "target object namespaceURI ", 0, null, SOAPParameter array.length, SOAPParameter array);
var response = mySOAPCall.invoke();

A SOAPCall object has a member called transportURI, which is the URI of the location where it should send the SOAP call to. The encode method requires the name of the method to call at the web service, its namespaceURI, the amount of SOAPParameters passed in, and an array of SOAPParameters which contains all the parameters. All these parameters can be found in the WSDL file for the web service, which is discussed in the Example section.

SOAP parameters are created using the SOAPParameter object. They are name/value pairs that are passed to the web service.

Figure 2 : Creating a SOAP parameter

var param = new SOAPParameter();
param.name = "translationmode";
param.value = "en_fr";


Handling a Response

Once invoke is called, Gecko generates the SOAP envelope and sends it to the URI specified. Since the request is synchronous, the response is the return value of invoke.

Figure 3 : Handling the response of a SOAP call

 var returnObject = mySOAPCall.invoke();

if(returnObject.fault){
  alert("An error occured: " + returnObject.fault.faultString);
} else {
  var response = new Array();
  response = returnObject.getParameters(false, {});
  alert("Return value: " + response[0].value);
}

The return value of invoke is stored and checked for a fault member. If it exists, an error occurred at the web service, and the error message is stored in fault.faultString. If fault doesn't exist, we call the getParameters function on the returned object to retrieve the returned SOAPParameters.

Example

The example uses an existing web service, Babelfish, which is provided by xmethods.net. The Babelfish web service allows translating between several languages. It takes as an input two parameter: a string in the format of "originalLanguage_resultLanguage" and the text to translate as another string. The WSDL file for the Babelfish web service is available here and contains all the information needed to setup a low-level SOAP call to the web service.

The first step is to figure out the location of the web service, which will be the value of the SOAPCall's transportURI member. This can be found in the WSDL's service element, specifically the location attribute of soap:address.

Figure 4 : Figuring out the location of a web service from its WSDL

<b>WSDL:</b>
<service name="BabelFishService">
  <documentation>
    Translates text of up to 5k in length, between a variety of languages.
  </documentation>
  <port name="BabelFishPort" binding="tns:BabelFishBinding">
    <soap:address location="http://services.xmethods.net:80/perl/soaplite.cgi"/>
  </port>
<service>

<b>JavaScript:</b>
var babelFishCall = new SOAPCall();
babelFishCall.transportURI = "http://services.xmethods.net:80/perl/soaplite.cgi";
...

The next step is the most complex one - figuring out exactly what the web service expects to be sent in terms of parameters. The Babelfish web service only has one method, "BabelFish", which in the WSDL is represented in operation tags, which is a child of the portType element. Each WSDL operation has two children: the input and output elements, which contain the type expected. The types are defined in message elements, of which there are two: BabelFishRequest, which is what is going to be passed into the web service, and BabelFishResponse, the return type. As can be seen, the operation BabelFish expects two in parameters: translationmode and sourcedata. The example will translate from english to french the string "I am".

Tracking the Soap Envelope

Resources

Revision Source

<div class="note">
<p>Migrating from devedge page <a class="external" href="http://devedge-temp.mozilla.org/viewsource/2003/soap/01/index_en.html">here</a>.  Not yet complete.
</p>
</div>
<p>This article shows how to access web services using the SOAP and JavaScript capabilities available in recent Gecko-based browsers.</p>
<h3 name="Introduction"> Introduction </h3>
<p>Simple Object Access Protocol (SOAP) is the basis on which web services are built. It is an XML-based protocol used to communicate and interoperate with web services. With Mozilla 1.0 (upon which Netscape 7.0x is built), it is now possible for the browser to directly communicate with web services using its low-level SOAP implementation through JavaScript.</p>
<p>Gecko's JavaScript interface for establishing SOAP calls is a low-level API which requires one to build the SOAP envelope using several SOAP-specific JavaScript objects. This article will cover the basic SOAP operations, for a more detailed look at the low-level SOAP API in Gecko click here.(Find the equivalent to "soap scripts in mozilla")</p>
<p>Using JavaScript to talk to web services is subject to the same security policies as other scripts in terms of cross domain access. Therefore, accessing web services on a server other than the one where the JavaScript lives will violate the cross-domain policy. This article will show how to temporarily circumvent this for testing purposes. </p>
<h3 name="Setting_Up_a_SOAP_Call"> Setting Up a SOAP Call </h3>
<p>The most basic object is SOAPCall, which is used to initiate and invoke a SOAP call.</p>
<p>Figure 1 : Basic setup and invocation of a SOAP Call
</p>
<pre>var mySOAPCall = new SOAPCall();
mySOAPCall.transportURI = "http-based service URI"
mySOAPCall.encode(0, "method name ", "target object namespaceURI ", 0, null, SOAPParameter array.length, SOAPParameter array);
var response = mySOAPCall.invoke();
</pre>
<p> A SOAPCall object has a member called transportURI, which is the URI of the location where it should send the SOAP call to. The encode method requires the name of the method to call at the web service, its namespaceURI, the amount of SOAPParameters passed in, and an array of SOAPParameters which contains all the parameters. All these parameters can be found in the WSDL file for the web service, which is discussed in the Example section.</p>
<p>
SOAP parameters are created using the SOAPParameter object. They are name/value pairs that are passed to the web service. </p>
<p>Figure 2 : Creating a SOAP parameter
</p>
<pre>var param = new SOAPParameter();
param.name = "translationmode";
param.value = "en_fr";

</pre>
<p><br>
</p>
<h3 name="Handling_a_Response"> Handling a Response </h3>
<p> Once invoke is called, Gecko generates the SOAP envelope and sends it to the URI specified. Since the request is synchronous, the response is the return value of invoke.  </p>
<p>Figure 3 : Handling the response of a SOAP call</p>
<pre> var returnObject = mySOAPCall.invoke();

if(returnObject.fault){
  alert("An error occured: " + returnObject.fault.faultString);
} else {
  var response = new Array();
  response = returnObject.getParameters(false, {});
  alert("Return value: " + response[0].value);
}
</pre>
<p>
 The return value of invoke is stored and checked for a fault member. If it exists, an error occurred at the web service, and the error message is stored in fault.faultString. If fault doesn't exist, we call the getParameters function on the returned object to retrieve the returned SOAPParameters.
</p>
<h3 name="Example"> Example </h3>
<p> The example uses an existing web service, Babelfish, which is provided by xmethods.net. The Babelfish web service allows translating between several languages. It takes as an input two parameter: a string in the format of "originalLanguage_resultLanguage" and the text to translate as another string. The WSDL file for the Babelfish web service is available here and contains all the information needed to setup a low-level SOAP call to the web service.</p>
<p>
The first step is to figure out the location of the web service, which will be the value of the SOAPCall's transportURI member. This can be found in the WSDL's service element, specifically the location attribute of soap:address.   </p>
<p>Figure 4 : Figuring out the location of a web service from its WSDL</p>
<pre>&lt;b&gt;WSDL:&lt;/b&gt;
&lt;service name="BabelFishService"&gt;
  &lt;documentation&gt;
    Translates text of up to 5k in length, between a variety of languages.
  &lt;/documentation&gt;
  &lt;port name="BabelFishPort" binding="tns:BabelFishBinding"&gt;
    &lt;soap:address location="http://services.xmethods.net:80/perl/soaplite.cgi"/&gt;
  &lt;/port&gt;
&lt;service&gt;

&lt;b&gt;JavaScript:&lt;/b&gt;
var babelFishCall = new SOAPCall();
babelFishCall.transportURI = "http://services.xmethods.net:80/perl/soaplite.cgi";
...
</pre>
<p> The next step is the most complex one - figuring out exactly what the web service expects to be sent in terms of parameters. The Babelfish web service only has one method, "BabelFish", which in the WSDL is represented in operation tags, which is a child of the portType element. Each WSDL operation  has two children: the input and output elements, which contain the type expected. The types are defined in message  elements, of which there are two: BabelFishRequest, which is what is going to be passed into the web service, and BabelFishResponse, the return type. As can be seen, the operation BabelFish expects two in parameters: translationmode  and sourcedata. The example will translate from english to french the string "I am".</p>
<h3 name="Tracking_the_Soap_Envelope"> Tracking the Soap Envelope </h3>
<p>  </p>
<h3 name="Resources"> Resources </h3>
<p>  </p>
Revert to this revision