Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / Pithos.java @ efede616

History | View | Annotate | Download (44.3 kB)

1
/*
2
 * Copyright 2011-2012 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *
12
 *   2. Redistributions in binary form must reproduce the above
13
 *      copyright notice, this list of conditions and the following
14
 *      disclaimer in the documentation and/or other materials
15
 *      provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
 * POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * The views and conclusions contained in the software and
31
 * documentation are those of the authors and should not be
32
 * interpreted as representing official policies, either expressed
33
 * or implied, of GRNET S.A.
34
 */
35
package gr.grnet.pithos.web.client;
36

    
37
import gr.grnet.pithos.web.client.commands.UploadFileCommand;
38
import gr.grnet.pithos.web.client.foldertree.AccountResource;
39
import gr.grnet.pithos.web.client.foldertree.File;
40
import gr.grnet.pithos.web.client.foldertree.Folder;
41
import gr.grnet.pithos.web.client.foldertree.FolderTreeView;
42
import gr.grnet.pithos.web.client.foldertree.FolderTreeViewModel;
43
import gr.grnet.pithos.web.client.foldertree.Resource;
44
import gr.grnet.pithos.web.client.grouptree.Group;
45
import gr.grnet.pithos.web.client.grouptree.GroupTreeView;
46
import gr.grnet.pithos.web.client.grouptree.GroupTreeViewModel;
47
import gr.grnet.pithos.web.client.mysharedtree.MysharedTreeView;
48
import gr.grnet.pithos.web.client.mysharedtree.MysharedTreeViewModel;
49
import gr.grnet.pithos.web.client.othersharedtree.OtherSharedTreeView;
50
import gr.grnet.pithos.web.client.othersharedtree.OtherSharedTreeViewModel;
51
import gr.grnet.pithos.web.client.rest.DeleteRequest;
52
import gr.grnet.pithos.web.client.rest.GetRequest;
53
import gr.grnet.pithos.web.client.rest.HeadRequest;
54
import gr.grnet.pithos.web.client.rest.PutRequest;
55
import gr.grnet.pithos.web.client.rest.RestException;
56
import gr.grnet.pithos.web.client.tagtree.Tag;
57

    
58
import java.util.ArrayList;
59
import java.util.HashMap;
60
import java.util.Iterator;
61
import java.util.List;
62
import java.util.Set;
63

    
64
import com.google.gwt.core.client.EntryPoint;
65
import com.google.gwt.core.client.GWT;
66
import com.google.gwt.core.client.Scheduler;
67
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
68
import com.google.gwt.event.dom.client.ClickEvent;
69
import com.google.gwt.event.dom.client.ClickHandler;
70
import com.google.gwt.event.logical.shared.ResizeEvent;
71
import com.google.gwt.event.logical.shared.ResizeHandler;
72
import com.google.gwt.http.client.Request;
73
import com.google.gwt.http.client.RequestBuilder;
74
import com.google.gwt.http.client.RequestCallback;
75
import com.google.gwt.http.client.RequestException;
76
import com.google.gwt.http.client.Response;
77
import com.google.gwt.http.client.URL;
78
import com.google.gwt.i18n.client.Dictionary;
79
import com.google.gwt.i18n.client.NumberFormat;
80
import com.google.gwt.json.client.JSONArray;
81
import com.google.gwt.json.client.JSONObject;
82
import com.google.gwt.json.client.JSONParser;
83
import com.google.gwt.json.client.JSONString;
84
import com.google.gwt.json.client.JSONValue;
85
import com.google.gwt.resources.client.ImageResource;
86
import com.google.gwt.resources.client.ImageResource.ImageOptions;
87
import com.google.gwt.user.client.Command;
88
import com.google.gwt.user.client.Cookies;
89
import com.google.gwt.user.client.Event;
90
import com.google.gwt.user.client.History;
91
import com.google.gwt.user.client.Window;
92
import com.google.gwt.user.client.ui.AbstractImagePrototype;
93
import com.google.gwt.user.client.ui.Button;
94
import com.google.gwt.user.client.ui.Composite;
95
import com.google.gwt.user.client.ui.HTML;
96
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
97
import com.google.gwt.user.client.ui.HasVerticalAlignment;
98
import com.google.gwt.user.client.ui.HorizontalPanel;
99
import com.google.gwt.user.client.ui.HorizontalSplitPanel;
100
import com.google.gwt.user.client.ui.RootPanel;
101
import com.google.gwt.user.client.ui.VerticalPanel;
102
import com.google.gwt.view.client.SelectionChangeEvent;
103
import com.google.gwt.view.client.SelectionChangeEvent.Handler;
104
import com.google.gwt.view.client.SingleSelectionModel;
105

    
106
/**
107
 * Entry point classes define <code>onModuleLoad()</code>.
108
 */
