PageRenderTime 21ms CodeModel.GetById 10ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

/src/name/carter/mark/flex/util/remote/xmlrpc/XMLRPCClient.as

http://transcriptstudio4isha.googlecode.com/
ActionScript | 93 lines | 75 code | 11 blank | 7 comment | 7 complexity | bf28fd19405dae8d94dca2b07cadff91 MD5 | raw file
 1package name.carter.mark.flex.util.remote.xmlrpc
 2{
 3	import flash.net.URLRequestHeader;
 4	import flash.net.URLRequestMethod;
 5	import flash.utils.getTimer;
 6	
 7	import mx.rpc.events.FaultEvent;
 8	import mx.rpc.events.ResultEvent;
 9	import mx.rpc.http.HTTPService;
10	
11	public class XMLRPCClient {
12		private var url:String;
13		private var authHeader:URLRequestHeader;
14			
15		public function XMLRPCClient(dbURL:String, authHeader:URLRequestHeader = null) {
16			this.url = dbURL + "/xmlrpc";
17			this.authHeader = authHeader;
18		}
19		
20		/**
21		 * @param successFunc Takes the form successFunc(response:Object):void
22		 * @param failureFunc Takes the form failureFunc(msg:String):void
23		 */
24		public function call(methodCallXML:XML, successFunc:Function, failureFunc:Function):void {
25			var callStartTime:int = getTimer();
26
27			trace("Calling XML-RPC method: " + condense(methodCallXML.toXMLString()));
28
29			var httpService:HTTPService = new HTTPService();
30			httpService.method = URLRequestMethod.POST;
31			httpService.url = url;
32			httpService.contentType = HTTPService.CONTENT_TYPE_XML;
33			httpService.resultFormat = HTTPService.RESULT_FORMAT_TEXT;
34			if (authHeader != null) {
35				httpService.headers = new Object();
36				httpService.headers[authHeader.name] = authHeader.value;
37			}
38			//httpService.requestTimeout = 5;
39
40			httpService.addEventListener(ResultEvent.RESULT, function(evt:ResultEvent):void {
41				trace("XML-RPC took " + (getTimer() - callStartTime) + "ms");
42				
43				var responseXML:XML;
44				try {
45					responseXML = new XML(evt.result);
46				}
47				catch (e:TypeError) {
48					failureFunc("Response from XML-RPC is not valid XML: " + evt.result);
49					return;
50				}
51				var parser:ResponseParser = new ResponseParser();
52				
53				if (responseXML.fault.length() > 0)
54				{
55					var parsedFault:Object = parser.parse(responseXML.fault.value.*[0]);
56					failureFunc(parsedFault.faultCode + ": " + parsedFault.faultString);
57				}
58				else if (responseXML.params.length() > 0)
59				{
60					var parsedResponse:Object = parser.parse(responseXML.params.param.value[0]);
61					successFunc(parsedResponse);
62					//successFunc(new XML("<result>" + evt.result + "</result>"));
63				}
64				else
65				{
66					failureFunc("Response to XML-RPC includes neither params nor fault");
67				}
68			});
69			httpService.addEventListener(FaultEvent.FAULT, function (evt:FaultEvent):void {
70				failureFunc("XML-RPC failed because: " + evt.fault.faultString);
71			});
72
73			httpService.send(methodCallXML);
74		}
75		
76		private static function condense(xmlString:String):String {
77			var regexp:RegExp = />([^<]{401,})</gs; // more than 200 chars from open to close tag
78			var result:String = "";
79			var index:int = 0;
80			var arr:Array = regexp.exec(xmlString);
81			while (arr != null) {
82				result += xmlString.substring(index, arr.index + 1);
83				result += "\n" + xmlString.substring(arr.index + 1, arr.index + 200);
84				result += "\n...<skipped>...\n";
85				result += xmlString.substring(regexp.lastIndex - 200, regexp.lastIndex - 1) + "\n";
86				index = regexp.lastIndex - 1;
87				arr = regexp.exec(xmlString);
88			}
89			// add the rest of the string
90			return result += xmlString.substring(index);
91		} 
92	}
93}