PageRenderTime 52ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/CMISSpaces/src/org/coderepos/atompub/AtompubClient.as

http://cmisspaces.googlecode.com/
ActionScript | 1192 lines | 619 code | 76 blank | 497 comment | 105 complexity | 6b1b75afd55881277e4a2cba473e11a5 MD5 | raw file
  1. /*
  2. Copyright (c) Lyo Kato (lyo.kato _at_ gmail.com)
  3. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  4. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  5. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  6. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  7. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  8. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  9. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  10. */
  11. package org.coderepos.atompub {
  12. import com.adobe.net.URI;
  13. import com.adobe.utils.StringUtil;
  14. import flash.events.ErrorEvent;
  15. import flash.events.Event;
  16. import flash.events.EventDispatcher;
  17. import flash.events.IOErrorEvent;
  18. import flash.events.SecurityErrorEvent;
  19. import flash.events.TimerEvent;
  20. import flash.utils.ByteArray;
  21. import flash.utils.Timer;
  22. import org.coderepos.atompub.cache.*;
  23. import org.coderepos.atompub.credentials.*;
  24. import org.coderepos.atompub.events.*;
  25. import org.coderepos.xml.atom.*;
  26. import org.httpclient.*;
  27. import org.httpclient.events.*;
  28. import org.httpclient.http.*;
  29. import org.integratedsemantics.cmis.atom.CMISAtomFeed;
  30. import org.integratedsemantics.cmisspaces.model.config.CMISConfig;
  31. import org.integratedsemantics.flexspaces.model.AppModelLocator;
  32. import org.integratedsemantics.util.HttpClient2;
  33. /**
  34. * Class represents Atompub Client
  35. *
  36. * @example
  37. * <listing version="3.0">
  38. *
  39. * import org.coderepos.atompub.AtompubClient;
  40. * import org.coderepos.atompub.credentials.BasicCredential;
  41. * import org.coderepos.atompub.events.*;
  42. * import com.adobe.net.URI;
  43. *
  44. * var client:AtompubClient = new AtompubClient();
  45. * client.cache = new AtompubCache();
  46. * client.credential = new BasicCredential("name", "password");
  47. * client.agent = "MyAgent/1.0.0";
  48. *
  49. * client.addEventListener(AtompubEvent.GET_SERVICE_COMPLETED, onCompletedToGetService);
  50. * client.addEventListener(AtompubEvent.GET_SERVICE_FAILED, onFailedToGetService);
  51. * client.getService(new URI("http://example.org/endpoint"));
  52. *
  53. * private function onCompletedToGetService(event:AtompubEvent):void {
  54. * var service:AtomService = event.result.service;
  55. * }
  56. *
  57. * private function onFailedToGetService(event:AtompubEvent):void {
  58. * var message:String = "Error: [" + event.result.code + "] " + event.result.message;
  59. * showErrorMessage(message);
  60. * }
  61. *
  62. * client.addEventListener(AtompubEvent.GET_CATEGORIES_COMPLETED, onCompletedToGetCategories);
  63. * client.addEventListener(AtompubEvent.GET_CATEGORIES_FAILED, onFailedToGetCategories);
  64. *
  65. * private function onCompletedToGetCategories(event:AtompubEvent):void {
  66. * var categories:AtomCategories = event.result.categories;
  67. * }
  68. *
  69. * private function onFailedToGetCategories(event:AtompubEvent):void {
  70. * var message:String = "Error: [" + event.result.code + "] " + event.result.message;
  71. * showErrorMessage(message);
  72. * }
  73. *
  74. * client.addEventListener(AtompubEvent.GET_FEED_COMPLETED, onCompletedToGetFeed);
  75. * client.addEventListener(AtompubEvent.GET_FEED_FAILED, onFailedToGetFeed);
  76. * client.getFeed(new URI("http://example.org/feed"));
  77. *
  78. * private function onCompletedToGetFeed(event:AtompubEvent):void {
  79. * var service:AtomService = event.result.service;
  80. * }
  81. *
  82. * private function onFailedToGetFeed(event:AtompubEvent):void {
  83. * var message:String = "Error: [" + event.result.code + "] " + event.result.message;
  84. * showErrorMessage(message);
  85. * }
  86. *
  87. * client.addEventListener(AtompubEvent.GET_ENTRY_COMPLETED, onCompletedToGetEntry);
  88. * client.addEventListener(AtompubEvent.GET_ENTRY_FAILED, onFailedToGetEntry);
  89. * client.getEntry(new URI("http://example.org/entry"));
  90. *
  91. * private function onCompletedToGetEntry(event:AtompubEvent):void {
  92. * var entry:AtomEntry = event.result.entry;
  93. * }
  94. *
  95. * private function onFailedToGetEntry(event:Atompub):void {
  96. * var message:String = "Error: [" + event.result.code + "] " + event.result.message;
  97. * showErrorMessage(message);
  98. * }
  99. *
  100. * client.addEventListener(AtompubEvent.GET_MEDIA_COMPLETED, onCompletedToGetMedia);
  101. * client.addEventListener(AtompubEvent.GET_MEDIA_FAILED, onFailedToGetMedia);
  102. * client.getMedia(new URI("http://example.org/mediaresource"));
  103. *
  104. * private function onCompletedToGetMedia(event:AtompubEvent):void {
  105. * var resource:ByteArray = event.result.resource;
  106. * }
  107. *
  108. * private function onFailedToGetMedia(event:Atompub):void {
  109. * var message:String = "Error: [" + event.result.code + "] " + event.result.message;
  110. * showErrorMessage(message);
  111. * }
  112. *
  113. * client.addEventListener(AtompubEvent.CREATE_ENTRY_COMPLETED, onComplatedToCreateEntry);
  114. * client.addEventListener(AtompubEvent.CREATE_ENTRY_FAILED, onFailedToCreateEntry);
  115. *
  116. * var newEntry:AtomEntry = new AtomEntry();
  117. * newEntry.title = "New Entry";
  118. * newEntry.summary = "This is a new entry!";
  119. * newEntry.published = new Date();
  120. * newEntry.updated = new Date();
  121. *
  122. * var author:AtomPerson = new AtomPerson();
  123. * author.name = "Lyo Kato";
  124. * author.email = "lyo.kato@gmail.com";
  125. *
  126. * newEntry.author = author;
  127. *
  128. * client.createEntry(new URI("http://example.org/collection"), newEntry);
  129. * // with slug: client.createEntry(new URI("http://example.org/collection"), newEntry, "new entry");
  130. *
  131. * private function onCompletedToCreateEntry(event:AtompubEvent):void {
  132. * var location:URI = event.result.location;
  133. * var entry:AtomEntry = event.result.entry;
  134. * }
  135. *
  136. * private function onFailedToCreateEntry(event:AtompubEvent):void {
  137. * var message:String = "Error: [" + event.result.code + "] " + event.result.message;
  138. * showErrorMessage(message);
  139. * }
  140. *
  141. * client.addEventListener(AtompubEvent.CREATE_MEDIA_COMPLETED, onCompletedToCreateMedia);
  142. * client.addEventListener(AtompubEvent.CREATE_MEDIA_FAILED, onFailedToCreateMedia);
  143. * var data:ByteArray =
  144. * client.createMedia(new URI("http://example.org/mediacollection"), data);
  145. *
  146. * private function onCompletedToCreateMedia(event:AtompubEvent):void {
  147. * var location:URI = event.result.location;
  148. * var entry:AtomEntry = event.result.entry;
  149. * }
  150. *
  151. * private function onFailedToCreateMedia(event:AtompubEvent):void {
  152. * var message:String = "Error: [" + event.result.code + "] " + event.result.message;
  153. * showErrorMessage(message);
  154. * }
  155. *
  156. * client.addEventListener(AtompubEvent.UPDATE_ENTRY_COMPLETED, onCompletedToUpdateEntry);
  157. * client.addEventListener(AtompubEvent.UPDATE_ENTRY_FAILED, onFailedToUpdateEntry);
  158. * entry.title = "changed";
  159. * entry.updated = new Date();
  160. * client.updateEntry(entry.editLink, entry);
  161. *
  162. * private function onCompletedToUpdateEntry(event:AtompubEvent):void {
  163. * var entry:AtomEntry = event.result.entry;
  164. * }
  165. *
  166. * private function onFailedToUpdateEntry():void {
  167. * var message:String = "Error: [" + event.result.code + "] " + event.result.message;
  168. * showErrorMessage(message);
  169. * }
  170. *
  171. * client.addEventListener(AtompubEvent.UPDATE_MEDIA_COMPLETED, onCompletedToUpdateMedia);
  172. * client.addEventListener(AtompubEvent.UPDATE_MEDIA_FAILED, onFailedToUpdateMedia);
  173. * var newData:ByteArray;
  174. * client.updateMedia(mediaLinkEntry.editMediaLink, newData);
  175. *
  176. * private function onCompletedToUpdateMedia(event:AtompubEvent):void {
  177. *
  178. * }
  179. *
  180. * private function onFailedToUpdateMedia(event:AtompubEvent):void {
  181. * var message:String = "Error: [" + event.result.code + "] " + event.result.message;
  182. * showErrorMessage(message);
  183. * }
  184. *
  185. * client.addEventListener(AtompubEvent.DELETE_ENTRY_COMPLETED, onCompletedToDeleteEntry);
  186. * client.addEventListener(AtompubEvent.DELETE_ENTRY_FAILED, onFailedToDeleteEntry);
  187. * client.deleteEntry(entry.editLink);
  188. *
  189. * private function onCompletedToDeleteEntry(event:AtompubEvent):void {
  190. *
  191. * }
  192. *
  193. * private function onFailedToDeleteEntry(event:AtompubEvent):void {
  194. * var message:String = "Error: [" + event.result.code + "] " + event.result.message;
  195. * showErrorMessage(message);
  196. * }
  197. *
  198. * client.addEventListener(AtompubEvent.DELETE_MEDIA_COMPLETED, onCompletedToDeleteMedia);
  199. * client.addEventListener(AtompubEvent.DELETE_MEDIA_FAILED, onFailedToDeleteMedia);
  200. * client.deleteMedia(mediaLinkEntry.editMediaLink);
  201. *
  202. * private function onCompletedToDeleteMedia(event:AtompubEvent):void {
  203. * }
  204. *
  205. * private function onFailedToDeleteMedia(event:AtompubEvent):void {
  206. * var message:String = "Error: [" + event.result.code + "] " + event.result.message;
  207. * showErrorMessage(message);
  208. * }
  209. *
  210. * </listing>
  211. *
  212. */
  213. public class AtompubClient extends EventDispatcher {
  214. protected var _credential:AtompubCredential;
  215. protected var _agent:String;
  216. protected var _isFetching:Boolean;
  217. //sreiner protected var _client:HttpClient;
  218. public var _client:HttpClient2;
  219. protected var _responseBody:ByteArray;
  220. protected var _response:HttpResponse;
  221. protected var _lastRequestURI:URI;
  222. protected var _cache:AtompubCache;
  223. protected var _timeout:int;
  224. /**
  225. * Constructor
  226. *
  227. * @langversion ActionScript 3.0
  228. * @playerversion 9.0
  229. */
  230. public function AtompubClient() {
  231. _isFetching = false;
  232. _credential = null;
  233. _agent = "as3atompub";
  234. _client = null;
  235. _responseBody = null;
  236. _response = null;
  237. _lastRequestURI = null;
  238. _timeout = -1;
  239. _cache = new AtompubCache();
  240. }
  241. /**
  242. * setter for cache storage, synonym of setCache method
  243. *
  244. * @param storage AtompubCache object
  245. * @langversion ActionScript 3.0
  246. * @playerversion 9.0
  247. */
  248. public function set cache(storage:AtompubCache):void {
  249. setCache(storage);
  250. }
  251. /**
  252. * setter for cache storage
  253. *
  254. * @param storage AtompubCache object
  255. * @langversion ActionScript 3.0
  256. * @playerversion 9.0
  257. */
  258. public function setCache(storage:AtompubCache):void {
  259. _cache = storage;
  260. }
  261. /**
  262. * setter for timeout, synonym of setTimeout method
  263. *
  264. * @param timeout
  265. * @langversion ActionScript 3.0
  266. * @playerversion 9.0
  267. */
  268. public function set timeout(t:int):void {
  269. setTimeout(t);
  270. }
  271. /**
  272. * setter for timeout
  273. *
  274. * @param timeout
  275. * @langversion ActionScript 3.0
  276. * @playerversion 9.0
  277. */
  278. public function setTimeout(t:int):void {
  279. _timeout = t;
  280. }
  281. /**
  282. * setter for agent name, synonym of setAgent method
  283. *
  284. * @param agent
  285. * @langversion ActionScript 3.0
  286. * @playerversion 9.0
  287. */
  288. public function set agent(name:String):void {
  289. setAgent(name);
  290. }
  291. /**
  292. * setter for agent name
  293. *
  294. * @param agent
  295. * @langversion ActionScript 3.0
  296. * @playerversion 9.0
  297. */
  298. public function setAgent(name:String):void {
  299. _agent = name;
  300. }
  301. /**
  302. * setter for credential, synonym of setCredential method
  303. *
  304. * @param credential AtompubCredential object
  305. * @langversion ActionScript 3.0
  306. * @playerversion 9.0
  307. */
  308. public function set credential(cred:AtompubCredential):void {
  309. setCredential(cred);
  310. }
  311. /**
  312. * setter for credential
  313. *
  314. * @param credential AtompubCredential object
  315. * @langversion ActionScript 3.0
  316. * @playerversion 9.0
  317. */
  318. public function setCredential(cred:AtompubCredential):void {
  319. _credential = cred;
  320. }
  321. /**
  322. * Check if client is fetching or not.
  323. *
  324. * @returns bool represents if or not client is fetching
  325. * @langversion ActionScript 3.0
  326. * @playerversion 9.0
  327. */
  328. public function get isFetching():Boolean {
  329. return _isFetching;
  330. }
  331. public function initializeHttpClient(onComplete:Function):void {
  332. //trace("AtompubClient initializeHttpClient()");
  333. //sreiner _client = new HttpClient();
  334. _client = new HttpClient2();
  335. _client.listener.onClose = onClose;
  336. _client.listener.onComplete = onComplete;
  337. _client.listener.onData = onData;
  338. _client.listener.onError = onError;
  339. _client.listener.onStatus = onStatus;
  340. _client.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
  341. _client.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
  342. }
  343. /**
  344. * clear last request and response data
  345. *
  346. * @langversion ActionScript 3.0
  347. * @playerversion 9.0
  348. */
  349. public function clear():void {
  350. //trace("AtompubClient clear()");
  351. _responseBody = new ByteArray();
  352. _response = null;
  353. _lastRequestURI = null;
  354. }
  355. protected function onClose(e:Event):void {
  356. //trace("AtompubClient onClose()");
  357. _isFetching = false;
  358. }
  359. protected function onData(e:HttpDataEvent):void {
  360. //trace("AtompubClient onData()");
  361. _responseBody.writeBytes(e.bytes);
  362. }
  363. protected function onError(e:ErrorEvent):void {
  364. //trace("AtompubClient onError()");
  365. _isFetching = false;
  366. var result:AtompubEventResult = new AtompubEventResult();
  367. //result.message = e.message;
  368. result.message = e.text;
  369. dispatchEvent(new AtompubEvent(AtompubEvent.TIMEOUT, result));
  370. }
  371. /* socket event handlers */
  372. protected function onIOError(e:IOErrorEvent):void {
  373. //trace("AtompubClient onIOError()");
  374. _isFetching = false;
  375. //todo dispatchEvent(e.clone());
  376. }
  377. protected function onSecurityError(e:SecurityErrorEvent):void {
  378. //trace("AtompubClient onSecurityError()");
  379. _isFetching = false;
  380. //todo dispatchEvent(e.clone());
  381. }
  382. // sreiner: as3httpclientlib changed type of returned from onStatus
  383. protected function onStatus(e:HttpStatusEvent):void
  384. {
  385. //trace("AtompubClient onStatus()");
  386. _response = e.response;
  387. }
  388. protected function setCommonRequestHeaders(uri:URI, req:AtompubRequest):void {
  389. //trace("AtompubClient setCommonRequestHeaders()");
  390. //sreiner req.addHeader("User-Agent", _agent);
  391. if (_credential != null)
  392. _credential.set(uri, req);
  393. }
  394. /**
  395. * Request using Socket
  396. * @param uri
  397. * @param req
  398. *
  399. */
  400. /* sreiner took out use of sockets
  401. protected function request(uri:URI, req:AtompubRequest):void {
  402. //trace("AtompubClient request()");
  403. setCommonRequestHeaders(uri, req);
  404. _isFetching = true;
  405. _lastRequestURI = uri;
  406. _client.request(uri, req, _timeout);
  407. }
  408. */
  409. /**
  410. * Request using HttpService
  411. * @param uri
  412. * @param req
  413. *
  414. */
  415. // sreiner added
  416. protected function requestHttpService(uri:URI, req:AtompubRequest):void {
  417. //trace("AtompubClient requestHttpService()");
  418. setCommonRequestHeaders(uri, req);
  419. _isFetching = true;
  420. _lastRequestURI = uri;
  421. _client.requestHTTPService(uri, req, _timeout);
  422. }
  423. /**
  424. * Request using URLLoader
  425. * @param uri
  426. * @param req
  427. *
  428. */
  429. // sreiner added
  430. protected function requestURLLoader(uri:URI, req:AtompubRequest):void {
  431. //trace("AtompubClient requestURLLoader()");
  432. setCommonRequestHeaders(uri, req);
  433. _isFetching = true;
  434. _lastRequestURI = uri;
  435. _client.requestURLLoader(uri, req, _timeout);
  436. }
  437. /**
  438. * start to get service document
  439. *
  440. * @param uri
  441. * @throws org.coderepos.atompub.events.AtomEvent.GET_SERVICE_COMPLETED
  442. * @throws org.coderepos.atompub.events.AtomEvent.GET_SERVICE_FAILED
  443. * @langversion ActionScript 3.0
  444. * @playerversion 9.0
  445. */
  446. public function getService(uri:URI):void
  447. {
  448. trace("AtompubClient getService()");
  449. if (_isFetching)
  450. {
  451. throw new Error("AtompubClient is fetching.");
  452. };
  453. clear();
  454. initializeHttpClient(onCompleteToGetService);
  455. var req:AtompubRequest = new AtompubRequest("GET");
  456. //var parms:Array = [ { name: "name", value: "value" } ];
  457. //req.setFormData(parms);
  458. // sreiner request(uri, req);
  459. requestHttpService(uri, req);
  460. }
  461. protected function onCompleteToGetService(e:HttpResponseEvent):void
  462. {
  463. trace("AtompubClient onCompleteToGetService()");
  464. _response = e.response;
  465. var code:uint = uint(_response.code);
  466. var result:AtompubEventResult = new AtompubEventResult();
  467. result.code = code;
  468. result.message = _response.message;
  469. _isFetching = false;
  470. if (code == 200) {
  471. // sreiner may not have headers in non sockets mode
  472. //var contentType:String = StringUtil.trim(_response.header.getValue("Content-Type"));
  473. //if (contentType != AtomMediaType.SERVICE.toString())
  474. //{
  475. // warn("getService: Bad content-type: " + contentType);
  476. //trace("AtompubClient getService: content-type: " + contentType);
  477. //}
  478. _responseBody.position = 0;
  479. var response:String = _responseBody.readUTFBytes(_responseBody.bytesAvailable);
  480. try {
  481. result.service = new AtomService(new XML(response));
  482. dispatchEvent(new AtompubEvent(AtompubEvent.GET_SERVICE_COMPLETED, result));
  483. } catch (e:Error) {
  484. result.message = e.message;
  485. dispatchEvent(new AtompubEvent(AtompubEvent.GET_SERVICE_FAILED, result));
  486. return;
  487. }
  488. } else {
  489. dispatchEvent(new AtompubEvent(AtompubEvent.GET_SERVICE_FAILED, result));
  490. }
  491. }
  492. /**
  493. * start to get categories
  494. *
  495. * @param uri
  496. * @throws org.coderepos.atompub.events.AtomEvent.GET_CATEGORIES_COMPLETED
  497. * @throws org.coderepos.atompub.events.AtomEvent.GET_CATEGORIES_FAILED
  498. * @langversion ActionScript 3.0
  499. * @playerversion 9.0
  500. */
  501. public function getCategories(uri:URI):void {
  502. trace("AtompubClient getCategorites()");
  503. if (_isFetching) { throw new Error("AtompubClient is fetching."); };
  504. clear();
  505. initializeHttpClient(onCompleteToGetCategories);
  506. // sreiner request(uri, new AtompubRequest("GET"));
  507. }
  508. protected function onCompleteToGetCategories():void {
  509. trace("AtompubClient onCompleteGetCategories()");
  510. var code:uint = uint(_response.code);
  511. var result:AtompubEventResult = new AtompubEventResult();
  512. result.code = code;
  513. result.message = _response.message;
  514. _isFetching = false;
  515. if (code == 200) {
  516. var contentType:String = StringUtil.trim(_response.header.getValue("Content-Type"));
  517. if (contentType != AtomMediaType.CATEGORIES.toString()) {
  518. warn("getCategories: Bad content-type: " + contentType);
  519. }
  520. _responseBody.position = 0;
  521. var response:String = _responseBody.readUTFBytes(_responseBody.bytesAvailable);
  522. try {
  523. var categories:AtomCategories = new AtomCategories(new XML(response));
  524. result.categories = categories;
  525. } catch (e:Error) {
  526. result.message = e.message;
  527. dispatchEvent(new AtompubEvent(AtompubEvent.GET_CATEGORIES_FAILED, result));
  528. return;
  529. }
  530. dispatchEvent(new AtompubEvent(AtompubEvent.GET_CATEGORIES_COMPLETED, result));
  531. } else {
  532. dispatchEvent(new AtompubEvent(AtompubEvent.GET_CATEGORIES_FAILED, result));
  533. }
  534. }
  535. /**
  536. * start to get feed
  537. *
  538. * @param uri
  539. * @throws org.coderepos.atompub.events.AtomEvent.GET_FEED_COMPLETED
  540. * @throws org.coderepos.atompub.events.AtomEvent.GET_FEED_FAILED
  541. * @langversion ActionScript 3.0
  542. * @playerversion 9.0
  543. */
  544. public function getFeed(uri:URI):void
  545. {
  546. trace("AtompubClient getFeed()");
  547. if (_isFetching)
  548. {
  549. throw new Error("AtompubClient is fetching.");
  550. };
  551. clear();
  552. initializeHttpClient(onCompleteToGetFeed);
  553. var req:AtompubRequest = new AtompubRequest("GET");
  554. // sreiner request(uri, req);
  555. requestHttpService(uri, req);
  556. }
  557. // sreiner httpclient now returns e:HttpResponseEvent
  558. protected function onCompleteToGetFeed(event:HttpResponseEvent):void
  559. {
  560. trace("AtompubClient onCompleteToGetFeed()");
  561. _response = event.response;
  562. var code:uint = uint(_response.code);
  563. var result:AtompubEventResult = new AtompubEventResult();
  564. result.code = code;
  565. result.message = _response.message;
  566. _isFetching = false;
  567. if (code == 200) {
  568. // sreiner may not have headers in non sockets mode
  569. //var contentType:String = StringUtil.trim(_response.header.getValue("Content-Type"));
  570. //sreiner added utf-8
  571. //if (contentType != (AtomMediaType.FEED.toString() + ";charset=UTF-8"))
  572. //{
  573. // warn("getFeed: Bad content-type: " + contentType);
  574. //}
  575. _responseBody.position = 0;
  576. var response:String = _responseBody.readUTFBytes(_responseBody.bytesAvailable);
  577. try {
  578. var xml:XML = new XML(response);
  579. var feed:CMISAtomFeed = new CMISAtomFeed(xml);
  580. result.feed = feed;
  581. } catch (e:Error) {
  582. result.message = e.message;
  583. dispatchEvent(new AtompubEvent(AtompubEvent.GET_FEED_FAILED, result));
  584. return;
  585. }
  586. dispatchEvent(new AtompubEvent(AtompubEvent.GET_FEED_COMPLETED, result));
  587. } else {
  588. dispatchEvent(new AtompubEvent(AtompubEvent.GET_FEED_FAILED, result));
  589. }
  590. }
  591. /**
  592. * start to get entry
  593. *
  594. * @param uri
  595. * @throws org.coderepos.atompub.events.AtomEvent.GET_ENTRY_COMPLETED
  596. * @throws org.coderepos.atompub.events.AtomEvent.GET_ENTRY_FAILED
  597. * @langversion ActionScript 3.0
  598. * @playerversion 9.0
  599. */
  600. public function getEntry(uri:URI):void
  601. {
  602. trace("AtompubClient getEntry()");
  603. if (_isFetching) { throw new Error("AtompubClient is fetching."); };
  604. clear();
  605. initializeHttpClient(onCompleteToGetEntry);
  606. var req:AtompubRequest = new AtompubRequest("GET");
  607. if (_cache != null) {
  608. var cr:AtompubCacheResource = _cache.get(uri);
  609. if (cr) {
  610. if (cr.etag)
  611. req.addHeader("If-None-Match", cr.etag);
  612. if (cr.lastModified)
  613. req.addHeader("If-Modified-Since", cr.lastModified);
  614. }
  615. }
  616. // sreiner request(uri, req);
  617. requestHttpService(uri, req);
  618. }
  619. protected function setCacheResourceForCurrentResponse():void
  620. {
  621. trace("AtompubClient setCacheResourceForCurrentResponse()");
  622. if (_cache != null) {
  623. var cr:AtompubCacheResource = new AtompubCacheResource();
  624. var lastModified:String = StringUtil.trim(_response.header.getValue("Last-Modified"));
  625. if (lastModified) cr.lastModified = lastModified;
  626. var etag:String = StringUtil.trim(_response.header.getValue("ETag"));
  627. if (etag) cr.etag = etag;
  628. cr.resource = new ByteArray();
  629. cr.resource.writeBytes(_responseBody);
  630. _cache.set(_lastRequestURI, cr);
  631. }
  632. }
  633. protected function onCompleteToGetEntry(event:HttpResponseEvent):void
  634. {
  635. trace("AtompubClient onCompleteToGetEntry()");
  636. _response = event.response;
  637. var code:uint = uint(_response.code);
  638. var result:AtompubEventResult = new AtompubEventResult();
  639. result.code = code;
  640. result.message = _response.message;
  641. _isFetching = false;
  642. var response:String;
  643. switch (code) {
  644. case 200:
  645. // sreiner may not have headers in non sockets mode
  646. //var contentType:String = StringUtil.trim(_response.header.getValue("Content-Type"));
  647. //if (contentType != AtomMediaType.ENTRY.toString()) {
  648. // warn("getEntry: Bad content-type: " + contentType);
  649. //}
  650. //setCacheResourceForCurrentResponse();
  651. _responseBody.position = 0;
  652. response = _responseBody.readUTFBytes(_responseBody.bytesAvailable);
  653. try {
  654. result.entry = new AtomEntry(new XML(response));
  655. } catch (e:Error) {
  656. result.message = e.message;
  657. dispatchEvent(new AtompubEvent(AtompubEvent.GET_ENTRY_FAILED, result));
  658. return;
  659. }
  660. dispatchEvent(new AtompubEvent(AtompubEvent.GET_ENTRY_COMPLETED, result));
  661. break;
  662. case 304:
  663. if (_cache == null) {
  664. result.message = "Cache not found.";
  665. dispatchEvent(new AtompubEvent(AtompubEvent.GET_ENTRY_FAILED, result));
  666. return;
  667. }
  668. var cr:AtompubCacheResource = _cache.get(_lastRequestURI);
  669. if (cr != null) {
  670. var rc:ByteArray = cr.resource;
  671. rc.position = 0;
  672. response = rc.readUTFBytes(rc.bytesAvailable);
  673. try {
  674. result.entry = new AtomEntry(new XML(response));
  675. } catch (e:Error) {
  676. result.message = e.message;
  677. dispatchEvent(new AtompubEvent(AtompubEvent.GET_ENTRY_FAILED, result));
  678. return;
  679. }
  680. dispatchEvent(new AtompubEvent(AtompubEvent.GET_ENTRY_COMPLETED, result));
  681. } else {
  682. result.message = "Cache not found.";
  683. dispatchEvent(new AtompubEvent(AtompubEvent.GET_ENTRY_FAILED, result));
  684. return;
  685. }
  686. break;
  687. default:
  688. dispatchEvent(new AtompubEvent(AtompubEvent.GET_ENTRY_FAILED, result));
  689. }
  690. }
  691. /**
  692. * start to get media resource
  693. *
  694. * @param uri
  695. * @throws org.coderepos.atompub.events.AtomEvent.GET_MEDIA_COMPLETED
  696. * @throws org.coderepos.atompub.events.AtomEvent.GET_MEDIA_FAILED
  697. * @langversion ActionScript 3.0
  698. * @playerversion 9.0
  699. */
  700. public function getMedia(uri:URI):void {
  701. trace("AtompubClient getMedia()");
  702. if (_isFetching) { throw new Error("AtompubClient is fetching."); };
  703. clear();
  704. initializeHttpClient(onCompleteToGetMedia);
  705. var req:AtompubRequest = new AtompubRequest("GET");
  706. var cr:AtompubCacheResource = _cache.get(uri);
  707. if (cr) {
  708. if (cr.etag)
  709. req.addHeader("If-None-Match", cr.etag);
  710. if (cr.lastModified)
  711. req.addHeader("If-Modified-Since", cr.lastModified);
  712. }
  713. // sreiner request(uri, req);
  714. }
  715. protected function onCompleteToGetMedia():void {
  716. trace("AtompubClient onCompleteToGetMedia()");
  717. var code:uint = uint(_response.code);
  718. var result:AtompubEventResult = new AtompubEventResult();
  719. result.code = code;
  720. result.message = _response.message;
  721. _isFetching = false;
  722. switch (code) {
  723. case 200:
  724. setCacheResourceForCurrentResponse();
  725. result.resource = new ByteArray();
  726. result.resource.writeBytes(_responseBody);
  727. dispatchEvent(new AtompubEvent(AtompubEvent.GET_MEDIA_COMPLETED, result));
  728. break;
  729. case 304:
  730. if (_cache == null) {
  731. result.message = "Cache not found.";
  732. dispatchEvent(new AtompubEvent(AtompubEvent.GET_MEDIA_FAILED, result));
  733. return;
  734. }
  735. var cr:AtompubCacheResource = _cache.get(_lastRequestURI);
  736. if (cr != null) {
  737. result.resource = new ByteArray();
  738. result.resource.writeBytes(cr.resource);
  739. dispatchEvent(new AtompubEvent(AtompubEvent.GET_MEDIA_COMPLETED, result));
  740. } else {
  741. result.message = "Cache not found.";
  742. dispatchEvent(new AtompubEvent(AtompubEvent.GET_MEDIA_FAILED, result));
  743. }
  744. break;
  745. default:
  746. dispatchEvent(new AtompubEvent(AtompubEvent.GET_MEDIA_FAILED, result));
  747. }
  748. }
  749. /**
  750. * start to create entry
  751. *
  752. * @param uri
  753. * @param entry
  754. * @param slug
  755. * @throws org.coderepos.atompub.events.AtomEvent.CREATE_ENTRY_COMPLETED
  756. * @throws org.coderepos.atompub.events.AtomEvent.CREATE_ENTRY_FAILED
  757. * @langversion ActionScript 3.0
  758. * @playerversion 9.0
  759. */
  760. public function createEntry(uri:URI, entry:AtomEntry, slug:String=null):void
  761. {
  762. trace("AtompubClient createEntry()");
  763. if (_isFetching) { throw new Error("AtompubClient is fetching."); };
  764. clear();
  765. var content:ByteArray = new ByteArray();
  766. var xmlStr:String = entry.toXMLString();
  767. content.writeUTFBytes(xmlStr);
  768. content.position = 0;
  769. initializeHttpClient(onCompleteToCreateEntry);
  770. var req:AtompubRequest = new AtompubRequest("POST");
  771. //if (slug != null)
  772. //{
  773. // req.addHeader("Slug", encodeURIComponent(decodeURIComponent(slug)));
  774. //}
  775. req.addHeader("Content-Type", AtomMediaType.ENTRY.toString());
  776. req.body = content;
  777. //sreiner request(uri, req);
  778. //req.header.remove("Connection");
  779. var model:AppModelLocator = AppModelLocator.getInstance();
  780. var cmisConfig:CMISConfig = CMISConfig(model.ecmServerConfig);
  781. if (cmisConfig.useProxy == true)
  782. {
  783. requestHttpService(uri, req);
  784. }
  785. else
  786. {
  787. requestURLLoader(uri, req);
  788. }
  789. }
  790. protected function onCompleteToCreateEntry(event:HttpResponseEvent):void {
  791. trace("AtompubClient onCompleteToCreateEntry()");
  792. _response = event.response;
  793. var code:uint = uint(_response.code);
  794. var result:AtompubEventResult = new AtompubEventResult();
  795. result.code = code;
  796. result.message = _response.message;
  797. _isFetching = false;
  798. if (code >= 200 && code < 300) {
  799. if (code != 201) {
  800. warn("createEntry: Bad response code: " + String(code));
  801. }
  802. // sreiner may not have headers in non sockets mode
  803. //var contentType:String = StringUtil.trim(_response.header.getValue("Content-Type"));
  804. //if (contentType != AtomMediaType.ENTRY.toString()) {
  805. //warn("createEntry: Bad content-type: " + contentType);
  806. //}
  807. //var location:String = StringUtil.trim(_response.header.getValue("Location"));
  808. //if (location == null) {
  809. // result.message = "Location not found.";
  810. // dispatchEvent(new AtompubEvent(AtompubEvent.CREATE_ENTRY_FAILED, result));
  811. // return;
  812. //}
  813. //result.location = new URI(location);
  814. if (_responseBody.length > 0) {
  815. _responseBody.position = 0;
  816. var response:String = _responseBody.readUTFBytes(_responseBody.bytesAvailable);
  817. try {
  818. result.entry = new AtomEntry(new XML(response));
  819. } catch (e:Error) {
  820. result.message = e.message;
  821. dispatchEvent(new AtompubEvent(AtompubEvent.CREATE_ENTRY_FAILED, result));
  822. return;
  823. }
  824. }
  825. dispatchEvent(new AtompubEvent(AtompubEvent.CREATE_ENTRY_COMPLETED, result));
  826. } else {
  827. dispatchEvent(new AtompubEvent(AtompubEvent.CREATE_ENTRY_FAILED, result));
  828. }
  829. }
  830. /**
  831. * start to create media
  832. *
  833. * @param uri
  834. * @param content
  835. * @param contenttype
  836. * @param slug
  837. * @throws org.coderepos.atompub.events.AtomEvent.CREATE_MEDIA_COMPLETED
  838. * @throws org.coderepos.atompub.events.AtomEvent.CREATE_MEDIA_FAILED
  839. * @langversion ActionScript 3.0
  840. * @playerversion 9.0
  841. */
  842. public function createMedia(uri:URI, content:ByteArray,
  843. type:String, slug:String=null):void {
  844. trace("AtompubClient createMedia()");
  845. if (_isFetching) { throw new Error("AtompubClient is fetching."); };
  846. clear();
  847. initializeHttpClient(onCompleteToCreateMedia);
  848. var req:AtompubRequest = new AtompubRequest("POST");
  849. if (slug != null)
  850. req.addHeader("Slug", encodeURIComponent(decodeURIComponent(slug)));
  851. req.addHeader("Content-Type", type);
  852. req.body = content;
  853. // sreiner request(uri, req);
  854. }
  855. protected function onCompleteToCreateMedia():void {
  856. trace("AtompubClient onCompleteToCreateMedia()");
  857. var code:uint = uint(_response.code);
  858. var result:AtompubEventResult = new AtompubEventResult();
  859. result.code = code;
  860. result.message = _response.message;
  861. _isFetching = false;
  862. if (code >= 200 && code < 300) {
  863. if (code != 201) {
  864. warn("Bad response code: " + String(code));
  865. }
  866. var contentType:String = StringUtil.trim(_response.header.getValue("Content-Type"));
  867. if (contentType != AtomMediaType.ENTRY.toString()) {
  868. warn("Bad content-type: " + contentType);
  869. }
  870. var location:String = StringUtil.trim(_response.header.getValue("Location"));
  871. if (location == null) {
  872. result.message = "Location not found.";
  873. dispatchEvent(new AtompubEvent(AtompubEvent.CREATE_MEDIA_FAILED, result));
  874. return;
  875. }
  876. result.location = new URI(location);
  877. if (_responseBody.length > 0) {
  878. _responseBody.position = 0;
  879. var response:String = _responseBody.readUTFBytes(_responseBody.bytesAvailable);
  880. try {
  881. result.entry = new AtomEntry(new XML(response));
  882. } catch (e:Error) {
  883. result.message = e.message;
  884. dispatchEvent(new AtompubEvent(AtompubEvent.CREATE_MEDIA_FAILED, result));
  885. return;
  886. }
  887. }
  888. dispatchEvent(new AtompubEvent(AtompubEvent.CREATE_MEDIA_COMPLETED, result));
  889. } else {
  890. dispatchEvent(new AtompubEvent(AtompubEvent.CREATE_MEDIA_FAILED, result));
  891. }
  892. }
  893. /**
  894. * start to update entry
  895. *
  896. * @param uri
  897. * @param entry
  898. * @throws org.coderepos.atompub.events.AtomEvent.UPDATE_ENTRY_COMPLETED
  899. * @throws org.coderepos.atompub.events.AtomEvent.UPDATE_ENTRY_FAILED
  900. * @langversion ActionScript 3.0
  901. * @playerversion 9.0
  902. */
  903. public function updateEntry(uri:URI, entry:AtomEntry):void {
  904. trace("AtompubClient updateEntry()");
  905. if (_isFetching) { throw new Error("AtompubClient is fetching."); };
  906. clear();
  907. initializeHttpClient(onCompleteToUpdateEntry);
  908. var req:AtompubRequest = new AtompubRequest("PUT");
  909. req.addHeader("Content-Type", AtomMediaType.ENTRY.toString());
  910. //var cr:AtompubCacheResource = _cache.get(uri);
  911. //if (cr) {
  912. // if (cr.etag)
  913. // req.addHeader("If-Match", cr.etag);
  914. // if (cr.lastModified)
  915. // req.addHeader("If-Not-Modified-Since", cr.lastModified);
  916. //}
  917. var content:ByteArray = new ByteArray();
  918. content.writeUTFBytes(entry.toXMLString());
  919. req.body = content;
  920. // sreiner request(uri, req);
  921. }
  922. protected function onCompleteToUpdateEntry(event:HttpResponseEvent):void {
  923. trace("AtompubClient onCompleteToUpdateEntry()");
  924. var code:uint = uint(_response.code);
  925. var result:AtompubEventResult = new AtompubEventResult();
  926. result.code = code;
  927. result.message = _response.message;
  928. _isFetching = false;
  929. if (code >= 200 && code < 300) {
  930. if (code != 200 || code != 204) {
  931. warn("Bad response code: " + String(code));
  932. }
  933. if (_responseBody.length > 0) {
  934. //sreiner setCacheResourceForCurrentResponse();
  935. result.resource = new ByteArray();
  936. result.resource.writeBytes(_responseBody);
  937. // sreiner may not have headers in non sockets mode
  938. /*
  939. if (StringUtil.trim(_response.header.getValue("Content-Type")) != AtomMediaType.ENTRY.toString()) {
  940. _responseBody.position = 0;
  941. var response:String = _responseBody.readUTFBytes(_responseBody.bytesAvailable);
  942. try {
  943. result.entry = new AtomEntry(new XML(response));
  944. } catch (e:Error) {
  945. result.message = e.message;
  946. dispatchEvent(new AtompubEvent(AtompubEvent.UPDATE_ENTRY_FAILED, result));
  947. return;
  948. }
  949. }
  950. */
  951. }
  952. dispatchEvent(new AtompubEvent(AtompubEvent.UPDATE_ENTRY_COMPLETED, result));
  953. } else {
  954. dispatchEvent(new AtompubEvent(AtompubEvent.UPDATE_ENTRY_FAILED, result));
  955. }
  956. }
  957. /**
  958. * start to update media
  959. *
  960. * @param uri
  961. * @param content
  962. * @param contentType
  963. * @throws org.coderepos.atompub.events.AtomEvent.UPDATE_MEDIA_COMPLETED
  964. * @throws org.coderepos.atompub.events.AtomEvent.UPDATE_MEDIA_FAILED
  965. * @langversion ActionScript 3.0
  966. * @playerversion 9.0
  967. */
  968. public function updateMedia(uri:URI, content:ByteArray,
  969. type:String):void {
  970. trace("AtompubClient updateMedia()");
  971. if (_isFetching) { throw new Error("AtompubClient is fetching."); };
  972. clear();
  973. initializeHttpClient(onCompleteToUpdateMedia);
  974. var req:AtompubRequest = new AtompubRequest("PUT");
  975. req.addHeader("Content-Type", type);
  976. var cr:AtompubCacheResource = _cache.get(uri);
  977. if (cr) {
  978. if (cr.etag)
  979. req.addHeader("If-Match", cr.etag);
  980. if (cr.lastModified)
  981. req.addHeader("If-Not-Modified-Since", cr.lastModified);
  982. }
  983. req.body = content;
  984. // sreiner request(uri, req);
  985. }
  986. protected function onCompleteToUpdateMedia(event:HttpResponseEvent):void
  987. {
  988. trace("AtompubClient onCompleteToUpdateMedia()");
  989. var code:uint = uint(_response.code);
  990. var result:AtompubEventResult = new AtompubEventResult();
  991. result.code = code;
  992. result.message = _response.message;
  993. _isFetching = false;
  994. if (code >= 200 && code < 300) {
  995. if (code != 200 || code != 204) {
  996. warn("Bad response code: " + String(code));
  997. }
  998. if (_responseBody.length > 0) {
  999. setCacheResourceForCurrentResponse();
  1000. result.resource = new ByteArray();
  1001. result.resource.writeBytes(_responseBody);
  1002. if (StringUtil.trim(_response.header.getValue("Content-Type")) != AtomMediaType.ENTRY.toString()) {
  1003. _responseBody.position = 0;
  1004. var response:String = _responseBody.readUTFBytes(_responseBody.bytesAvailable);
  1005. try {
  1006. result.entry = new AtomEntry(new XML(response));
  1007. } catch (e:Error) {
  1008. result.message = e.message;
  1009. dispatchEvent(new AtompubEvent(AtompubEvent.UPDATE_MEDIA_FAILED, result));
  1010. return;
  1011. }
  1012. }
  1013. }
  1014. dispatchEvent(new AtompubEvent(AtompubEvent.UPDATE_MEDIA_COMPLETED, result));
  1015. } else {
  1016. dispatchEvent(new AtompubEvent(AtompubEvent.UPDATE_MEDIA_FAILED, result));
  1017. }
  1018. }
  1019. /**
  1020. * start to delete entry
  1021. *
  1022. * @param uri
  1023. * @throws org.coderepos.atompub.events.AtomEvent.DELETE_ENTRY_COMPLETED
  1024. * @throws org.coderepos.atompub.events.AtomEvent.DELETE_ENTRY_FAILED
  1025. * @langversion ActionScript 3.0
  1026. * @playerversion 9.0
  1027. */
  1028. public function deleteEntry(uri:URI):void
  1029. {
  1030. trace("AtompubClient deleteEntry()");
  1031. if (_isFetching)
  1032. {
  1033. throw new Error("AtompubClient is fetching.");
  1034. };
  1035. clear();
  1036. initializeHttpClient(onCompleteToDeleteEntry);
  1037. var req:AtompubRequest = new AtompubRequest("DELETE");
  1038. // sreiner request(uri, req);
  1039. // todo: add check for non air, and put up msg not supported unless using proxy
  1040. var model:AppModelLocator = AppModelLocator.getInstance();
  1041. var cmisConfig:CMISConfig = CMISConfig(model.ecmServerConfig);
  1042. if (cmisConfig.useProxy == true)
  1043. {
  1044. requestHttpService(uri, req);
  1045. // todo: remove this workaround
  1046. // send delayed complete event since blazeds proxy gives fault even though delete worked
  1047. var delay:Timer = new Timer(3000);
  1048. delay.addEventListener(TimerEvent.TIMER, deleteCompleteWorkaround);
  1049. delay.start();
  1050. }
  1051. else
  1052. {
  1053. requestURLLoader(uri, req);
  1054. }
  1055. }
  1056. private function deleteCompleteWorkaround(event:TimerEvent):void
  1057. {
  1058. var result:AtompubEventResult = new AtompubEventResult();
  1059. dispatchEvent(new AtompubEvent(AtompubEvent.DELETE_ENTRY_COMPLETED, result));
  1060. }
  1061. protected function onCompleteToDeleteEntry(event:HttpResponseEvent):void
  1062. {
  1063. trace("AtompubClient onCompleteToDeleteEntry()");
  1064. var result:AtompubEventResult = new AtompubEventResult();
  1065. var code:uint = uint(_response.code);
  1066. result.code = code;
  1067. result.message = _response.message;
  1068. _isFetching = false;
  1069. if (code >= 200 && code < 300) {
  1070. if (code != 200 && code != 204) {
  1071. warn("Bad response code: " + String(code));
  1072. }
  1073. dispatchEvent(new AtompubEvent(AtompubEvent.DELETE_ENTRY_COMPLETED, result));
  1074. } else {
  1075. dispatchEvent(new AtompubEvent(AtompubEvent.DELETE_ENTRY_FAILED, result));
  1076. }
  1077. }
  1078. /**
  1079. * start to delete media
  1080. *
  1081. * @param uri
  1082. * @throws org.coderepos.atompub.events.AtomEvent.DELETE_MEDIA_COMPLETED
  1083. * @throws org.coderepos.atompub.events.AtomEvent.DELETE_MEDIA_FAILED
  1084. * @langversion ActionScript 3.0
  1085. * @playerversion 9.0
  1086. */
  1087. public function deleteMedia(uri:URI):void {
  1088. trace("AtompubClient deleteMedia()");
  1089. if (_isFetching) { throw new Error("AtompubClient is fetching."); };
  1090. clear();
  1091. initializeHttpClient(onCompleteToDeleteMedia);
  1092. // sreiner request(uri, new AtompubRequest("DELETE"));
  1093. }
  1094. protected function onCompleteToDeleteMedia():void {
  1095. trace("AtompubClient onCompleteToDeleteMedia()");
  1096. var result:AtompubEventResult = new AtompubEventResult();
  1097. var code:uint = uint(_response.code);
  1098. result.code = code;
  1099. result.message = _response.message;
  1100. _isFetching = false;
  1101. if (code >= 200 && code < 300) {
  1102. if (code != 200 && code != 204) {
  1103. warn("Bad response code: " + String(code));
  1104. }
  1105. dispatchEvent(new AtompubEvent(AtompubEvent.DELETE_MEDIA_COMPLETED, result));
  1106. } else {
  1107. dispatchEvent(new AtompubEvent(AtompubEvent.DELETE_MEDIA_FAILED, result));
  1108. }
  1109. }
  1110. protected function warn(message:String):void
  1111. {
  1112. Log.debug(message);
  1113. }
  1114. }
  1115. }