1 | package clarin.cmdi.componentregistry.services { |
---|
2 | import clarin.cmdi.componentregistry.common.ItemDescription; |
---|
3 | import clarin.cmdi.componentregistry.importer.UploadCompleteEvent; |
---|
4 | |
---|
5 | import com.adobe.net.URI; |
---|
6 | import com.hurlant.util.Base64; |
---|
7 | |
---|
8 | import flash.events.DataEvent; |
---|
9 | import flash.events.ErrorEvent; |
---|
10 | import flash.events.Event; |
---|
11 | import flash.events.IOErrorEvent; |
---|
12 | import flash.events.ProgressEvent; |
---|
13 | import flash.events.SecurityErrorEvent; |
---|
14 | import flash.net.FileFilter; |
---|
15 | import flash.net.FileReference; |
---|
16 | |
---|
17 | import mx.controls.ProgressBar; |
---|
18 | import mx.managers.CursorManager; |
---|
19 | |
---|
20 | import org.httpclient.HttpClient; |
---|
21 | import org.httpclient.events.HttpDataEvent; |
---|
22 | import org.httpclient.events.HttpResponseEvent; |
---|
23 | import org.httpclient.http.Post; |
---|
24 | import org.httpclient.http.multipart.Multipart; |
---|
25 | import org.httpclient.http.multipart.Part; |
---|
26 | |
---|
27 | [Event(name="uploadComplete", type="clarin.cmdi.componentregistry.importer.UploadCompleteEvent")] |
---|
28 | public class UploadService { |
---|
29 | |
---|
30 | public function UploadService() { |
---|
31 | } |
---|
32 | |
---|
33 | [Bindable] |
---|
34 | public var selectedFile:String = ""; |
---|
35 | [Bindable] |
---|
36 | public var message:String = ""; |
---|
37 | |
---|
38 | private var fileRef:FileReference; |
---|
39 | private var httpClient:HttpClient; |
---|
40 | private var pb:ProgressBar; |
---|
41 | |
---|
42 | public function init(progressBar:ProgressBar):void { |
---|
43 | pb = progressBar; |
---|
44 | } |
---|
45 | |
---|
46 | private function createAndInitRequest():void { |
---|
47 | CursorManager.setBusyCursor(); |
---|
48 | httpClient = new HttpClient(); |
---|
49 | httpClient.listener.onError = httpclientErrorHandler; |
---|
50 | httpClient.listener.onData = httpclientDataHandler; |
---|
51 | httpClient.listener.onComplete = httpclientCompleteHandler; |
---|
52 | } |
---|
53 | |
---|
54 | private function createAndInitFileReference():void { |
---|
55 | fileRef = new FileReference(); |
---|
56 | fileRef.addEventListener(Event.SELECT, selectHandler); |
---|
57 | fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, responseHandler); |
---|
58 | fileRef.addEventListener(ProgressEvent.PROGRESS, progressHandler); |
---|
59 | fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); |
---|
60 | fileRef.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * submits a profile, data parameter can be null which implies a file was selected using selectXmlFile(); |
---|
65 | */ |
---|
66 | public function submitProfile(description:ItemDescription, data:String = null):void { |
---|
67 | createAndInitRequest(); |
---|
68 | submit(description, data, new URI(Config.instance.uploadProfileUrl)); |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * submits a component, data parameter can be null which implies a file was selected using selectXmlFile(); |
---|
73 | */ |
---|
74 | public function submitComponent(description:ItemDescription, data:String = null):void { |
---|
75 | createAndInitRequest(); |
---|
76 | submit(description, data, new URI(Config.instance.uploadComponentUrl)); |
---|
77 | } |
---|
78 | |
---|
79 | private function getCredentials():String { |
---|
80 | return Base64.encode("tomcat:tomcat"); |
---|
81 | } |
---|
82 | |
---|
83 | private function submit(description:ItemDescription, data:String, uri:URI):void { |
---|
84 | message = ""; |
---|
85 | try { |
---|
86 | if (data != null) { |
---|
87 | var parts:Array = new Array(); |
---|
88 | parts.push(new Part("description", description.description)); |
---|
89 | parts.push(new Part("name", description.name)); |
---|
90 | parts.push(new Part("group", description.groupName)); |
---|
91 | parts.push(new Part("data", data, "application/octet-stream", [{name: "filename", value: description.name + ".xml"}])); |
---|
92 | |
---|
93 | var multipart:Multipart = new Multipart(parts); |
---|
94 | startUploadHandler(); |
---|
95 | var post:Post = new Post(); |
---|
96 | post.addHeader("Authorization", "BASIC " + getCredentials()); |
---|
97 | post.setMultipart(multipart); |
---|
98 | httpClient.request(uri, post); |
---|
99 | } else { |
---|
100 | // Cannot sent credentials with FileReference.upload so just load the data and then submit through HttpClient |
---|
101 | startUploadHandler(); |
---|
102 | if (fileRef.data == null) { |
---|
103 | //only load if not loaded before otherwise sent the already loaded file. You can only force a reload of the file by selecting it again (it is a flash thingy). |
---|
104 | fileRef.addEventListener(Event.COMPLETE, function(event:Event):void { |
---|
105 | submit(description, new String(fileRef.data), uri); |
---|
106 | }); |
---|
107 | fileRef.load(); |
---|
108 | } else { |
---|
109 | submit(description, new String(fileRef.data), uri); |
---|
110 | } |
---|
111 | } |
---|
112 | } catch (error:Error) { |
---|
113 | trace("Unable to upload file. Error: " + error); |
---|
114 | CursorManager.removeBusyCursor(); |
---|
115 | throw error; |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | public function selectXmlFile(event:Event):void { |
---|
120 | createAndInitFileReference(); |
---|
121 | var filter:FileFilter = new FileFilter("Xml Files (*.xml)", "*.xml"); |
---|
122 | fileRef.browse(new Array(filter)); |
---|
123 | } |
---|
124 | |
---|
125 | private function selectHandler(event:Event):void { |
---|
126 | selectedFile = fileRef.name; |
---|
127 | message = ""; |
---|
128 | pb.visible = false; |
---|
129 | } |
---|
130 | |
---|
131 | private function httpclientCompleteHandler(event:HttpResponseEvent):void { |
---|
132 | if (!event.response.isSuccess) { |
---|
133 | addToMessage("Server Failed to handle registration. Unexpected error, try again later. (httpstatus code was: " + event.response.code + ")\n"); |
---|
134 | } |
---|
135 | CursorManager.removeBusyCursor(); |
---|
136 | httpClient.close(); |
---|
137 | } |
---|
138 | |
---|
139 | private function httpclientDataHandler(event:HttpDataEvent):void { |
---|
140 | uploadComplete(); |
---|
141 | var response:XML = new XML(event.bytes); |
---|
142 | handleResponse(response); |
---|
143 | } |
---|
144 | |
---|
145 | private function httpclientErrorHandler(event:ErrorEvent):void { |
---|
146 | addToMessage("Server Failed to handle registration. Unexpected error, try again later. (error: " + event.text + ")\n"); |
---|
147 | } |
---|
148 | |
---|
149 | private function securityErrorHandler(event:SecurityErrorEvent):void { |
---|
150 | addToMessage("Server Failed to handle registration. Unexpected error, try again later. (error: " + event.text + ")\n"); |
---|
151 | } |
---|
152 | |
---|
153 | private function ioErrorHandler(event:IOErrorEvent):void { |
---|
154 | addToMessage("Unable to load file. (error: " + event.text + ")\n"); |
---|
155 | } |
---|
156 | |
---|
157 | private function progressHandler(event:ProgressEvent):void { |
---|
158 | trace("Uploading: " + event.bytesLoaded + "/" + event.bytesTotal); |
---|
159 | pb.setProgress(event.bytesLoaded, event.bytesTotal); |
---|
160 | } |
---|
161 | |
---|
162 | |
---|
163 | private function responseHandler(event:DataEvent):void { |
---|
164 | uploadComplete(); |
---|
165 | var response:XML = new XML(event.data); |
---|
166 | handleResponse(response); |
---|
167 | } |
---|
168 | |
---|
169 | private function startUploadHandler():void { |
---|
170 | trace("uploading start"); |
---|
171 | pb.label = "uploading %3%%"; |
---|
172 | pb.visible = true; |
---|
173 | pb.includeInLayout = true; |
---|
174 | } |
---|
175 | |
---|
176 | private function handleResponse(response:XML):void { |
---|
177 | if (response.@registered == true) { |
---|
178 | var item:ItemDescription = new ItemDescription(); |
---|
179 | if (response.@isProfile == true) { |
---|
180 | item.createProfile(response.description[0]); |
---|
181 | } else { |
---|
182 | item.createComponent(response.description[0]); |
---|
183 | } |
---|
184 | dispatchEvent(new UploadCompleteEvent(item)); |
---|
185 | } else { |
---|
186 | createErrorMessage(response); |
---|
187 | } |
---|
188 | |
---|
189 | } |
---|
190 | |
---|
191 | private function uploadComplete():void { |
---|
192 | pb.label = "Upload complete"; |
---|
193 | pb.setProgress(100, 100); |
---|
194 | } |
---|
195 | |
---|
196 | private function createErrorMessage(response:XML):void { |
---|
197 | addToMessage("Failed to register:"); |
---|
198 | var errors:XMLList = response.errors.error; |
---|
199 | for each (var error:XML in errors) { |
---|
200 | message += " - " + error.toString() + "\n"; |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | private function addToMessage(text:String):void { |
---|
205 | message += text; |
---|
206 | } |
---|
207 | |
---|
208 | } |
---|
209 | } |
---|