109
public class Pithos implements EntryPoint, ResizeHandler {
110

    
111
        public static final String HOME_CONTAINER = "pithos";
112

    
113
        public static final String TRASH_CONTAINER = "trash";
114

    
115
        public static final Configuration config = GWT.create(Configuration.class);
116
        
117
        /**
118
         * Instantiate an application-level image bundle. This object will provide
119
         * programmatic access to all the images needed by widgets.
120
         */
121
        static Images images = (Images) GWT.create(Images.class);
122

    
123
    public String getUsername() {
124
        return username;
125
    }
126

    
127
    public void setAccount(AccountResource acct) {
128
        account = acct;
129
    }
130

    
131
    public AccountResource getAccount() {
132
        return account;
133
    }
134

    
135
    public void updateFolder(Folder f, boolean showfiles, Command callback) {
136
        folderTreeView.updateFolder(f, showfiles, callback);
137
    }
138

    
139
    public void updateGroupNode(Group group) {
140
        groupTreeView.updateGroupNode(group);
141
    }
142

    
143
    public void updateMySharedRoot() {
144
            mysharedTreeView.updateRoot();
145
    }
146
    
147
    public void updateSharedFolder(Folder f, boolean showfiles) {
148
            mysharedTreeView.updateFolder(f, showfiles);
149
    }
150
    
151
    public void updateOtherSharedFolder(Folder f, boolean showfiles) {
152
            otherSharedTreeView.updateFolder(f, showfiles);
153
    }
154

    
155
    public MysharedTreeView getMySharedTreeView() {
156
        return mysharedTreeView;
157
    }
158

    
159
    /**
160
         * An aggregate image bundle that pulls together all the images for this
161
         * application into a single bundle.
162
         */
163
        public interface Images extends TopPanel.Images, FileList.Images, ToolsMenu.Images {
164

    
165
                @Source("gr/grnet/pithos/resources/document.png")
166
                ImageResource folders();
167

    
168
                @Source("gr/grnet/pithos/resources/advancedsettings.png")
169
                @ImageOptions(width=32, height=32)
170
                ImageResource tools();
171
        }
172

    
173
        private Throwable error;
174
        
175
        /**
176
         * The Application Clipboard implementation;
177
         */
178
        private Clipboard clipboard = new Clipboard();
179

    
180
        /**
181
         * The top panel that contains the menu bar.
182
         */
183
        private TopPanel topPanel;
184

    
185
        /**
186
         * The panel that contains the various system messages.
187
         */
188
        private MessagePanel messagePanel = new MessagePanel(this, Pithos.images);
189

    
190
        /**
191
         * The bottom panel that contains the status bar.
192
         */
193
        private StatusPanel statusPanel = null;
194

    
195
        /**
196
         * The file list widget.
197
         */
198
        private FileList fileList;
199

    
200
        /**
201
         * The tab panel that occupies the right side of the screen.
202
         */
203
        private VerticalPanel inner = new VerticalPanel();
204

    
205

    
206
        /**
207
         * The split panel that will contain the left and right panels.
208
         */
209
        private HorizontalSplitPanel splitPanel = new HorizontalSplitPanel();
210

    
211
        /**
212
         * The currently selected item in the application, for use by the Edit menu
213
         * commands. Potential types are Folder, File, User and Group.
214
         */
215
        private Object currentSelection;
216

    
217
        public HashMap<String, String> userFullNameMap = new HashMap<String, String>();
218

    
219
    private String username = null;
220

    
221
    /**
222
     * The authentication token of the current user.
223
     */
224
    private String token;
225

    
226
    VerticalPanel trees;
227
    
228
    SingleSelectionModel<Folder> folderTreeSelectionModel;
229
    FolderTreeViewModel folderTreeViewModel;
230
    FolderTreeView folderTreeView;
231

    
232
    SingleSelectionModel<Folder> mysharedTreeSelectionModel;
233
    MysharedTreeViewModel mysharedTreeViewModel;
234
    MysharedTreeView mysharedTreeView = null;;
235

    
236
    protected SingleSelectionModel<Folder> otherSharedTreeSelectionModel;
237
    OtherSharedTreeViewModel otherSharedTreeViewModel;
238
    OtherSharedTreeView otherSharedTreeView = null;
239

    
240
    GroupTreeViewModel groupTreeViewModel;
241
    private GroupTreeView groupTreeView;
242

    
243
    TreeView selectedTree;
244
    protected AccountResource account;
245
    
246
    Folder trash;
247
    
248
    List<Composite> treeViews = new ArrayList<Composite>();
249

    
250
    @SuppressWarnings("rawtypes") List<SingleSelectionModel> selectionModels = new ArrayList<SingleSelectionModel>();
251
    
252
    Button upload;
253
    
254
    private HTML usedBytes;
255
    
256
    private HTML totalBytes;
257
    
258
    private HTML usedPercent;
259
    
260
    private HTML numOfFiles;
261
    
262
    private Toolbar toolbar;
263
    
264
    private FileUploadDialog fileUploadDialog;
265
    
266
        @Override
267
        public void onModuleLoad() {
268
                if (parseUserCredentials())
269
            initialize();
270
        }
271

    
272
    private void initialize() {
273
            boolean bareContent = Window.Location.getParameter("noframe") != null;
274
            String contentWidth = bareContent ? "100%" : "75%";
275

    
276
            VerticalPanel outer = new VerticalPanel();
277
        outer.setWidth("100%");
278
            if (!bareContent) {
279
                    outer.addStyleName("pithos-outer");
280
            }
281

    
282
        if (!bareContent) {
283
                topPanel = new TopPanel(this, Pithos.images);
284
                topPanel.setWidth("100%");
285
                outer.add(topPanel);
286
                outer.setCellHorizontalAlignment(topPanel, HasHorizontalAlignment.ALIGN_CENTER);
287
        }
288
        
289
        messagePanel.setVisible(false);
290
        outer.add(messagePanel);
291
        outer.setCellHorizontalAlignment(messagePanel, HasHorizontalAlignment.ALIGN_CENTER);
292
        outer.setCellVerticalAlignment(messagePanel, HasVerticalAlignment.ALIGN_MIDDLE);
293

    
294
        HorizontalPanel header = new HorizontalPanel();
295
        header.addStyleName("pithos-header");
296
        header.setWidth(contentWidth);
297
        if (bareContent)
298
                header.addStyleName("pithos-header-noframe");
299
        upload = new Button("Upload", new ClickHandler() {
300
            @Override
301
            public void onClick(ClickEvent event) {
302
                    if (getSelection() != null)
303
                            new UploadFileCommand(Pithos.this, null, getSelection()).execute();
304
            }
305
        });
306
        upload.addStyleName("pithos-uploadButton");
307
        header.add(upload);
308
        header.setCellHorizontalAlignment(upload, HasHorizontalAlignment.ALIGN_LEFT);
309
        header.setCellVerticalAlignment(upload, HasVerticalAlignment.ALIGN_MIDDLE);
310

    
311
        toolbar = new Toolbar(this);
312
        header.add(toolbar);
313
        header.setCellHorizontalAlignment(toolbar, HasHorizontalAlignment.ALIGN_CENTER);
314
        header.setCellVerticalAlignment(toolbar, HasVerticalAlignment.ALIGN_MIDDLE);
315
        
316
        HorizontalPanel folderStatistics = new HorizontalPanel();
317
        folderStatistics.addStyleName("pithos-folderStatistics");
318
        numOfFiles = new HTML();
319
        folderStatistics.add(numOfFiles);
320
        folderStatistics.setCellVerticalAlignment(numOfFiles, HasVerticalAlignment.ALIGN_MIDDLE);
321
        HTML numOfFilesLabel = new HTML("&nbsp;Files");
322
        folderStatistics.add(numOfFilesLabel);
323
        folderStatistics.setCellVerticalAlignment(numOfFilesLabel, HasVerticalAlignment.ALIGN_MIDDLE);
324
        header.add(folderStatistics);
325
        header.setCellHorizontalAlignment(folderStatistics, HasHorizontalAlignment.ALIGN_RIGHT);
326
        header.setCellVerticalAlignment(folderStatistics, HasVerticalAlignment.ALIGN_MIDDLE);
327
        header.setCellWidth(folderStatistics, "40px");
328
        outer.add(header);
329
        outer.setCellHorizontalAlignment(header, HasHorizontalAlignment.ALIGN_CENTER);
330
        // Inner contains the various lists.nner
331
        inner.sinkEvents(Event.ONCONTEXTMENU);
332
        inner.setWidth("100%");
333

    
334
        folderTreeSelectionModel = new SingleSelectionModel<Folder>();
335
        folderTreeSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
336
            @Override
337
            public void onSelectionChange(SelectionChangeEvent event) {
338
                if (folderTreeSelectionModel.getSelectedObject() != null) {
339
                    deselectOthers(folderTreeView, folderTreeSelectionModel);
340
                    applyPermissions(folderTreeSelectionModel.getSelectedObject());
341
                    Folder f = folderTreeSelectionModel.getSelectedObject();
342
                            updateFolder(f, true, new Command() {
343
                                    
344
                                    @Override
345
                                    public void execute() {
346
                                            updateStatistics();
347
                                    }
348
                            });
349
                }
350
                showRelevantToolbarButtons();
351
            }
352
        });
