/examples/jsonrpc/public/services/phpolait/jsolait/lib/jsonrpc.js
JavaScript | 232 lines | 232 code | 0 blank | 0 comment | 26 complexity | fead95c48a0a5c2d3003aa0986cd6952 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
1Module("jsonrpc","0.4.2", function(mod){ 2var urllib = importModule("urllib"); 3mod.InvalidServerResponse = Class(mod.Exception, function(publ, supr){ 4publ.init= function(status){ 5supr(this).init("The server did not respond with a status 200 (OK) but with: " + status); 6this.status = status; 7} 8publ.status; 9}) 10mod.MalformedJSONRpc = Class(mod.Exception, function(publ, supr){ 11publ.init= function(msg, s, trace){ 12supr(this).init(msg,trace); 13this.source = s; 14} 15publ.source; 16}) 17mod.JSONRPCError = Class(mod.Exception, function(publ, supr){ 18publ.init= function(err, trace){ 19supr(this).init(err,trace); 20} 21}) 22mod.marshall = function(obj){ 23if(obj == null){ 24return "null"; 25}else if(obj.toJSON){ 26return obj.toJSON(); 27}else{ 28var v=[]; 29for(var attr in obj){ 30if(typeof obj[attr] != "function"){ 31v.push('"' + attr + '": ' + mod.marshall(obj[attr])); 32} 33} 34return "{" + v.join(", ") + "}"; 35} 36} 37mod.unmarshall = function(source){ 38try { 39var obj; 40eval("obj=" + source); 41return obj; 42}catch(e){ 43throw new mod.MalformedJSONRpc("The server's response could not be parsed.", source, e); 44} 45} 46mod.JSONRPCMethod =Class(function(publ){ 47var postData = function(url, user, pass, data, callback){ 48if(callback == null){ 49var rslt = urllib.postURL(url, user, pass, data, [["Content-Type", "text/plain"]]); 50return rslt; 51}else{ 52urllib.postURL(url, user, pass, data, [["Content-Type", "text/xml"]], callback); 53} 54} 55var handleResponse=function(resp){ 56var status=null; 57try{ 58status = resp.status; 59}catch(e){ 60} 61if(status == 200){ 62var respTxt = ""; 63try{ 64respTxt=resp.responseText; 65}catch(e){ 66} 67if(respTxt == null || respTxt == ""){ 68throw new mod.MalformedJSONRpc("The server responded with an empty document.", ""); 69}else{ 70var rslt = mod.unmarshall(respTxt); 71if(rslt.error != null){ 72throw new mod.JSONRPCError(rslt.error); 73}else{ 74return rslt.result; 75} 76} 77}else{ 78throw new mod.InvalidServerResponse(status); 79} 80} 81var jsonRequest = function(id, methodName, args){ 82var p = [mod.marshall(id), mod.marshall(methodName), mod.marshall(args)]; 83return '{"id":' + p[0] + ', "method":' + p[1] + ', "params":' + p[2] + "}"; 84} 85publ.init = function(url, methodName, user, pass){ 86var fn=function(){ 87var args=new Array(); 88for(var i=0;i<arguments.length;i++){ 89args.push(arguments[i]); 90} 91if(typeof arguments[arguments.length-1] != "function"){ 92var data=jsonRequest("httpReq", fn.methodName, args); 93var resp = postData(fn.url, fn.user, fn.password, data); 94return handleResponse(resp); 95}else{ 96var cb = args.pop(); 97var data=jsonRequest("httpReq", fn.methodName, args); 98postData(fn.url, fn.user, fn.password, data, function(resp){ 99var rslt = null; 100var exc =null; 101try{ 102rslt = handleResponse(resp); 103}catch(e){ 104exc = e; 105} 106try{ 107cb(rslt,exc); 108}catch(e){ 109} 110args = null; 111resp = null; 112}); 113} 114} 115fn.methodName = methodName; 116fn.notify = this.notify; 117fn.url = url; 118fn.user = user; 119fn.password=pass; 120fn.toString = this.toString; 121fn.setAuthentication=this.setAuthentication; 122fn.constructor = this.constructor; 123return fn; 124} 125publ.setAuthentication = function(user, pass){ 126this.user = user; 127this.password = pass; 128} 129publ.notify = function(){ 130var args=new Array(); 131for(var i=0;i<arguments.length;i++){ 132args.push(arguments[i]); 133} 134var data=jsonRequest(null, this.methodName, args); 135postData(this.url, this.user, this.password, data, function(resp){}); 136} 137publ.methodName; 138publ.url; 139publ.user; 140publ.password; 141}) 142mod.ServiceProxy=Class("ServiceProxy", function(publ){ 143publ.init = function(url, methodNames, user, pass){ 144this._url = url; 145this._user = user; 146this._password = pass; 147this._addMethodNames(methodNames); 148} 149publ._addMethodNames = function(methodNames){ 150for(var i=0;i<methodNames.length;i++){ 151var obj = this; 152var names = methodNames[i].split("."); 153for(var n=0;n<names.length-1;n++){ 154var name = names[n]; 155if(obj[name]){ 156obj = obj[name]; 157}else{ 158obj[name] = new Object(); 159obj = obj[name]; 160} 161} 162var name = names[names.length-1]; 163if(obj[name]){ 164}else{ 165var mth = new mod.JSONRPCMethod(this._url, methodNames[i], this._user, this._password); 166obj[name] = mth; 167this._methods.push(mth); 168} 169} 170} 171publ._setAuthentication = function(user, pass){ 172this._user = user; 173this._password = pass; 174for(var i=0;i<this._methods.length;i++){ 175this._methods[i].setAuthentication(user, pass); 176} 177} 178publ._url; 179publ._user; 180publ._password; 181publ._methods=new Array(); 182}) 183mod.ServerProxy= mod.ServiceProxy; 184String.prototype.toJSON = function(){ 185var s = '"' + this.replace(/(["\\])/g, '\\$1') + '"'; 186s = s.replace(/(\n)/g,"\\n"); 187return s; 188} 189Number.prototype.toJSON = function(){ 190return this.toString(); 191} 192Boolean.prototype.toJSON = function(){ 193return this.toString(); 194} 195Date.prototype.toJSON= function(){ 196var padd=function(s, p){ 197s=p+s 198return s.substring(s.length - p.length) 199} 200var y = padd(this.getUTCFullYear(), "0000"); 201var m = padd(this.getUTCMonth() + 1, "00"); 202var d = padd(this.getUTCDate(), "00"); 203var h = padd(this.getUTCHours(), "00"); 204var min = padd(this.getUTCMinutes(), "00"); 205var s = padd(this.getUTCSeconds(), "00"); 206var isodate = y + m + d + "T" + h + ":" + min + ":" + s; 207return '{"jsonclass":["sys.ISODate", ["' + isodate + '"]]}'; 208} 209Array.prototype.toJSON = function(){ 210var v = []; 211for(var i=0;i<this.length;i++){ 212v.push(mod.marshall(this[i])) ; 213} 214return "[" + v.join(", ") + "]"; 215} 216mod.test = function(){ 217try{ 218print("creating ServiceProxy object using introspection for method construction...\n"); 219var s = new mod.ServiceProxy("http://localhost/testj.py",["echo"]); 220print("%s created\n".format(s)); 221print("creating and marshalling test data:\n"); 222var o = [1.234, 5, {a:"Hello ' \" World", b:new Date()}]; 223print(mod.marshall(o)); 224print("\ncalling echo() on remote service...\n"); 225var r = s.echo(o); 226print("service returned data(marshalled again):\n") 227print(mod.marshall(r)); 228}catch(e){ 229reportException(e); 230} 231} 232})