Fork me on GitHub Fork me on GitHub

WSRequest


<%

function invokeGetVersion() {
	var log = new Log();
	var ws = require('ws');

	var version = new ws.WSRequest();
	var options = new Array();
	options.useSOAP = 1.2;
	options.useWSA = 1.0;
	options.action = "urn:getVersion";
	var payload = null;
	var result;

	try {
		version.open(options, "http://stratoslive.wso2.com/services/Version", false);
		version.send(payload);
		result = version.responseE4X;
	} catch (e) {
		log.error(e.toString());
		return e.toString();
	}
	return result;
}

print(invokeGetVersion()); 

%>

Overview

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.


Members

Member Type Description
EventListener onreadystatechange EventListenerThis property can be set to a function object, which is invoked when the state of an asynchronous request changes (e.g. the request completes).
readyState Unsigned Short
var jsonObj = {};
jsonObj.name = "nuwan";
jsonObj.company = "wso2";

A JSON Object

responseXMLDocumentThe 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)
responseE4X * XMLThe parsed E4X XML message representing the response from the service.
responseTextStringThe 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.
error WebService ErrorWhen an asynchronous operation failed to complete successfully (including internal errors, or protocol errors such as SOAP faults) the error property is a WebServiceError object

Operations

Operation Returns Description
open ( object options | String httpMethod, String url [, Boolean async [, String username [, String password]]])None

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 whether 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.
  • mep - Allows you to control the Message Exchange Pattern for an invocation. Supported values are in-only (need to send a request and do not care about the response) and in-out (request - response). Defaults to in-out.
  • username - This will be used as the username to invoke the service with HTTP Basic Authentication enabled.
  • password - This will be used as the password 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"
  • rampart - This property is used to specify rampart configurations for to be used with the service policies. The object pointed by "rampart" key should be in the following format.
    {
        user: "admin", //username for to use UT and other scenarios
        userPassword: "admin", //in UT, this is the user password, else private key
        keyPassword: "client",
        userCertAlias: "client",
        stsAlias: "sts",
        encryptionUser: "server",
        timestampTTL: "10000",
        timestampMaxSkew: "10",
        timestampPrecisionInMilliseconds: "100",
        signatureCrypto: {
            type: "jks", //keystore type
            file: "client.jks", //keystore file relative to resource folder
            password: "client", //keystore password
            enableCryptoCaching: true,
            cacheRefreshInterval: 3000
        },
        encryptionCrypto: {
            type: "jks",
            file: "client.jks",
            password: "client",
            enableCryptoCaching: true,
            cacheRefreshInterval: 3000
        },
        decryptionCrypto: {
            type: "jks",
            file: "client.jks",
            password: "client"
        },
        stsCrypto: {
            type: "jks",
            file: "client.jks",
            password: "client"
        },
        kerberosConfig: {
            /**
    		 * "key" : "value" properties, you can also use ${resources.dir}
    		 * in property values
    		 */
            "client.principal.name": "client",
            "client.principal.password": "client",
            "service.principal.name": "service",
            "java.security.auth.login.config": "jassconfig",
            "javax.security.auth.useSubjectCredsOnly": "true",
            "kdc.des.aes.factor": "4",
            "java.security.krb5.conf": "/home/ruchira/wso2/conf"
        }
    }
    
  • policy - This property is used to specify a custome policy. An XML object should be assigned. You can use ${resources.dir} in keystore paths and Kerberos configurations to get the path of the resources directory. i.e. When you specify a path to a keystore in your resources directory, you can use this. Then it will be replaced with the correct path at the server.
  • HTTPHeaders - Used to specify custom HTTP headers to be send with the request. This should be an array of elements. The value for this property should be in the following format. Both name and value properties should be Strings.
    options["HTTPHeaders"] = [
    	{ name : "HEADER1", value : "VALUE1"},
    	{ name : "HEADER2", value : "VALUE2"},
    	...
    ];
  • SOAPHeaders - Used to specify custom SOAP headers to be send along with the SOAP message. This should be an array of elements. You can specify a header as an XML object or just as an object in the following structure. valueObject could be either a String or an XML object
    options["HTTPHeaders"] = [ 
    	<MyHeader1 xmlns="http://example.com">headerValue</MyHeader1>,
    	...
    	{qName : new QName("http://example.com", "MyHeader2"), value : valueObject},
    	...
    ];
    
  • 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 - This will be used as the username to invoke the service with HTTP Basic Authentication enabled.
  • password - This will be used as the password to invoke the service with HTTP Basic Authentication enabled.

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);
send ( XML payload | XMLString payload )NoneThis 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.
openWSDL ( String wsdlURL, Boolean async, [Object options,[QName serviceName, [String endpointName]]]) None

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.
  • mep - Allows you to control the Message Exchange Pattern for an invocation. Supported values are in-only (need to send a request and do not care about the response) and in-out (request - response). Defaults to in-out.
  • 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.
send ( String operationName | QName operationName, XML payload | XMLString payload ) NoneThis 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.