WSRequest Host Object

1.0 Introduction

The WSO2 Mashup Server automatically generates stubs to simplify the consumption of the Web Services it hosts. But if you're consuming a service from somewhere else, we don't yet provide the same level of ease. Instead of a stub which hides some of the details of exercising an operation, you must use the WSRequest object directly to formulate the messages.

The WSRequest object is similar to the XMLHTTPRequest object. It's usage typically involves specifying the endpoint address and setting up options on how to frame the message, invoking the operation with a specific XML payload, and then checking and extracting information from the result.

1.1 Example

function invokeGetVersion(){
var version = new WSRequest();
var options = new Array();
options.useSOAP = 1.2;
options.useWSA = 1.0;
options.action = "http://services.mashup.wso2.org/version/ServiceInterface/getVersionRequest";
var payload = null;
try {
version.open(options,"http://localhost:7762/services/system/version", false);
version.send(payload);
result = version.responseE4X;
} catch (e) {
system.log(e.toString(),"error");
return e.toString();
}
return result;
}

2.0 WSRequest Object

2.1 WSRequest Interface

interface WSRequest
{
    property EventListener onreadystatechange;
    property unsigned short readyState;
    function void open ( object options | String httpMethod, String url [, boolean async [, String username [, String password]]]);
    function void send ( XML payload | XMLString payload );  // To be used when the open method of WSRequest is used  
    function void openWSDL ( String wsdlURL, boolean async, [Object options,[QName serviceName, [String endpointName]]]);
    function void send ( String operationName | QName operationName, XML payload | XMLString payload );  // To be used when the openWSDL method of WSRequest is used
    readonly property String responseText;
    readonly property Document responseXML;
    readonly property XML responseE4X;
    readonly property WebServiceError error;
}

2.2 API Documentation

Member Description
function void open ( object options | String httpMethod, String url [, boolean async [, String username [, String password]]]) This method prepares the WSRequest object to invoke a Web service.  It accepts the following parameters:
  • options (array): an array of options for formulating the message.  These options correspond to the message framing required by the service as documented in the service's WSDL and/or documentation.  
The following is a list of supported options
    • useSOAP - Specify the SOAP version to be used or weather not to use SOAP. Supported values are "1.1" | "1.2" | "false"
    • HTTPMethod - The HTTP method to be used. Supported values are "GET" | "POST" | "PUT" | "DELETE"
    • useWSA - Weather WS-Addressing needs to be used. Supported values are "1.0" | "submission" | "true". If the version of WS-Addressing is specified and useWSA is set to true the "1.0" version is used.
When WS-Addressing is been used the following options will also be processed
      • action - Will be used as the wsa:to value. Can be used to specify to address that the message will be sent to.
      • from  -  Will be used as the wsa:from value. Can be used to specify the address the request was sent from.
      • replyTo  -  Will be used as the wsa:replyTo value. Can be used to specify the address the response should be sent to.
      • faultTo  -  Will be used as the wsa:faultTo value. Can be used to specify the address a fault should be sent to.
    • action - If useWSA is false will be used as the SOAPAction.
    • useWSS - Used to specify weather WS-Security needs to be used. Currently if useWSS is set to true it assumes that the service is secured using Username Token over HTTPS.
    • username - If the useWSS option is set to true the username will be used to invoke the destination service using Username Token (using WS-Security) otherwise it will be used to invoke the service with HTTP Basic Authentication enabled. 
    • password - If the useWSS option is set to true the password will be used to invoke the destination service using Username Token (using WS-Security) otherwise it will be used to invoke the service with HTTP Basic Authentication enabled.  
    • HTTPLocation - Used to specify the URL pattern of the operation been invoked. Refer http://wso2.org/library/3715 for details.
    • HTTPLocationIgnoreUncited - Specifies weather parameters not cited in the HTTPLocation should be ignored.
    • HTTPQueryParameterSeparator - Specifies the query parameter seperator to be used. Defaults to &.
    • HTTPInputSerialization - Specifies the message format of the request. The Mashup Server can be extended to support custom serialization. The following are supported by this distribution. application/x-www-form-urlencoded, multipart/form-data, application/json, application/json/badgerfish, application/soap+xml, text/xml, application/xml
    • HTTPContentEncoding - Used to speficy the content-encoding of the request. Supported values are "gzip" | "compress"
  • httpMethod (string): the httpMethod to be used when the operation is invoked.
  • url (string): a URL representing where to send the message.
  • async (boolean): a boolean flag representing whether the operation should be invoked asynchrounously or not. Defaults to true.
  • username (string): The username. If the useWSS option is set to true the username will be used to invoke the destination service using Username Token (using WS-Security) otherwise it will be used to invoke the service with HTTP Basic Authentication enabled. 
  • password (string): The password. If the useWSS option is set to true the password will be used to invoke the destination service using Username Token (using WS-Security) otherwise it will be used to invoke the service with HTTP Basic Authentication enabled.  

    The open() function throws an exception if the WSRequest object cannot accommodate the request (e.g., the options are malformed.)
To be specific here are the various ways which the open fucntion can be used in
  • open(httpMethod, url, async);
  • open(options, url, async);
  • open(httpMethod, url, username);
  • open(options, url, username);
  • open(httpMethod, url, username, password);
  • open(options, url, username, password);
  • open(httpMethod, url, async, username);
  • open(options, url, async, username);
function void send ( XML payload | XMLString payload ) This method invokes the Web service with the requested payload. To be used when the WSRequest object was configured using the open method.
  • payload: an XML object or a string containing the XML source for the request.
