3 Modification information for LGPL compliance
5 r56990 - 2010-06-16 13:05:36 -0700 (Wed, 16 Jun 2010) - kjing - snapshot "Mango" svn branch to a new one for GitHub sync
7 r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
9 r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
11 r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3 tags and updated the build system
13 r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
15 r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
17 r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
19 r21575 - 2007-04-09 16:28:53 -0700 (Mon, 09 Apr 2007) - chris - Bug 12274 - SECURITY: CSRF (cross-site request forgery) fix.
30 ./include/jsclass_async.js
31 ./include/jsclass_base.js
32 ./include/javascript/yui/connection.js
33 ./include/jsolait/lib/jsonrpc.js
34 ./include/jsolait/lib/jsonrpclite.js
37 ./modules/Emails/Email.js
39 r21544 - 2007-04-09 15:12:37 -0700 (Mon, 09 Apr 2007) - chris - Bug 12274 - SECURITY: CSRF (cross-site request forgery) fix.
46 include/jsolait/lib/jsonrpc.js
48 r6158 - 2005-07-12 12:06:05 -0700 (Tue, 12 Jul 2005) - andrew - Need to pass data.
50 r6129 - 2005-07-08 16:42:58 -0700 (Fri, 08 Jul 2005) - julian - Added in-depth error reporting for "Not well formed" error
52 r5313 - 2005-05-13 10:01:43 -0700 (Fri, 13 May 2005) - ajay - 1010, this script now uses json.js for decoding.
54 r4286 - 2005-04-18 04:04:05 -0700 (Mon, 18 Apr 2005) - robert - scheduler:
57 r4085 - 2005-04-13 17:30:42 -0700 (Wed, 13 Apr 2005) - robert - adding meeting scheduler and accept/decline
63 Copyright (c) 2003-2004 Jan-Klaas Kollhof
65 This file is part of the JavaScript o lait library(jsolait).
67 jsolait is free software; you can redistribute it and/or modify
68 it under the terms of the GNU Lesser General Public License as published by
69 the Free Software Foundation; either version 2.1 of the License, or
70 (at your option) any later version.
72 This software is distributed in the hope that it will be useful,
73 but WITHOUT ANY WARRANTY; without even the implied warranty of
74 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
75 GNU Lesser General Public License for more details.
77 You should have received a copy of the GNU Lesser General Public License
78 along with this software; if not, write to the Free Software
79 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
83 Provides an JSON-RPC imlementation.
85 Module("jsonrpc", "0.3.2", function(mod){
87 var lang = importModule("lang");
88 var tokens = lang.tokens;
90 var ObjectBuffer=Class("ObjectBuffer", function(publ, supr){
94 publ.getObjects=function(data){
96 var t = new lang.Tokenizer(this.data);
100 while(! t.finished()){
102 if(n.type != tokens.ERR){
105 }else if(n.value == "}"){
108 var s = this.data.slice(readCnt, n.pos+1);
117 this.data = this.data.slice(readCnt);
122 var nameAllowed=function(name){
123 return name.match(/^[a-zA-Z]\w*$/) != null;
126 var getMethodByName=function(obj, name){
127 try{//to get a method by asking the service
128 obj = obj._getMethodByName(name)
130 var names = name.split(".");
131 for(var i=0;i<names.length;i++){
133 if(nameAllowed(name)){
141 var Response=Class("Response", function(publ, supr){
142 publ.init=function(id, result, error){
144 this.result = result;
147 publ._toJSON=function(){
148 var p = [lang.objToJson(this.id), lang.objToJson(this.result),lang.objToJson(this.error)];
149 return '{"id":' + p[0] + ', "result":' + p[1] + ', "error":' + p[2] + "}";
153 var Request=Class("Request", function(publ, supr){
154 publ.init=function(id, method, params){
156 this.method = method;
157 this.params = params;
160 for(hello in this.params)
162 str += "var:"+hello+":"+this.params[hello]+":\n";
167 publ._toJSON=function(){
168 var p = [lang.objToJson(this.id), lang.objToJson(this.method),lang.objToJson(this.params)];
169 return '{"id":' + p[0] + ', "method":' + p[1] + ', "params":' + p[2] + "}";
173 var Notification=Class("Notification", function(publ, supr){
174 publ.init=function(method, params){
175 this.method = method;
176 this.params = params;
178 publ._toJSON=function(){
179 var p = [lang.objToJson(this.method),lang.objToJson(this.params)];
180 return '{"method":' + p[0] + ', "params":' + p[1] + "}";
184 var ResponseHandler=Class("ResponseHandler", function(publ, supr){
185 publ.init=function(callback){
186 this.callback=callback;
188 publ.handleResponse=function(resp){
189 this.callback(resp.result, resp.error);
193 var RPCLib = Class("RPCLib", function(publ, supr){
197 var BaseConnectionHandler = Class("BaseConnectionHandler", function(publ, supr){
198 publ.init=function(service){
199 this.service = service;
200 this.jsonParser = new lang.JSONParser();
201 this.jsonParser.addLib(new RPCLib(), "rpc", []);
202 this.respHandlers = [];
203 this.objBuffer = new ObjectBuffer();
206 publ.addResponseHandler=function(cb){
208 while(this.respHandlers[""+id] ){
212 this.respHandlers[id] = new ResponseHandler(cb);
216 publ.send = function(data){
219 publ.sendNotify = function(name, args){
220 var n = new Notification(name, args);
221 n = this.jsonParser.objToJson(n);
225 publ.sendRequest = function(name, args, callback){
226 var id = this.addResponseHandler(callback);
227 var r = new Request(id, name, args);
228 r = this.jsonParser.objToJson(r);
232 publ.sendResponse = function(id, result, error){
233 var r = new Response(id, result, error);
234 r = this.jsonParser.objToJson(r);
238 publ.handleRequest = function(req){
239 var name = req.method;
240 var params = req.params;
242 if(this.service[name]){
244 var rslt = this.service[name].apply(this.service,params);
245 this.sendResponse(id, rslt, null)
247 this.sendResponse(id, null, new ApplicationError("" + e))
250 this.sendResponse(id, null, new MethodNotFound());
254 publ.handleNotification = function(notif){
255 if(this.service[notif.method]){
257 this.service[notif.method].apply(this.service, notif.params);
263 publ.handleResponse = function(resp){
265 var h = this.respHandlers[id];
266 h.handleResponse(resp)
267 delete this.respHandlers[id]
270 publ.handleData = function(data){
271 var objs = this.objBuffer.getObjects(data);
272 for(var i=0;i<objs.length;i++){
274 var obj = this.jsonParser.jsonToObj(objs[i]);
276 throw "Not well formed";
278 if(obj.method != null){
280 this.handleRequest(new Request(obj.id, obj.method, obj.params));
282 this.handleNotification(new Notification(obj.method, obj.params));
284 }else if(obj.id != null){
285 this.handleResponse(new Response(obj.id, obj.result, obj.error));
287 throw "Unknown Data";
293 var SocketConnectionHandler = Class("SocketConnectionHandler", BaseConnectionHandler, function(publ, supr){
294 publ.init=function(socket, localService){
295 this.socket = socket;
296 socket.addEventListener("connectionData", this, false);
297 supr(this).init( localService);
300 publ.handleEvent=function(evt){
301 this.handleData(evt.data);
304 publ.send=function(data){
305 this.socket.send(data);
308 publ.close=function(data){
313 var HTTPConnectionHandler = Class("HTTPConnectionHandler", BaseConnectionHandler, function(publ, supr){
316 publ.init=function(url, localService){
317 urllib=importModule("urllib");
319 supr(this).init( localService);
322 publ.handleData = function(data){
325 var obj = JSON.parse(data);
327 throw " Not well formed\n\n" + e + "\n\nResponse from server:\n\n " + data;
332 throw "Unknown Data (No id property found)";
336 publ.sendRequest = function(name, args, callback){
338 if(typeof callback != "function"){//see if it is sync
342 var data = new Request(this.request_id++, name, args);
343 // cn: bug 12274 - defend against CSRF
344 data = JSON.stringify(data); // creates security envelope wrapped JSON object
347 var rsp = urllib.postURL(this.url, data, [["Content-Type", "text/plain"]]);
348 rsp = this.handleData(rsp.responseText);
354 }else{//async connection uses the respHandler to handle the repsonse
356 var request_id = this.request_id;
357 urllib.postURL(this.url, data, [["Content-Type", "text/plain"]], function(rsp){
359 rsp = self.handleData(rsp.responseText);
362 callback(request_id,null,e);
365 callback(request_id,rsp.result, rsp.error);
366 //callback(this.request_id,rsp.result, rsp.error);
367 //callback(rsp.result, rsp.error);
372 publ.sendNotify = function(name, args){
373 var data = new Notification(name, args);
374 data = this.jsonParser.objToJson(data);
375 urllib.postURL(this.url, data, [["Content-Type", "text/plain"]], function(rsp){});
379 var PeerObject=Class("PeerObject", function(publ, supr){
380 publ.init=function(name, conn){
383 for(var i=0;i<arguments.length;i++){
384 args[i] = arguments[i];
387 return conn.sendRequest(name, args, cb);
393 var PeerNotifyObject=Class("PeerNotifyObject", function(publ, supr){
394 publ.init=function(name, conn){
397 for(var i=0;i<arguments.length;i++){
398 args[i] = arguments[i];
400 conn.sendNotify(name, args);
406 var BasePeer = Class("BasePeer", function(publ, supr){
407 publ.init=function(conn, methodNames){
409 this.notify = new PeerObject("notify", conn);
410 this._add(methodNames);
413 var setupPeerMethod=function(root, methodName, conn, MethClass){
414 var names = methodName.split(".");
416 for(var n=0;n<names.length-1;n++){
421 obj[name] = new Object();
425 var name = names[names.length-1];
428 var mth = new MethClass(methodName, conn);
433 publ._add = function(methodNames){
434 for(var i=0;i<methodNames.length;i++){
435 setupPeerMethod(this, methodNames[i], this._conn, PeerObject);
436 setupPeerMethod(this.notify, methodNames[i], this._conn, PeerNotifyObject);
442 mod.ServiceProxy = Class("ServiceProxy", BasePeer, function(publ, supr){
443 publ.init = function(url, methodNames, localService){
444 var n = url.match(/^jsonrpc:\/\/(.*:\d*)$/);
445 if(n!=null){//is it json-rpc over TCP protocoll
448 var socket = createConnection();
450 throw "Can't create a socket connection."
452 socket.connect(hostaddr);
453 supr(this).init( new SocketConnectionHandler(socket, localService), methodNames);
454 }else{//or is it json-rpc over http
455 this.httpConn = new HTTPConnectionHandler(url, localService);
456 supr(this).init( this.httpConn, methodNames);