PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/CMISSpaces/src/org/integratedsemantics/util/HttpClient2.as

http://cmisspaces.googlecode.com/
ActionScript | 277 lines | 182 code | 60 blank | 35 comment | 19 complexity | 262c99676a8e68902af56f12b71ce2a3 MD5 | raw file
  1. package org.integratedsemantics.util
  2. {
  3. import com.adobe.net.URI;
  4. import flash.events.Event;
  5. import flash.events.HTTPStatusEvent;
  6. import flash.events.IOErrorEvent;
  7. import flash.events.ProgressEvent;
  8. import flash.events.SecurityErrorEvent;
  9. import flash.net.URLLoader;
  10. import flash.net.URLLoaderDataFormat;
  11. import flash.net.URLRequest;
  12. import flash.net.URLRequestHeader;
  13. import flash.utils.ByteArray;
  14. import mx.messaging.ChannelSet;
  15. import mx.messaging.channels.HTTPChannel;
  16. import mx.messaging.channels.SecureHTTPChannel;
  17. import mx.rpc.events.FaultEvent;
  18. import mx.rpc.events.ResultEvent;
  19. import mx.rpc.http.HTTPService;
  20. import org.httpclient.HttpClient;
  21. import org.httpclient.HttpHeader;
  22. import org.httpclient.HttpRequest;
  23. import org.httpclient.HttpResponse;
  24. import org.httpclient.events.HttpDataEvent;
  25. import org.httpclient.events.HttpListener;
  26. import org.httpclient.events.HttpRequestEvent;
  27. import org.httpclient.events.HttpResponseEvent;
  28. import org.httpclient.events.HttpStatusEvent;
  29. import org.integratedsemantics.cmisspaces.model.config.CMISConfig;
  30. import org.integratedsemantics.flexspaces.model.AppModelLocator;
  31. [Event(name=Event.CLOSE, type="flash.events.Event")]
  32. [Event(name=HttpRequestEvent.CONNECT, type="org.httpclient.events.HttpRequestEvent")]
  33. [Event(name=HttpResponseEvent.COMPLETE, type="org.httpclient.events.HttpResponseEvent")]
  34. [Event(name=HttpDataEvent.DATA, type="org.httpclient.events.HttpDataEvent")]
  35. [Event(name=HttpStatusEvent.STATUS, type="org.httpclient.events.HttpStatusEvent")]
  36. [Event(name=HttpRequestEvent.COMPLETE, type="org.httpclient.events.HttpRequestEvent")]
  37. [Event(name=HttpErrorEvent.ERROR, type="org.httpclient.events.HttpErrorEvent")]
  38. [Event(name=HttpErrorEvent.TIMEOUT_ERROR, type="org.httpclient.events.HttpErrorEvent")]
  39. [Event(name=IOErrorEvent.IO_ERROR, type="flash.events.IOErrorEvent")]
  40. [Event(name=SecurityErrorEvent.SECURITY_ERROR, type="flash.events.SecurityErrorEvent")]
  41. /**
  42. * Extends HttpClient in as3httpclientlib to support URLLoader, HttpService requests
  43. * in addition to Socket request support in HttpClient
  44. * (note that URLLoader and HttpService requests have limitations in Flex
  45. * in terms of the headers and http methods use can use
  46. *
  47. */
  48. public class HttpClient2 extends HttpClient
  49. {
  50. /**
  51. * Create HTTP client
  52. */
  53. public function HttpClient2()
  54. {
  55. super();
  56. }
  57. /**
  58. * Request using URLLoader
  59. * @param uri URI
  60. * @param request HTTP request
  61. * @param timeout Timeout (in millis)
  62. */
  63. public function requestURLLoader(uri:URI, httpRequest:HttpRequest, timeout:int = -1, listener:HttpListener = null):void
  64. {
  65. //trace("URLLoaderHttpClient requestURLLoader()");
  66. var request:URLRequest = new URLRequest(uri.toString());
  67. var headers:Array = httpRequest.header._headers;
  68. for (var h:int = 0; h < headers.length; h++)
  69. {
  70. var header:URLRequestHeader = new URLRequestHeader(headers[h]["name"], headers[h]["value"]);
  71. request.requestHeaders.push(header);
  72. }
  73. request.method = httpRequest.method;
  74. request.data = httpRequest.body;
  75. var loader:URLLoader = new URLLoader();
  76. // use binary as what HttpClient excepts (response as ByteArray)
  77. loader.dataFormat = URLLoaderDataFormat.BINARY;
  78. loader.addEventListener(Event.COMPLETE, onComplete);
  79. loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onStatus);
  80. loader.addEventListener(ProgressEvent.PROGRESS, onProgress);
  81. loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
  82. loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
  83. dispatchEvent(new HttpRequestEvent(httpRequest, httpRequest.header.toString(), HttpRequestEvent.CONNECT));
  84. dispatchEvent(new HttpRequestEvent(httpRequest, httpRequest.header.toString()));
  85. loader.load(request);
  86. }
  87. /**
  88. * Request using HTTPService
  89. * @param uri URI
  90. * @param request HTTP request
  91. * @param timeout Timeout (in millis)
  92. *
  93. */
  94. public function requestHTTPService(uri:URI, httpRequest:HttpRequest, timeout:int = -1, listener:HttpListener = null):void
  95. {
  96. //trace("URLLoaderHttpClient requestHTTPService()");
  97. var httpService:HTTPService = new HTTPService();
  98. httpService.url = uri.toString();
  99. var headers:Array = httpRequest.header._headers;
  100. for (var h:int = 0; h < headers.length; h++)
  101. {
  102. var headerName:String = headers[h]["name"];
  103. var headerValue:String = headers[h]["value"];
  104. httpService.headers[headerName] = headerValue;
  105. }
  106. httpService.method = httpRequest.method;
  107. // use text so result not parsed
  108. httpService.resultFormat = "text";
  109. httpService.addEventListener(ResultEvent.RESULT, onResult);
  110. httpService.addEventListener(FaultEvent.FAULT, onFault);
  111. dispatchEvent(new HttpRequestEvent(httpRequest, httpRequest.header.toString(), HttpRequestEvent.CONNECT));
  112. dispatchEvent(new HttpRequestEvent(httpRequest, httpRequest.header.toString()));
  113. var model:AppModelLocator = AppModelLocator.getInstance();
  114. var cmisConfig:CMISConfig = CMISConfig(model.ecmServerConfig);
  115. if (cmisConfig.useProxy == true)
  116. {
  117. if ( (httpRequest.method == "POST") || (httpRequest.method == "PUT") || (httpRequest.method == "DELETE") )
  118. {
  119. httpService.useProxy = true;
  120. if (model.remotingChannelSet == null)
  121. {
  122. setupChannels();
  123. }
  124. httpService.channelSet = model.remotingChannelSet;
  125. var body:String = String(httpRequest.body);
  126. httpService.send(body);
  127. }
  128. else
  129. {
  130. httpService.send();
  131. }
  132. }
  133. else
  134. {
  135. httpService.send();
  136. }
  137. }
  138. private function onStatus(e:HTTPStatusEvent):void
  139. {
  140. trace("URLLoaderHttpClient onStatus()");
  141. // todo create/init diff type status: HttpStatusEvent
  142. //dispatchEvent(e.clone());
  143. }
  144. private function onProgress(e:ProgressEvent):void
  145. {
  146. trace("URLLoaderHttpClient onProgress()");
  147. dispatchEvent(e.clone());
  148. }
  149. private function onIOError(e:IOErrorEvent):void
  150. {
  151. trace("URLLoaderHttpClient onIOError()");
  152. dispatchEvent(e.clone());
  153. }
  154. private function onSecurityError(e:SecurityErrorEvent):void
  155. {
  156. trace("URLLoaderHttpClient onSecurityError()");
  157. dispatchEvent(e.clone());
  158. }
  159. private function onComplete(event:Event):void
  160. {
  161. trace("URLLoaderHttpClient onComplete()");
  162. var loader:URLLoader = URLLoader(event.target);
  163. var headers:Array = [];
  164. var httpResponse:HttpResponse = new HttpResponse("1.1", "200", "", new HttpHeader(headers));
  165. //construct status event
  166. dispatchEvent(new HttpStatusEvent(httpResponse));
  167. //construct http data event
  168. dispatchEvent(new HttpDataEvent(loader.data));
  169. //construct response event
  170. dispatchEvent(new HttpResponseEvent(httpResponse));
  171. }
  172. private function onFault(event:FaultEvent):void
  173. {
  174. trace("URLLoaderHttpClient onFault()");
  175. }
  176. private function onResult(event:ResultEvent):void
  177. {
  178. trace("URLLoaderHttpClient onResult()");
  179. var ba:ByteArray = new ByteArray();
  180. if (event.result != null)
  181. {
  182. var strData:String = event.result as String
  183. ba.writeUTFBytes(strData);
  184. }
  185. var headers:Array = [];
  186. var httpResponse:HttpResponse = new HttpResponse("1.1", "200", "", new HttpHeader(headers));
  187. //construct status event
  188. dispatchEvent(new HttpStatusEvent(httpResponse));
  189. //construct http data event
  190. dispatchEvent(new HttpDataEvent(ba));
  191. //construct response event
  192. dispatchEvent(new HttpResponseEvent(httpResponse));
  193. }
  194. private function setupChannels():void
  195. {
  196. var model:AppModelLocator = AppModelLocator.getInstance();
  197. var cmisConfig:CMISConfig = CMISConfig(model.ecmServerConfig);
  198. var channelSet:ChannelSet = new ChannelSet();
  199. var baseUrl:String = cmisConfig.proxyProtocol + "://" + cmisConfig.proxyDomain + ":" + cmisConfig.proxyPort + cmisConfig.proxyPrefixUrl;
  200. if (cmisConfig.proxyProtocol == "http")
  201. {
  202. var channelUrl:String = baseUrl + "/messagebroker/http";
  203. var channelId:String = "remoting-http";
  204. var channel:HTTPChannel = new HTTPChannel(channelId, channelUrl);
  205. channelSet.addChannel(channel);
  206. }
  207. else if (cmisConfig.proxyProtocol == "https")
  208. {
  209. var channelUrl3:String = baseUrl + "/messagebroker/httpsecure";
  210. var channelId3:String = "remoting-secure-http";
  211. var channel3:SecureHTTPChannel = new SecureHTTPChannel(channelId3, channelUrl3);
  212. channelSet.addChannel(channel3);
  213. }
  214. model.remotingChannelSet = channelSet;
  215. }
  216. }
  217. }