2 * Copyright (c) 2011 Greek Research and Technology Network
4 package gr.grnet.pithos.web.client;
6 import gr.grnet.pithos.web.client.rest.PostCommand;
7 import gr.grnet.pithos.web.client.rest.RestCommand;
8 import gr.grnet.pithos.web.client.rest.RestException;
9 import gr.grnet.pithos.web.client.rest.resource.FileResource;
10 import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.HashMap;
15 import java.util.List;
18 import com.google.gwt.core.client.GWT;
19 import com.google.gwt.event.dom.client.ClickEvent;
20 import com.google.gwt.event.dom.client.ClickHandler;
21 import com.google.gwt.gears.client.Factory;
22 import com.google.gwt.gears.client.desktop.Desktop;
23 import com.google.gwt.gears.client.desktop.File;
24 import com.google.gwt.gears.client.desktop.OpenFilesHandler;
25 import com.google.gwt.gears.client.httprequest.HttpRequest;
26 import com.google.gwt.gears.client.httprequest.ProgressEvent;
27 import com.google.gwt.gears.client.httprequest.ProgressHandler;
28 import com.google.gwt.gears.client.httprequest.RequestCallback;
29 import com.google.gwt.http.client.URL;
30 import com.google.gwt.json.client.JSONObject;
31 import com.google.gwt.json.client.JSONString;
32 import com.google.gwt.user.client.DeferredCommand;
33 import com.google.gwt.user.client.ui.Button;
34 import com.google.gwt.user.client.ui.FlexTable;
35 import com.google.gwt.user.client.ui.HTML;
36 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
37 import com.google.gwt.user.client.ui.HorizontalPanel;
38 import com.google.gwt.user.client.ui.VerticalPanel;
41 * The 'File upload' dialog box implementation with Google Gears support.
43 public class FileUploadGearsDialog extends FileUploadDialog implements Updateable {
45 protected final Factory factory = Factory.getInstance();
48 * The array of files to upload.
50 private File[] fileObjects;
53 * A list of files to upload, created from files array. Used to signal
54 * finished state when empty.
56 protected List<File> selectedFiles = new ArrayList<File>();
59 * The list of progress bars for individual files.
61 protected List<ProgressBar> progressBars = new ArrayList<ProgressBar>();
63 private Button browse;
65 private Button submit;
67 private FlexTable generalTable;
69 private Map<String, FileResource> toRename;
71 protected List<HttpRequest> requests = new ArrayList<HttpRequest>();
73 private boolean canContinue = true;
76 * The widget's constructor.
78 public FileUploadGearsDialog() {
79 // Set the dialog's caption.
80 setText("File upload");
81 setAnimationEnabled(true);
82 // Create a panel to hold all of the dialog widgets.
83 VerticalPanel panel = new VerticalPanel();
84 final HTML info = new HTML("You may select one or more files to upload.");
85 info.addStyleName("pithos-uploadNote");
87 // Add an informative label with the folder name.
88 Object selection = GSS.get().getTreeView().getSelection();
89 folder = ((RestResourceWrapper) selection).getResource();
91 browse = new Button("Browse...");
93 HorizontalPanel fileUploadPanel = new HorizontalPanel();
94 fileUploadPanel.add(browse);
96 generalTable = new FlexTable();
97 generalTable.setText(0, 0, "Folder");
98 generalTable.setText(1, 0, "File");
99 generalTable.setText(0, 1, folder.getName());
100 generalTable.setWidget(1, 1, fileUploadPanel);
101 generalTable.getCellFormatter().setStyleName(0, 0, "props-labels");
102 generalTable.getCellFormatter().setStyleName(1, 0, "props-labels");
103 generalTable.getCellFormatter().setStyleName(0, 1, "props-values");
104 generalTable.getCellFormatter().setStyleName(1, 1, "props-values");
105 generalTable.setCellSpacing(4);
107 panel.add(generalTable);
109 // Create a panel to hold the buttons.
110 HorizontalPanel buttons = new HorizontalPanel();
112 submit = new Button("Upload");
113 submit.addClickHandler(new ClickHandler() {
115 public void onClick(ClickEvent event) {
119 submit.setEnabled(false);
121 buttons.setCellHorizontalAlignment(submit, HasHorizontalAlignment.ALIGN_CENTER);
122 // Create the 'Cancel' button, along with a listener that hides the
123 // dialog when the button is clicked.
124 Button cancel = new Button("Cancel", new ClickHandler() {
126 public void onClick(ClickEvent event) {
129 GSS.get().showFileList(true);
133 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
134 buttons.setSpacing(8);
135 buttons.addStyleName("pithos-DialogBox");
137 browse.addClickHandler(new ClickHandler() {
139 public void onClick(ClickEvent event) {
140 Desktop desktop = factory.createDesktop();
141 desktop.openFiles(new OpenFilesHandler() {
144 public void onOpenFiles(OpenFilesEvent ofevent) {
145 fileObjects = ofevent.getFiles();
146 selectedFiles.addAll(Arrays.asList(fileObjects));
147 for (int i = 0; i< selectedFiles.size(); i++) {
148 generalTable.setText(i+1, 0, "File");
149 generalTable.setText(i+1, 1, selectedFiles.get(i).getName());
150 ProgressBar progress = new ProgressBar(20, 0);
151 generalTable.setWidget(i+1, 2, progress);
152 progressBars.add(progress);
153 generalTable.getCellFormatter().setStyleName(i+1, 0, "props-labels");
154 generalTable.getCellFormatter().setStyleName(i+1, 1, "props-values");
156 submit.setEnabled(true);
163 panel.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
164 panel.addStyleName("pithos-DialogBox");
165 addStyleName("pithos-DialogBox");
170 * Cancels the file upload.
172 private void cancelUpload() {
173 for (HttpRequest request: requests)
179 * Check whether the specified file name exists in the selected folder.
181 private boolean canContinue(File file) {
182 String fileName = getFilename(file.getName());
183 if (getFileForName(fileName) == null)
184 // For file creation, check to see if the file already exists.
185 for (FileResource fileRes : files)
186 if (!fileRes.isDeleted() && fileRes.getName().equals(fileName))
192 public void prepareAndSubmit() {
194 if (selectedFiles.size() == 0) {
195 app.displayError("You must select a file!");
199 for (File file: selectedFiles)
200 if (!canContinue(file)) {
201 app.displayError("The file name " + file.getName() +
202 " already exists in this folder");
206 submit.setEnabled(false);
207 browse.setVisible(false);
208 List<String> toUpdate = new ArrayList<String>();
209 toRename = new HashMap<String, FileResource>();
210 for (File file: selectedFiles) {
211 String fname = getFilename(file.getName());
212 if (getFileForName(fname) == null) {
213 // We are going to create a file, so we check to see if there is a
214 // trashed file with the same name.
215 FileResource same = null;
216 for (FileResource fres : folder.getFiles())
217 if (fres.isDeleted() && fres.getName().equals(fname))
219 // In that case add it to the list of files to rename.
221 toRename.put(getBackupFilename(fname), same);
223 // If we are updating a file add it to the list of files to update.
227 if (!toUpdate.isEmpty()) {
228 StringBuffer sb = new StringBuffer();
229 for (String name: toUpdate)
230 sb.append(name).append("<br/>");
231 // We are going to update existing files, so show a confirmation dialog.
232 ConfirmationDialog confirm = new ConfirmationDialog("Are you sure " +
233 "you want to update the following files?<br/><i>" + sb +
237 public void cancel() {
242 public void confirm() {
253 * Confirm the renames of synonymous files already in the trash.
255 private void confirmRename() {
256 if (!toRename.isEmpty()) {
257 StringBuffer sb = new StringBuffer();
258 for (FileResource file: toRename.values())
259 sb.append(file.getName()).append("<br/>");
260 ConfirmationDialog confirm = new ConfirmationDialog("Files " +
261 "with the following names already exist in the trash. If" +
262 " you continue,<br/>the trashed files will be renamed " +
263 "automatically for you:<br/><i>" + sb + "</i>", "Continue") {
266 public void cancel() {
271 public void confirm() {
272 updateTrashedFiles();
282 * Rename the conflicting trashed files with the supplied new names.
284 private void updateTrashedFiles() {
285 for (final String name: toRename.keySet()) {
286 JSONObject json = new JSONObject();
287 json.put("name", new JSONString(name));
288 PostCommand cf = new PostCommand(toRename.get(name).getUri() + "?update=", json.toString(), 200) {
291 public void onComplete() {
292 toRename.remove(name);
297 public void onError(Throwable t) {
300 if (t instanceof RestException) {
301 int statusCode = ((RestException) t).getHttpStatusCode();
302 if (statusCode == 405)
303 app.displayError("You don't have the necessary permissions");
304 else if (statusCode == 404)
305 app.displayError("User in permissions does not exist");
306 else if (statusCode == 409)
307 app.displayError("A file with the same name already exists");
308 else if (statusCode == 413)
309 app.displayError("Your quota has been exceeded");
311 app.displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());
313 app.displayError("System error modifying file:" + t.getMessage());
317 DeferredCommand.addCommand(cf);
322 * Checks if the renaming step for already trashed files is complete and
323 * starts file uploads.
325 private void uploadFiles() {
326 if (!toRename.isEmpty()) return;
328 doSend(selectedFiles);
333 * Perform the HTTP request to upload the specified file.
335 protected void doSend(final List<File> filesRemaining) {
336 final GSS app = GSS.get();
337 HttpRequest request = factory.createHttpRequest();
338 requests.add(request);
339 String method = "PUT";
342 final String filename = getFilename(filesRemaining.get(0).getName());
343 path = folder.getUri();
344 if (!path.endsWith("/"))
346 path = path + encode(filename);
348 String token = app.getToken();
349 String resource = path.substring(app.getApiPath().length()-1, path.length());
350 String date = RestCommand.getDate();
351 String sig = RestCommand.calculateSig(method, date, resource, RestCommand.base64decode(token));
352 request.open(method, path);
353 request.setRequestHeader("X-GSS-Date", date);
354 request.setRequestHeader("Authorization", app.getCurrentUserResource().getUsername() + " " + sig);
355 request.setRequestHeader("Accept", "application/json; charset=utf-8");
356 request.setCallback(new RequestCallback() {
358 public void onResponseReceived(HttpRequest req) {
359 int state = req.getReadyState();
360 if (state != 4) return;
361 switch(req.getStatus()) {
362 case 201: // Created falls through to updated.
364 filesRemaining.remove(0);
365 if(filesRemaining.isEmpty()){
369 doSend(filesRemaining);
372 SessionExpiredDialog dlg = new SessionExpiredDialog();
376 app.displayError("You don't have permission to " +
377 "upload file " + filename);
380 app.displayError("A folder with the name " + filename +
381 " already exists at this level");
384 app.displayError("There is not enough free space " +
385 "available for uploading " + filename);
388 app.displayError("Error uploading file " + filename +
389 ": " + req.getStatus());
393 request.getUpload().setProgressHandler(new ProgressHandler() {
395 public void onProgress(ProgressEvent event) {
396 double pcnt = (double) event.getLoaded() / event.getTotal();
397 progressBars.get(0).setProgress((int) Math.floor(pcnt * 100));
399 progressBars.remove(0);
402 request.send(filesRemaining.get(0).getBlob());
406 * Perform the final actions after the files are uploaded.
408 protected void finish() {
410 //GSS.get().showFileList(true);
411 GSS.get().getTreeView().updateNode(GSS.get().getTreeView().getSelection());//showFileList(true);
412 GSS.get().getStatusPanel().updateStats();
416 * Same as URL.encode, but also encode apostrophe since browsers aren't
417 * consistent about it (FF encodes, IE does not).
419 protected String encode(String decodedURL) {
420 String retv = decodedURL.replaceAll("@", "_"); // Replace bad character
421 retv = URL.encodeComponent(retv);
422 retv = retv.replaceAll("'", "%27");