353
        selectionModels.add(folderTreeSelectionModel);
354

    
355
        folderTreeViewModel = new FolderTreeViewModel(this, folderTreeSelectionModel);
356
        folderTreeView = new FolderTreeView(folderTreeViewModel);
357
        treeViews.add(folderTreeView);
358
        
359
        fileList = new FileList(this, images, folderTreeView);
360
        inner.add(fileList);
361

    
362
        groupTreeViewModel = new GroupTreeViewModel(this);
363
        groupTreeView = new GroupTreeView(groupTreeViewModel);
364
        treeViews.add(groupTreeView);
365
        
366
        trees = new VerticalPanel();
367
        trees.setWidth("100%");
368

    
369
        
370
        HorizontalPanel treeHeader = new HorizontalPanel();
371
        treeHeader.addStyleName("pithos-treeHeader");
372
        treeHeader.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
373
        treeHeader.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
374
        HorizontalPanel statistics = new HorizontalPanel();
375
        statistics.addStyleName("pithos-statistics");
376
        statistics.add(new HTML("Used:&nbsp;"));
377
        usedBytes = new HTML();
378
        statistics.add(usedBytes);
379
        statistics.add(new HTML("&nbsp;of&nbsp;"));
380
        totalBytes = new HTML();
381
        statistics.add(totalBytes);
382
        statistics.add(new HTML("&nbsp;("));
383
        usedPercent = new HTML();
384
        statistics.add(usedPercent);
385
        statistics.add(new HTML(")"));
386
        treeHeader.add(statistics);
387
        treeHeader.setCellHorizontalAlignment(statistics, HasHorizontalAlignment.ALIGN_LEFT);
388
        trees.add(treeHeader);
389

    
390
        trees.add(folderTreeView);
391
        trees.add(groupTreeView);
392
        // Add the left and right panels to the split panel.
393
        splitPanel.setLeftWidget(trees);
394
        splitPanel.setRightWidget(inner);
395
        splitPanel.setSplitPosition("35%");
396
        splitPanel.setSize("100%", "100%");
397
        splitPanel.addStyleName("pithos-splitPanel");
398
        splitPanel.setWidth(contentWidth);
399
        outer.add(splitPanel);
400
        outer.setCellHorizontalAlignment(splitPanel, HasHorizontalAlignment.ALIGN_CENTER);
401

    
402
        if (!bareContent) {
403
                statusPanel = new StatusPanel();
404
                statusPanel.setWidth("100%");
405
                outer.add(statusPanel);
406
                outer.setCellHorizontalAlignment(statusPanel, HasHorizontalAlignment.ALIGN_CENTER);
407
        }
408
        else
409
                splitPanel.addStyleName("pithos-splitPanel-noframe");
410

    
411
        // Hook the window resize event, so that we can adjust the UI.
412
        Window.addResizeHandler(this);
413
        // Clear out the window's built-in margin, because we want to take
414
        // advantage of the entire client area.
415
        Window.setMargin("0px");
416
        // Finally, add the outer panel to the RootPanel, so that it will be
417
        // displayed.
418
        RootPanel.get().add(outer);
419
        // Call the window resized handler to get the initial sizes setup. Doing
420
        // this in a deferred command causes it to occur after all widgets'
421
        // sizes have been computed by the browser.
422
        Scheduler.get().scheduleDeferred(new ScheduledCommand() {
423

    
424
            @Override
425
            public void execute() {
426
                onWindowResized(Window.getClientHeight());
427
            }
428
        });
429

    
430
        Scheduler.get().scheduleDeferred(new ScheduledCommand() {
431
            @Override
432
            public void execute() {
433
                fetchAccount(new Command() {
434
                                        
435
                                        @Override
436
                                        public void execute() {
437
                                if (!account.hasHomeContainer())
438
                                    createHomeContainer(account, this);
439
                                else if (!account.hasTrashContainer())
440
                                        createTrashContainer(this);
441
                                else {
442
                                        for (Folder f : account.getContainers())
443
                                                if (f.getName().equals(Pithos.TRASH_CONTAINER)) {
444
                                                        trash = f;
445
                                                        break;
446
                                                }
447
                                    folderTreeViewModel.initialize(account, new Command() {
448
                                                                
449
                                                                @Override
450
                                                                public void execute() {
451
                                                    createMySharedTree();
452
                                                                }
453
                                                        });
454
                                    groupTreeViewModel.initialize();
455
                                    showStatistics();
456
                                }
457
                                        }
458
                                });
459
            }
460
        });
