* @author Jamie Pratt */ import mx.controls.Label; import mx.controls.Alert; import mx.messaging.channels.AMFChannel; import com.adobe.serialization.json.JSON; // Import the debugger // import nl.demonsters.debugger.MonsterDebugger; protected var methods:Array; protected var introspector:String; public var rooturl:String; [Bindable] public var serviceurl:String; public var channel:AMFChannel; [Bindable] public var argumentToolTip:String = "You can use JSON syntax for method arguments ie. an array is written like this [item1, item2, etc.] objects are written {\"propname\":value, \"propname2\":value2, etc}"; // Variable to hold the debugger // private var debugger:MonsterDebugger; /** * restores the last settings if available */ public function init():void { // Init the debugger // debugger = new MonsterDebugger(this); // Send a simple trace // MonsterDebugger.trace(this, "Hello World!"); var so:SharedObject = SharedObject.getLocal('AMFTester'); if (so.data.token) { token.text = so.data.token; } if (so.data.username) { username.text = so.data.username; password.text = so.data.password; } if (so.data.mode == 'username'){ loginType.selectedIndex = 1; } this.rememberpassword.selected = so.data.rememberpassword; this.remembertoken.selected = so.data.remembertoken; this.rooturl = Application.application.parameters.rooturl; this.urllabel1.text = 'Root URL :'+this.rooturl; this.urllabel2.text = 'Root URL :'+this.rooturl; } public function doConnectToken():void { serviceurl = this.rooturl + '/webservice/amf/server.php?'+ 'wstoken='+this.token.text; this.doConnect(); // saving settings for next time var so:SharedObject = SharedObject.getLocal('AMFTester'); if (this.rememberpassword.selected == true ){ so.setProperty('token', this.token.text); } else { so.setProperty('token', null);//delete shared obj prop } so.setProperty('remembertoken', this.remembertoken.selected); so.setProperty('mode', 'token'); so.flush(); } public function doConnectUsername():void { serviceurl = this.rooturl + '/webservice/amf/simpleserver.php?'+ 'wsusername=' + this.username.text+ '&wspassword=' + this.password.text; this.doConnect(); // saving settings for next time var so:SharedObject = SharedObject.getLocal('AMFTester'); if (this.rememberpassword.selected == true ){ so.setProperty('username', this.username.text); so.setProperty('password', this.password.text); } else { so.setProperty('username', null);//delete shared obj prop so.setProperty('password', null); } so.setProperty('rememberpassword', this.rememberpassword.selected); so.setProperty('mode', 'username'); so.flush(); } /** * initializes the connection */ private function doConnect():void { push("Connecting to "+serviceurl); //api.source = 'MethodDescriptor'; api.setCredentials(this.username.text, this.password.text); api.removeEventListener("result", outputResult); api.addEventListener("result", handleConnection); api.getOperation("MethodDescriptor.getMethods").send(); if (!api.hasEventListener("fault")) { api.addEventListener("fault", faultHandler); } this.panelDebug.enabled = false; } /** * initializes the debugger dialog with the method list and everything */ protected function handleConnection(event:ResultEvent):void { methods = []; for (var cls:String in event.result) { for (var meth:String in event.result[cls]['methods']) { methods.push({label: cls+'.'+meth, docs: event.result[cls]['methods'][meth]['docs'], args: event.result[cls]['methods'][meth]['params']}); } } methods.sortOn('label', Array.CASEINSENSITIVE); this.panelDebug.enabled = true; this.maintabs.selectedIndex = 1; func.dataProvider = methods; api.removeEventListener("result", handleConnection); api.addEventListener("result", outputResult); reloadArgs(); } /** * outputs a response from the server */ protected function outputResult(event:ResultEvent):void { var keys:Array = new Array(); for (var key:String in event.result){ keys.push(key); } push("Result returned \n" + obj2string(event.result)); } protected function obj2string(obj:Object, depth:Number=0):String{ var string:String = ''; if (obj==null){ return 'NULL'; } switch (typeof(obj)){ case 'object': if (obj is Array){ string += "Array\n"; } else { string += "Object\n"; } string += depth2string(depth+1)+"(\n"; for (var key:String in obj){ string += depth2string(depth+2)+'['+key+'] => '+obj2string(obj[key], depth+3); } string += depth2string(depth+1)+")\n"; break; case 'string': var formattedstring:String = obj.toString(); formattedstring = formattedstring.replace(/\r/g, ""); formattedstring = formattedstring.replace(/\n/g, "\n"+depth2string(depth+3)); string += '"'+formattedstring+'"'+"-("+typeof(obj)+")"+"\n"; break; default : string += obj.toString()+"-("+typeof(obj)+")"+"\n"; } return string; } protected function depth2string(depth:Number):String{ var string:String = ''; var i:Number = 0; while (i= 1){ this['cbarg'+i].selected = true; i--; } } else { i++; while (i <= 7){ this['cbarg'+i].selected = false; i++; } } } /** * calls a method on the server */ protected function execute():void { var input:TextInput; var argumentArray:Array = []; var argumentErrors:Array = Validator.validateAll(argumentValidators); if (argumentErrors.length != 0){ // MonsterDebugger.trace(this, argumentErrors); return; } for(var i:int = 1; i <= 7; i++) { input = this['arg' +i] as TextInput; if(input && input.visible) { if (!this['cbarg' +i].selected){ break; } else if (input.text.indexOf("\"") == 0 || input.text.indexOf("{") == 0 || input.text.indexOf("[") == 0) try { argumentArray.push(JSON.decode(input.text)); } catch (err:Error){ return; } else argumentArray.push(input.text as String); } } //no other way to pass arguments as array : switch (argumentArray.length){ case 0: api.getOperation(func.selectedLabel).send(); break; case 1: api.getOperation(func.selectedLabel).send(argumentArray[0]); break; case 2: api.getOperation(func.selectedLabel).send(argumentArray[0], argumentArray[1]); break; case 3: api.getOperation(func.selectedLabel).send(argumentArray[0], argumentArray[1], argumentArray[2]); break; case 4: api.getOperation(func.selectedLabel).send(argumentArray[0], argumentArray[1], argumentArray[2], argumentArray[3]); break; case 5: api.getOperation(func.selectedLabel).send(argumentArray[0], argumentArray[1], argumentArray[2], argumentArray[3], argumentArray[4]); break; case 6: api.getOperation(func.selectedLabel).send(argumentArray[0], argumentArray[1], argumentArray[2], argumentArray[3], argumentArray[4], argumentArray[5]); break; case 7: api.getOperation(func.selectedLabel).send(argumentArray[0], argumentArray[1], argumentArray[2], argumentArray[3], argumentArray[4], argumentArray[5], argumentArray[6]); break; } // MonsterDebugger.trace(this, [func.selectedLabel, argumentArray[0], argumentArray[1], argumentArray[2], argumentArray[3], argumentArray[4], argumentArray[5], argumentArray[6]]); push("Calling "+func.selectedLabel+" with arguments \n"+obj2string(argumentArray)); } /** * clears debug consoles */ protected function clear():void { output.text = output.text = ""; } /** * clears debug consoles */ protected function goBottom():void { output.verticalScrollPosition = output.maxVerticalScrollPosition; } /** * refreshes the method list */ protected function refresh():void { api.removeEventListener("result", outputResult); api.addEventListener("result", handleConnection); api.exec(introspector); } /** * returns timestamp string */ protected function time():String { var d:Date = new Date(); var ret:String = d.hours+":"+d.minutes+":"+d.seconds+"."+d.milliseconds; return ret + "000000000000".substring(ret.length); } /** * handler for specific net events */ public function faultHandler(event:FaultEvent):void { push("Error("+event.type+" - "+ event.fault.faultCode + "): "+event.fault.faultString+", "+event.fault.faultDetail); } /** * pushes text into a console and scrolls it down automatically */ public function push(text:String):void { output.text += time() + ": "+ text + "\n"; output.verticalScrollPosition = output.maxVerticalScrollPosition; } ]]>