PageRenderTime 627ms CodeModel.GetById 614ms RepoModel.GetById 0ms app.codeStats 0ms

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

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