461
        
462
//        Scheduler.get().scheduleDeferred(new Command() {
463
//                        
464
//                        @Override
465
//                        public void execute() {
466
//                                displayError("lalala");
467
//                                
468
//                        }
469
//                });
470
    }
471

    
472
    public void applyPermissions(Folder f) {
473
            if (f != null) {
474
                    if (f.isInTrash())
475
                            upload.setEnabled(false);
476
                    else {
477
                            Boolean[] perms = f.getPermissions().get(username);
478
                            if (f.getOwner().equals(username) || (perms != null && perms[1] != null && perms[1])) {
479
                                    upload.setEnabled(true);
480
                            }
481
                            else
482
                                    upload.setEnabled(false);
483
                    }
484
            }
485
            else
486
                    upload.setEnabled(false);
487
        }
488

    
489
        @SuppressWarnings({ "rawtypes", "unchecked" })
490
        public void deselectOthers(TreeView _selectedTree, SingleSelectionModel model) {
491
            selectedTree = _selectedTree;
492
            
493
            for (Composite c : treeViews)
494
                    if (c.equals(selectedTree))
495
                            c.addStyleName("cellTreeWidget-selectedTree");
496
                    else
497
                            c.removeStyleName("cellTreeWidget-selectedTree");
498
            
499
        for (SingleSelectionModel s : selectionModels)
500
            if (!s.equals(model))
501
                s.setSelected(s.getSelectedObject(), false);
502
    }
503

    
504
    public void showFiles(final Folder f) {
505
        Set<File> files = f.getFiles();
506
        showFiles(files);
507
    }
508

    
509
    public void showFiles(Set<File> files) {
510
        fileList.setFiles(new ArrayList<File>(files));
511
    }
512

    
513
    /**
514
         * Parse and store the user credentials to the appropriate fields.
515
         */
516
        private boolean parseUserCredentials() {
517
        username = Window.Location.getParameter("user");
518
        token = Window.Location.getParameter("token");
519
        Configuration conf = (Configuration) GWT.create(Configuration.class);
520
                Dictionary otherProperties = Dictionary.getDictionary("otherProperties");
521
        if (username == null || username.length() == 0 || token == null || token.length() == 0) {
522
            String cookie = otherProperties.get("authCookie");
523
            String auth = Cookies.getCookie(cookie);
524
            if (auth == null) {
525
                authenticateUser();
526
                return false;
527
            }
528
            if (auth.startsWith("\""))
529
                    auth = auth.substring(1);
530
            if (auth.endsWith("\""))
531
                    auth = auth.substring(0, auth.length() - 1);
532
                        String[] authSplit = auth.split("\\" + conf.cookieSeparator(), 2);
533
                        if (authSplit.length != 2) {
534
                            authenticateUser();
535
                            return false;
536
                        }
537
                        username = authSplit[0];
538
                        token = authSplit[1];
539
        }
540
        else
541
                Cookies.setCookie(otherProperties.get("authCookie"), username + conf.cookieSeparator() + token, null, "", "/", false);
542

    
543
        String gotoUrl = Window.Location.getParameter("goto");
544
                if (gotoUrl != null && gotoUrl.length() > 0) {
545
                        Window.Location.assign(gotoUrl + "?X-Auth-Token=" + token);
546
                        return false;
547
                }
548
                return true;
549
    }
550

    
551
    /**
552
         * Redirect the user to the login page for authentication.
553
         */
554
        protected void authenticateUser() {
555
                Dictionary otherProperties = Dictionary.getDictionary("otherProperties");
556
        Window.Location.assign(otherProperties.get("loginUrl") + Window.Location.getHref());
557
        }
558

    
559
        protected void fetchAccount(final Command callback) {
560
        String path = "?format=json";
561

    
562
        GetRequest<AccountResource> getAccount = new GetRequest<AccountResource>(AccountResource.class, getApiPath(), username, path) {
563
            @Override
564
            public void onSuccess(AccountResource _result) {
565
                account = _result;
566
                if (callback != null)
567
                        callback.execute();
568
            }
569

    
570
            @Override
571
            public void onError(Throwable t) {
572
                GWT.log("Error getting account", t);
573
                                setError(t);
574
                if (t instanceof RestException)
575
                    displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
576
                else
577
                    displayError("System error fetching user data: " + t.getMessage());
578
            }
579

    
580
                        @Override
581
                        protected void onUnauthorized(Response response) {
582
                                sessionExpired();
583
                        }
584
        };
585
        getAccount.setHeader("X-Auth-Token", token);
586
        Scheduler.get().scheduleDeferred(getAccount);
587
    }
588

    
589
    public void updateStatistics() {
590
            HeadRequest<AccountResource> headAccount = new HeadRequest<AccountResource>(AccountResource.class, getApiPath(), username, "", account) {
591

    
592
                        @Override
593
                        public void onSuccess(AccountResource _result) {
594
                                showStatistics();
595
                        }
596

    
597
                        @Override
598
                        public void onError(Throwable t) {
599
                GWT.log("Error getting account", t);
600
                                setError(t);
601
                if (t instanceof RestException)
602
                    displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
603
                else
604
                    displayError("System error fetching user data: " + t.getMessage());
605
                        }
606

    
607
                        @Override
608
                        protected void onUnauthorized(Response response) {
609
                                sessionExpired();
610
                        }
611
                };
612
                headAccount.setHeader("X-Auth-Token", token);
613
                Scheduler.get().scheduleDeferred(headAccount);
614
        }
615

    
616
        protected void showStatistics() {
617
            usedBytes.setHTML(String.valueOf(account.getFileSizeAsString()));
618
            totalBytes.setHTML(String.valueOf(account.getQuotaAsString()));
619
            NumberFormat nf = NumberFormat.getPercentFormat();
620
            usedPercent.setHTML(nf.format(account.getUsedPercentage()));
621
        }
