Changeset 22
- Timestamp:
- 11/16/05 15:58:40 (3 years ago)
- Files:
-
- jsolait/trunk/jsolait/lib/jsonrpc.js (modified) (22 diffs)
- jsolait/trunk/jsolait/lib/testing.js (modified) (1 diff)
- jsolait/trunk/jsolait/lib/urllib.js (modified) (14 diffs)
- jsolait/trunk/jsolait/lib/xmlrpc.js (modified) (42 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
jsolait/trunk/jsolait/lib/jsonrpc.js
r17 r22 1 1 /* 2 2 Copyright (c) 2005 Jan-Klaas Kollhof 3 3 4 4 This file is part of the JavaScript o lait library(jsolait). 5 5 6 6 jsolait is free software; you can redistribute it and/or modify 7 7 it under the terms of the GNU Lesser General Public License as published by 8 8 the Free Software Foundation; either version 2.1 of the License, or 9 9 (at your option) any later version. 10 10 11 11 This software is distributed in the hope that it will be useful, 12 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 14 GNU Lesser General Public License for more details. 15 15 16 16 You should have received a copy of the GNU Lesser General Public License 17 17 along with this software; if not, write to the Free Software … … 43 43 publ.status; 44 44 }); 45 45 46 46 /** 47 47 Thrown if an JSON-RPC response is not well formed. … … 74 74 }; 75 75 }); 76 77 76 77 78 78 /** 79 79 Marshalls an object to JSON.(Converts an object into JSON conforming source.) 80 80 It just calls the toJSON function of the objcect. 81 So, to customize serialization of objects one just needs to specify/override the toXmlRpc method 81 So, to customize serialization of objects one just needs to specify/override the toXmlRpc method 82 82 which should return an xml string conforming with XML-RPC spec. 83 83 @param obj The object to marshall … … 99 99 } 100 100 }; 101 102 /** 103 Unmarshalls a JSON source to a JavaScript object. 101 102 /** 103 Unmarshalls a JSON source to a JavaScript object. 104 104 @param source The source to unmarshall. 105 105 @return The JavaScript object created. … … 119 119 The return value of this call will be the return value of the RPC call. 120 120 RPC-Errors will be raised as Exceptions. 121 121 122 122 Asynchronous operation: 123 If the last parameter passed to the method is an JSONRPCAsyncCallback object, 124 then the remote method will be called asynchronously. 123 If the last parameter passed to the method is an JSONRPCAsyncCallback object, 124 then the remote method will be called asynchronously. 125 125 The results and errors are passed to the callback. 126 126 */ 127 127 mod.JSONRPCMethod =Class(function(publ){ 128 128 129 129 var postData = function(url, user, pass, data, callback){ 130 130 if(callback == null){//todo ===undefined … … 135 135 } 136 136 }; 137 137 138 138 var handleResponse=function(resp){ 139 139 var status=null; … … 143 143 } 144 144 if(status == 200){ 145 var respTxt = ""; 146 try{ 145 var respTxt = ""; 146 try{ 147 147 respTxt=resp.responseText; 148 148 }catch(e){ … … 162 162 } 163 163 }; 164 164 165 165 var jsonRequest = function(id, methodName, args){ 166 166 var p = [mod.marshall(id), mod.marshall(methodName), mod.marshall(args)]; … … 180 180 this.password=pass; 181 181 }; 182 182 183 183 publ.__call__=function(){ 184 184 var args=new Array(); … … 187 187 } 188 188 //sync or async call 189 if( !(arguments[arguments.length-1] instanceof Function)){189 if(typeof arguments[arguments.length-1] !='function'){ 190 190 var data=jsonRequest("httpReq", this.methodName, args); 191 191 var resp = postData(this.url, this.user, this.password, data); … … 220 220 this.password = pass; 221 221 }; 222 223 /** 222 223 /** 224 224 Sends the call as a notification which does not have a response. 225 225 Call this as if you would call the method itself. Callbacks are ignored. … … 233 233 postData(this.url, this.user, this.password, data, function(resp){}); 234 234 }; 235 235 236 236 ///The name of the remote method. 237 237 publ.methodName; … … 243 243 publ.password; 244 244 }); 245 245 246 246 /** 247 247 Creates proxy objects which resemble the remote service. … … 255 255 ServerProxy("url", ["methodName1",...], "user", "pass") 256 256 ServerProxy("url", "user", "pass") 257 257 258 258 @param url The url of the service. 259 259 @param methodNames Array of names of methods that can be called on the server. … … 267 267 this._addMethodNames(methodNames); 268 268 }; 269 269 270 270 /** 271 271 Adds new JSONRPCMethods to the proxy server which can then be invoked. … … 295 295 } 296 296 }; 297 297 298 298 /** 299 299 Sets username and password for HTTP Authentication for all methods of this service. … … 308 308 } 309 309 }; 310 310 311 311 ///The url of the service to resemble. 312 312 publ._url; … … 318 318 publ._methods=new Array(); 319 319 }); 320 320 321 321 ///@deprecated Use ServiceProxy instead. 322 322 mod.ServerProxy= mod.ServiceProxy; 323 323 324 324 /** 325 325 Converts a String to JSON. … … 330 330 return s; 331 331 }; 332 332 333 333 /** 334 334 Converts a Number to JSON. … … 337 337 return this.toString(); 338 338 }; 339 339 340 340 /** 341 341 Converts a Boolean to JSON. … … 344 344 return this.toString(); 345 345 }; 346 346 347 347 /** 348 348 Converts a Date to JSON. … … 360 360 var min = padd(this.getUTCMinutes(), "00"); 361 361 var s = padd(this.getUTCSeconds(), "00"); 362 362 363 363 var isodate = y + m + d + "T" + h + ":" + min + ":" + s; 364 364 365 365 return '{"jsonclass":["sys.ISODate", ["' + isodate + '"]]}'; 366 366 }; 367 367 368 368 /** 369 369 Converts an Array to JSON. jsolait/trunk/jsolait/lib/testing.js
r21 r22 95 95 this.testScope(); 96 96 }catch(e){ 97 if(e instanceofmod.AssertFailed){97 if(e.constructor == mod.AssertFailed){ 98 98 this.error = e; 99 99 this.failed=true; jsolait/trunk/jsolait/lib/urllib.js
r8 r22 1 1 /* 2 2 Copyright (c) 2003 Jan-Klaas Kollhof 3 3 4 4 This file is part of the JavaScript o lait library(jsolait). 5 5 6 6 jsolait is free software; you can redistribute it and/or modify 7 7 it under the terms of the GNU Lesser General Public License as published by 8 8 the Free Software Foundation; either version 2.1 of the License, or 9 9 (at your option) any later version. 10 10 11 11 This software is distributed in the hope that it will be useful, 12 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 14 GNU Lesser General Public License for more details. 15 15 16 16 You should have received a copy of the GNU Lesser General Public License 17 17 along with this software; if not, write to the Free Software … … 39 39 }; 40 40 }); 41 41 42 42 /** 43 43 Thrown if an HTTP request could not be opened. … … 52 52 }; 53 53 }); 54 54 55 55 /** 56 56 Thrown is arequest could not be sent to the server. … … 65 65 }; 66 66 }); 67 67 68 68 /** 69 69 Mimics the HTTPRequest object using Adobe's SVG Viewer's postURL and getURL. … … 137 137 }; 138 138 }); 139 139 140 140 /** 141 141 Creates an HTTP request object for retreiving files. 142 142 @return HTTP request object. 143 */ 143 */ 144 144 var getHTTP=function() { 145 145 var obj; … … 154 154 }catch(e){ 155 155 try{// to get the old MS HTTP request object 156 obj = new ActiveXObject("microsoft.XMLHTTP"); 156 obj = new ActiveXObject("microsoft.XMLHTTP"); 157 157 }catch(e){ 158 158 try{//to create the ASV request object. … … 162 162 } 163 163 } 164 } 164 } 165 165 } 166 166 } … … 173 173 sendRequest("get", "url") 174 174 sendRequest("post", "url", "data") 175 175 176 176 with headers: 177 177 sendRequest("get", "url", [["headername","value"]]) 178 178 sendRequest("post", "url", "data", [["headername","value"]]) 179 179 180 180 with user information: 181 181 sendRequest("get", "url", "user", "pass") 182 182 sendRequest("post", "url", "user", "pass", "data") 183 183 184 184 with headers and user information: 185 185 sendRequest("get", "url", "user", "pass", [["headername","value"]]) 186 186 sendRequest("post", "url", "user", "pass", "data", [["headername","value"]]) 187 187 188 188 To make the request asynchronous just add a callback function as the last argument to the calls above. 189 189 190 190 @param type Type of connection (GET, POST, ...). 191 191 @param url The URL to retrieve. … … 200 200 var async=false; 201 201 //check if the last argument is a function and treat it as callback; 202 if( arguments[arguments.length-1] instanceof Function){202 if(typeof arguments[arguments.length-1] == 'function'){ 203 203 var async=true; 204 204 callback = arguments[arguments.length-1]; … … 238 238 for(var i=0;i< headers.length;i++){ 239 239 try{//opera 8b does not support setRequestHeader todo: 240 xmlhttp.setRequestHeader(headers[i][0], headers[i][1]); 240 xmlhttp.setRequestHeader(headers[i][0], headers[i][1]); 241 241 }catch(e){ 242 242 } 243 243 } 244 244 245 245 if(async){//set up a callback 246 246 xmlhttp.onreadystatechange=function(){ … … 249 249 xmlhttp = null; //help IE with garbage collection 250 250 }else if (xmlhttp.readyState==2){ 251 //status property should be available (MS IXMLHTTPRequest documentation) 251 //status property should be available (MS IXMLHTTPRequest documentation) 252 252 //in Mozilla it is not if the request failed(server not reachable) 253 253 //in IE it is not available at all ?! … … 261 261 } 262 262 }catch(e){ 263 263 264 264 } 265 265 } 266 266 }; 267 267 } 268 268 269 269 try{ 270 270 xmlhttp.send(data); 271 }catch(e){ 271 }catch(e){ 272 272 if(async){ 273 273 callback(xmlhttp, e); … … 290 290 @return HTTP request object. 291 291 */ 292 mod.getURL=function(url, user, pass, headers, callback) { 292 mod.getURL=function(url, user, pass, headers, callback) { 293 293 var a=["GET"]; 294 294 for(var i=0;i<arguments.length;i++){ … … 309 309 @return HTTP request object. 310 310 */ 311 mod.postURL=function(url, user, pass, data, headers, callback) { 311 mod.postURL=function(url, user, pass, data, headers, callback) { 312 312 var a= ["POST"]; 313 313 for(var i=0;i<arguments.length;i++){ jsolait/trunk/jsolait/lib/xmlrpc.js
r17 r22 1 1 /* 2 2 Copyright (c) 2003-2005 Jan-Klaas Kollhof 3 3 4 4 This file is part of the JavaScript o lait library(jsolait). 5 5 6 6 jsolait is free software; you can redistribute it and/or modify 7 7 it under the terms of the GNU Lesser General Public License as published by 8 8 the Free Software Foundation; either version 2.1 of the License, or 9 9 (at your option) any later version. 10 10 11 11 This software is distributed in the hope that it will be useful, 12 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 14 GNU Lesser General Public License for more details. 15 15 16 16 You should have received a copy of the GNU Lesser General Public License 17 17 along with this software; if not, write to the Free Software … … 45 45 publ.status; 46 46 }); 47 47 48 48 /** 49 49 Thrown if an XML-RPC response is not well formed. … … 64 64 }); 65 65 /** 66 Thrown if the RPC response is a Fault. 66 Thrown if the RPC response is a Fault. 67 67 */ 68 68 mod.Fault = Class(mod.Exception, function(publ, supr){ … … 86 86 Marshalls an object to XML-RPC.(Converts an object into XML-RPC conforming xml.) 87 87 It just calls the toXmlRpc function of the objcect. 88 So, to customize serialization of objects one just needs to specify/override the toXmlRpc method 88 So, to customize serialization of objects one just needs to specify/override the toXmlRpc method 89 89 which should return an xml string conforming with XML-RPC spec. 90 90 @param obj The object to marshall … … 105 105 } 106 106 }; 107 107 108 108 /** 109 109 Unmarshalls an XML document to a JavaScript object. (Converts xml to JavaScript object.) … … 122 122 return rslt; 123 123 }; 124 124 125 125 /** 126 126 Unmarshalls an XML document to a JavaScript object like unmarshall but expects a DOM document as parameter. … … 144 144 } 145 145 }catch(e){ 146 if(e instanceofmod.Fault){//just rethrow the fault.146 if(e.constructor == mod.Fault){//just rethrow the fault. 147 147 throw e; 148 148 }else { 149 throw new mod.MalformedXmlRpc("Unmarshalling of XML failed.", xml, e); 150 } 151 } 152 }; 153 149 throw new mod.MalformedXmlRpc("Unmarshalling of XML failed.", xml, e); 150 } 151 } 152 }; 153 154 154 /** 155 155 Parses a methodeResponse element. … … 173 173 } 174 174 default: 175 throw new mod.MalformedXmlRpc("'fault' or 'params' element expected.\nFound: '" + child.tagName + "'"); 175 throw new mod.MalformedXmlRpc("'fault' or 'params' element expected.\nFound: '" + child.tagName + "'"); 176 176 } 177 177 } 178 178 } 179 179 //no child elements found 180 throw new mod.MalformedXmlRpc("No child elements found."); 181 }catch(e){ 182 if(e instanceofmod.Fault){180 throw new mod.MalformedXmlRpc("No child elements found."); 181 }catch(e){ 182 if(e.constructor == mod.Fault){ 183 183 throw e; 184 184 }else{ 185 throw new mod.MalformedXmlRpc("'methodResponse' element could not be parsed.",null,e); 185 throw new mod.MalformedXmlRpc("'methodResponse' element could not be parsed.",null,e); 186 186 } 187 187 } … … 190 190 Parses a methodCall element. 191 191 @param node The methodCall element. 192 @return Array [methodName,params]. 193 */ 192 @return Array [methodName,params]. 193 */ 194 194 var parseMethodCall = function(node){ 195 195 try{ … … 207 207 break; 208 208 default: 209 throw new mod.MalformedXmlRpc("'methodName' or 'params' element expected.\nFound: '" + child.tagName + "'"); 209 throw new mod.MalformedXmlRpc("'methodName' or 'params' element expected.\nFound: '" + child.tagName + "'"); 210 210 } 211 211 } … … 217 217 } 218 218 }catch(e){ 219 throw new mod.MalformedXmlRpc("'methodCall' element could not be parsed.",null,e); 219 throw new mod.MalformedXmlRpc("'methodCall' element could not be parsed.",null,e); 220 220 } 221 221 }; … … 223 223 Parses a params element. 224 224 @param node The params element. 225 @return Array of params values. 225 @return Array of params values. 226 226 */ 227 227 var parseParams = function(node){ … … 236 236 break; 237 237 default: 238 throw new mod.MalformedXmlRpc("'param' element expected.\nFound: '" + child.tagName + "'"); 238 throw new mod.MalformedXmlRpc("'param' element expected.\nFound: '" + child.tagName + "'"); 239 239 } 240 240 } … … 243 243 return params; 244 244 }catch(e){ 245 throw new mod.MalformedXmlRpc("'params' element could not be parsed.",null,e); 245 throw new mod.MalformedXmlRpc("'params' element could not be parsed.",null,e); 246 246 } 247 247 }; … … 260 260 return parseValue(child); 261 261 default: 262 throw new mod.MalformedXmlRpc("'value' element expected.\nFound: '" + child.tagName + "'"); 262 throw new mod.MalformedXmlRpc("'value' element expected.\nFound: '" + child.tagName + "'"); 263 263 } 264 264 } … … 267 267 throw new mod.MalformedXmlRpc("'value' element expected.But none found."); 268 268 }catch(e){ 269 throw new mod.MalformedXmlRpc("'param' element could not be parsed.",null,e); 269 throw new mod.MalformedXmlRpc("'param' element could not be parsed.",null,e); 270 270 } 271 271 }; … … 306 306 return null; 307 307 default: 308 throw new mod.MalformedXmlRpc("'string','int','i4','double','boolean','base64','dateTime.iso8601','array' or 'struct' element expected.\nFound: '" + child.tagName + "'"); 308 throw new mod.MalformedXmlRpc("'string','int','i4','double','boolean','base64','dateTime.iso8601','array' or 'struct' element expected.\nFound: '" + child.tagName + "'"); 309 309 } 310 310 } … … 322 322 } 323 323 }catch(e){ 324 throw new mod.MalformedXmlRpc("'value' element could not be parsed.",null,e); 324 throw new mod.MalformedXmlRpc("'value' element could not be parsed.",null,e); 325 325 } 326 326 }; … … 335 335 return s.decode("base64"); 336 336 }catch(e){ 337 throw new mod.MalformedXmlRpc("'base64' element could not be parsed.",null,e); 337 throw new mod.MalformedXmlRpc("'base64' element could not be parsed.",null,e); 338 338 } 339 339 }; … … 351 351 } 352 352 }catch(e){ 353 throw new mod.MalformedXmlRpc("'dateTime.iso8601' element could not be parsed.",null,e); 353 throw new mod.MalformedXmlRpc("'dateTime.iso8601' element could not be parsed.",null,e); 354 354 } 355 355 }; … … 368 368 return parseData(child); 369 369 default: 370 throw new mod.MalformedXmlRpc("'data' element expected.\nFound: '" + child.tagName + "'"); 371 } 372 } 373 } 374 throw new mod.MalformedXmlRpc("'data' element expected. But not found."); 375 }catch(e){ 376 throw new mod.MalformedXmlRpc("'array' element could not be parsed.",null,e); 370 throw new mod.MalformedXmlRpc("'data' element expected.\nFound: '" + child.tagName + "'"); 371 } 372 } 373 } 374 throw new mod.MalformedXmlRpc("'data' element expected. But not found."); 375 }catch(e){ 376 throw new mod.MalformedXmlRpc("'array' element could not be parsed.",null,e); 377 377 } 378 378 }; … … 393 393 break; 394 394 default: 395 throw new mod.MalformedXmlRpc("'value' element expected.\nFound: '" + child.tagName + "'"); 396 } 397 } 395 throw new mod.MalformedXmlRpc("'value' element expected.\nFound: '" + child.tagName + "'"); 396 } 397 } 398 398 } 399 399 return rslt; 400 400 }catch(e){ 401 throw new mod.MalformedXmlRpc("'data' element could not be parsed.",null,e); 401 throw new mod.MalformedXmlRpc("'data' element could not be parsed.",null,e); 402 402 } 403 403 }; … … 421 421 break; 422 422 default: 423 throw new mod.MalformedXmlRpc("'data' element expected.\nFound: '" + child.tagName + "'"); 423 throw new mod.MalformedXmlRpc("'data' element expected.\nFound: '" + child.tagName + "'"); 424 424 } 425 425 } … … 427 427 return struct; 428 428 }catch(e){ 429 throw new mod.MalformedXmlRpc("'struct' element could not be parsed.",null,e); 429 throw new mod.MalformedXmlRpc("'struct' element could not be parsed.",null,e); 430 430 } 431 431 }; … … 444 444 switch (child.tagName){ 445 445 case "value": 446 value = parseValue(child); 446 value = parseValue(child); 447 447 break; 448 448 case "name": … … 452 452 break; 453 453 default: 454 throw new mod.MalformedXmlRpc("'value' or 'name' element expected.\nFound: '" + child.tagName + "'"); 454 throw new mod.MalformedXmlRpc("'value' or 'name' element expected.\nFound: '" + child.tagName + "'"); 455 455 } 456 456 } … … 463 463 return [name, value]; 464 464 }catch(e){ 465 throw new mod.MalformedXmlRpc("'member' element could not be parsed.",null,e); 465 throw new mod.MalformedXmlRpc("'member' element could not be parsed.",null,e); 466 466 } 467 467 }; … … 478 478 switch (child.tagName){ 479 479 case "value": 480 var flt = parseValue(child); 480 var flt = parseValue(child); 481 481 return new mod.Fault(flt.faultCode, flt.faultString); 482 482 default: 483 throw new mod.MalformedXmlRpc("'value' element expected.\nFound: '" + child.tagName + "'"); 484 } 485 } 486 } 487 throw new mod.MalformedXmlRpc("'value' element expected. But not found."); 488 }catch(e){ 489 throw new mod.MalformedXmlRpc("'fault' element could not be parsed.",null,e); 490 } 491 }; 492 493 483 throw new mod.MalformedXmlRpc("'value' element expected.\nFound: '" + child.tagName + "'"); 484 } 485 } 486 } 487 throw new mod.MalformedXmlRpc("'value' element expected. But not found."); 488 }catch(e){ 489 throw new mod.MalformedXmlRpc("'fault' element could not be parsed.",null,e); 490 } 491 }; 492 493 494 494 /** 495 495 Class for creating XML-RPC methods. … … 497 497 The return value of this call will be the return value of the RPC call. 498 498 RPC-Faults will be raised as Exceptions. 499 499 500 500 Asynchronous operation: 501 If the last parameter passed to the method is an XMLRPCAsyncCallback object, 502 then the remote method will be called asynchronously. 501 If the last parameter passed to the method is an XMLRPCAsyncCallback object, 502 then the remote method will be called asynchronously. 503 503 The results and errors are passed to the callback. 504 504 */ … … 512 512 } 513 513 }; 514 514 515 515 var handleResponse=function(resp){ 516 516 var status=null; … … 525 525 }catch(e){ 526 526 } 527 var respTxt = ""; 528 try{ 527 var respTxt = ""; 528 try{ 529 529 respTxt=resp.responseText; 530 530 }catch(e){ … … 543 543 } 544 544 }; 545 545 546 546 var getXML = function(methodName, args){ 547 547 var data='<?xml version="1.0"?><methodCall><methodName>' + methodName + '</methodName>'; … … 556 556 return data; 557 557 }; 558 559 558 559 560 560 /** 561 561 Initializes the XML-RPC method. … … 572 572 this.password=pass; 573 573 }; 574 575 574 575 576 576 publ.__call__=function(){ 577 577 //sync or async call 578 if( !(arguments[arguments.length-1] instanceof Function)){578 if(typeof arguments[arguments.length-1] != 'function'){ 579 579 var data=getXML(this.methodName,arguments); 580 580 var resp = postData(this.url, this.user, this.password, data); … … 604 604 } 605 605 }; 606 606 607 607 /** 608 608 Returns the method representation for system.multicall. … … 638 638 publ.password; 639 639 }); 640 640 641 641 /** 642 642 Creates proxy objects which resemble the remote service. … … 651 651 ServerProxy("url", ["methodName1",...], "user", "pass") 652 652 ServerProxy("url", "user", "pass") 653 653 654 654 @param url The url of the service. 655 655 @param methodNames=[] Array of names of methods that can be called on the server. … … 682 682 } 683 683 }; 684 684 685 685 /** 686 686 Adds new XMLRPCMethods to the proxy server which can then be invoked. … … 710 710 } 711 711 }; 712 712 713 713 /** 714 714 Sets username and password for HTTP Authentication for all methods of this service. … … 723 723 } 724 724 }; 725 725 726 726 /** 727 727 Initiate XML-RPC introspection to retrieve methodnames from the server … … 742 742 publ._methods=new Array(); 743 743 }); 744 744 745 745 ///@deprecated Use ServiceProxy instead. 746 746 mod.ServerProxy= mod.ServiceProxy; 747 747 748 748 /** 749 749 XML-RPC representation of a string. … … 793 793 var min = padd(this.getUTCMinutes(), "00"); 794 794 var s = padd(this.getUTCSeconds(), "00"); 795 795 796 796 var isodate = y + m + d + "T" + h + ":" + min + ":" + s; 797 797 798 798 return "<dateTime.iso8601>" + isodate + "</dateTime.iso8601>"; 799 799 };
