-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsoap.js
384 lines (331 loc) · 13.5 KB
/
soap.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
Javascript "SOAP Client" library
Copyright (C) 2006 Matteo Casati http://www.guru4.net/
Copyright (C) 2009 Tom Hughes-Croucher http://kid666.com/
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
SOAPClient = function() {
SOAPClientParameters = function() {
var pl = new Array();
var serialize = function(o) {
var s = "";
switch (typeof(o))
{
case "string":
s += o.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
break;
case "number":
case "boolean":
s += o.toString();
break;
case "object":
// Date
if (o.constructor.toString().indexOf("function Date()") > -1) {
var year = o.getFullYear().toString();
var month = (o.getMonth() + 1).toString();
month = (month.length == 1) ? "0" + month: month;
var date = o.getDate().toString();
date = (date.length == 1) ? "0" + date: date;
var hours = o.getHours().toString();
hours = (hours.length == 1) ? "0" + hours: hours;
var minutes = o.getMinutes().toString();
minutes = (minutes.length == 1) ? "0" + minutes: minutes;
var seconds = o.getSeconds().toString();
seconds = (seconds.length == 1) ? "0" + seconds: seconds;
var milliseconds = o.getMilliseconds().toString();
var tzminutes = Math.abs(o.getTimezoneOffset());
var tzhours = 0;
while (tzminutes >= 60) {
tzhours++;
tzminutes -= 60;
}
tzminutes = (tzminutes.toString().length == 1) ? "0" + tzminutes.toString() : tzminutes.toString();
tzhours = (tzhours.toString().length == 1) ? "0" + tzhours.toString() : tzhours.toString();
var timezone = ((o.getTimezoneOffset() < 0) ? "+": "-") + tzhours + ":" + tzminutes;
s += year + "-" + month + "-" + date + "T" + hours + ":" + minutes + ":" + seconds + "." + milliseconds + timezone;
} else if (o.constructor.toString().indexOf("function Array()") > -1) {
// Array
for (var p in o) {
if (!isNaN(p)) {
// linear array
(/function\s+(\w*)\s*\(/ig).exec(o[p].constructor.toString());
var type = RegExp.$1;
switch (type) {
case "":
type = typeof(o[p]);
case "String":
type = "string";
break;
case "Number":
type = "int";
break;
case "Boolean":
type = "bool";
break;
case "Date":
type = "DateTime";
break;
}
s += "<" + type + ">" + serialize(o[p]) + "</" + type + ">";
} else {
// associative array
s += "<" + p + ">" + serialize(o[p]) + "</" + p + ">";
}
}
} else {
// Object or custom function
for (var p in o) {
s += "<" + p + ">" + serialize(o[p]) + "</" + p + ">";
break;
}
}
default:
break;
// throw new Error(500, "SOAPClientParameters: type '" + typeof(o) + "' is not supported");
}
return s;
};
return {
add : function(name, value) {
pl[name] = value;
},
toXml : function() {
default xml namespace = "";
var xml = <></>;
for(var p in pl)
{
switch(typeof(pl[p]))
{
case "string":
case "number":
case "boolean":
case "object":
xml += <{p}>{serialize(pl[p])}</{p}>;
break;
default:
break;
}
}
return xml;
}
};
};
var username = null;
var password = null;
var wsdlCache = [];
var toBase64 = function(input) {
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var output = "";
var chr1,
chr2,
chr3;
var enc1,
enc2,
enc3,
enc4;
var i = 0;
do {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
keyStr.charAt(enc3) + keyStr.charAt(enc4);
}
while (i < input.length);
return output;
};
var getTypeFromWsdl = function(elementname, wsdlTypes) {
var type = wsdlTypes[elementname] + "";
return (type == "undefined") ? "": type;
};
var getTypesFromWsdl = function(wsdl) {
var wsdlTypes = new Array();
// IE
var ell = wsdl.getElementsByTagName("s:element");
var useNamedItem = true;
// MOZ
if (ell.length == 0)
{
ell = wsdl.getElementsByTagName("element");
useNamedItem = false;
}
for (var i = 0; i < ell.length; i++)
{
if (useNamedItem)
{
if (ell[i].attributes.getNamedItem("name") != null && ell[i].attributes.getNamedItem("type") != null)
wsdlTypes[ell[i].attributes.getNamedItem("name").nodeValue] = ell[i].attributes.getNamedItem("type").nodeValue;
}
else
{
if (ell[i].attributes["name"] != null && ell[i].attributes["type"] != null)
wsdlTypes[ell[i].attributes["name"].value] = ell[i].attributes["type"].value;
}
}
return wsdlTypes;
};
var extractValue = function(node, wsdlTypes) {
var value = node.nodeValue;
switch (getTypeFromWsdl(node.parentNode.nodeName, wsdlTypes).toLowerCase())
{
case "s:string":
return (value != null) ? value + "": "";
case "s:boolean":
return value + "" == "true";
case "s:int":
case "s:long":
return (value != null) ? parseInt(value + "", 10) : 0;
case "s:double":
return (value != null) ? parseFloat(value + "") : 0;
case "s:datetime":
if (value == null) {
return null;
}
else {
value = value + "";
value = value.substring(0, (value.lastIndexOf(".") == -1 ? value.length: value.lastIndexOf(".")));
value = value.replace(/T/gi, " ");
value = value.replace(/-/gi, "/");
var d = new Date();
d.setTime(Date.parse(value));
return d;
}
default:
}
};
var node2object = function(node, wsdlTypes) {
// null node
if (node == null)
return null;
// text node
if (node.nodeType == 3 || node.nodeType == 4)
return extractValue(node, wsdlTypes);
// leaf node
if (node.childNodes.length == 1 && (node.childNodes[0].nodeType == 3 || node.childNodes[0].nodeType == 4))
return node2object(node.childNodes[0], wsdlTypes);
var isarray = getTypeFromWsdl(node.nodeName, wsdlTypes).toLowerCase().indexOf("arrayof") != -1;
// object node
if (!isarray)
{
var obj = null;
if (node.hasChildNodes())
obj = new Object();
for (var i = 0; i < node.childNodes.length; i++)
{
var p = node2object(node.childNodes[i], wsdlTypes);
obj[node.childNodes[i].nodeName] = p;
}
return obj;
}
// list node
else
{
// create node ref
var l = new Array();
for (var i = 0; i < node.childNodes.length; i++)
l[l.length] = node2object(node.childNodes[i], wsdlTypes);
return l;
}
return null;
};
var soapresult2object = function(node, wsdl) {
var wsdlTypes = getTypesFromWsdl(wsdl);
return node2object(node, wsdlTypes);
};
var onSendSoapRequest = function(method, wsdl, response) {
//get the namespace
var ns = wsdl.@["targetNamespace"];
//cludgy - I don't like this eval but I can't get xml namespaces to work with y.xpath
var path = "response.." + method + "Response";
default xml namespace = ns;
var nd = eval(path);
// code from Matteo's original lib not sure exactly what it's for
// probably still need some error checking code here
/*if (nd.length == 0)
nd = getElementsByTagName(req.responseXML, "return");
//PHP web Service?
if (nd.length == 0)
{
if (req.responseXML.getElementsByTagName("faultcode").length > 0)
{
if (async || callback)
o = new Error(500, req.responseXML.getElementsByTagName("faultstring")[0].childNodes[0].nodeValue);
else
throw new Error(500, req.responseXML.getElementsByTagName("faultstring")[0].childNodes[0].nodeValue);
}
}
else
*/
//since everything went well and there were no errors we gor a doc body to return
return nd;
};
var sendSoapRequest = function(url, method, parameters, wsdl) {
// get namespace
default xml namespace = "http://schemas.xmlsoap.org/wsdl/http/";
ns = wsdl.@["targetNamespace"];
// build SOAP request
var soap = new Namespace('http://schemas.xmlsoap.org/wsdl/soap/');
var sr = <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><{method} xmlns={ns}>{parameters.toXml()}</{method}></soap:Body></soap:Envelope>;
//var post = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + sr.toString();
post = sr;
var xmlHttp = y.rest(url);
if (SOAPClient.username && SOAPClient.password) {
// Some WS implementations (i.e. BEA WebLogic Server 10.0 JAX-WS) don't support Challenge/Response HTTP BASIC, so we send authorization headers in the first request
xmlHttp.header("Authorization", "Basic " + toBase64(username + ":" + password));
}
//if ns doesnt end in a / add one
if (ns.substr(-1, 1) !== "/") {
var soapaction = ns + "/" + method;
} else {
var soapaction = ns + method;
}
xmlHttp.header("SOAPAction", soapaction);
xmlHttp.header("Content-Type", "text/xml; charset=utf-8");
xmlHttp.accept("application/xml");
var response = xmlHttp.post(post).response;
return response;
};
var loadWsdl = function(url, method, parameters) {
var wsdl = wsdlCache[url];
// if cache isn't primed get wsdl and cache it
if(typeof wsdl == 'undefined' || wsdl === "") {
var xmlHttp = y.rest(url + "?wsdl");
wsdl = xmlHttp.get().response;
wsdlCache[url] = wsdl;
}
return wsdl;
};
return {
invoke : function(url, method, parameters) {
wsdl = loadWsdl(url, method, parameters);
var response = sendSoapRequest(url, method, parameters, wsdl);
var obj = onSendSoapRequest(method, wsdl, response);
response = <></>;
response += obj.*;
return response;
//o = soapresult2object(nd, wsdl);
},
spawnParameters : function() {
return new SOAPClientParameters;
}
};
}();