early commit - first attemp, regarding giving a uri and fetching the proper directory
[pithos] / src / gr / ebs / gss / client / FileUploadGearsIEDialog.java
1 /*\r
2  * Copyright 2009 Electronic Business Systems Ltd.\r
3  *\r
4  * This file is part of GSS.\r
5  *\r
6  * GSS is free software: you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation, either version 3 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * GSS is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.\r
18  */\r
19 package gr.ebs.gss.client;\r
20 \r
21 import gr.ebs.gss.client.rest.RestCommand;\r
22 \r
23 import com.google.gwt.gears.client.desktop.File;\r
24 import com.google.gwt.gears.client.httprequest.HttpRequest;\r
25 import com.google.gwt.gears.client.httprequest.ProgressEvent;\r
26 import com.google.gwt.gears.client.httprequest.ProgressHandler;\r
27 import com.google.gwt.gears.client.httprequest.RequestCallback;\r
28 \r
29 /**\r
30  * The 'File upload' dialog box implementation with Google Gears support\r
31  * for IE.\r
32  */\r
33 public class FileUploadGearsIEDialog extends FileUploadGearsDialog implements Updateable {\r
34 \r
35         /**\r
36          * Perform the HTTP request to upload the specified file.\r
37          */\r
38         @Override\r
39         protected void doSend(final File file, final int index) {\r
40                 final GSS app = GSS.get();\r
41                 HttpRequest request = factory.createHttpRequest();\r
42                 requests.add(request);\r
43                 String method = "POST";\r
44 \r
45                 String path;\r
46                 final String filename = getFilename(file.getName());\r
47                 path = folder.getUri();\r
48                 if (!path.endsWith("/"))\r
49                         path = path + "/";\r
50                 path = path + encode(filename);\r
51 \r
52                 String token = app.getToken();\r
53                 String resource = path.substring(app.getApiPath().length()-1, path.length());\r
54                 String date = RestCommand.getDate();\r
55                 String sig = RestCommand.calculateSig(method, date, resource, RestCommand.base64decode(token));\r
56                 request.open(method, path);\r
57                 request.setRequestHeader("X-GSS-Date", date);\r
58                 request.setRequestHeader("Authorization", app.getCurrentUserResource().getUsername() + " " + sig);\r
59                 request.setRequestHeader("Accept", "application/json; charset=utf-8");\r
60                 request.setCallback(new RequestCallback() {\r
61                         public void onResponseReceived(HttpRequest req) {\r
62                                 // XXX: No error checking, since IE throws an Internal Error\r
63                                 // when accessing req.getStatus().\r
64                                 selectedFiles.remove(file);\r
65                                 finish();\r
66                         }\r
67                 });\r
68                 request.getUpload().setProgressHandler(new ProgressHandler() {\r
69                         public void onProgress(ProgressEvent event) {\r
70                                 double pcnt = (double) event.getLoaded() / event.getTotal();\r
71                                 progressBars.get(index).setProgress((int) Math.floor(pcnt * 100));\r
72                         }\r
73                 });\r
74                 request.send(file.getBlob());\r
75         }\r
76 \r
77 }\r