PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/as3/bentbit-connector-client/src/org/bentbit/http/HttpServiceQue.as

http://bentbit-connector.googlecode.com/
ActionScript | 566 lines | 448 code | 95 blank | 23 comment | 45 complexity | 533fb068636dc938a331567dcb5993d9 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. package org.bentbit.http
  2. {
  3. //#####Define Imports.
  4. import flash.events.ProgressEvent;
  5. import flash.utils.clearTimeout;
  6. import flash.utils.setTimeout;
  7. import mx.collections.ArrayCollection;
  8. import mx.collections.XMLListCollection;
  9. import mx.rpc.events.FaultEvent;
  10. import mx.rpc.events.ResultEvent;
  11. import mx.rpc.http.HTTPService;
  12. import mx.utils.Base64Encoder;
  13. [Event(name="complete", type="flash.events.Event")]
  14. [Event(name="cancel", type="flash.events.Event")]
  15. [Event(name="progress", type="flash.events.ProgressEvent")]
  16. public class HttpServiceQue
  17. {
  18. //#####Private Variables.
  19. [Bindable] private var _oHttp:HTTPService = new HTTPService();
  20. [Bindable] private var _aHttpQue:ArrayCollection = new ArrayCollection();
  21. [Bindable] private var _iHttpQue:Number = 0;
  22. [Bindable] private var _bCancel:Boolean = false;
  23. [Bindable] private var _bProcessing:Boolean = false;
  24. [Bindable] private var _nTimeout:Number;
  25. [Bindable] private var _sTempMethod:String = "POST";
  26. [Bindable] private var _sTempResultFormat:String = "e4x";
  27. //#####Public Variables.
  28. [Bindable] public var serializeResult:Boolean = false;
  29. [Bindable] public var queueDelay:Number = 1;
  30. //#####Getter / Setter Functions.
  31. [Inspectable(defaultValue="undefined", category="General")]
  32. public function get headers():Object
  33. {
  34. return _oHttp.headers;
  35. }
  36. public function set headers(r:Object):void
  37. {
  38. _oHttp.headers = r;
  39. }
  40. [Inspectable(enumeration="GET,get,POST,post,HEAD,head,OPTIONS,options,PUT,put,TRACE,trace,DELETE,delete", defaultValue="POST", category="General")]
  41. public function get method():String
  42. {
  43. return _oHttp.method;
  44. }
  45. public function set method(m:String):void
  46. {
  47. _oHttp.method = m;
  48. }
  49. [Inspectable(enumeration="object,array,xml,flashvars,text,e4x", defaultValue="e4x", category="General")]
  50. public function get resultFormat():String
  51. {
  52. return _oHttp.resultFormat;
  53. }
  54. public function set resultFormat(rf:String):void
  55. {
  56. _oHttp.resultFormat = rf;
  57. }
  58. [Inspectable(defaultValue="false", category="General")]
  59. public function get showBusyCursor():Boolean
  60. {
  61. return _oHttp.showBusyCursor;
  62. }
  63. public function set showBusyCursor(sbc:Boolean):void
  64. {
  65. _oHttp.showBusyCursor = sbc;
  66. }
  67. public function get url():String
  68. {
  69. return _oHttp.url;
  70. }
  71. public function set url(url:String):void
  72. {
  73. _oHttp.url = url;
  74. }
  75. //#####Init Function.
  76. public function HttpServiceQue()
  77. {
  78. try
  79. {
  80. _oHttp.headers = headers;
  81. _oHttp.method = "POST";
  82. _oHttp.resultFormat = "e4x";
  83. _oHttp.showBusyCursor = showBusyCursor;
  84. configureListeners(_oHttp);
  85. }
  86. catch(E:Error){}
  87. }
  88. //#####Public Functions.
  89. public function add(oParameters:Object = null, oFunctionResult:Function = null, oFunctionFault:Function = null, sUrl:String = null, bCache:Boolean = true):void
  90. {
  91. try
  92. {
  93. if (bCache == false) sUrl = getCacheUrl(sUrl);
  94. _aHttpQue.addItem({Parameters:oParameters, FunctionResult:oFunctionResult, FunctionFault:oFunctionFault, Url:sUrl, Cache:bCache, Command:false});
  95. }
  96. catch(E:Error){}
  97. }
  98. public function command(sName:String, oParameters:Object = null, oFunctionResult:Function = null, oFunctionFault:Function = null, sUrl:String = null, bCache:Boolean = false):void
  99. {
  100. try
  101. {
  102. if (oParameters == null) oParameters = new Object;
  103. oParameters._COMMAND = sName;
  104. }
  105. catch(E:Error){}
  106. try
  107. {
  108. if (bCache == false) sUrl = getCacheUrl(sUrl);
  109. _aHttpQue.addItem({Parameters:oParameters, FunctionResult:oFunctionResult, FunctionFault:oFunctionFault, Url:sUrl, Cache:bCache, Command:true});
  110. }
  111. catch(E:Error){}
  112. }
  113. public function send():void
  114. {
  115. try
  116. {
  117. doSend();
  118. }
  119. catch(E:Error){}
  120. }
  121. public function cancel():void
  122. {
  123. try
  124. {
  125. doCancel();
  126. }
  127. catch(E:Error){}
  128. }
  129. public function clear():void
  130. {
  131. try
  132. {
  133. doClear();
  134. }
  135. catch(E:Error){}
  136. }
  137. public function setCredentials(sUsername:String, sPassword:String):void
  138. {
  139. var oEncoder:Base64Encoder = new Base64Encoder();
  140. oEncoder.insertNewLines = false;
  141. oEncoder.encode(sUsername + ":" + sPassword);
  142. _oHttp.method = "POST";
  143. _oHttp.headers = {Authorization:"Basic " + oEncoder.toString()};
  144. //_oHttp.setCredentials(sUsername, sPassword); // This method doesn't work
  145. }
  146. //#####Private Functions.
  147. public function getCacheUrl(sUrl:String):String
  148. {
  149. var rtnVal:String = sUrl;
  150. try
  151. {
  152. var dDate:Date = new Date;
  153. if (sUrl.indexOf("?") > 0)
  154. {
  155. rtnVal = rtnVal + "&"
  156. }
  157. else
  158. {
  159. rtnVal = rtnVal + "?"
  160. }
  161. rtnVal = rtnVal + "___CacheGuidTimestamp=" + String(dDate.fullYearUTC) + String(dDate.monthUTC) + String(dDate.dayUTC) + String(dDate.dateUTC) + String(dDate.hoursUTC) + String(dDate.minutesUTC) + String(dDate.secondsUTC) + String(dDate.minutesUTC)
  162. }
  163. catch(E:Error)
  164. {
  165. rtnVal = sUrl;
  166. }
  167. return rtnVal;
  168. }
  169. //doSend: main send function
  170. private function doSend():void
  171. {
  172. try
  173. {
  174. if (_bProcessing == false)
  175. {
  176. if(_bCancel == true) doCancel2();
  177. _bProcessing = true;
  178. _bCancel = false;
  179. _iHttpQue = 0;
  180. doProgress();
  181. doSend2();
  182. }
  183. }
  184. catch(E:Error){}
  185. }
  186. //doSend2: item send function
  187. private function doSend2():void
  188. {
  189. try
  190. {
  191. try{clearTimeout(_nTimeout)}catch(E:Event){}
  192. if(_bCancel == true) doCancel2();
  193. if(_iHttpQue < _aHttpQue.length)
  194. {
  195. //set the proper result format
  196. if (_aHttpQue.getItemAt(_iHttpQue)["Command"] == true)
  197. {
  198. _sTempResultFormat = _oHttp.resultFormat;
  199. _oHttp.resultFormat = "e4x";
  200. }
  201. else
  202. {
  203. _oHttp.resultFormat = _sTempResultFormat;
  204. }
  205. //set the proper method
  206. if (_aHttpQue.getItemAt(_iHttpQue)["Cache"] == false)
  207. {
  208. _sTempMethod = _oHttp.method;
  209. _oHttp.method = "POST";
  210. }
  211. else
  212. {
  213. _oHttp.method = _sTempMethod;
  214. }
  215. //set the url
  216. if (_aHttpQue.getItemAt(_iHttpQue)["Url"] != null) _oHttp.url = _aHttpQue.getItemAt(_iHttpQue)["Url"];
  217. //send the command
  218. _oHttp.send(_aHttpQue.getItemAt(_iHttpQue)["Parameters"]);
  219. //increment the command
  220. _iHttpQue = _iHttpQue + 1;
  221. }
  222. else
  223. {
  224. doClear();
  225. _bCancel = false;
  226. _bProcessing = false;
  227. var doEvent:Event = new Event("complete",false,false);
  228. dispatchEvent(doEvent);
  229. }
  230. }
  231. catch(E:Error){}
  232. }
  233. //doCancel: main cancel function (cancel happens after current item is processed)
  234. private function doCancel():void
  235. {
  236. try
  237. {
  238. _bCancel = true;
  239. }
  240. catch(E:Error){}
  241. }
  242. //doCancel2: complete the cancel function
  243. private function doCancel2():void
  244. {
  245. try
  246. {
  247. _bCancel = false;
  248. _bProcessing = false;
  249. doClear();
  250. var doEvent:Event = new Event("cancel",false,false);
  251. dispatchEvent(doEvent);
  252. }
  253. catch(E:Error){}
  254. }
  255. //doClear: clear the que and reset vars
  256. private function doClear():void
  257. {
  258. try
  259. {
  260. _iHttpQue = 0;
  261. _aHttpQue.removeAll();
  262. }
  263. catch(E:Error){}
  264. }
  265. //doProgress: Notify of progress
  266. private function doProgress():void
  267. {
  268. try
  269. {
  270. var doEvent:ProgressEvent = new ProgressEvent("progress",false,false,_iHttpQue,_aHttpQue.length);
  271. dispatchEvent(doEvent);
  272. }
  273. catch(E:Error){}
  274. }
  275. //#####Handlers.
  276. private function configureListeners(dispatcher:IEventDispatcher):void
  277. {
  278. try
  279. {
  280. dispatcher.addEventListener(ResultEvent.RESULT, resultHandler);
  281. dispatcher.addEventListener(FaultEvent.FAULT, faultHandler);
  282. dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
  283. }
  284. catch(E:Error){}
  285. }
  286. private function resultHandler(event:ResultEvent):void
  287. {
  288. try
  289. {
  290. var oFunction:Function = _aHttpQue.getItemAt(_iHttpQue-1)["FunctionResult"];
  291. if (oFunction != null && serializeResult == true) oFunction.call(null, event, serializeResultData(event));
  292. if (oFunction != null && serializeResult != true) oFunction.call(null, event);
  293. doProgress();
  294. _nTimeout = setTimeout(doSend2, queueDelay);
  295. }
  296. catch(E:Error){}
  297. }
  298. private function faultHandler(event:FaultEvent):void
  299. {
  300. try
  301. {
  302. var oFunction:Function = _aHttpQue.getItemAt(_iHttpQue-1)["FunctionFault"];
  303. if (oFunction != null) oFunction.call(null, event);
  304. doProgress();
  305. }
  306. catch(E:Error){}
  307. }
  308. private function progressHandler(event:ProgressEvent):void
  309. {
  310. try
  311. {
  312. var nLoaded:Number;
  313. var nTotal:Number;
  314. nLoaded = (_iHttpQue * ((event.bytesLoaded / event.bytesTotal) * 100));
  315. nTotal = (_aHttpQue.length * 100);
  316. if (nLoaded < 0) nLoaded = 0;
  317. if (nTotal <= 0) nTotal = 1;
  318. var doEvent:ProgressEvent = new ProgressEvent("progress",false,false,nLoaded,nTotal);
  319. dispatchEvent(doEvent);
  320. }
  321. catch(E:Error){}
  322. }
  323. private function serializeResultData(event:ResultEvent):* //currently only supports xml in bentbit-connector format
  324. {
  325. var rtnVal:*;
  326. try
  327. {
  328. rtnVal = serializeData(event.result.Body.data);
  329. }
  330. catch(E:Error)
  331. {
  332. rtnVal = null;
  333. }
  334. return rtnVal;
  335. }
  336. private function serializeData(oValue:*, sType:String = null):* //currently only supports xml in bentbit-connector format
  337. {
  338. var rtnVal:*;
  339. try
  340. {
  341. if (sType == null) sType = oValue.@type.toString();
  342. switch(sType)
  343. {
  344. case "Null":
  345. rtnVal = null;
  346. break;
  347. case "Number":
  348. rtnVal = Number(oValue);
  349. break;
  350. case "Date":
  351. var sValue:String = String(oValue);
  352. if (sValue != "")
  353. {
  354. sValue = sValue.replace(/-/g, "/");
  355. sValue = sValue.replace("T", " ");
  356. sValue = sValue.replace("Z", " GMT-0000");
  357. rtnVal = new Date(Date.parse(sValue))
  358. }
  359. else
  360. {
  361. sValue = null;
  362. }
  363. break;
  364. case "String":
  365. rtnVal = String(oValue);
  366. break;
  367. case "Object":
  368. case "Binary":
  369. rtnVal = oValue;
  370. break;
  371. case "Boolean":
  372. if (String(oValue).toLowerCase() == "true")
  373. {
  374. rtnVal = true;
  375. }
  376. else
  377. {
  378. rtnVal = false;
  379. }
  380. break;
  381. case "Array":
  382. case "ArraySingle":
  383. var oData:XMLList = oValue.data as XMLList;
  384. var oArray:ArrayCollection = new ArrayCollection;
  385. for each (var oItem:Object in oData)
  386. {
  387. try
  388. {
  389. oArray.addItem(serializeData(oItem));
  390. }
  391. catch(E:Error){}
  392. }
  393. rtnVal = oArray;
  394. break;
  395. case "ArrayMulti":
  396. var oData:XMLList = oValue.row as XMLList;
  397. var oDataHeader:XMLList = oValue.header as XMLList;
  398. var oArrayCollection:ArrayCollection = new ArrayCollection;
  399. for each (var oItem:Object in oData)
  400. {
  401. try
  402. {
  403. var oNewItem:Object = new Object;
  404. for (var oProperty:Object in oItem.children())
  405. {
  406. var sPropertyName:String = oItem.children()[oProperty].name();
  407. var oPropertyValue:Object = oItem.children()[oProperty];
  408. oNewItem[sPropertyName] = serializeData(oPropertyValue);
  409. }
  410. }
  411. catch(E:Error){}
  412. oArrayCollection.addItem(oNewItem);
  413. }
  414. rtnVal = oArrayCollection;
  415. //rtnVal = oValue.row as XMLList;
  416. break;
  417. case "Recordset":
  418. var oData:XMLList = oValue.row as XMLList;
  419. var oDataHeader:XMLList = oValue.header as XMLList;
  420. var oArrayCollection:ArrayCollection = new ArrayCollection;
  421. for each (var oItem:Object in oData)
  422. {
  423. try
  424. {
  425. var oNewItem:Object = new Object;
  426. for (var oProperty:Object in oItem.children())
  427. {
  428. var sPropertyName:String = oItem.children()[oProperty].name();
  429. var oPropertyValue:Object = oItem.children()[oProperty];
  430. oNewItem[sPropertyName] = serializeData(oPropertyValue, String(oDataHeader[0][sPropertyName].@type));
  431. }
  432. }
  433. catch(E:Error){}
  434. oArrayCollection.addItem(oNewItem);
  435. }
  436. rtnVal = oArrayCollection;
  437. //rtnVal = oValue.row as XMLList;
  438. break;
  439. case "Dictionary":
  440. var oData:XMLList = oValue.row as XMLList;
  441. var oDataHeader:XMLList = oValue.header as XMLList;
  442. var oNewItem:Object = new Object;
  443. for each (var oItem:Object in oData)
  444. {
  445. try
  446. {
  447. for (var oProperty:Object in oItem.children())
  448. {
  449. var sPropertyName:String = oItem.children()[oProperty].name();
  450. var oPropertyValue:Object = oItem.children()[oProperty];
  451. oNewItem[sPropertyName] = serializeData(oPropertyValue, String(oDataHeader[0][sPropertyName].@type));
  452. }
  453. }
  454. catch(E:Error){}
  455. }
  456. rtnVal = oNewItem;
  457. //rtnVal = oValue.row as XMLList;
  458. break;
  459. default:
  460. rtnVal = null;
  461. break;
  462. }
  463. }
  464. catch(E:Error)
  465. {
  466. rtnVal = null;
  467. }
  468. return rtnVal;
  469. }
  470. }
  471. }