The SOAP connector enables LoopBack applications to interact with SOAP-based web services.Note: This page was generated from the loopback-connector-soap/README.md.

See also:

loopback-connector-soap

The SOAP connector enables LoopBack applications to interact withSOAP-based web services described usingWSDL.

For more information, see theLoopBack documentation.

Installation

In your application root directory, enter:

  1. $ npm install loopback-connector-soap --save

This will install the module from npm and add it as a dependency to the application’s package.json file.

Overview

There are two ways to use the SOAP connector:

  • Use the LoopBack CLI lb soap command to automatically create a set of models based on a SOAP service WSDL file. Often, this will be the easiest way to connect to a SOAP web service, but may not be suitable for all applications. For more information, see SOAP generator.
  • Write the code manually, calling the loopback-connector-soap and data source APIs directly. This is the approach illustrated here.While both approaches use the loopback-connector-soap data source connector, they appear quite different.

SOAP data source properties

The following table describes the SOAP data source properties you can set in datasources.json.

PropertyTypeDescription
urlStringURL to the SOAP web service endpoint. If not present, defaults to thelocation attribute of the SOAP address for the service/portfrom the WSDL document; for example below it is http://www.webservicex.net/periodictable.asmx:
  1. <wsdl:service name="periodictable"><wsdl:port name="periodictableSoap" binding="tns:periodictableSoap"><soap:address location="http://www.webservicex.net/periodictable.asmx"/&gt;</wsdl:port></wsdl:service>
wsdlStringHTTP URL or local file system path to the WSDL file. Default is ?wsdl.In the example above, it would be http://www.webservicex.net/periodictable.asmx?wsdl.
wsdl_optionsObjectIndicates additonal options to pass to the SOAP connector, for example allowing self signed certificates.For example:
  1. wsdl_options: { rejectUnauthorized: false, strictSSL: false, requestCert: true,}
wsdl_headersObjectIndicates additonal headers to pass to the SOAP connector, for example for sending http authorizations header.For example:
  1. wsdl_headers: { Authorization: "Basic UGVyc29uYWwgYWNjb3VudDpORVdsazIwMTVAKSEl"}
remotingEnabledBooleanIndicates whether the operations are exposed as REST APIs. To expose or hide a specific method, override with:
  1. <Model>.<method>.shared = true | false;
operationsObjectMaps WSDL binding operations to Node.js methods. Each key in the JSONobject becomes the name of a method on the model.See operations property below.
securityObjectsecurity configuration.See security property below.
soapHeadersArray of objects.Custom SOAP headers. An array of header properties. For example:
  1. soapHeaders: [{element: {myHeader: 'XYZ'}, // The XML element in JSON object format prefix: 'p1', // The XML namespace prefix for the header namespace: 'http://ns1&#39; // The XML namespace URI for the header}]

operations property

The operations property value is a JSON object that has a property (key) for eachmethod being defined for the model. The corresponding value is an object with thefollowing properties:

PropertyTypeDescription
serviceStringWSDL service name
portStringWSDL port name
operationStringWSDL operation name

Here is an example operations property for the periodic table service:

  1. operations: {
  2. // The key is the method name
  3. periodicTable: {
  4. service: 'periodictable', // The WSDL service name
  5. port: 'periodictableSoap', // The WSDL port name
  6. operation: 'GetAtomicNumber' // The WSDL operation name
  7. }
  8. }

IMPORTANT: When using the CLI data source generator, you must supply the “stringified JSON” value for this property.For example:

  1. {"getAtomicWeight":{"service":"periodictable","port":"periodictableSoap","operation":"GetAtomicWeight"},"getAtomicNumber":{"service":"periodictable","port":"periodictableSoap","operation":"GetAtomicNumber"}}

To generate the stringified value, you can use the following code (for example):

  1. var operations = {
  2. "operations": {
  3. "getAtomicWeight": {
  4. "service": "periodictable",
  5. "port": "periodictableSoap",
  6. "operation": "GetAtomicWeight"
  7. },
  8. "getAtomicNumber": {
  9. "service": "periodictable",
  10. "port": "periodictableSoap",
  11. "operation": "GetAtomicNumber"
  12. }
  13. }
  14. };
  15. var stringifiedOps = JSON.stringify (operations);
  16. console.log(stringifiedOps);

security property

The security property value is a JSON object with a scheme property.The other properties of the object depend on the value of scheme. For example:

  1. security: {
  2. scheme: 'WS',
  3. username: 'test',
  4. password: 'testpass',
  5. passwordType: 'PasswordDigest'
  6. }
SchemeDescriptionOther properties
WSWSSecurity scheme- username: the user name- password: the password- passwordType: default is 'PasswordText'
BasicAuthBasic auth scheme- username: the user name- password: the password
ClientSSLClientSSL scheme- keyPath: path to the private key file- certPath: path to the certificate file

Creating a model from a SOAP data source

Instead of defining a data source with datasources.json, you can define a data source in code; for example:

  1. ds.once('connected', function () {
  2. // Create the model
  3. var PeriodictableService = ds.createModel('PeriodictableService', {});
  4. // External PeriodTable WebService operation exposed as REST APIs through LoopBack
  5. PeriodictableService.atomicnumber = function (elementName, cb) {
  6. PeriodictableService.GetAtomicNumber({ElementName: elementName || 'Copper'}, function (err, response) {
  7. var result = response;
  8. cb(err, result);
  9. });
  10. };
  11. ...
  12. }

Extending a model to wrap and mediate SOAP operations

You can extend a LoopBack model to wrap or mediate SOAP operationsand define new methods.The following example simplifies the GetAtomicNumber operation:

  1. periodictableperiodictableSoap.GetAtomicNumber = function(GetAtomicNumber, callback) {
  2. periodictableperiodictableSoap.GetAtomicNumber(GetAtomicNumber, function (err, response) {
  3. var result = response;
  4. callback(err, result);
  5. });
  6. }

Creating a model from a SOAP data source

The SOAP connector loads WSDL documents asynchronously.As a result, the data source won’t be ready to create models until it’s connected.The recommended way is to use an event handler for the ‘connected’ event; for exampleas shown below.

Once you define the model, you can extend it to wrap or mediate SOAP operationsand define new methods. The example below shows adding a LoopBack remote methodfor the SOAP service’s GetAtomicNumber operation.

  1. ...
  2. ds.once('connected', function () {
  3. // Create the model
  4. var PeriodictableService = ds.createModel('PeriodictableService', {});
  5. // External PeriodTable WebService operation exposed as REST APIs through LoopBack
  6. PeriodictableService.atomicnumber = function (elementName, cb) {
  7. PeriodictableService.GetAtomicNumber({ElementName: elementName || 'Copper'}, function (err, response) {
  8. var result = response;
  9. cb(err, result);
  10. });
  11. };
  12. // Map to REST/HTTP
  13. loopback.remoteMethod(
  14. PeriodictableService.atomicnumber, {
  15. accepts: [
  16. {arg: 'elementName', type: 'string', required: true,
  17. http: {source: 'query'}}
  18. ],
  19. returns: {arg: 'result', type: 'object', root: true},
  20. http: {verb: 'get', path: '/GetAtomicNumber'}
  21. }
  22. );
  23. })
  24. ...

Example

For a complete example using the LoopBack SOAP connector, see loopback-example-connector. The repository provides examples in the soap branch.

Tags: connectorsreadme