622

    
623
        protected void createHomeContainer(final AccountResource _account, final Command callback) {
624
        String path = "/" + Pithos.HOME_CONTAINER;
625
        PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
626
            @Override
627
            public void onSuccess(Resource result) {
628
                    if (!_account.hasTrashContainer())
629
                            createTrashContainer(callback);
630
                    else
631
                            fetchAccount(callback);
632
            }
633

    
634
            @Override
635
            public void onError(Throwable t) {
636
                GWT.log("Error creating pithos", t);
637
                                setError(t);
638
                if (t instanceof RestException)
639
                    displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
640
                else
641
                    displayError("System error Error creating pithos: " + t.getMessage());
642
            }
643

    
644
                        @Override
645
                        protected void onUnauthorized(Response response) {
646
                                sessionExpired();
647
                        }
648
        };
649
        createPithos.setHeader("X-Auth-Token", getToken());
650
        Scheduler.get().scheduleDeferred(createPithos);
651
    }
652

    
653
    protected void createTrashContainer(final Command callback) {
654
        String path = "/" + Pithos.TRASH_CONTAINER;
655
        PutRequest createPithos = new PutRequest(getApiPath(), getUsername(), path) {
656
            @Override
657
            public void onSuccess(Resource result) {
658
                           fetchAccount(callback);
659
            }
660

    
661
            @Override
662
            public void onError(Throwable t) {
663
                GWT.log("Error creating pithos", t);
664
                                setError(t);
665
                if (t instanceof RestException)
666
                    displayError("Error creating pithos: " + ((RestException) t).getHttpStatusText());
667
                else
668
                    displayError("System error Error creating pithos: " + t.getMessage());
669
            }
670

    
671
                        @Override
672
                        protected void onUnauthorized(Response response) {
673
                                sessionExpired();
674
                        }
675
        };
676
        createPithos.setHeader("X-Auth-Token", getToken());
677
        Scheduler.get().scheduleDeferred(createPithos);
678
    }
679

    
680
    /**
681
         * Creates an HTML fragment that places an image & caption together, for use
682
         * in a group header.
683
         *
684
         * @param imageProto an image prototype for an image
685
         * @param caption the group caption
686
         * @return the header HTML fragment
687
         */
688
        private String createHeaderHTML(AbstractImagePrototype imageProto, String caption) {
689
                String captionHTML = "<table class='caption' cellpadding='0' " 
690
                + "cellspacing='0'>" + "<tr><td class='lcaption'>" + imageProto.getHTML() 
691
                + "</td><td id =" + caption +" class='rcaption'><b style='white-space:nowrap'>&nbsp;" 
692
                + caption + "</b></td></tr></table>";
693
                return captionHTML;
694
        }
695

    
696
        protected void onWindowResized(int height) {
697
                // Adjust the split panel to take up the available room in the window.
698
                int newHeight = height - splitPanel.getAbsoluteTop();
699
                if (newHeight < 1)
700
                        newHeight = 1;
701
                splitPanel.setHeight("" + newHeight);
702
                inner.setHeight("" + newHeight);
703
        }
704

    
705
        @Override
706
        public void onResize(ResizeEvent event) {
707
                int height = event.getHeight();
708
                onWindowResized(height);
709
        }
710

    
711
        /**
712
         * Display an error message.
713
         *
714
         * @param msg the message to display
715
         */
716
        public void displayError(String msg) {
717
                messagePanel.displayError(msg);
718
        }
719

    
720
        /**
721
         * Display a warning message.
722
         *
723
         * @param msg the message to display
724
         */
725
        public void displayWarning(String msg) {
726
                messagePanel.displayWarning(msg);
727
        }
728

    
729
        /**
730
         * Display an informational message.
731
         *
732
         * @param msg the message to display
733
         */
734
        public void displayInformation(String msg) {
735
                messagePanel.displayInformation(msg);
736
        }
737

    
738
        /**
739
         * Retrieve the fileList.
740
         *
741
         * @return the fileList
742
         */
743
        public FileList getFileList() {
744
                return fileList;
745
        }
746

    
747
        /**
748
         * Retrieve the topPanel.
749
         *
750
         * @return the topPanel
751
         */
752
        TopPanel getTopPanel() {
753
                return topPanel;
754
        }
755

    
756
        /**
757
         * Retrieve the clipboard.
758
         *
759
         * @return the clipboard
760
         */
761
        public Clipboard getClipboard() {
762
                return clipboard;
763
        }
764

    
765
        public StatusPanel getStatusPanel() {
766
                return statusPanel;
767
        }
768

    
769
        public String getToken() {
770
                return token;
771
        }
772

    
773
        public static native void preventIESelection() /*-{
774
                $doc.body.onselectstart = function () { return false; };
775
        }-*/;
776

    
777
        public static native void enableIESelection() /*-{
778
                if ($doc.body.onselectstart != null)
779
                $doc.body.onselectstart = null;
780
        }-*/;
781

    
782
        /**
783
         * @return the absolute path of the API root URL
784
         */
785
        public String getApiPath() {
786
                Configuration conf = (Configuration) GWT.create(Configuration.class);
787
                return conf.apiPath();
788
        }
789

    
790
        /**
791
         * History support for folder navigation
792
         * adds a new browser history entry
793
         *
794
         * @param key
795
         */
796
        public void updateHistory(String key){
797
//                Replace any whitespace of the initial string to "+"
798
//                String result = key.replaceAll("\\s","+");
799
//                Add a new browser history entry.
800
//                History.newItem(result);
801
                History.newItem(key);
802
        }
