forked from kwhinnery/Suds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
suds.js
143 lines (133 loc) · 4.59 KB
/
suds.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
/**
* Suds: A Lightweight JavaScript SOAP Client
* Copyright: 2009 Kevin Whinnery (http://www.kevinwhinnery.com)
* License: http://www.apache.org/licenses/LICENSE-2.0.html
* Source: http://github.com/kwhinnery/Suds
*/
function SudsClient(_options) {
//A generic extend function - thanks MooTools
function extend(original, extended) {
extended = extended || {};
for (var key in extended) {
if (extended.hasOwnProperty(key)) {
original[key] = extended[key];
}
}
return original;
}
//Check if an object is an array
function isArray(obj) {
return Object.prototype.toString.call(obj) == '[object Array]';
}
//Grab an XMLHTTPRequest Object
function getXHR() {
var xhr;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}
else {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
return xhr;
}
//Parse a string and create an XML DOM object
function xmlDomFromString(_xml) {
var xmlDoc = null;
if (window.DOMParser) {
parser = new DOMParser();
xmlDoc = parser.parseFromString(_xml,"text/xml");
}
else {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(_xml);
}
return xmlDoc;
}
// Convert a JavaScript object to an XML string - takes either an
function convertToXml(_obj) {
var xml = '';
if (isArray(_obj)) {
for (var i = 0; i < _obj.length; i++) {
xml += convertToXml(_obj[i]);
}
} else {
//For now assuming we either have an array or an object graph
for (var key in _obj) {
xml += '<'+key+'>';
if (isArray(_obj[key]) || (typeof _obj[key] == 'object' && _obj[key] != null)) {
xml += convertToXml(_obj[key]);
}
else {
xml += _obj[key];
}
xml += '</'+key+'>';
}
}
return xml;
}
// Client Configuration
var config = extend({
endpoint:'http://localhost',
targetNamespace: 'http://localhost',
envelopeBegin: '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:ns0="PLACEHOLDER" 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/">',
headerBegin: '<soap:Header>',
headerNode:'head',
headerEnd: '</soap:Header>',
bodyBegin:'<soap:Body>',
envelopeEnd: '</soap:Body></soap:Envelope>',
timeout: 5000,
responseType: 'object'
},_options);
// Invoke a web service
this.invoke = function(_soapAction,_body,_callback,_error,_header) {
//Build request body
var body = _body;
var header = _header;
//Allow straight string input for XML body - if not, build from object
if (typeof body !== 'string') {
body = '<'+_soapAction+' xmlns="'+config.targetNamespace+'">';
body += convertToXml(_body);
body += '</'+_soapAction+'>';
}
var ebegin = config.envelopeBegin;
config.envelopeBegin = ebegin.replace('PLACEHOLDER', config.targetNamespace);
//Build Soapaction header - if no trailing slash in namespace, need to splice one in for soap action
var soapAction = '';
if (config.targetNamespace.lastIndexOf('/') != config.targetNamespace.length - 1) {
soapAction = config.targetNamespace+'/'+_soapAction;
}
else {
soapAction = config.targetNamespace+_soapAction;
}
//POST XML document to service endpoint
var xhr = getXHR();
xhr.onload = function() {
_callback.call(this, config.responseType == 'object' ? xmlDomFromString(this.responseText) : this.responseText );
};
xhr.onerror = function() {
_error.call();
}
xhr.setTimeout(config.timeout);
var sendXML = '';
if(!header) {
sendXML = config.envelopeBegin+config.bodyBegin+body+config.envelopeEnd;
} else {
//Allow straight string input for XML body - if not, build from object
if (typeof header !== 'string') {
header = '<'+_soapAction+' xmlns="'+config.targetNamespace+'">';
header += convertToXml(_header);
header += '</'+_soapAction+'>';
}
sendXML = config.envelopeBegin+config.headerBegin+header+config.headerEnd+config.bodyBegin+body+config.envelopeEnd;
}
xhr.open('POST',config.endpoint);
xhr.setRequestHeader('Content-Type', 'text/xml');
xhr.setRequestHeader('Content-Length', sendXML.length);
xhr.setRequestHeader('SOAPAction', soapAction);
if (config.authorization !== undefined) {
xhr.setRequestHeader('Authorization', 'Basic ' + config.authorization);
}
xhr.send(sendXML);
};
}