function void openWSDL ( String wsdlURL, boolean async, [Object options,[QName serviceName, [String endpointName]]])This method can be used to invoke a external web service which advertices a WSDL. Using this method had many advantages over using the open method.
  1. The user does not need to know how to frame the request. i.e. Whcih SOAP version to use? What should be the request be sent to? How do I configure WS-Security? and so on
  2. Can be used to invoke services that are secured using WS-Security
This method acceps the following parameters
  • wsdlURL (string) - The url at which the WSDL is available at. Currently supports WSDL 1.1
  • async (boolean): A boolean flag representing whether the operation should be invoked asynchrounously or not.
  • options (array): An array of options needed to parse in information to invoke secured services.
The following is a list of supported options
    • username - Used to specify the username if the service needs a username token to carry out any WS-Security related operations. 
    • password - Used to specify the password if the service needs a password token to carry out any WS-Security related operations. 
    • encryptionUser - Used to specify the alias of the certificate to use in Encryption if the service needs the request to be encrypted.
  • serviceName (QName) - The QName of the service to be invoked. Could be usefull if the WSDL has multiple services. If not specified the ffirst service in the WSDL is used.
  • endpointName (String) - The name of the endpoint to use wehn invoking the service. Could be usefull if the service has multiple endpoints and you prefer to use a particular one. If not specified the first SOAP endpoint is used.
function void send ( String operationName | QName operationName, XML payload | XMLString payload ) This method invokes the specified operation of the Web service with the requested payload. To be used when the WSRequest object was configured using the openWSDL method.
  • operationName (String) - The name of the operation to be invoked
  • payload : an XML object or a string containing the XML source for the request.
property EventListener   onreadystatechange This property can be set to a function object, which is invoked when the state of an asynchronous request changes (e.g. the request completes).
property unsigned short readyState The current state of the object, which can be one of the following values:
  • 0: The object has not been initialized by calling the open() method.
  • 1: The object has been initialized successfully, but the send() method has not been called.
  • 2: The request is pending
  • 3: The request is partially complete (some data has been received, and may be available in the responseText property.
  • 4: The request is complete, all data has been received.
    Of these, typically only the last (readyState == 4) is used.
readonly property Document responseXML The parsed XML message representing the response from the service. (Currently this is same as responseE4X, but this will be fixed to return a DOM document in the future versions)
readonly property XML responseE4X The parsed E4X XML message representing the response from the service.
readonly property String responseText The raw text representing the XML (or non-XML) response.  If the responseXML property is empty, you can check the responseText property to see if a non-XML response was received.
readonly property WebServiceError error When an asynchronous operation failed to complete successfully (including internal errors, or protocol errors such as SOAP faults) the error property is a WebServiceError object

3.0 Example Usage

2.1 Calling Services in a asynchronous manner

Its important to note that when a service is called in the manner below (assynchronously) the thread will return to the caller immidiatly (Even before the result of the web service invocation is received).
// Demonstrates calling the getVersion operation of the version service in a asynchronous manner using the open method
function invokeGetVersionAsync(){
var version = new WSRequest();
version.onreadystatechange = function() {
handleResponse(version);
};
var options = new Array();
options.useSOAP = 1.2;
options.useWSA = 1.0;
options.action = "http://services.mashup.wso2.org/version/ServiceInterface/getVersionRequest";
var payload = null;
try {
version.open(options,"http://localhost:7762/services/system/version", true);
version.send();
} catch (e) {
system.log(e.toString(),"error");
return e.toString();
}
return "Invoked getVersion in a asynchronous manner";
}

handleResponse.visible=false;
function handleResponse(version){
if (version.readyState == 4) {
system.log(version.responseText);
}
}
// Demonstrates calling the getVersion operation of the version service in a asynchronous manner using the openWSDL method
function invokeGetVersionAsync(){
var request = new WSRequest();
request.onreadystatechange = function() {
handleResponse(request);
};
request.openWSDL("http://localhost:7762/services/system/version?wsdl",true);
request.send("getVersion",null);
return "Invoked getVersion in a asynchronous manner";
}

handleResponse.visible=false;
function handleResponse(version){
if (version.readyState == 4) {
system.log(version.responseText);
}
}

2.1 Calling Services which are secured using WS-Security

Calling secured services using the WSO2 Mashup Server is simple. The client given below can handle sevaral security scenarios provided that the proper certificate provisioning has taken place.
// Demonstrates calling the getVersion operation of the version service. This client will work for most 
// WS-Security scenarios (Works for all the scenarios the Mashup Server ships)
function invokeGetVersion(){
var request = new WSRequest();
var options = new Array();
options["username"] = "keith";
// Will be used if the service policy requires a Username Token. Assuming that access has been granted to keith
options["password"] = "keith";
// Will be used if the service policy requires a Username Token. Assuming that access has been granted to keith
 options["encryptionUser"] = "versionCert";
// The alias of the certificate that will be used to encrypt the request.
// This is the public certificate of the Keystore that the version service is secured with.
// If the security policy of the version service needs the request signed the client will do so.
// But the keystore of the version service needs to have keith's (the clients) public certificate
var service = new QName("http://services.mashup.wso2.org/version","version");
request.openWSDL("http://localhost:7762/services/system/version?wsdl",true, options,service,"SecureSOAP11Endpoint");
request.send("getVersion",null);
return request.responseXML;
}
See Ideas for evolving WSRequest for additional options under development.

4.0 References

© 2007-2008 WSO2 Inc.