803

    
804
    public void deleteFolder(final Folder folder) {
805
        String path = getApiPath() + folder.getOwner() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(folder.getPrefix()) + "&t=" + System.currentTimeMillis();
806
        RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
807
        builder.setHeader("X-Auth-Token", getToken());
808
        try {
809
            builder.sendRequest("", new RequestCallback() {
810
                @Override
811
                public void onResponseReceived(Request request, Response response) {
812
                    if (response.getStatusCode() == Response.SC_OK) {
813
                        JSONValue json = JSONParser.parseStrict(response.getText());
814
                        JSONArray array = json.isArray();
815
                        int i = 0;
816
                        if (array != null) {
817
                            deleteObject(folder, i, array);
818
                        }
819
                    }
820
                }
821

    
822
                @Override
823
                public void onError(Request request, Throwable exception) {
824
                        setError(exception);
825
                    displayError("System error unable to delete folder: " + exception.getMessage());
826
                }
827
            });
828
        }
829
        catch (RequestException e) {
830
        }
831
    }
832

    
833
    void deleteObject(final Folder folder, final int i, final JSONArray array) {
834
        if (i < array.size()) {
835
            JSONObject o = array.get(i).isObject();
836
            if (o != null && !o.containsKey("subdir")) {
837
                JSONString name = o.get("name").isString();
838
                String path = "/" + folder.getContainer() + "/" + name.stringValue();
839
                DeleteRequest delete = new DeleteRequest(getApiPath(), folder.getOwner(), URL.encode(path)) {
840
                    @Override
841
                    public void onSuccess(Resource result) {
842
                        deleteObject(folder, i + 1, array);
843
                    }
844

    
845
                    @Override
846
                    public void onError(Throwable t) {
847
                        GWT.log("", t);
848
                                                setError(t);
849
                        displayError("System error unable to delete folder: " + t.getMessage());
850
                    }
851

    
852
                                    @Override
853
                                    protected void onUnauthorized(Response response) {
854
                                            sessionExpired();
855
                                    }
856
                };
857
                delete.setHeader("X-Auth-Token", getToken());
858
                Scheduler.get().scheduleDeferred(delete);
859
            }
860
            else if (o != null) {
861
                String subdir = o.get("subdir").isString().stringValue();
862
                subdir = subdir.substring(0, subdir.length() - 1);
863
                String path = getApiPath() + getUsername() + "/" + folder.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(subdir) + "&t=" + System.currentTimeMillis();
864
                RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
865
                builder.setHeader("X-Auth-Token", getToken());
866
                try {
867
                    builder.sendRequest("", new RequestCallback() {
868
                        @Override
869
                        public void onResponseReceived(Request request, Response response) {
870
                            if (response.getStatusCode() == Response.SC_OK) {
871
                                JSONValue json = JSONParser.parseStrict(response.getText());
872
                                JSONArray array2 = json.isArray();
873
                                if (array2 != null) {
874
                                    int l = array.size();
875
                                    for (int j=0; j<array2.size(); j++) {
876
                                        array.set(l++, array2.get(j));
877
                                    }
878
                                }
879
                                deleteObject(folder, i + 1, array);
880
                            }
881
                        }
882

    
883
                        @Override
884
                        public void onError(Request request, Throwable exception) {
885
                                setError(exception);
886
                            displayError("System error unable to delete folder: " + exception.getMessage());
887
                        }
888
                    });
889
                }
890
                catch (RequestException e) {
891
                }
892
            }
893
        }
894
        else {
895
            String path = folder.getUri();
896
            DeleteRequest deleteFolder = new DeleteRequest(getApiPath(), getUsername(), URL.encode(path)) {
897
                @Override
898
                public void onSuccess(Resource result) {
899
                    updateFolder(folder.getParent(), true, new Command() {
900
                                                
901
                                                @Override
902
                                                public void execute() {
903
                                                        updateStatistics();
904
                                                }
905
                                        });
906
                }
907

    
908
                @Override
909
                public void onError(Throwable t) {
910
                    GWT.log("", t);
911
                                        setError(t);
912
                    if (t instanceof RestException) {
913
                            if (((RestException) t).getHttpStatusCode() != Response.SC_NOT_FOUND)
914
                                    displayError("Unable to delete folder: "+((RestException) t).getHttpStatusText());
915
                            else
916
                                    onSuccess(null);
917
                    }
918
                    else
919
                        displayError("System error unable to delete folder: " + t.getMessage());
920
                }
921

    
922
                                @Override
923
                                protected void onUnauthorized(Response response) {
924
                                        sessionExpired();
925
                                }
926
            };
927
            deleteFolder.setHeader("X-Auth-Token", getToken());
928
            Scheduler.get().scheduleDeferred(deleteFolder);
929
        }
930
    }
931

    
932
    public FolderTreeView getFolderTreeView() {
933
        return folderTreeView;
934
    }
935

    
936
    public void copyFiles(final Iterator<File> iter, final String targetUsername, final String targetUri, final Command callback) {
937
        if (iter.hasNext()) {
938
            File file = iter.next();
939
            String path = targetUri + "/" + file.getName();
940
            PutRequest copyFile = new PutRequest(getApiPath(), targetUsername, path) {
941
                @Override
942
                public void onSuccess(Resource result) {
943
                    copyFiles(iter, targetUsername, targetUri, callback);
944
                }
945

    
946
                @Override
947
                public void onError(Throwable t) {
948
                    GWT.log("", t);
949
                                        setError(t);
950
                    if (t instanceof RestException) {
951
                        displayError("Unable to copy file: " + ((RestException) t).getHttpStatusText());
952
                    }
953
                    else
954
                        displayError("System error unable to copy file: "+t.getMessage());
955
                }
956

    
957
                                @Override
958
                                protected void onUnauthorized(Response response) {
959
                                        sessionExpired();
960
                                }
961
            };
962
            copyFile.setHeader("X-Auth-Token", getToken());
963
            copyFile.setHeader("X-Copy-From", URL.encodePathSegment(file.getUri()));
964
            if (!file.getOwner().equals(targetUsername))
965
                    copyFile.setHeader("X-Source-Account", URL.encodePathSegment(file.getOwner()));
966
            copyFile.setHeader("Content-Type", file.getContentType());
967
            Scheduler.get().scheduleDeferred(copyFile);
968
        }
969
        else  if (callback != null) {
970
            callback.execute();
971
        }
972
    }
