1 | package clarin.cmdi.componentregistry.services { |
---|
2 | import clarin.cmdi.componentregistry.common.ItemDescription; |
---|
3 | |
---|
4 | import com.adobe.net.URI; |
---|
5 | import com.hurlant.util.Base64; |
---|
6 | |
---|
7 | import flash.events.Event; |
---|
8 | import flash.events.EventDispatcher; |
---|
9 | |
---|
10 | import mx.controls.Alert; |
---|
11 | import mx.utils.StringUtil; |
---|
12 | |
---|
13 | import org.httpclient.HttpClient; |
---|
14 | import org.httpclient.events.HttpErrorEvent; |
---|
15 | import org.httpclient.events.HttpResponseEvent; |
---|
16 | import org.httpclient.events.HttpStatusEvent; |
---|
17 | import org.httpclient.http.Delete; |
---|
18 | |
---|
19 | [Event(name="itemDeleted", type="flash.events.Event")] |
---|
20 | |
---|
21 | public class DeleteService extends EventDispatcher { |
---|
22 | public static const ITEM_DELETED:String = "itemDeleted"; |
---|
23 | private var service:HttpClient; |
---|
24 | |
---|
25 | public function DeleteService() { |
---|
26 | service = new HttpClient(); |
---|
27 | service.listener.onComplete = handleResult; |
---|
28 | service.listener.onError = handleError; |
---|
29 | service.listener.onStatus = handleStatus; |
---|
30 | } |
---|
31 | |
---|
32 | private function getCredentials():String { |
---|
33 | return Base64.encode("tomcat:tomcat"); |
---|
34 | } |
---|
35 | |
---|
36 | public function deleteItem(item:ItemDescription):void { |
---|
37 | var uri:URI = new URI(item.dataUrl); |
---|
38 | var httpDelete:Delete = new Delete(); |
---|
39 | httpDelete.addHeader("Authorization", "BASIC " + getCredentials()); |
---|
40 | service.request(uri, httpDelete); |
---|
41 | } |
---|
42 | |
---|
43 | private function handleResult(resultEvent:HttpResponseEvent):void { |
---|
44 | if (resultEvent.response.code == "200") { |
---|
45 | dispatchEvent(new Event(ITEM_DELETED)); |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | public function handleError(faultEvent:HttpErrorEvent):void { |
---|
50 | var errorMessage:String = StringUtil.substitute("Error in {0}: {1}", this, faultEvent.text); |
---|
51 | throw new Error(errorMessage); |
---|
52 | } |
---|
53 | |
---|
54 | private function handleStatus(event:HttpStatusEvent):void { |
---|
55 | if (event.code != "200") { |
---|
56 | if (event.code == "401") { |
---|
57 | Alert.show("Unauthorized to delete item, you are not the creator."); |
---|
58 | } |
---|
59 | trace("(httpstatus code was: " + event.code + ")"); |
---|
60 | } |
---|
61 | } |
---|
62 | } |
---|
63 | } |
---|