Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / FileUploadGearsIEDialog.java @ fad48f0d

History | View | Annotate | Download (3.8 kB)

1 a57faaf0 Christos Stathis
/*
2 63366925 Christos Stathis
 * Copyright 2011 GRNET S.A. All rights reserved.
3 63366925 Christos Stathis
 *
4 63366925 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 63366925 Christos Stathis
 * without modification, are permitted provided that the following
6 63366925 Christos Stathis
 * conditions are met:
7 63366925 Christos Stathis
 *
8 63366925 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 63366925 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 63366925 Christos Stathis
 *      disclaimer.
11 63366925 Christos Stathis
 *
12 63366925 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 63366925 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 63366925 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 63366925 Christos Stathis
 *      provided with the distribution.
16 63366925 Christos Stathis
 *
17 63366925 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 63366925 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 63366925 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 63366925 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 63366925 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 63366925 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 63366925 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 63366925 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 63366925 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 63366925 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 63366925 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 63366925 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 63366925 Christos Stathis
 *
30 63366925 Christos Stathis
 * The views and conclusions contained in the software and
31 63366925 Christos Stathis
 * documentation are those of the authors and should not be
32 63366925 Christos Stathis
 * interpreted as representing official policies, either expressed
33 63366925 Christos Stathis
 * or implied, of GRNET S.A.
34 a57faaf0 Christos Stathis
 */
35 a57faaf0 Christos Stathis
package gr.grnet.pithos.web.client;
36 a57faaf0 Christos Stathis
37 a57faaf0 Christos Stathis
import java.util.List;
38 a57faaf0 Christos Stathis
39 a57faaf0 Christos Stathis
import gr.grnet.pithos.web.client.rest.RestCommand;
40 a57faaf0 Christos Stathis
41 a57faaf0 Christos Stathis
import com.google.gwt.gears.client.desktop.File;
42 a57faaf0 Christos Stathis
import com.google.gwt.gears.client.httprequest.HttpRequest;
43 a57faaf0 Christos Stathis
import com.google.gwt.gears.client.httprequest.ProgressEvent;
44 a57faaf0 Christos Stathis
import com.google.gwt.gears.client.httprequest.ProgressHandler;
45 a57faaf0 Christos Stathis
import com.google.gwt.gears.client.httprequest.RequestCallback;
46 a57faaf0 Christos Stathis
47 a57faaf0 Christos Stathis
/**
48 a57faaf0 Christos Stathis
 * The 'File upload' dialog box implementation with Google Gears support
49 a57faaf0 Christos Stathis
 * for IE.
50 a57faaf0 Christos Stathis
 */
51 bdda6b2f Christos Stathis
public class FileUploadGearsIEDialog extends FileUploadGearsDialog {
52 a57faaf0 Christos Stathis
53 a57faaf0 Christos Stathis
        /**
54 a57faaf0 Christos Stathis
         * Perform the HTTP request to upload the specified file.
55 a57faaf0 Christos Stathis
         */        
56 a57faaf0 Christos Stathis
        @Override
57 a57faaf0 Christos Stathis
        protected void doSend(final List<File> filesRemaining) {
58 a57faaf0 Christos Stathis
                final GSS app = GSS.get();
59 a57faaf0 Christos Stathis
                HttpRequest request = factory.createHttpRequest();
60 a57faaf0 Christos Stathis
                requests.add(request);
61 a57faaf0 Christos Stathis
                String method = "POST";
62 a57faaf0 Christos Stathis
63 a57faaf0 Christos Stathis
                String path;
64 a57faaf0 Christos Stathis
                final String filename = getFilename(filesRemaining.get(0).getName());
65 a57faaf0 Christos Stathis
                path = folder.getUri();
66 a57faaf0 Christos Stathis
                if (!path.endsWith("/"))
67 a57faaf0 Christos Stathis
                        path = path + "/";
68 a57faaf0 Christos Stathis
                path = path + encode(filename);
69 a57faaf0 Christos Stathis
70 a57faaf0 Christos Stathis
                String token = app.getToken();
71 a57faaf0 Christos Stathis
                String resource = path.substring(app.getApiPath().length()-1, path.length());
72 a57faaf0 Christos Stathis
                String date = RestCommand.getDate();
73 a57faaf0 Christos Stathis
                String sig = RestCommand.calculateSig(method, date, resource, RestCommand.base64decode(token));
74 a57faaf0 Christos Stathis
                request.open(method, path);
75 a57faaf0 Christos Stathis
                request.setRequestHeader("X-GSS-Date", date);
76 a57faaf0 Christos Stathis
                request.setRequestHeader("Authorization", app.getCurrentUserResource().getUsername() + " " + sig);
77 a57faaf0 Christos Stathis
                request.setRequestHeader("Accept", "application/json; charset=utf-8");
78 a57faaf0 Christos Stathis
                request.setCallback(new RequestCallback() {
79 a57faaf0 Christos Stathis
                        @Override
80 a57faaf0 Christos Stathis
                        public void onResponseReceived(HttpRequest req) {
81 a57faaf0 Christos Stathis
                                // XXX: No error checking, since IE throws an Internal Error
82 a57faaf0 Christos Stathis
                                // when accessing req.getStatus().
83 a57faaf0 Christos Stathis
                                filesRemaining.remove(0);
84 a57faaf0 Christos Stathis
                                if(filesRemaining.isEmpty()){                                        
85 a57faaf0 Christos Stathis
                                        finish();                                        
86 a57faaf0 Christos Stathis
                                }
87 a57faaf0 Christos Stathis
                                doSend(filesRemaining);        
88 a57faaf0 Christos Stathis
                        }
89 a57faaf0 Christos Stathis
                });
90 a57faaf0 Christos Stathis
                request.getUpload().setProgressHandler(new ProgressHandler() {
91 a57faaf0 Christos Stathis
                        @Override
92 a57faaf0 Christos Stathis
                        public void onProgress(ProgressEvent event) {
93 a57faaf0 Christos Stathis
                                double pcnt = (double) event.getLoaded() / event.getTotal();
94 a57faaf0 Christos Stathis
                                progressBars.get(0).setProgress((int) Math.floor(pcnt * 100));
95 a57faaf0 Christos Stathis
                                if(pcnt*100 == 100)
96 a57faaf0 Christos Stathis
                                        progressBars.remove(0);
97 a57faaf0 Christos Stathis
                        }
98 a57faaf0 Christos Stathis
                });
99 a57faaf0 Christos Stathis
                request.send(filesRemaining.get(0).getBlob());
100 a57faaf0 Christos Stathis
        }
101 a57faaf0 Christos Stathis
102 a57faaf0 Christos Stathis
}