973

    
974
    public void copySubfolders(final Iterator<Folder> iter, final String targetUsername, final String targetUri, final Command callback) {
975
        if (iter.hasNext()) {
976
            final Folder f = iter.next();
977
            copyFolder(f, targetUsername, targetUri, new Command() {
978
                                
979
                                @Override
980
                                public void execute() {
981
                                        copySubfolders(iter, targetUsername, targetUri, callback);
982
                                }
983
                        });
984
        }
985
        else  if (callback != null) {
986
            callback.execute();
987
        }
988
    }
989

    
990
    public void copyFolder(final Folder f, final String targetUsername, final String targetUri, final Command callback) {
991
        String path = targetUri + "/" + f.getName();
992
        PutRequest createFolder = new PutRequest(getApiPath(), targetUsername, path) {
993
            @Override
994
            public void onSuccess(Resource result) {
995
                    GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, getApiPath(), f.getOwner(), "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(f.getPrefix()), f) {
996

    
997
                                        @Override
998
                                        public void onSuccess(final Folder _f) {
999
                                Iterator<File> iter = _f.getFiles().iterator();
1000
                                copyFiles(iter, targetUsername, targetUri + "/" + _f.getName(), new Command() {
1001
                                    @Override
1002
                                    public void execute() {
1003
                                        Iterator<Folder> iterf = _f.getSubfolders().iterator();
1004
                                        copySubfolders(iterf, targetUsername, targetUri + "/" + _f.getName(), callback);
1005
                                    }
1006
                                });
1007
                                        }
1008

    
1009
                                        @Override
1010
                                        public void onError(Throwable t) {
1011
                                GWT.log("", t);
1012
                                                setError(t);
1013
                                if (t instanceof RestException) {
1014
                                    displayError("Unable to get folder: " + ((RestException) t).getHttpStatusText());
1015
                                }
1016
                                else
1017
                                    displayError("System error getting folder: " + t.getMessage());
1018
                                        }
1019

    
1020
                                        @Override
1021
                                        protected void onUnauthorized(Response response) {
1022
                                                sessionExpired();
1023
                                        }
1024
                                };
1025
                                getFolder.setHeader("X-Auth-Token", getToken());
1026
                                Scheduler.get().scheduleDeferred(getFolder);
1027
            }
1028

    
1029
            @Override
1030
            public void onError(Throwable t) {
1031
                GWT.log("", t);
1032
                                setError(t);
1033
               if (t instanceof RestException) {
1034
                    displayError("Unable to create folder: " + ((RestException) t).getHttpStatusText());
1035
                }
1036
                else
1037
                    displayError("System error creating folder: " + t.getMessage());
1038
            }
1039

    
1040
                        @Override
1041
                        protected void onUnauthorized(Response response) {
1042
                                sessionExpired();
1043
                        }
1044
        };
1045
        createFolder.setHeader("X-Auth-Token", getToken());
1046
        createFolder.setHeader("Accept", "*/*");
1047
        createFolder.setHeader("Content-Length", "0");
1048
        createFolder.setHeader("Content-Type", "application/folder");
1049
        Scheduler.get().scheduleDeferred(createFolder);
1050
    }
1051
    
1052
    public void addSelectionModel(@SuppressWarnings("rawtypes") SingleSelectionModel model) {
1053
            selectionModels.add(model);
1054
    }
1055

    
1056
        public OtherSharedTreeView getOtherSharedTreeView() {
1057
                return otherSharedTreeView;
1058
        }
1059

    
1060
        public void updateTrash(boolean showFiles, Command callback) {
1061
                updateFolder(trash, showFiles, callback);
1062
        }
1063

    
1064
        public void updateGroupsNode() {
1065
                groupTreeView.updateGroupNode(null);
1066
        }
1067

    
1068
        public void addGroup(String groupname) {
1069
                Group newGroup = new Group(groupname);
1070
                account.addGroup(newGroup);
1071
                groupTreeView.updateGroupNode(null);
1072
        }
1073

    
1074
        public void removeGroup(Group group) {
1075
                account.removeGroup(group);
1076
                updateGroupsNode();
1077
        }
1078

    
1079
        public TreeView getSelectedTree() {
1080
                return selectedTree;
1081
        }
1082
        
1083
        public Folder getSelection() {
1084
                return selectedTree.getSelection();
1085
        }
1086

    
1087
        public void showFolderStatistics(int folderFileCount) {
1088
                numOfFiles.setHTML(String.valueOf(folderFileCount));
1089
        }
1090

    
1091
        public GroupTreeView getGroupTreeView() {
1092
                return groupTreeView;
1093
        }
1094

    
1095
        public void sessionExpired() {
1096
                new SessionExpiredDialog(this).center();
1097
        }
1098

    
1099
        public void updateRootFolder(Command callback) {
1100
                updateFolder(account.getPithos(), false, callback);
1101
        }
1102

    
1103
        void createMySharedTree() {
1104
                mysharedTreeSelectionModel = new SingleSelectionModel<Folder>();
1105
                mysharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
1106
                    @Override
1107
                    public void onSelectionChange(SelectionChangeEvent event) {
1108
                        if (mysharedTreeSelectionModel.getSelectedObject() != null) {
1109
                            deselectOthers(mysharedTreeView, mysharedTreeSelectionModel);
1110
                            upload.setEnabled(false);
1111
                            updateSharedFolder(mysharedTreeSelectionModel.getSelectedObject(), true);
1112
                        }
1113
                        showRelevantToolbarButtons();
1114
                     }
1115
                });
1116
                selectionModels.add(mysharedTreeSelectionModel);
1117
                mysharedTreeViewModel = new MysharedTreeViewModel(Pithos.this, mysharedTreeSelectionModel);
1118
                mysharedTreeViewModel.initialize(new Command() {
1119
                        
1120
                        @Override
1121
                        public void execute() {
1122
                            mysharedTreeView = new MysharedTreeView(mysharedTreeViewModel);
1123
                                trees.insert(mysharedTreeView, 2);
1124
                                treeViews.add(mysharedTreeView);
1125
                                createOtherSharedTree();
1126
                        }
1127
                });
1128
        }
