Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (3.7 kB)

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