Use an exponential backoff strategy for retrying rolled back transactions.
[pithos] / src / gr / ebs / gss / client / FileUploadDialog.java
1 /*\r
2  * Copyright 2007, 2008, 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.GetCommand;\r
22 import gr.ebs.gss.client.rest.PostCommand;\r
23 import gr.ebs.gss.client.rest.RestCommand;\r
24 import gr.ebs.gss.client.rest.RestException;\r
25 import gr.ebs.gss.client.rest.resource.FileResource;\r
26 import gr.ebs.gss.client.rest.resource.FolderResource;\r
27 import gr.ebs.gss.client.rest.resource.UploadStatusResource;\r
28 \r
29 import java.util.ArrayList;\r
30 import java.util.List;\r
31 \r
32 import com.google.gwt.core.client.GWT;\r
33 import com.google.gwt.http.client.URL;\r
34 import com.google.gwt.json.client.JSONObject;\r
35 import com.google.gwt.json.client.JSONString;\r
36 import com.google.gwt.user.client.DeferredCommand;\r
37 import com.google.gwt.user.client.Timer;\r
38 import com.google.gwt.user.client.ui.Button;\r
39 import com.google.gwt.user.client.ui.ClickListener;\r
40 import com.google.gwt.user.client.ui.DialogBox;\r
41 import com.google.gwt.user.client.ui.FileUpload;\r
42 import com.google.gwt.user.client.ui.FormHandler;\r
43 import com.google.gwt.user.client.ui.FormPanel;\r
44 import com.google.gwt.user.client.ui.FormSubmitCompleteEvent;\r
45 import com.google.gwt.user.client.ui.FormSubmitEvent;\r
46 import com.google.gwt.user.client.ui.Grid;\r
47 import com.google.gwt.user.client.ui.HTML;\r
48 import com.google.gwt.user.client.ui.HasHorizontalAlignment;\r
49 import com.google.gwt.user.client.ui.Hidden;\r
50 import com.google.gwt.user.client.ui.HorizontalPanel;\r
51 import com.google.gwt.user.client.ui.KeyboardListener;\r
52 import com.google.gwt.user.client.ui.Label;\r
53 import com.google.gwt.user.client.ui.VerticalPanel;\r
54 import com.google.gwt.user.client.ui.Widget;\r
55 \r
56 /**\r
57  * The 'File upload' dialog box implementation.\r
58  */\r
59 public class FileUploadDialog extends DialogBox implements Updateable {\r
60 \r
61         protected int prgBarInterval = 1500;\r
62 \r
63         private ProgressBar progressBar;\r
64 \r
65         protected RepeatingTimer repeater = new RepeatingTimer(this, prgBarInterval);\r
66 \r
67         public static final boolean DONE = true;\r
68 \r
69         /**\r
70          * The Form element that performs the file upload.\r
71          */\r
72         private final FormPanel form = new FormPanel();\r
73 \r
74         private final FileUpload upload = new FileUpload();\r
75 \r
76         protected final Label filenameLabel = new Label("");\r
77 \r
78         protected List<FileResource> files;\r
79 \r
80         protected boolean cancelEvent = false;\r
81 \r
82         protected String fileNameToUse;\r
83 \r
84         protected FolderResource folder;\r
85 \r
86         /**\r
87          * The widget's constructor.\r
88          */\r
89         public FileUploadDialog() {\r
90                 // Set the dialog's caption.\r
91                 setText("File upload");\r
92                 setAnimationEnabled(true);\r
93                 // Since we're going to add a FileUpload widget, we'll need to set the\r
94                 // form to use the POST method, and multipart MIME encoding.\r
95                 form.setEncoding(FormPanel.ENCODING_MULTIPART);\r
96                 form.setMethod(FormPanel.METHOD_POST);\r
97 \r
98                 // Create a panel to hold all of the form widgets.\r
99                 VerticalPanel panel = new VerticalPanel();\r
100                 form.setWidget(panel);\r
101                 final HTML info = new HTML("You may select a file to upload. Install" +\r
102                                 " <a href='http://gears.google.com/' target='_blank'>Google " +\r
103                                 "Gears</a><br> for uploading multiple files simultaneously.");\r
104                 info.addStyleName("gss-uploadNote");\r
105                 panel.add(info);\r
106                 final Hidden date = new Hidden("Date", "");\r
107                 panel.add(date);\r
108                 final Hidden auth = new Hidden("Authorization", "");\r
109                 panel.add(auth);\r
110                 // Add an informative label with the folder name.\r
111                 Object selection = GSS.get().getFolders().getCurrent().getUserObject();\r
112                 folder = (FolderResource) selection;\r
113                 upload.setName("file");\r
114                 filenameLabel.setText("");\r
115                 filenameLabel.setVisible(false);\r
116                 filenameLabel.setStyleName("props-labels");\r
117                 HorizontalPanel fileUloadPanel = new HorizontalPanel();\r
118                 fileUloadPanel.add(filenameLabel);\r
119                 fileUloadPanel.add(upload);\r
120                 Grid generalTable = new Grid(2, 2);\r
121                 generalTable.setText(0, 0, "Folder");\r
122                 generalTable.setText(1, 0, "File");\r
123                 generalTable.setText(0, 1, folder.getName());\r
124                 generalTable.setWidget(1, 1, fileUloadPanel);\r
125                 generalTable.getCellFormatter().setStyleName(0, 0, "props-labels");\r
126                 generalTable.getCellFormatter().setStyleName(1, 0, "props-labels");\r
127                 generalTable.getCellFormatter().setStyleName(0, 1, "props-values");\r
128                 generalTable.getCellFormatter().setStyleName(1, 1, "props-values");\r
129                 generalTable.setCellSpacing(4);\r
130 \r
131                 panel.add(generalTable);\r
132 \r
133                 // Create a panel to hold the buttons.\r
134                 HorizontalPanel buttons = new HorizontalPanel();\r
135 \r
136                 // Create the 'upload' button, along with a listener that submits the\r
137                 // form.\r
138                 final Button submit = new Button("Upload", new ClickListener() {\r
139                         public void onClick(Widget sender) {\r
140                                 prepareAndSubmit();\r
141                         }\r
142                 });\r
143                 buttons.add(submit);\r
144                 buttons.setCellHorizontalAlignment(submit, HasHorizontalAlignment.ALIGN_CENTER);\r
145                 // Create the 'Cancel' button, along with a listener that hides the\r
146                 // dialog when the button is clicked.\r
147                 final Button cancel = new Button("Cancel", new ClickListener() {\r
148 \r
149                         public void onClick(Widget sender) {\r
150                                 repeater.finish();\r
151                                 hide();\r
152                         }\r
153                 });\r
154                 buttons.add(cancel);\r
155                 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);\r
156                 buttons.setSpacing(8);\r
157                 buttons.addStyleName("gss-DialogBox");\r
158 \r
159                 // Add an event handler to the form.\r
160                 form.addFormHandler(new FormHandler() {\r
161 \r
162                         public void onSubmit(final FormSubmitEvent event) {\r
163                                 GSS app = GSS.get();\r
164                                 // This event is fired just before the form is submitted. We can\r
165                                 // take this opportunity to perform validation.\r
166                                 if (upload.getFilename().length() == 0) {\r
167                                         app.displayError("You must select a file!");\r
168                                         event.setCancelled(true);\r
169                                         hide();\r
170                                 } else {\r
171 \r
172                                         canContinue();\r
173                                         GWT.log("Cancel:" + cancelEvent, null);\r
174                                         if (cancelEvent) {\r
175                                                 cancelEvent = false;\r
176                                                 app.displayError("The specified file name already exists in this folder");\r
177                                                 event.setCancelled(true);\r
178                                                 hide();\r
179                                         } else {\r
180 \r
181                                                 fileNameToUse = getFilename(upload.getFilename());\r
182                                                 String apath;\r
183                                                 FileResource selectedFile = getFileForName(fileNameToUse);\r
184                                                 if (selectedFile == null ) {\r
185                                                         //we are going to create a file\r
186                                                         apath = folder.getUri();\r
187                                                         if (!apath.endsWith("/"))\r
188                                                                 apath = apath + "/";\r
189                                                         apath = apath + encodeComponent(fileNameToUse);\r
190                                                 } else\r
191                                                         apath = selectedFile.getUri();\r
192                                                 form.setAction(apath);\r
193                                                 String dateString = RestCommand.getDate();\r
194                                                 String resource = apath.substring(app.getApiPath().length() - 1, apath.length());\r
195                                                 String sig = RestCommand.calculateSig("POST", dateString, resource, RestCommand.base64decode(app.getToken()));\r
196                                                 date.setValue(dateString);\r
197                                                 auth.setValue(app.getCurrentUserResource().getUsername() + " " + sig);\r
198                                                 GWT.log("FolderPATH:" + folder.getUri(), null);\r
199                                                 submit.setEnabled(false);\r
200                                                 upload.setVisible(false);\r
201                                                 filenameLabel.setText(fileNameToUse);\r
202                                                 filenameLabel.setVisible(true);\r
203                                                 repeater.start();\r
204                                                 progressBar.setVisible(true);\r
205                                         }\r
206                                 }\r
207                         }\r
208 \r
209                         public void onSubmitComplete(final FormSubmitCompleteEvent event) {\r
210                                 // When the form submission is successfully completed, this\r
211                                 // event is fired. Assuming the service returned a response\r
212                                 // of type text/html, we can get the result text here (see\r
213                                 // the FormPanel documentation for further explanation).\r
214                                 String results = event.getResults();\r
215 \r
216                                 // Unfortunately the results are never empty, even in\r
217                                 // the absense of errors, so we have to check for '<pre></pre>'.\r
218                                 if (!results.equalsIgnoreCase("<pre></pre>")) {\r
219                                         GWT.log(results, null);\r
220                                         GSS.get().displayError(results);\r
221                                 }\r
222                                 progressBar.setProgress(100);\r
223                                 repeater.finish();\r
224                                 hide();\r
225                                 GSS.get().showFileList(true);\r
226                                 GSS.get().getStatusPanel().updateStats();\r
227                         }\r
228                 });\r
229 \r
230                 panel.add(buttons);\r
231                 progressBar = new ProgressBar(50, ProgressBar.SHOW_TIME_REMAINING);\r
232                 panel.add(progressBar);\r
233                 progressBar.setVisible(false);\r
234                 panel.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);\r
235                 panel.setCellHorizontalAlignment(progressBar, HasHorizontalAlignment.ALIGN_CENTER);\r
236                 panel.addStyleName("gss-DialogBox");\r
237                 addStyleName("gss-DialogBox");\r
238                 setWidget(form);\r
239         }\r
240 \r
241         @Override\r
242         public boolean onKeyDownPreview(char key, int modifiers) {\r
243                 // Use the popup's key preview hooks to close the dialog when either\r
244                 // enter or escape is pressed.\r
245                 switch (key) {\r
246                         case KeyboardListener.KEY_ENTER:\r
247                                 prepareAndSubmit();\r
248                                 break;\r
249                         case KeyboardListener.KEY_ESCAPE:\r
250                                 repeater.finish();\r
251                                 hide();\r
252                                 break;\r
253                 }\r
254 \r
255                 return true;\r
256         }\r
257 \r
258         /**\r
259          * Make any last minute checks and start the upload.\r
260          */\r
261         public void prepareAndSubmit() {\r
262                 final String fname = getFilename(upload.getFilename());\r
263                 if (getFileForName(fname) == null) {\r
264                         //we are going to create a file, so we check to see if there is a trashed file with the same name\r
265                         FileResource same = null;\r
266                         for (FileResource fres : folder.getFiles())\r
267                                 if (fres.isDeleted() && fres.getName().equals(fname))\r
268                                         same = fres;\r
269                         if (same == null)\r
270                                 form.submit();\r
271                         else {\r
272                                 final FileResource sameFile = same;\r
273                                 GWT.log("Same deleted file", null);\r
274                                 ConfirmationDialog confirm = new ConfirmationDialog("A file with " +\r
275                                                 "the same name exists in the trash. If you continue,<br/>the trashed " +\r
276                                                 "file  '" + fname + "' will be renamed automatically for you.", "Continue") {\r
277 \r
278                                         @Override\r
279                                         public void cancel() {\r
280                                                 FileUploadDialog.this.hide();\r
281                                         }\r
282 \r
283                                         @Override\r
284                                         public void confirm() {\r
285                                                 updateTrashedFile(getBackupFilename(fname), sameFile);\r
286                                         }\r
287 \r
288                                 };\r
289                                 confirm.center();\r
290                         }\r
291                 }\r
292                 else {\r
293                         // We are going to update an existing file, so show a confirmation dialog.\r
294                         ConfirmationDialog confirm = new ConfirmationDialog("Are you sure " +\r
295                                         "you want to update " + fname + "?", "Update") {\r
296 \r
297                                 @Override\r
298                                 public void cancel() {\r
299                                         FileUploadDialog.this.hide();\r
300                                 }\r
301 \r
302                                 @Override\r
303                                 public void confirm() {\r
304                                         form.submit();\r
305                                 }\r
306 \r
307                         };\r
308                         confirm.center();\r
309                 }\r
310         }\r
311 \r
312         /**\r
313          * Returns the file name from a potential full path argument. Apparently IE\r
314          * insists on sending the full path name of a file when uploading, forcing\r
315          * us to trim the extra path info. Since this is only observed on Windows we\r
316          * get to check for a single path separator value.\r
317          *\r
318          * @param name the potentially full path name of a file\r
319          * @return the file name without extra path information\r
320          */\r
321         protected String getFilename(String name) {\r
322                 int pathSepIndex = name.lastIndexOf("\\");\r
323                 if (pathSepIndex == -1) {\r
324                         pathSepIndex = name.lastIndexOf("/");\r
325                         if (pathSepIndex == -1)\r
326                                 return name;\r
327                 }\r
328                 return name.substring(pathSepIndex + 1);\r
329         }\r
330 \r
331         /**\r
332          * Check whether the file name exists in selected folder.\r
333          *\r
334          * @return\r
335          */\r
336         private boolean canContinue() {\r
337                 if (files == null)\r
338                         return false;\r
339                 String fileName = getFilename(upload.getFilename());\r
340                 if (getFileForName(fileName) == null) {\r
341                         // For file creation, check to see if the file already exists.\r
342                         GWT.log("filename to upload:" + fileName, null);\r
343                         for (FileResource dto : files) {\r
344                                 GWT.log("Check:" + dto.getName() + "/" + fileName, null);\r
345                                 if (!dto.isDeleted() && dto.getName().equals(fileName)) {\r
346                                         cancelEvent = true;\r
347                                         return true;\r
348                                 }\r
349                         }\r
350                 }\r
351                 return true;\r
352         }\r
353 \r
354         class RepeatingTimer extends Timer {\r
355 \r
356                 private Updateable updateable;\r
357 \r
358                 private int interval = 1500;\r
359 \r
360                 private boolean running = true;\r
361 \r
362                 RepeatingTimer(Updateable _updateable, int _interval) {\r
363                         updateable = _updateable;\r
364                         interval = _interval;\r
365                 }\r
366 \r
367                 @Override\r
368                 public void run() {\r
369                         updateable.update();\r
370                 }\r
371 \r
372                 public void start() {\r
373                         running = true;\r
374 \r
375                         scheduleRepeating(interval);\r
376                 }\r
377 \r
378                 public void finish() {\r
379                         running = false;\r
380                         cancel();\r
381                 }\r
382 \r
383                 public int getInterval() {\r
384                         return interval;\r
385                 }\r
386 \r
387                 public void setInterval(int anInterval) {\r
388                         if (interval != anInterval) {\r
389                                 interval = anInterval;\r
390                                 if (running) {\r
391                                         finish();\r
392                                         start();\r
393                                 }\r
394                         }\r
395                 }\r
396         }\r
397 \r
398         /* (non-Javadoc)\r
399          * @see gr.ebs.gss.client.Updateable#update()\r
400          */\r
401         public void update() {\r
402                 String apath = folder.getUri();\r
403                 if (!apath.endsWith("/"))\r
404                         apath = apath + "/";\r
405                 apath = apath + encodeComponent(fileNameToUse) + "?progress=" + encodeComponent(fileNameToUse);\r
406                 GetCommand eg = new GetCommand<UploadStatusResource>(UploadStatusResource.class, apath, false) {\r
407 \r
408                         @Override\r
409                         public void onComplete() {\r
410                                 UploadStatusResource res = getResult();\r
411                                 progressBar.setProgress(res.percent());\r
412                         }\r
413 \r
414                         @Override\r
415                         public void onError(Throwable t) {\r
416                                 GWT.log("", t);\r
417                         }\r
418 \r
419                 };\r
420                 DeferredCommand.addCommand(eg);\r
421         }\r
422 \r
423         protected String getBackupFilename(String filename) {\r
424                 List<FileResource> filesInSameFolder = new ArrayList<FileResource>();\r
425                 for (FileResource deleted : folder.getFiles())\r
426                         if (deleted.isDeleted())\r
427                                 filesInSameFolder.add(deleted);\r
428                 int i = 1;\r
429                 for (FileResource same : filesInSameFolder)\r
430                         if (same.getName().startsWith(filename)) {\r
431                                 String toCheck = same.getName().substring(filename.length(), same.getName().length());\r
432                                 if (toCheck.startsWith(" ")) {\r
433                                         int test = -1;\r
434                                         try {\r
435                                                 test = Integer.valueOf(toCheck.replace(" ", ""));\r
436                                         } catch (NumberFormatException e) {\r
437                                                 // Do nothing since string is not a number.\r
438                                         }\r
439                                         if (test >= i)\r
440                                                 i = test + 1;\r
441                                 }\r
442                         }\r
443 \r
444                 return filename + " " + i;\r
445         }\r
446 \r
447         /**\r
448          * Rename the conflicting trashed file with the supplied new name.\r
449          */\r
450         private void updateTrashedFile(String newName, FileResource trashedFile) {\r
451                 JSONObject json = new JSONObject();\r
452                 json.put("name", new JSONString(newName));\r
453                 PostCommand cf = new PostCommand(trashedFile.getUri() + "?update=", json.toString(), 200) {\r
454 \r
455                         @Override\r
456                         public void onComplete() {\r
457                                 form.submit();\r
458                         }\r
459 \r
460                         @Override\r
461                         public void onError(Throwable t) {\r
462                                 GSS app = GSS.get();\r
463                                 GWT.log("", t);\r
464                                 if (t instanceof RestException) {\r
465                                         int statusCode = ((RestException) t).getHttpStatusCode();\r
466                                         if (statusCode == 405)\r
467                                                 app.displayError("You don't have the necessary permissions");\r
468                                         else if (statusCode == 404)\r
469                                                 app.displayError("User in permissions does not exist");\r
470                                         else if (statusCode == 409)\r
471                                                 app.displayError("A file with the same name already exists");\r
472                                         else if (statusCode == 413)\r
473                                                 app.displayError("Your quota has been exceeded");\r
474                                         else\r
475                                                 app.displayError("Unable to modify file:" + ((RestException) t).getHttpStatusText());\r
476                                 } else\r
477                                         app.displayError("System error modifying file:" + t.getMessage());\r
478                         }\r
479 \r
480                 };\r
481                 DeferredCommand.addCommand(cf);\r
482         }\r
483 \r
484         protected FileResource getFileForName(String name){\r
485                 for (FileResource f : folder.getFiles())\r
486                         if (!f.isDeleted() && f.getName().equals(name))\r
487                                 return f;\r
488                 return null;\r
489         }\r
490 \r
491 \r
492         /**\r
493          * Same as URL.encodeComponent, but also\r
494          * encode apostrophe since browsers aren't consistent about it\r
495          * (FF encodes, IE does not).\r
496          *\r
497          * @param decodedURLComponent\r
498          * @return\r
499          */\r
500         protected String encodeComponent(String decodedURLComponent) {\r
501                 String retv = URL.encodeComponent(decodedURLComponent);\r
502                 retv = retv.replaceAll("'", "%27");\r
503                 return retv;\r
504         }\r
505 \r
506         /**\r
507          * Modify the files.\r
508          *\r
509          * @param newFiles the files to set\r
510          */\r
511         public void setFiles(List<FileResource> newFiles) {\r
512                 files = newFiles;\r
513         }\r
514 }\r