forked from chameleonbr/node-red-contrib-soap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
soap-node.js
69 lines (67 loc) · 3.07 KB
/
soap-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
module.exports = function (RED) {
function SoapCall(n) {
var soap = require('soap');
RED.nodes.createNode(this, n);
this.topic = n.topic;
this.name = n.name;
this.wsdl = n.wsdl;
this.server = RED.nodes.getNode(this.wsdl);
this.method = n.method;
this.payload = n.payload;
var node = this;
this.status({});
try {
node.on('input', function (msg) {
var server = (msg.server)?{wsdl:msg.server, auth:0}:node.server;
var lastFiveChar = server.wsdl.substr(server.wsdl.length-5);
if(server.wsdl.indexOf("://")>0 && lastFiveChar !== '?wsdl'){
server.wsdl += '?wsdl';
}
soap.createClient(server.wsdl, msg.options||{}, function (err, client) {
if (err) {
node.status({fill: "red", shape: "dot", text: "WSDL Config Error: " + err});
node.error("WSDL Config Error: " + err);
return;
}
switch (node.server.auth) {
case '1':
client.setSecurity(new soap.BasicAuthSecurity(server.user, server.pass));
break;
case '2':
client.setSecurity(new soap.ClientSSLSecurity(server.key, server.cert, {}));
break;
case '3':
client.setSecurity(new soap.WSSecurity(server.user, server.pass));
break;
case '4':
client.setSecurity(new soap.BearerSecurity(server.token));
break;
}
node.status({fill: "yellow", shape: "dot", text: "SOAP Request..."});
if(msg.headers){
client.addSoapHeader(msg.headers);
}
if(client.hasOwnProperty(node.method)){
client[node.method](msg.payload, function (err, result) {
if (err) {
node.status({fill: "red", shape: "dot", text: "Service Call Error: " + err});
node.error("Service Call Error: " + err);
return;
}
node.status({fill:"green", shape:"dot", text:"SOAP result received"});
msg.payload = result;
node.send(msg);
});
} else {
node.status({fill:"red", shape:"dot", text:"Method does not exist"});
node.error("Method does not exist!");
};
});
});
} catch (err) {
node.status({fill: "red", shape: "dot", text: err.message});
node.error(err.message);
}
}
RED.nodes.registerType("soap request", SoapCall);
};