soap 0.20.0 → 0.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/.editorconfig +23 -23
  2. package/.jshintrc +29 -29
  3. package/.travis.yml +22 -22
  4. package/CONTRIBUTING.md +52 -58
  5. package/History.md +52 -2
  6. package/LICENSE +7 -7
  7. package/PUBLISHING.md +28 -28
  8. package/Readme.md +1062 -931
  9. package/coverage/coverage.json +1 -0
  10. package/coverage/lcov-report/base.css +212 -0
  11. package/coverage/lcov-report/index.html +119 -0
  12. package/coverage/lcov-report/node-soap/index.html +93 -0
  13. package/coverage/lcov-report/node-soap/index.js.html +74 -0
  14. package/coverage/lcov-report/node-soap/lib/client.js.html +1001 -0
  15. package/coverage/lcov-report/node-soap/lib/http.js.html +416 -0
  16. package/coverage/lcov-report/node-soap/lib/index.html +171 -0
  17. package/coverage/lcov-report/node-soap/lib/nscontext.js.html +734 -0
  18. package/coverage/lcov-report/node-soap/lib/security/BasicAuthSecurity.js.html +137 -0
  19. package/coverage/lcov-report/node-soap/lib/security/BearerSecurity.js.html +134 -0
  20. package/coverage/lcov-report/node-soap/lib/security/ClientSSLSecurity.js.html +296 -0
  21. package/coverage/lcov-report/node-soap/lib/security/ClientSSLSecurityPFX.js.html +218 -0
  22. package/coverage/lcov-report/node-soap/lib/security/WSSecurity.js.html +278 -0
  23. package/coverage/lcov-report/node-soap/lib/security/index.html +158 -0
  24. package/coverage/lcov-report/node-soap/lib/security/index.js.html +92 -0
  25. package/coverage/lcov-report/node-soap/lib/server.js.html +1139 -0
  26. package/coverage/lcov-report/node-soap/lib/soap.js.html +314 -0
  27. package/coverage/lcov-report/node-soap/lib/utils.js.html +161 -0
  28. package/coverage/lcov-report/node-soap/lib/wsdl.js.html +6275 -0
  29. package/coverage/lcov-report/prettify.css +1 -0
  30. package/coverage/lcov-report/prettify.js +1 -0
  31. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  32. package/coverage/lcov-report/sorter.js +158 -0
  33. package/coverage/lcov.info +3325 -0
  34. package/index.js +3 -3
  35. package/lib/client.js +434 -425
  36. package/lib/http.js +129 -129
  37. package/lib/nscontext.js +223 -223
  38. package/lib/security/BasicAuthSecurity.js +24 -24
  39. package/lib/security/BearerSecurity.js +23 -23
  40. package/lib/security/ClientSSLSecurity.js +82 -65
  41. package/lib/security/ClientSSLSecurityPFX.js +51 -51
  42. package/lib/security/WSSecurity.js +90 -90
  43. package/lib/security/WSSecurityCert.js +78 -78
  44. package/lib/security/index.js +10 -10
  45. package/lib/security/templates/wsse-security-header.ejs +12 -12
  46. package/lib/security/templates/wsse-security-token.ejs +3 -3
  47. package/lib/server.js +474 -444
  48. package/lib/soap.d.ts +220 -0
  49. package/lib/soap.js +110 -110
  50. package/lib/utils.js +30 -30
  51. package/lib/wsdl.js +2272 -2234
  52. package/package.json +8 -8
  53. package/soap-stub.js +148 -148
  54. package/.npmignore +0 -2
