-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxmlrpc.js
More file actions
401 lines (331 loc) · 10.1 KB
/
Copy pathxmlrpc.js
File metadata and controls
401 lines (331 loc) · 10.1 KB
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
Copyright (c) 2005 Redstone Handelsbolag
This library is free software; you can redistribute it and/or modify it under the terms
of the GNU Lesser General Public License as published by the Free Software Foundation;
either version 2.1 of the License, or (at your option) any later version.
This library 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this
library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
Note from Southpaw Technology:
This library was taken from http://xmlrpc.sourceforge.net/
It was distributed as ajax.js which we have renamed to xmlrpc.js. There
was no license under the original file, so we have added the same
GNU Lesser General Public License that came with the rest of the
distribution (which was all in Java).
We have made a number of enhancements including reworking the serialization
so that it does not affect change the prototype of base types
*/
/**
* Creates a connection object that may be used to post messages
* to a server.
*/
function Connection()
{
var self = this;
this.callback = null;
this.set_callback = function(callback) {
this.callback = callback;
}
/**
* Returns an XmlHttpRequest object for the current browser.
*/
this.getXmlHttpRequest = function()
{
var result = null;
try
{
result = new XMLHttpRequest();
}
catch ( e )
{
try
{
result = new ActiveXObject( 'Msxml2.XMLHTTP' )
}
catch( e )
{
var success = false;
var MSXML_XMLHTTP_PROGIDS = new Array(
'Microsoft.XMLHTTP',
'MSXML2.XMLHTTP',
'MSXML2.XMLHTTP.5.0',
'MSXML2.XMLHTTP.4.0',
'MSXML2.XMLHTTP.3.0' );
for ( var i = 0; i < MSXML_XMLHTTP_PROGIDS.length && !success; i++ )
{
try
{
result = new ActiveXObject( MSXML_XMLHTTP_PROGIDS[ i ] );
success = true;
}
catch ( e )
{
result = null;
}
}
}
}
self.xmlHttpRequest = result;
return result;
}
/**
* Posts a message to a server.
*/
this.post = function( url, content, contentType )
{
if ( typeof self.xmlHttpRequest.abort == 'function' && self.xmlHttpRequest.readyState != 0 )
{
self.xmlHttpRequest.abort();
}
// by default, async is false
var async = false;
// handle asynchronous mode
if (this.callback != null) {
async = true;
var callback = this.callback;
var request = self.xmlHttpRequest;
self.xmlHttpRequest.onreadystatechange = function() {
callback(request);
}
}
self.xmlHttpRequest.open( 'POST', url, async );
if ( typeof self.xmlHttpRequest.setRequestHeader == 'function' )
{
self.xmlHttpRequest.setRequestHeader(
'Content-Type',
contentType );
}
try {
self.xmlHttpRequest.send( content );
}
catch(e) {
spt.error("Error connecting to TACTIC server. Please contact the Administrator of this server." );
spt.app_busy.hide();
throw(e);
}
//return self.xmlHttpRequest.responseText;
//return self.xmlHttpRequest.responseXML;
// only send the request back now
return self.xmlHttpRequest
}
if ( !this.getXmlHttpRequest() )
{
throw new Error( "Could not load XMLHttpRequest object" );
}
}
/**
* Client object constructor.
*/
function AjaxService( url, handlerName )
{
this.url = url;
this.handlerName = handlerName;
this.connection = new Connection();
}
AjaxService.prototype.set_callback = function(callback) {
this.connection.set_callback(callback);
}
/**
* Posts an XML-RPC message to the supplied method using the
* given arguments.
*/
AjaxService.prototype.invoke = function( method, arguments )
{
return this.connection.post( this.url, this.getMessage( method, arguments ), 'text/xml' );
}
/**
* Generates an XML-RPC message based on the supplied method name
* and argument array.
*/
AjaxService.prototype.getMessage = function( method, arguments )
{
if ( arguments == null )
{
arguments = new Array();
}
/*
var message =
'<?xml version="1.0"?><methodCall><methodName>' +
this.handlerName + '.' + method +
'</methodName>';
*/
var message =
'<?xml version="1.0"?><methodCall><methodName>' +
method +
'</methodName>';
if ( arguments.length > 0 )
{
message += '<params>';
for ( var i = 0; i < arguments.length; i++ )
{
var argument = arguments[ i ];
if (argument == undefined) {
spt.js_log.debug("WARNING: argument index ["+i+"] of method ["+method+"] is undefined");
}
var serialized = '<param><value>' + spt.xmlrpc.serialize(argument) + '</value></param>';
message += serialized;
}
message += '</params>';
}
message += '</methodCall>';
//alert(message);
return message;
}
spt.xmlrpc = {};
//
// Main serialization function
//
spt.xmlrpc.serialize = function(element) {
//var type = $type(element);
// Use Mootools type to handle their base types as well
var type = typeOf(element);
if (type == 'element') {
spt.alert('Illegal attempt to serialize an html element. Please use a hash as arguments of a JS Client API call.');
return;
}
if (type == 'array') {
return spt.xmlrpc.array(element);
}
else if (type == 'hash') {
return spt.xmlrpc.hash(element);
}
else if (type == 'string') {
return spt.xmlrpc.string(element);
}
else if (type == 'number') {
return spt.xmlrpc.number(element);
}
else if (type == 'boolean') {
return spt.xmlrpc.boolean(element);
}
else if (type == 'date') {
return spt.xmlrpc.boolean(element);
}
//if (type == 'object') {
else {
//alert("type: " + type + " in serializer");
return spt.xmlrpc.object(element);
}
}
//
// Serialization functions for the complex datatypes.
//
spt.xmlrpc.object = function(element)
{
var result = '<struct>';
for ( var member in element )
{
var sub_element = element[member];
if ( sub_element == undefined ) {
continue;
}
if ( typeof( sub_element ) != 'function' )
{
result += '<member>';
result += '<name>' + member + '</name>';
result += '<value>' + spt.xmlrpc.serialize(sub_element) + '</value>';
result += '</member>';
}
}
result += '</struct>';
return result;
}
spt.xmlrpc.array = function(element)
{
var result = '<array><data>';
for ( var i = 0; i < element.length; i++ )
{
var sub_element = element[i];
result += '<value>' + spt.xmlrpc.serialize(sub_element) + '</value>';
}
result += '</data></array>';
return result;
}
spt.xmlrpc.hash = function(element)
{
var result = '<struct>';
for ( var member in element )
{
var sub_element = element[member];
if ( sub_element == undefined ) {
continue;
}
if ( typeof( sub_element ) != 'function' )
{
result += '<member>';
result += '<name>' + member + '</name>';
result += '<value>' + spt.xmlrpc.serialize(sub_element) + '</value>';
result += '</member>';
}
}
result += '</struct>';
return result;
}
//
// Serialization functions for the String datatype.
//
spt.xmlrpc.string = function(element)
{
var expression = '' + element;
// process unicode
/*
var value = expression;
var new_value = "";
for ( var i = 0; i < value.length; i++ ) {
var char = value.substr(i,1);
var ord = String.charCodeAt(char);
if (ord > 127) {
char = "&#" + ord + ";";
}
new_value += char;
}
expression = new_value;
*/
expression = expression.replace(/&/g, "&");
expression = expression.replace(/</g, "<");
expression = expression.replace(/>/g, ">");
expression = '<string>' + expression + '</string>';
return expression
}
/**
* Serialization functions for the Number datatype.
*/
spt.xmlrpc.number = function(element)
{
if ( element % 1 != 0 ) // Very crude way of determining type. May be mismatch at server.
{
return '<double>' + element + '</double>';
}
else
{
return '<i4>' + element + '</i4>';
}
}
/**
* Serialization function for the Boolean datatype.
*/
spt.xmlrpc.boolean = function(element)
{
if (element == true)
element = 1;
else
element = 0;
return '<boolean>' + element + '</boolean>';
}
/**
* Serialization function for Date datatype.
*/
spt.xmlrpc.date = function(element)
{
return '<dateTime.iso8601>' +
element.getFullYear() +
( element.getMonth() < 10 ? '0' : '' ) + element.getMonth() +
( element.getDay() < 10 ? '0' : '' ) + element.getDay() + 'T' +
( element.getHours() < 10 ? '0' : '' ) + element.getHours() + ':' +
( element.getMinutes() < 10 ? '0' : '' ) + element.getMinutes() + ':' +
( element.getMinutes() < 10 ? '0' : '' ) + element.getSeconds() +
'</dateTime.iso8601>'; // Phew :-)
}