PageRenderTime 98ms CodeModel.GetById 21ms RepoModel.GetById 4ms app.codeStats 1ms

/examples/dynamictable/public/phpolait/jsolait/lib/xmlrpc.js

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