package/lib/client.js CHANGED
@@ -1,425 +1,434 @@
1
- /*
2
- * Copyright (c) 2011 Vinay Pulim <vinay@milewise.com>
3
- * MIT Licensed
4
- */
5
-
6
- "use strict";
7
-
8
-
9
- var HttpClient = require('./http'),
10
- assert = require('assert'),
11
- events = require('events'),
12
- util = require('util'),
13
- debug = require('debug')('node-soap'),
14
- findPrefix = require('./utils').findPrefix,
15
- _ = require('lodash'),
16
- concatStream = require('concat-stream'),
17
- BluebirdPromise = require("bluebird"),
18
- uuid = require('uuid');
19
-
20
- var Client = function(wsdl, endpoint, options) {
21
- events.EventEmitter.call(this);
22
- options = options || {};
23
- this.wsdl = wsdl;
24
- this._initializeOptions(options);
25
- this._initializeServices(endpoint);
26
- this.httpClient = options.httpClient || new HttpClient(options);
27
- BluebirdPromise.promisifyAll(this);
28
- };
29
- util.inherits(Client, events.EventEmitter);
30
-
31
- Client.prototype.addSoapHeader = function(soapHeader, name, namespace, xmlns) {
32
- if (!this.soapHeaders) {
33
- this.soapHeaders = [];
34
- }
35
- if (typeof soapHeader === 'object') {
36
- soapHeader = this.wsdl.objectToXML(soapHeader, name, namespace, xmlns, true);
37
- }
38
- return this.soapHeaders.push(soapHeader) - 1;
39
- };
40
-
41
- Client.prototype.changeSoapHeader = function(index, soapHeader, name, namespace, xmlns) {
42
- if (!this.soapHeaders) {
43
- this.soapHeaders = [];
44
- }
45
- if (typeof soapHeader === 'object') {
46
- soapHeader = this.wsdl.objectToXML(soapHeader, name, namespace, xmlns, true);
47
- }
48
- this.soapHeaders[index] = soapHeader;
49
- };
50
-
51
- Client.prototype.getSoapHeaders = function() {
52
- return this.soapHeaders;
53
- };
54
-
55
- Client.prototype.clearSoapHeaders = function() {
56
- this.soapHeaders = null;
57
- };
58
-
59
- Client.prototype.addHttpHeader = function(name, value) {
60
- if (!this.httpHeaders) {
61
- this.httpHeaders = {};
62
- }
63
- this.httpHeaders[name] = value;
64
- };
65
-
66
- Client.prototype.getHttpHeaders = function() {
67
- return this.httpHeaders;
68
- };
69
-
70
- Client.prototype.clearHttpHeaders = function() {
71
- this.httpHeaders = {};
72
- };
73
-
74
-
75
- Client.prototype.addBodyAttribute = function(bodyAttribute, name, namespace, xmlns) {
76
- if (!this.bodyAttributes) {
77
- this.bodyAttributes = [];
78
- }
79
- if (typeof bodyAttribute === 'object') {
80
- var composition = '';
81
- Object.getOwnPropertyNames(bodyAttribute).forEach(function(prop, idx, array) {
82
- composition += ' ' + prop + '="' + bodyAttribute[prop] + '"';
83
- });
84
- bodyAttribute = composition;
85
- }
86
- if (bodyAttribute.substr(0, 1) !== ' ') bodyAttribute = ' ' + bodyAttribute;
87
- this.bodyAttributes.push(bodyAttribute);
88
- };
89
-
90
- Client.prototype.getBodyAttributes = function() {
91
- return this.bodyAttributes;
92
- };
93
-
94
- Client.prototype.clearBodyAttributes = function() {
95
- this.bodyAttributes = null;
96
- };
97
-
98
- Client.prototype.setEndpoint = function(endpoint) {
99
- this.endpoint = endpoint;
100
- this._initializeServices(endpoint);
101
- };
102
-
103
- Client.prototype.describe = function() {
104
- var types = this.wsdl.definitions.types;
105
- return this.wsdl.describeServices();
106
- };
107
-
108
- Client.prototype.setSecurity = function(security) {
109
- this.security = security;
110
- };
111
-
112
- Client.prototype.setSOAPAction = function(SOAPAction) {
113
- this.SOAPAction = SOAPAction;
114
- };
115
-
116
- Client.prototype._initializeServices = function(endpoint) {
117
- var definitions = this.wsdl.definitions,
118
- services = definitions.services;
119
- for (var name in services) {
120
- this[name] = this._defineService(services[name], endpoint);
121
- }
122
- };
123
-
124
- Client.prototype._initializeOptions = function(options) {
125
- this.streamAllowed = options.stream;
126
- this.wsdl.options.attributesKey = options.attributesKey || 'attributes';
127
- this.wsdl.options.envelopeKey = options.envelopeKey || 'soap';
128
- if(options.ignoredNamespaces !== undefined) {
129
- if(options.ignoredNamespaces.override !== undefined) {
130
- if(options.ignoredNamespaces.override === true) {
131
- if(options.ignoredNamespaces.namespaces !== undefined) {
132
- this.wsdl.options.ignoredNamespaces = options.ignoredNamespaces.namespaces;
133
- }
134
- }
135
- }
136
- }
137
- if(options.overrideRootElement !== undefined) {
138
- this.wsdl.options.overrideRootElement = options.overrideRootElement;
139
- }
140
- this.wsdl.options.forceSoap12Headers = !!options.forceSoap12Headers;
141
- };
142
-
143
- Client.prototype._defineService = function(service, endpoint) {
144
- var ports = service.ports,
145
- def = {};
146
- for (var name in ports) {
147
- def[name] = this._definePort(ports[name], endpoint ? endpoint : ports[name].location);
148
- }
149
- return def;
150
- };
151
-
152
- Client.prototype._definePort = function(port, endpoint) {
153
- var location = endpoint,
154
- binding = port.binding,
155
- methods = binding.methods,
156
- def = {};
157
- for (var name in methods) {
158
- def[name] = this._defineMethod(methods[name], location);
159
- this[name] = def[name];
160
- }
161
- return def;
162
- };
163
-
164
- Client.prototype._defineMethod = function(method, location) {
165
- var self = this;
166
- var temp;
167
- return function(args, callback, options, extraHeaders) {
168
- if (typeof args === 'function') {
169
- callback = args;
170
- args = {};
171
- } else if (typeof options === 'function') {
172
- temp = callback;
173
- callback = options;
174
- options = temp;
175
- } else if (typeof extraHeaders === 'function') {
176
- temp = callback;
177
- callback = extraHeaders;
178
- extraHeaders = options;
179
- options = temp;
180
- }
181
- self._invoke(method, args, location, function(error, result, raw, soapHeader) {
182
- callback(error, result, raw, soapHeader);
183
- }, options, extraHeaders);
184
- };
185
- };
186
-
187
- Client.prototype._invoke = function(method, args, location, callback, options, extraHeaders) {
188
- var self = this,
189
- name = method.$name,
190
- input = method.input,
191
- output = method.output,
192
- style = method.style,
193
- defs = this.wsdl.definitions,
194
- envelopeKey = this.wsdl.options.envelopeKey,
195
- ns = defs.$targetNamespace,
196
- encoding = '',
197
- message = '',
198
- xml = null,
199
- req = null,
200
- soapAction,
201
- alias = findPrefix(defs.xmlns, ns),
202
- headers = {
203
- "Content-Type": "text/xml; charset=utf-8"
204
- },
205
- xmlnsSoap = "xmlns:" + envelopeKey + "=\"http://schemas.xmlsoap.org/soap/envelope/\"";
206
-
207
- if (this.wsdl.options.forceSoap12Headers) {
208
- headers["Content-Type"] = "application/soap+xml; charset=utf-8";
209
- xmlnsSoap = "xmlns:" + envelopeKey + "=\"http://www.w3.org/2003/05/soap-envelope\"";
210
- }
211
-
212
- if (this.SOAPAction) {
213
- soapAction = this.SOAPAction;
214
- } else if (method.soapAction !== undefined && method.soapAction !== null) {
215
- soapAction = method.soapAction;
216
- } else {
217
- soapAction = ((ns.lastIndexOf("/") !== ns.length - 1) ? ns + "/" : ns) + name;
218
- }
219
-
220
- if (!this.wsdl.options.forceSoap12Headers) {
221
- headers.SOAPAction = '"' + soapAction + '"';
222
- }
223
-
224
- options = options || {};
225
-
226
- //Add extra headers
227
- for (var header in this.httpHeaders ) { headers[header] = this.httpHeaders[header]; }
228
- for (var attr in extraHeaders) { headers[attr] = extraHeaders[attr]; }
229
-
230
- // Allow the security object to add headers
231
- if (self.security && self.security.addHeaders)
232
- self.security.addHeaders(headers);
233
- if (self.security && self.security.addOptions)
234
- self.security.addOptions(options);
235
-
236
- if ((style === 'rpc')&& ( ( input.parts || input.name==="element" ) || args === null) ) {
237
- assert.ok(!style || style === 'rpc', 'invalid message definition for document style binding');
238
- message = self.wsdl.objectToRpcXML(name, args, alias, ns,(input.name!=="element" ));
239
- (method.inputSoap === 'encoded') && (encoding = 'soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" ');
240
- } else {
241
- assert.ok(!style || style === 'document', 'invalid message definition for rpc style binding');
242
- // pass `input.$lookupType` if `input.$type` could not be found
243
- message = self.wsdl.objectToDocumentXML(input.$name, args, input.targetNSAlias, input.targetNamespace, (input.$type || input.$lookupType));
244
- }
245
- xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
246
- "<" + envelopeKey + ":Envelope " +
247
- xmlnsSoap + " " +
248
- "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
249
- encoding +
250
- this.wsdl.xmlnsInEnvelope + '>' +
251
- ((self.soapHeaders || self.security) ?
252
- (
253
- "<" + envelopeKey + ":Header>" +
254
- (self.soapHeaders ? self.soapHeaders.join("\n") : "") +
255
- (self.security && !self.security.postProcess ? self.security.toXML() : "") +
256
- "</" + envelopeKey + ":Header>"
257
- )
258
- :
259
- ''
260
- ) +
261
- "<" + envelopeKey + ":Body" +
262
- (self.bodyAttributes ? self.bodyAttributes.join(' ') : '') +
263
- (self.security && self.security.postProcess ? ' Id="_0"' : '') +
264
- ">" +
265
- message +
266
- "</" + envelopeKey + ":Body>" +
267
- "</" + envelopeKey + ":Envelope>";
268
-
269
- if(self.security && self.security.postProcess){
270
- xml = self.security.postProcess(xml, envelopeKey);
271
- }
272
-
273
- if(options && options.postProcess){
274
- xml = options.postProcess(xml);
275
- }
276
-
277
- self.lastMessage = message;
278
- self.lastRequest = xml;
279
- self.lastEndpoint = location;
280
-
281
- var eid = options.exchangeId || uuid.v4();
282
-
283
- self.emit('message', message, eid);
284
- self.emit('request', xml, eid);
285
-
286
- var tryJSONparse = function(body) {
287
- try {
288
- return JSON.parse(body);
289
- }
290
- catch(err) {
291
- return undefined;
292
- }
293
- };
294
-
295
- if (this.streamAllowed && typeof self.httpClient.requestStream === 'function') {
296
- callback = _.once(callback);
297
- var startTime = Date.now();
298
- req = self.httpClient.requestStream(location, xml, headers, options, self);
299
- self.lastRequestHeaders = req.headers;
300
- var onError = function onError(err) {
301
- self.lastResponse = null;
302
- self.lastResponseHeaders = null;
303
- self.lastElapsedTime = null;
304
- self.emit('response', null, null, eid);
305
-
306
- callback(err);
307
- };
308
- req.on('error', onError);
309
- req.on('response', function (response) {
310
- response.on('error', onError);
311
-
312
- // When the output element cannot be looked up in the wsdl, play it safe and
313
- // don't stream
314
- if(response.statusCode !== 200 || !output || !output.$lookupTypes) {
315
- response.pipe(concatStream({encoding: 'string'}, function (body) {
316
- self.lastResponse = body;
317
- self.lastResponseHeaders = response && response.headers;
318
- self.lastElapsedTime = Date.now() - startTime;
319
- self.emit('response', body, response, eid);
320
-
321
- return parseSync(body, response);
322
-
323
- }));
324
- return;
325
- }
326
-
327
- self.wsdl.xmlToObject(response, function (error, obj) {
328
- self.lastResponse = response;
329
- self.lastResponseHeaders = response && response.headers;
330
- self.lastElapsedTime = Date.now() - startTime;
331
- self.emit('response', '<stream>', response, eid);
332
-
333
- if (error) {
334
- error.response = response;
335
- error.body = '<stream>';
336
- self.emit('soapError', error, eid);
337
- return callback(error, response);
338
- }
339
-
340
- return finish(obj, '<stream>', response);
341
- });
342
- });
343
- return;
344
- }
345
-
346
- req = self.httpClient.request(location, xml, function(err, response, body) {
347
- self.lastResponse = body;
348
- self.lastResponseHeaders = response && response.headers;
349
- self.lastElapsedTime = response && response.elapsedTime;
350
- self.emit('response', body, response, eid);
351
-
352
- if (err) {
353
- callback(err);
354
- } else {
355
- return parseSync(body, response);
356
- }
357
- }, headers, options, self);
358
-
359
- function parseSync(body, response) {
360
- var obj;
361
- try {
362
- obj = self.wsdl.xmlToObject(body);
363
- } catch (error) {
364
- // When the output element cannot be looked up in the wsdl and the body is JSON
365
- // instead of sending the error, we pass the body in the response.
366
- if(!output || !output.$lookupTypes) {
367
- debug('Response element is not present. Unable to convert response xml to json.');
368
- // If the response is JSON then return it as-is.
369
- var json = _.isObject(body) ? body : tryJSONparse(body);
370
- if (json) {
371
- return callback(null, response, json);
372
- }
373
- }
374
- error.response = response;
375
- error.body = body;
376
- self.emit('soapError', error, eid);
377
- return callback(error, response, body);
378
- }
379
- return finish(obj, body, response);
380
- }
381
-
382
- function finish(obj, body, response) {
383
- var result;
384
-
385
- if (!output){
386
- // one-way, no output expected
387
- return callback(null, null, body, obj.Header);
388
- }
389
-
390
- if( typeof obj.Body !== 'object' ) {
391
- var error = new Error('Cannot parse response');
392
- error.response = response;
393
- error.body = body;
394
- return callback(error, obj, body);
395
- }
396
-
397
- // if Soap Body is empty
398
- if (!obj.Body) {
399
- return callback(null, obj, body, obj.Header);
400
- }
401
-
402
- result = obj.Body[output.$name];
403
- // RPC/literal response body may contain elements with added suffixes I.E.
404
- // 'Response', or 'Output', or 'Out'
405
- // This doesn't necessarily equal the ouput message name. See WSDL 1.1 Section 2.4.5
406
- if(!result){
407
- result = obj.Body[output.$name.replace(/(?:Out(?:put)?|Response)$/, '')];
408
- }
409
- if (!result) {
410
- ['Response', 'Out', 'Output'].forEach(function (term) {
411
- if (obj.Body.hasOwnProperty(name + term)) {
412
- return result = obj.Body[name + term];
413
- }
414
- });
415
- }
416
-
417
- callback(null, result, body, obj.Header);
418
- }
419
-
420
- // Added mostly for testability, but possibly useful for debugging
421
- if(req && req.headers) //fixes an issue when req or req.headers is indefined
422
- self.lastRequestHeaders = req.headers;
423
- };
424
-
425
- exports.Client = Client;
1
+ /*
2
+ * Copyright (c) 2011 Vinay Pulim <vinay@milewise.com>
3
+ * MIT Licensed
4
+ */
5
+
6
+ "use strict";
7
+
8
+
9
+ var HttpClient = require('./http'),
10
+ assert = require('assert'),
11
+ events = require('events'),
12
+ util = require('util'),
13
+ debug = require('debug')('node-soap'),
14
+ findPrefix = require('./utils').findPrefix,
15
+ _ = require('lodash'),
16
+ concatStream = require('concat-stream'),
17
+ BluebirdPromise = require("bluebird"),
18
+ uuid4 = require('uuid/v4');
19
+
20
+ var nonIdentifierChars = /[^a-z$_0-9]/i;
21
+
22
+ var Client = function(wsdl, endpoint, options) {
23
+ events.EventEmitter.call(this);
24
+ options = options || {};
25
+ this.wsdl = wsdl;
26
+ this._initializeOptions(options);
27
+ this._initializeServices(endpoint);
28
+ this.httpClient = options.httpClient || new HttpClient(options);
29
+ var promiseOptions = { multiArgs: true };
30
+ if (options.overridePromiseSuffix) {
31
+ promiseOptions.suffix = options.overridePromiseSuffix;
32
+ }
33
+ BluebirdPromise.promisifyAll(this, promiseOptions);
34
+ };
35
+ util.inherits(Client, events.EventEmitter);
36
+
37
+ Client.prototype.addSoapHeader = function(soapHeader, name, namespace, xmlns) {
38
+ if (!this.soapHeaders) {
39
+ this.soapHeaders = [];
40
+ }
41
+ if (typeof soapHeader === 'object') {
42
+ soapHeader = this.wsdl.objectToXML(soapHeader, name, namespace, xmlns, true);
43
+ }
44
+ return this.soapHeaders.push(soapHeader) - 1;
45
+ };
46
+
47
+ Client.prototype.changeSoapHeader = function(index, soapHeader, name, namespace, xmlns) {
48
+ if (!this.soapHeaders) {
49
+ this.soapHeaders = [];
50
+ }
51
+ if (typeof soapHeader === 'object') {
52
+ soapHeader = this.wsdl.objectToXML(soapHeader, name, namespace, xmlns, true);
53
+ }
54
+ this.soapHeaders[index] = soapHeader;
55
+ };
56
+
57
+ Client.prototype.getSoapHeaders = function() {
58
+ return this.soapHeaders;
59
+ };
60
+
61
+ Client.prototype.clearSoapHeaders = function() {
62
+ this.soapHeaders = null;
63
+ };
64
+
65
+ Client.prototype.addHttpHeader = function(name, value) {
66
+ if (!this.httpHeaders) {
67
+ this.httpHeaders = {};
68
+ }
69
+ this.httpHeaders[name] = value;
70
+ };
71
+
72
+ Client.prototype.getHttpHeaders = function() {
73
+ return this.httpHeaders;
74
+ };
75
+
76
+ Client.prototype.clearHttpHeaders = function() {
77
+ this.httpHeaders = {};
78
+ };
79
+
80
+
81
+ Client.prototype.addBodyAttribute = function(bodyAttribute, name, namespace, xmlns) {
82
+ if (!this.bodyAttributes) {
83
+ this.bodyAttributes = [];
84
+ }
85
+ if (typeof bodyAttribute === 'object') {
86
+ var composition = '';
87
+ Object.getOwnPropertyNames(bodyAttribute).forEach(function(prop, idx, array) {
88
+ composition += ' ' + prop + '="' + bodyAttribute[prop] + '"';
89
+ });
90
+ bodyAttribute = composition;
91
+ }
92
+ if (bodyAttribute.substr(0, 1) !== ' ') bodyAttribute = ' ' + bodyAttribute;
93
+ this.bodyAttributes.push(bodyAttribute);
94
+ };
95
+
96
+ Client.prototype.getBodyAttributes = function() {
97
+ return this.bodyAttributes;
98
+ };
99
+
100
+ Client.prototype.clearBodyAttributes = function() {
101
+ this.bodyAttributes = null;
102
+ };
103
+
104
+ Client.prototype.setEndpoint = function(endpoint) {
105
+ this.endpoint = endpoint;
106
+ this._initializeServices(endpoint);
107
+ };
108
+
109
+ Client.prototype.describe = function() {
110
+ var types = this.wsdl.definitions.types;
111
+ return this.wsdl.describeServices();
112
+ };
113
+
114
+ Client.prototype.setSecurity = function(security) {
115
+ this.security = security;
116
+ };
117
+
118
+ Client.prototype.setSOAPAction = function(SOAPAction) {
119
+ this.SOAPAction = SOAPAction;
120
+ };
121
+
122
+ Client.prototype._initializeServices = function(endpoint) {
123
+ var definitions = this.wsdl.definitions,
124
+ services = definitions.services;
125
+ for (var name in services) {
126
+ this[name] = this._defineService(services[name], endpoint);
127
+ }
128
+ };
129
+
130
+ Client.prototype._initializeOptions = function(options) {
131
+ this.streamAllowed = options.stream;
132
+ this.normalizeNames = options.normalizeNames;
133
+ this.wsdl.options.attributesKey = options.attributesKey || 'attributes';
134
+ this.wsdl.options.envelopeKey = options.envelopeKey || 'soap';
135
+ this.wsdl.options.preserveWhitespace = !!options.preserveWhitespace;
136
+ if(options.ignoredNamespaces !== undefined) {
137
+ if(options.ignoredNamespaces.override !== undefined) {
138
+ if(options.ignoredNamespaces.override === true) {
139
+ if(options.ignoredNamespaces.namespaces !== undefined) {
140
+ this.wsdl.options.ignoredNamespaces = options.ignoredNamespaces.namespaces;
141
+ }
142
+ }
143
+ }
144
+ }
145
+ if(options.overrideRootElement !== undefined) {
146
+ this.wsdl.options.overrideRootElement = options.overrideRootElement;
147
+ }
148
+ this.wsdl.options.forceSoap12Headers = !!options.forceSoap12Headers;
149
+ };
150
+
151
+ Client.prototype._defineService = function(service, endpoint) {
152
+ var ports = service.ports,
153
+ def = {};
154
+ for (var name in ports) {
155
+ def[name] = this._definePort(ports[name], endpoint ? endpoint : ports[name].location);
156
+ }
157
+ return def;
158
+ };
159
+
160
+ Client.prototype._definePort = function(port, endpoint) {
161
+ var location = endpoint,
162
+ binding = port.binding,
163
+ methods = binding.methods,
164
+ def = {};
165
+ for (var name in methods) {
166
+ def[name] = this._defineMethod(methods[name], location);
167
+ var methodName = this.normalizeNames ? name.replace(nonIdentifierChars, '_') : name;
168
+ this[methodName] = def[name];
169
+ }
170
+ return def;
171
+ };
172
+
173
+ Client.prototype._defineMethod = function(method, location) {
174
+ var self = this;
175
+ var temp;
176
+ return function(args, callback, options, extraHeaders) {
177
+ if (typeof args === 'function') {
178
+ callback = args;
179
+ args = {};
180
+ } else if (typeof options === 'function') {
181
+ temp = callback;
182
+ callback = options;
183
+ options = temp;
184
+ } else if (typeof extraHeaders === 'function') {
185
+ temp = callback;
186
+ callback = extraHeaders;
187
+ extraHeaders = options;
188
+ options = temp;
189
+ }
190
+ self._invoke(method, args, location, function(error, result, rawResponse, soapHeader, rawRequest) {
191
+ callback(error, result, rawResponse, soapHeader, rawRequest);
192
+ }, options, extraHeaders);
193
+ };
194
+ };
195
+
196
+ Client.prototype._invoke = function(method, args, location, callback, options, extraHeaders) {
197
+ var self = this,
198
+ name = method.$name,
199
+ input = method.input,
200
+ output = method.output,
201
+ style = method.style,
202
+ defs = this.wsdl.definitions,
203
+ envelopeKey = this.wsdl.options.envelopeKey,
204
+ ns = defs.$targetNamespace,
205
+ encoding = '',
206
+ message = '',
207
+ xml = null,
208
+ req = null,
209
+ soapAction,
210
+ alias = findPrefix(defs.xmlns, ns),
211
+ headers = {
212
+ "Content-Type": "text/xml; charset=utf-8"
213
+ },
214
+ xmlnsSoap = "xmlns:" + envelopeKey + "=\"http://schemas.xmlsoap.org/soap/envelope/\"";
215
+
216
+ if (this.wsdl.options.forceSoap12Headers) {
217
+ headers["Content-Type"] = "application/soap+xml; charset=utf-8";
218
+ xmlnsSoap = "xmlns:" + envelopeKey + "=\"http://www.w3.org/2003/05/soap-envelope\"";
219
+ }
220
+
221
+ if (this.SOAPAction) {
222
+ soapAction = this.SOAPAction;
223
+ } else if (method.soapAction !== undefined && method.soapAction !== null) {
224
+ soapAction = method.soapAction;
225
+ } else {
226
+ soapAction = ((ns.lastIndexOf("/") !== ns.length - 1) ? ns + "/" : ns) + name;
227
+ }
228
+
229
+ if (!this.wsdl.options.forceSoap12Headers) {
230
+ headers.SOAPAction = '"' + soapAction + '"';
231
+ }
232
+
233
+ options = options || {};
234
+
235
+ //Add extra headers
236
+ for (var header in this.httpHeaders ) { headers[header] = this.httpHeaders[header]; }
237
+ for (var attr in extraHeaders) { headers[attr] = extraHeaders[attr]; }
238
+
239
+ // Allow the security object to add headers
240
+ if (self.security && self.security.addHeaders)
241
+ self.security.addHeaders(headers);
242
+ if (self.security && self.security.addOptions)
243
+ self.security.addOptions(options);
244
+
245
+ if ((style === 'rpc')&& ( ( input.parts || input.name==="element" ) || args === null) ) {
246
+ assert.ok(!style || style === 'rpc', 'invalid message definition for document style binding');
247
+ message = self.wsdl.objectToRpcXML(name, args, alias, ns,(input.name!=="element" ));
248
+ (method.inputSoap === 'encoded') && (encoding = 'soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" ');
249
+ } else {
250
+ assert.ok(!style || style === 'document', 'invalid message definition for rpc style binding');
251
+ // pass `input.$lookupType` if `input.$type` could not be found
252
+ message = self.wsdl.objectToDocumentXML(input.$name, args, input.targetNSAlias, input.targetNamespace, (input.$type || input.$lookupType));
253
+ }
254
+ xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
255
+ "<" + envelopeKey + ":Envelope " +
256
+ xmlnsSoap + " " +
257
+ "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
258
+ encoding +
259
+ this.wsdl.xmlnsInEnvelope + '>' +
260
+ ((self.soapHeaders || self.security) ?
261
+ (
262
+ "<" + envelopeKey + ":Header>" +
263
+ (self.soapHeaders ? self.soapHeaders.join("\n") : "") +
264
+ (self.security && !self.security.postProcess ? self.security.toXML() : "") +
265
+ "</" + envelopeKey + ":Header>"
266
+ )
267
+ :
268
+ ''
269
+ ) +
270
+ "<" + envelopeKey + ":Body" +
271
+ (self.bodyAttributes ? self.bodyAttributes.join(' ') : '') +
272
+ (self.security && self.security.postProcess ? ' Id="_0"' : '') +
273
+ ">" +
274
+ message +
275
+ "</" + envelopeKey + ":Body>" +
276
+ "</" + envelopeKey + ":Envelope>";
277
+
278
+ if(self.security && self.security.postProcess){
279
+ xml = self.security.postProcess(xml, envelopeKey);
280
+ }
281
+
282
+ if(options && options.postProcess){
283
+ xml = options.postProcess(xml);
284
+ }
285
+
286
+ self.lastMessage = message;
287
+ self.lastRequest = xml;
288
+ self.lastEndpoint = location;
289
+
290
+ var eid = options.exchangeId || uuid4();
291
+
292
+ self.emit('message', message, eid);
293
+ self.emit('request', xml, eid);
294
+
295
+ var tryJSONparse = function(body) {
296
+ try {
297
+ return JSON.parse(body);
298
+ }
299
+ catch(err) {
300
+ return undefined;
301
+ }
302
+ };
303
+
304
+ if (this.streamAllowed && typeof self.httpClient.requestStream === 'function') {
305
+ callback = _.once(callback);
306
+ var startTime = Date.now();
307
+ req = self.httpClient.requestStream(location, xml, headers, options, self);
308
+ self.lastRequestHeaders = req.headers;
309
+ var onError = function onError(err) {
310
+ self.lastResponse = null;
311
+ self.lastResponseHeaders = null;
312
+ self.lastElapsedTime = null;
313
+ self.emit('response', null, null, eid);
314
+
315
+ callback(err, undefined, undefined, undefined, xml);
316
+ };
317
+ req.on('error', onError);
318
+ req.on('response', function (response) {
319
+ response.on('error', onError);
320
+
321
+ // When the output element cannot be looked up in the wsdl, play it safe and
322
+ // don't stream
323
+ if(response.statusCode !== 200 || !output || !output.$lookupTypes) {
324
+ response.pipe(concatStream({encoding: 'string'}, function (body) {
325
+ self.lastResponse = body;
326
+ self.lastResponseHeaders = response && response.headers;
327
+ self.lastElapsedTime = Date.now() - startTime;
328
+ self.emit('response', body, response, eid);
329
+
330
+ return parseSync(body, response);
331
+
332
+ }));
333
+ return;
334
+ }
335
+
336
+ self.wsdl.xmlToObject(response, function (error, obj) {
337
+ self.lastResponse = response;
338
+ self.lastResponseHeaders = response && response.headers;
339
+ self.lastElapsedTime = Date.now() - startTime;
340
+ self.emit('response', '<stream>', response, eid);
341
+
342
+ if (error) {
343
+ error.response = response;
344
+ error.body = '<stream>';
345
+ self.emit('soapError', error, eid);
346
+ return callback(error, response, undefined, undefined, xml);
347
+ }
348
+
349
+ return finish(obj, '<stream>', response);
350
+ });
351
+ });
352
+ return;
353
+ }
354
+
355
+ req = self.httpClient.request(location, xml, function(err, response, body) {
356
+ self.lastResponse = body;
357
+ self.lastResponseHeaders = response && response.headers;
358
+ self.lastElapsedTime = response && response.elapsedTime;
359
+ self.emit('response', body, response, eid);
360
+
361
+ if (err) {
362
+ callback(err, undefined, undefined, undefined, xml);
363
+ } else {
364
+ return parseSync(body, response);
365
+ }
366
+ }, headers, options, self);
367
+
368
+ function parseSync(body, response) {
369
+ var obj;
370
+ try {
371
+ obj = self.wsdl.xmlToObject(body);
372
+ } catch (error) {
373
+ // When the output element cannot be looked up in the wsdl and the body is JSON
374
+ // instead of sending the error, we pass the body in the response.
375
+ if(!output || !output.$lookupTypes) {
376
+ debug('Response element is not present. Unable to convert response xml to json.');
377
+ // If the response is JSON then return it as-is.
378
+ var json = _.isObject(body) ? body : tryJSONparse(body);
379
+ if (json) {
380
+ return callback(null, response, json, undefined, xml);
381
+ }
382
+ }
383
+ error.response = response;
384
+ error.body = body;
385
+ self.emit('soapError', error, eid);
386
+ return callback(error, response, body, undefined, xml);
387
+ }
388
+ return finish(obj, body, response);
389
+ }
390
+
391
+ function finish(obj, body, response) {
392
+ var result;
393
+
394
+ if (!output){
395
+ // one-way, no output expected
396
+ return callback(null, null, body, obj.Header, xml);
397
+ }
398
+
399
+ // If it's not HTML and Soap Body is empty
400
+ if (!obj.html && !obj.Body) {
401
+ return callback(null, obj, body, obj.Header);
402
+ }
403
+
404
+ if( typeof obj.Body !== 'object' ) {
405
+ var error = new Error('Cannot parse response');
406
+ error.response = response;
407
+ error.body = body;
408
+ return callback(error, obj, body, undefined, xml);
409
+ }
410
+
411
+ result = obj.Body[output.$name];
412
+ // RPC/literal response body may contain elements with added suffixes I.E.
413
+ // 'Response', or 'Output', or 'Out'
414
+ // This doesn't necessarily equal the ouput message name. See WSDL 1.1 Section 2.4.5
415
+ if(!result){
416
+ result = obj.Body[output.$name.replace(/(?:Out(?:put)?|Response)$/, '')];
417
+ }
418
+ if (!result) {
419
+ ['Response', 'Out', 'Output'].forEach(function (term) {
420
+ if (obj.Body.hasOwnProperty(name + term)) {
421
+ return result = obj.Body[name + term];
422
+ }
423
+ });
424
+ }
425
+
426
+ callback(null, result, body, obj.Header, xml);
427
+ }
428
+
429
+ // Added mostly for testability, but possibly useful for debugging
430
+ if(req && req.headers) //fixes an issue when req or req.headers is indefined
431
+ self.lastRequestHeaders = req.headers;
432
+ };
433
+
434
+ exports.Client = Client;