1129

    
1130
        void createOtherSharedTree() {
1131
                otherSharedTreeSelectionModel = new SingleSelectionModel<Folder>();
1132
                otherSharedTreeSelectionModel.addSelectionChangeHandler(new Handler() {
1133
                    @Override
1134
                    public void onSelectionChange(SelectionChangeEvent event) {
1135
                        if (otherSharedTreeSelectionModel.getSelectedObject() != null) {
1136
                            deselectOthers(otherSharedTreeView, otherSharedTreeSelectionModel);
1137
                            otherSharedTreeView.addStyleName("cellTreeWidget-selectedTree");
1138
                            applyPermissions(otherSharedTreeSelectionModel.getSelectedObject());
1139
                            updateOtherSharedFolder(otherSharedTreeSelectionModel.getSelectedObject(), true);
1140
                        }
1141
                        showRelevantToolbarButtons();
1142
                     }
1143
                });
1144
                selectionModels.add(otherSharedTreeSelectionModel);
1145
                otherSharedTreeViewModel = new OtherSharedTreeViewModel(Pithos.this, otherSharedTreeSelectionModel);
1146
                otherSharedTreeViewModel.initialize(new Command() {
1147
                        
1148
                        @Override
1149
                        public void execute() {
1150
                            otherSharedTreeView = new OtherSharedTreeView(otherSharedTreeViewModel);
1151
                                trees.insert(otherSharedTreeView, 3);
1152
                                treeViews.add(otherSharedTreeView);
1153
                        }
1154
                });
1155
        }
1156

    
1157
        public native void log1(String message)/*-{
1158
                $wnd.console.log(message);
1159
        }-*/;
1160

    
1161
        public String getErrorData() {
1162
                if (error != null)
1163
                        return error.toString();
1164
                return "";
1165
        }
1166
        
1167
        public void setError(Throwable t) {
1168
                error = t;
1169
        }
1170
        
1171
        public void showRelevantToolbarButtons() {
1172
                toolbar.showRelevantButtons();
1173
        }
1174

    
1175
        public FileUploadDialog getFileUploadDialog() {
1176
                if (fileUploadDialog == null)
1177
                        fileUploadDialog = new FileUploadDialog(this);
1178
                return fileUploadDialog;
1179
        }
1180

    
1181
        public void hideUploadIndicator() {
1182
                upload.removeStyleName("pithos-uploadButton-loading");
1183
                upload.setTitle("");
1184
        }
1185
        
1186
        public void showUploadIndicator() {
1187
                upload.addStyleName("pithos-uploadButton-loading");
1188
                upload.setTitle("Upload in progress. Click for details.");
1189
        }
1190

    
1191
        public void scheduleFolderHeadCommand(final Folder folder, final Command callback) {
1192
                HeadRequest<Folder> headFolder = new HeadRequest<Folder>(Folder.class, getApiPath(), folder.getOwner(), folder.getUri(), folder) {
1193

    
1194
                        @Override
1195
                        public void onSuccess(Folder _result) {
1196
                                if (callback != null)
1197
                                        callback.execute();
1198
                        }
1199

    
1200
                        @Override
1201
                        public void onError(Throwable t) {
1202
                        if (t instanceof RestException) {
1203
                                if (((RestException) t).getHttpStatusCode() == Response.SC_NOT_FOUND) {
1204
                        final String path = folder.getUri();
1205
                        PutRequest newFolder = new PutRequest(getApiPath(), folder.getOwner(), path) {
1206
                            @Override
1207
                            public void onSuccess(Resource _result) {
1208
                                    scheduleFolderHeadCommand(folder, callback);
1209
                            }
1210

    
1211
                            @Override
1212
                            public void onError(Throwable _t) {
1213
                                GWT.log("", _t);
1214
                                                        setError(_t);
1215
                                if(_t instanceof RestException){
1216
                                    displayError("Unable to create folder: " + ((RestException) _t).getHttpStatusText());
1217
                                }
1218
                                else
1219
                                    displayError("System error creating folder: " + _t.getMessage());
1220
                            }
1221

    
1222
                                            @Override
1223
                                            protected void onUnauthorized(Response response) {
1224
                                                    sessionExpired();
1225
                                            }
1226
                        };
1227
                        newFolder.setHeader("X-Auth-Token", getToken());
1228
                        newFolder.setHeader("Content-Type", "application/folder");
1229
                        newFolder.setHeader("Accept", "*/*");
1230
                        newFolder.setHeader("Content-Length", "0");
1231
                        Scheduler.get().scheduleDeferred(newFolder);
1232
                                }
1233
                                else
1234
                                        displayError("Error heading folder: " + ((RestException) t).getHttpStatusText());
1235
                        }
1236
                        else
1237
                            displayError("System error heading folder: " + t.getMessage());
1238

    
1239
                        GWT.log("Error heading folder", t);
1240
                                setError(t);
1241
                        }
1242

    
1243
                        @Override
1244
                        protected void onUnauthorized(Response response) {
1245
                                sessionExpired();
1246
                        }
1247
                };
1248
                headFolder.setHeader("X-Auth-Token", getToken());
1249
                Scheduler.get().scheduleDeferred(headFolder);
1250
        }
1251

    
1252
        public void scheduleFileHeadCommand(File f, final Command callback) {
1253
                HeadRequest<File> headFile = new HeadRequest<File>(File.class, getApiPath(), f.getOwner(), f.getUri(), f) {
1254

    
1255
                        @Override
1256
                        public void onSuccess(File _result) {
1257
                                if (callback != null)
1258
                                        callback.execute();
1259
                        }
1260

    
1261
                        @Override
1262
                        public void onError(Throwable t) {
1263
                        GWT.log("Error heading file", t);
1264
                                setError(t);
1265
                        if (t instanceof RestException)
1266
                            displayError("Error heading file: " + ((RestException) t).getHttpStatusText());
1267
                        else
1268
                            displayError("System error heading file: " + t.getMessage());
1269
                        }
1270

    
1271
                        @Override
1272
                        protected void onUnauthorized(Response response) {
1273
                                sessionExpired();
1274
                        }
1275
                };
1276
                headFile.setHeader("X-Auth-Token", getToken());
1277
                Scheduler.get().scheduleDeferred(headFile);
1278
        }
1279
}