PageRenderTime 118ms CodeModel.GetById 62ms app.highlight 49ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/jsonrpc/public/services/phpolait/jsolait/lib/xmlrpc.js

http://pyjamas.googlecode.com/
JavaScript | 567 lines | 566 code | 1 blank | 0 comment | 89 complexity | 52f94ff4f75ea0aada18938958f4edde MD5 | raw file
  1Module("xmlrpc","1.3.3", function(mod){
  2var xmlext = importModule("xml");
  3var urllib = importModule("urllib");
  4mod.InvalidServerResponse = Class("InvalidServerResponse", mod.Exception, function(publ, supr){
  5publ.init= function(status){
  6supr(this).init("The server did not respond with a status 200 (OK) but with: " + status);
  7this.status = status;
  8}
  9publ.status;
 10})
 11mod.MalformedXmlRpc = Class("MalformedXmlRpc", mod.Exception, function(publ, supr){
 12publ.init= function(msg, xml, trace){
 13supr(this).init(msg,trace);
 14this.xml = xml;
 15}
 16publ.xml;
 17})
 18mod.Fault = Class("Fault", mod.Exception, function(publ, supr){
 19publ.init= function(faultCode, faultString){
 20supr(this).init("XML-RPC Fault: " +  faultCode + "\n\n" + faultString);
 21this.faultCode = faultCode;
 22this.faultString = faultString;
 23}
 24publ.faultCode;
 25publ.faultString;
 26})
 27mod.marshall = function(obj){
 28if(obj.toXmlRpc){
 29return obj.toXmlRpc();
 30}else{
 31var s = "<struct>";
 32for(var attr in obj){
 33if(typeof obj[attr] != "function"){
 34s += "<member><name>" + attr + "</name><value>" + mod.marshall(obj[attr]) + "</value></member>";
 35}
 36}
 37s += "</struct>";
 38return s;
 39}
 40}
 41mod.unmarshall = function(xml){
 42try {
 43var doc = xmlext.parseXML(xml);
 44}catch(e){
 45throw new mod.MalformedXmlRpc("The server's response could not be parsed.", xml, e);
 46}
 47var rslt = mod.unmarshallDoc(doc, xml);
 48doc=null;
 49return rslt;
 50}
 51mod.unmarshallDoc = function(doc, xml){
 52try{
 53var node = doc.documentElement;
 54if(node==null){
 55throw new mod.MalformedXmlRpc("No documentElement found.", xml);
 56}
 57switch(node.tagName){
 58case "methodResponse":
 59return parseMethodResponse(node);
 60case "methodCall":
 61return parseMethodCall(node);
 62default:
 63throw new mod.MalformedXmlRpc("'methodCall' or 'methodResponse' element expected.\nFound: '" + node.tagName + "'", xml);
 64}
 65}catch(e){
 66if(e instanceof mod.Fault){
 67throw e;
 68}else {
 69throw new mod.MalformedXmlRpc("Unmarshalling of XML failed.", xml, e);    
 70}
 71}
 72}
 73var parseMethodResponse=function(node){
 74try{
 75for(var i=0;i<node.childNodes.length;i++){
 76var child = node.childNodes.item(i);
 77if (child.nodeType == 1){
 78switch (child.tagName){
 79case "fault": 
 80throw parseFault(child);
 81case "params":
 82var params = parseParams(child);
 83if(params.length == 1){
 84return params[0];
 85}else{
 86throw new mod.MalformedXmlRpc("'params' element inside 'methodResponse' must have exactly ONE 'param' child element.\nFound: " + params.length);
 87}
 88default:
 89throw new mod.MalformedXmlRpc("'fault' or 'params' element expected.\nFound: '" + child.tagName + "'");                        
 90}
 91}
 92}
 93throw new mod.MalformedXmlRpc("No child elements found.");    
 94}catch(e){
 95if(e instanceof mod.Fault){
 96throw e;
 97}else{
 98throw new mod.MalformedXmlRpc("'methodResponse' element could not be parsed.",null,e);    
 99}
100}
101}
102        
103var parseMethodCall = function(node){
104try{
105var methodName = null;
106var params = new Array();
107for(var i=0;i<node.childNodes.length;i++){
108var child = node.childNodes.item(i);
109if (child.nodeType == 1){
110switch (child.tagName){
111case "methodName":
112methodName = new String(child.firstChild.nodeValue);
113break;
114case "params":
115params = parseParams(child);
116break;
117default:
118throw new mod.MalformedXmlRpc("'methodName' or 'params' element expected.\nFound: '" + child.tagName + "'");                        
119}
120}
121}
122if(methodName==null){
123throw new mod.MalformedXmlRpc("'methodName' element expected.");
124}else{
125return new Array(methodName, params);
126}
127}catch(e){
128throw new mod.MalformedXmlRpc("'methodCall' element could not be parsed.",null,e);    
129}
130}
131var parseParams = function(node){
132try{
133var params=new Array();
134for(var i=0;i<node.childNodes.length;i++){
135var child = node.childNodes.item(i);
136if (child.nodeType == 1){
137switch (child.tagName){
138case "param":
139params.push(parseParam(child));
140break;
141default:
142throw new mod.MalformedXmlRpc("'param' element expected.\nFound: '" + child.tagName + "'");                        
143}
144}
145}
146return params;
147}catch(e){
148throw new mod.MalformedXmlRpc("'params' element could not be parsed.",null,e);    
149}
150}
151var parseParam = function(node){
152try{
153for(var i=0;i<node.childNodes.length;i++){
154var child = node.childNodes.item(i);
155if (child.nodeType == 1){
156switch (child.tagName){
157case "value":
158return parseValue(child);
159default:
160throw new mod.MalformedXmlRpc("'value' element expected.\nFound: '" + child.tagName + "'");                        
161}
162}
163}
164throw new mod.MalformedXmlRpc("'value' element expected.But none found.");
165}catch(e){
166throw new mod.MalformedXmlRpc("'param' element could not be parsed.",null,e);    
167}
168}
169var parseValue = function(node){
170try{
171for(var i=0;i<node.childNodes.length;i++){
172var child = node.childNodes.item(i);
173if (child.nodeType == 1){
174switch (child.tagName){
175case "string":
176var s="" 
177for(var j=0;j<child.childNodes.length;j++){
178s+=new String(child.childNodes.item(j).nodeValue);
179}
180return s;
181case "int":
182case "i4":
183case "double":
184return (child.firstChild) ? new Number(child.firstChild.nodeValue) : 0;
185case "boolean":
186return Boolean(isNaN(parseInt(child.firstChild.nodeValue)) ? (child.firstChild.nodeValue == "true") : parseInt(child.firstChild.nodeValue));
187case "base64":
188return parseBase64(child);
189case "dateTime.iso8601":
190return parseDateTime(child);
191case "array":
192return parseArray(child);
193case "struct":
194return parseStruct(child);
195case "nil": 
196return null;
197default:
198throw new mod.MalformedXmlRpc("'string','int','i4','double','boolean','base64','dateTime.iso8601','array' or 'struct' element expected.\nFound: '" + child.tagName + "'");                        
199}
200}
201}
202if(node.firstChild){
203var s="" 
204for(var j=0;j<node.childNodes.length;j++){
205s+=new String(node.childNodes.item(j).nodeValue);
206}
207return s;
208}else{
209return "";
210}
211}catch(e){
212throw new mod.MalformedXmlRpc("'value' element could not be parsed.",null,e);    
213}
214}
215var parseBase64=function(node){
216try{
217var s = node.firstChild.nodeValue;
218return s.decode("base64");
219}catch(e){
220throw new mod.MalformedXmlRpc("'base64' element could not be parsed.",null,e);    
221}
222}
223var parseDateTime=function(node){
224try{
225if(/^(\d{4})-?(\d{2})-?(\d{2})T(\d{2}):?(\d{2}):?(\d{2})/.test(node.firstChild.nodeValue)){
226return new Date(Date.UTC(RegExp.$1, RegExp.$2-1, RegExp.$3, RegExp.$4, RegExp.$5, RegExp.$6));
227}else{ 
228throw new mod.MalformedXmlRpc("Could not convert the given date.");
229}
230}catch(e){
231throw new mod.MalformedXmlRpc("'dateTime.iso8601' element could not be parsed.",null,e);    
232}
233}
234var parseArray=function(node){
235try{
236for(var i=0;i<node.childNodes.length;i++){
237var child = node.childNodes.item(i);
238if (child.nodeType == 1){
239switch (child.tagName){
240case "data":
241return parseData(child);
242default:
243throw new mod.MalformedXmlRpc("'data' element expected.\nFound: '" + child.tagName + "'");                        
244}
245}
246}
247throw new mod.MalformedXmlRpc("'data' element expected. But not found.");   
248}catch(e){
249throw new mod.MalformedXmlRpc("'array' element could not be parsed.",null,e);    
250}
251}
252var parseData=function(node){
253try{
254var rslt = new Array();
255for(var i=0;i<node.childNodes.length;i++){
256var child = node.childNodes.item(i);
257if (child.nodeType == 1){
258switch (child.tagName){
259case "value":
260rslt.push(parseValue(child));
261break;
262default:
263throw new mod.MalformedXmlRpc("'value' element expected.\nFound: '" + child.tagName + "'");                        
264}
265}
266}
267return rslt;
268}catch(e){
269throw new mod.MalformedXmlRpc("'data' element could not be parsed.",null,e);    
270}
271}
272var parseStruct=function(node){
273try{
274var struct = new Object();
275for(var i=0;i<node.childNodes.length;i++){
276var child = node.childNodes.item(i);
277if (child.nodeType == 1){
278switch (child.tagName){
279case "member":
280var member = parseMember(child); 
281if(member[0] != ""){
282struct[member[0]] = member[1];
283}
284break;
285default:
286throw new mod.MalformedXmlRpc("'data' element expected.\nFound: '" + child.tagName + "'");                        
287}
288}
289}
290return struct;
291}catch(e){
292throw new mod.MalformedXmlRpc("'struct' element could not be parsed.",null,e);    
293}
294}
295var parseMember=function(node){
296try{
297var name="";
298var value=null;
299for(var i=0;i<node.childNodes.length;i++){
300var child = node.childNodes.item(i);
301if (child.nodeType == 1){
302switch (child.tagName){
303case "value":
304value = parseValue(child); 
305break;
306case "name":
307if(child.hasChildNodes()){
308name = new String(child.firstChild.nodeValue);
309}
310break;
311default:
312throw new mod.MalformedXmlRpc("'value' or 'name' element expected.\nFound: '" + child.tagName + "'");                        
313}
314}
315}
316return [name, value];
317}catch(e){
318throw new mod.MalformedXmlRpc("'member' element could not be parsed.",null,e);    
319}
320}
321var parseFault = function(node){
322try{
323for(var i=0;i<node.childNodes.length;i++){
324var child = node.childNodes.item(i);
325if (child.nodeType == 1){
326switch (child.tagName){
327case "value":
328var flt = parseValue(child); 
329return new mod.Fault(flt.faultCode, flt.faultString);
330default:
331throw new mod.MalformedXmlRpc("'value' element expected.\nFound: '" + child.tagName + "'");                        
332}
333}
334}
335throw new mod.MalformedXmlRpc("'value' element expected. But not found.");                        
336}catch(e){
337throw new mod.MalformedXmlRpc("'fault' element could not be parsed.",null,e);    
338}
339}
340mod.XMLRPCMethod =Class("XMLRPCMethod", function(publ){
341var postData = function(url, user, pass, data, callback){
342if(callback == null){
343var rslt = urllib.postURL(url, user, pass, data, [["Content-Type", "text/xml"]]);
344return rslt;
345}else{
346urllib.postURL(url, user, pass, data, [["Content-Type", "text/xml"]], callback);
347}
348}
349var handleResponse=function(resp){
350var status=null;
351try{
352status = resp.status;
353}catch(e){
354}
355if(status == 200){
356var respDoc=null;
357try{
358respDoc = resp.responseXML;
359}catch(e){
360}
361var respTxt = ""; 
362try{                 
363respTxt=resp.responseText;
364}catch(e){
365}
366if(respDoc == null){
367if(respTxt == null || respTxt == ""){
368throw new mod.MalformedXmlRpc("The server responded with an empty document.", "");
369}else{
370return mod.unmarshall(respTxt);
371}
372}else{ 
373return mod.unmarshallDoc(respDoc, respTxt);
374}
375}else{
376throw new mod.InvalidServerResponse(status);
377}
378}
379var getXML = function(methodName, args){
380var data='<?xml version="1.0"?><methodCall><methodName>' + methodName + '</methodName>';
381if (args.length>0){
382data += "<params>";
383for(var i=0;i<args.length;i++){
384data += '<param><value>' + mod.marshall(args[i]) + '</value></param>';
385}
386data += '</params>';
387}
388data += '</methodCall>';
389return data;
390}
391publ.init = function(url, methodName, user, pass){
392var fn=function(){
393if(typeof arguments[arguments.length-1] != "function"){
394var data=getXML(fn.methodName,arguments);
395var resp = postData(fn.url, fn.user, fn.password, data);
396return handleResponse(resp);
397}else{
398var args=new Array();
399for(var i=0;i<arguments.length;i++){
400args.push(arguments[i]);
401}
402var cb = args.pop();
403var data=getXML(fn.methodName, args);
404postData(fn.url, fn.user, fn.password, data, function(resp){
405var rslt = null;
406var exc =null;
407try{
408rslt = handleResponse(resp);
409}catch(e){
410exc = e;
411}
412try{
413cb(rslt,exc);
414}catch(e){
415}
416args = null;
417resp = null;
418});
419}
420}
421fn.methodName = methodName;
422fn.url = url;
423fn.user = user;
424fn.password=pass;
425fn.toMulticall = this.toMulticall;
426fn.toString = this.toString;
427fn.setAuthentication=this.setAuthentication;
428fn.constructor = this.constructor;
429return fn;
430}
431publ.toMulticall = function(){
432var multiCallable = new Object();
433multiCallable.methodName = this.methodName;
434var params = [];
435for(var i=0;i<arguments.length;i++){
436params[i] = arguments[i];
437}
438multiCallable.params = params;
439return multiCallable;
440}
441publ.setAuthentication = function(user, pass){
442this.user = user;
443this.password = pass;
444}
445publ.methodName;
446publ.url;
447publ.user;
448publ.password;
449})
450mod.ServiceProxy=Class("ServiceProxy", function(publ){
451publ.init = function(url, methodNames, user, pass){
452if(methodNames instanceof Array){
453if(methodNames.length > 0){
454var tryIntrospection=false;
455}else{
456var tryIntrospection=true;
457}
458}else{
459pass=user;
460user=methodNames;
461methodNames=[];
462var tryIntrospection=true;
463}
464this._url = url;
465this._user = user;
466this._password = pass;
467this._addMethodNames(methodNames);
468if(tryIntrospection){
469try{
470this._introspect();
471}catch(e){
472}
473}
474}
475publ._addMethodNames = function(methodNames){
476for(var i=0;i<methodNames.length;i++){
477var obj = this;
478var names = methodNames[i].split(".");
479for(var n=0;n<names.length-1;n++){
480var name = names[n];
481if(obj[name]){
482obj = obj[name];
483}else{
484obj[name]  = new Object();
485obj = obj[name];
486}
487}
488var name = names[names.length-1];
489if(obj[name]){
490}else{
491var mth = new mod.XMLRPCMethod(this._url, methodNames[i], this._user, this._password);
492obj[name] = mth;
493this._methods.push(mth);
494}
495}
496}
497publ._setAuthentication = function(user, pass){
498this._user = user;
499this._password = pass;
500for(var i=0;i<this._methods.length;i++){
501this._methods[i].setAuthentication(user, pass);
502}
503}
504publ._introspect = function(){
505this._addMethodNames(["system.listMethods","system.methodHelp", "system.methodSignature"]);
506var m = this.system.listMethods();
507this._addMethodNames(m);
508}
509publ._url;
510publ._user;
511publ._password;
512publ._methods=new Array();
513})
514mod.ServerProxy= mod.ServiceProxy;
515String.prototype.toXmlRpc = function(){
516return "<string>" + this.replace(/&/g, "&amp;").replace(/</g, "&lt;") + "</string>";
517}
518Number.prototype.toXmlRpc = function(){
519if(this == parseInt(this)){
520return "<int>" + this + "</int>";
521}else if(this == parseFloat(this)){
522return "<double>" + this + "</double>";
523}else{
524return false.toXmlRpc();
525}
526}
527Boolean.prototype.toXmlRpc = function(){
528if(this == true) {
529return "<boolean>1</boolean>";
530}else{
531return "<boolean>0</boolean>";
532}
533}
534Date.prototype.toXmlRpc = function(){
535var padd=function(s, p){
536s=p+s
537return s.substring(s.length - p.length)
538}
539var y = padd(this.getUTCFullYear(), "0000");
540var m = padd(this.getUTCMonth() + 1, "00");
541var d = padd(this.getUTCDate(), "00");
542var h = padd(this.getUTCHours(), "00");
543var min = padd(this.getUTCMinutes(), "00");
544var s = padd(this.getUTCSeconds(), "00");
545var isodate = y +  m  + d + "T" + h +  ":" + min + ":" + s
546return "<dateTime.iso8601>" + isodate + "</dateTime.iso8601>";
547}
548Array.prototype.toXmlRpc = function(){
549var retstr = "<array><data>";
550for(var i=0;i<this.length;i++){
551retstr += "<value>" + mod.marshall(this[i]) + "</value>";
552}
553return retstr + "</data></array>";
554}
555mod.test = function(){
556print("creating ServiceProxy object using introspection for method construction...\n");
557var s = new mod.ServiceProxy("http://localhost/testx.py");
558print("%s created\n".format(s));
559print("creating and marshalling test data:\n");
560var o = [1.234, 5, {a:"Hello & < ", b:new Date()}];
561print(mod.marshall(o));
562print("\ncalling echo() on remote service...\n");
563var r = s.echo(o);
564print("service returned data(marshalled again):\n")
565print(mod.marshall(r));
566}
567})