Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / rest / resource / FileResource.java @ 63366925

History | View | Annotate | Download (11.8 kB)

1
/*
2
 * Copyright 2011 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.rest.resource;
36

    
37
import java.util.ArrayList;
38
import java.util.Date;
39
import java.util.HashSet;
40
import java.util.List;
41
import java.util.Set;
42

    
43
import com.google.gwt.core.client.GWT;
44
import com.google.gwt.http.client.URL;
45
import com.google.gwt.i18n.client.NumberFormat;
46
import com.google.gwt.json.client.JSONArray;
47
import com.google.gwt.json.client.JSONObject;
48
import com.google.gwt.json.client.JSONParser;
49
import com.google.gwt.json.client.JSONString;
50

    
51
public class FileResource extends RestResource {
52

    
53
        public FileResource(String aUri) {
54
                super(aUri);
55
        }
56

    
57
        String name;
58

    
59
        String owner;
60

    
61
        String createdBy;
62

    
63
        String modifiedBy;
64

    
65
        Date creationDate;
66

    
67
        Date modificationDate;
68

    
69
        String contentType;
70

    
71
        Long contentLength;
72

    
73
        boolean readForAll;
74

    
75
        boolean versioned;
76

    
77
        Integer version;
78

    
79
        String etag;
80

    
81
        boolean deleted = false;
82

    
83
        List<String> tags = new ArrayList<String>();
84

    
85
        Set<PermissionHolder> permissions = new HashSet<PermissionHolder>();
86

    
87
        String folderURI;
88

    
89
        String path;
90

    
91
        String folderName;
92
        Boolean shared;
93
        
94
        
95
        
96
        
97
        /**
98
         * Modify the shared.
99
         *
100
         * @param _shared the shared to set
101
         */
102
        public void setShared(Boolean _shared) {
103
                this.shared = _shared;
104
        }
105
        
106
        /**
107
         * Retrieve the folderName.
108
         *
109
         * @return the folderName
110
         */
111
        public String getFolderName() {
112
                return folderName;
113
        }
114

    
115
        /**
116
         * Modify the folderName.
117
         *
118
         * @param aFolderName the folderName to set
119
         */
120
        public void setFolderName(String aFolderName) {
121
                folderName = aFolderName;
122
        }
123

    
124
        /**
125
         * Retrieve the path.
126
         *
127
         * @return the path
128
         */
129
        public String getPath() {
130
                return path;
131
        }
132

    
133
        /**
134
         * Modify the path.
135
         *
136
         * @param aPath the path to set
137
         */
138
        public void setPath(String aPath) {
139
                path = aPath;
140
        }
141

    
142
        /**
143
         * Retrieve the name.
144
         *
145
         * @return the name
146
         */
147
        @Override
148
        public String getName() {
149
                return name;
150
        }
151

    
152
        /**
153
         * Modify the name.
154
         *
155
         * @param aName the name to set
156
         */
157
        public void setName(String aName) {
158
                name = aName;
159
        }
160

    
161
        /**
162
         * Retrieve the owner.
163
         *
164
         * @return the owner
165
         */
166
        public String getOwner() {
167
                return owner;
168
        }
169

    
170
        /**
171
         * Modify the owner.
172
         *
173
         * @param newOwner the owner to set
174
         */
175
        public void setOwner(String newOwner) {
176
                owner = newOwner;
177
        }
178

    
179
        /**
180
         * Retrieve the createdBy.
181
         *
182
         * @return the createdBy
183
         */
184
        public String getCreatedBy() {
185
                return createdBy;
186
        }
187

    
188
        /**
189
         * Modify the createdBy.
190
         *
191
         * @param aCreatedBy the createdBy to set
192
         */
193
        public void setCreatedBy(String aCreatedBy) {
194
                createdBy = aCreatedBy;
195
        }
196

    
197
        /**
198
         * Retrieve the modifiedBy.
199
         *
200
         * @return the modifiedBy
201
         */
202
        public String getModifiedBy() {
203
                return modifiedBy;
204
        }
205

    
206
        /**
207
         * Modify the modifiedBy.
208
         *
209
         * @param aModifiedBy the modifiedBy to set
210
         */
211
        public void setModifiedBy(String aModifiedBy) {
212
                modifiedBy = aModifiedBy;
213
        }
214

    
215
        /**
216
         * Retrieve the creationDate.
217
         *
218
         * @return the creationDate
219
         */
220
        public Date getCreationDate() {
221
                return creationDate;
222
        }
223

    
224
        /**
225
         * Modify the creationDate.
226
         *
227
         * @param aCreationDate the creationDate to set
228
         */
229
        public void setCreationDate(Date aCreationDate) {
230
                creationDate = aCreationDate;
231
        }
232

    
233
        /**
234
         * Retrieve the modificationDate.
235
         *
236
         * @return the modificationDate
237
         */
238
        public Date getModificationDate() {
239
                return modificationDate;
240
        }
241

    
242
        /**
243
         * Modify the modificationDate.
244
         *
245
         * @param aModificationDate the modificationDate to set
246
         */
247
        public void setModificationDate(Date aModificationDate) {
248
                modificationDate = aModificationDate;
249
        }
250

    
251
        /**
252
         * Retrieve the contentType.
253
         *
254
         * @return the contentType
255
         */
256
        public String getContentType() {
257
                return contentType;
258
        }
259

    
260
        /**
261
         * Modify the contentType.
262
         *
263
         * @param newContentType the contentType to set
264
         */
265
        public void setContentType(String newContentType) {
266
                contentType = newContentType;
267
        }
268

    
269
        /**
270
         * Retrieve the contentLength.
271
         *
272
         * @return the contentLength
273
         */
274
        public Long getContentLength() {
275
                return contentLength;
276
        }
277

    
278
        /**
279
         * Modify the contentLength.
280
         *
281
         * @param newContentLength the contentLength to set
282
         */
283
        public void setContentLength(Long newContentLength) {
284
                contentLength = newContentLength;
285
        }
286

    
287
        /**
288
         * Retrieve the readForAll.
289
         *
290
         * @return the readForAll
291
         */
292
        public boolean isReadForAll() {
293
                return readForAll;
294
        }
295

    
296
        /**
297
         * Modify the readForAll.
298
         *
299
         * @param newReadForAll the readForAll to set
300
         */
301
        public void setReadForAll(boolean newReadForAll) {
302
                readForAll = newReadForAll;
303
        }
304

    
305
        /**
306
         * Retrieve the versioned.
307
         *
308
         * @return the versioned
309
         */
310
        public boolean isVersioned() {
311
                return versioned;
312
        }
313

    
314
        /**
315
         * Modify the versioned.
316
         *
317
         * @param newVersioned the versioned to set
318
         */
319
        public void setVersioned(boolean newVersioned) {
320
                versioned = newVersioned;
321
        }
322

    
323
        /**
324
         * Retrieve the version.
325
         *
326
         * @return the version
327
         */
328
        public Integer getVersion() {
329
                return version;
330
        }
331

    
332
        /**
333
         * Modify the version.
334
         *
335
         * @param aVersion the version to set
336
         */
337
        public void setVersion(Integer aVersion) {
338
                version = aVersion;
339
        }
340

    
341
        /**
342
         * Retrieve the etag.
343
         *
344
         * @return the etag
345
         */
346
        public String getEtag() {
347
                return etag;
348
        }
349

    
350
        /**
351
         * Modify the etag.
352
         *
353
         * @param anEtag the etag to set
354
         */
355
        public void setEtag(String anEtag) {
356
                etag = anEtag;
357
        }
358

    
359
        /**
360
         * Retrieve the tags.
361
         *
362
         * @return the tags
363
         */
364
        public List<String> getTags() {
365
                return tags;
366
        }
367

    
368
        /**
369
         * Modify the tags.
370
         *
371
         * @param newTags the tags to set
372
         */
373
        public void setTags(List<String> newTags) {
374
                tags = newTags;
375
        }
376

    
377
        /**
378
         * Retrieve the permissions.
379
         *
380
         * @return the permissions
381
         */
382
        public Set<PermissionHolder> getPermissions() {
383
                return permissions;
384
        }
385

    
386
        /**
387
         * Modify the permissions.
388
         *
389
         * @param newPermissions the permissions to set
390
         */
391
        public void setPermissions(Set<PermissionHolder> newPermissions) {
392
                permissions = newPermissions;
393
        }
394

    
395
        /**
396
         * Retrieve the deleted.
397
         *
398
         * @return the deleted
399
         */
400
        public boolean isDeleted() {
401
                return deleted;
402
        }
403

    
404
        /**
405
         * Modify the deleted.
406
         *
407
         * @param newDeleted the deleted to set
408
         */
409
        public void setDeleted(boolean newDeleted) {
410
                deleted = newDeleted;
411
        }
412

    
413
        /**
414
         * Retrieve the folderURI.
415
         *
416
         * @return the folderURI
417
         */
418
        public String getFolderURI() {
419
                return folderURI;
420
        }
421

    
422
        /**
423
         * Modify the folderURI.
424
         *
425
         * @param aFolderURI the folderURI to set
426
         */
427
        public void setFolderURI(String aFolderURI) {
428
                folderURI = aFolderURI;
429
        }
430

    
431
        @Override
432
        public void createFromJSON(String text) {
433
                JSONObject metadata = (JSONObject) JSONParser.parse(text);
434
                name = unmarshallString(metadata, "name");
435
                name = URL.decodeComponent(name);
436
                path = unmarshallString(metadata, "path");
437
                path = URL.decodeComponent(path);
438
                owner = unmarshallString(metadata, "owner");
439
                contentType = unmarshallString(metadata, "content");
440
                readForAll = unmarshallBoolean(metadata, "readForAll");
441
                versioned = unmarshallBoolean(metadata, "versioned");
442
                createdBy = unmarshallString(metadata, "createdBy");
443
                modifiedBy = unmarshallString(metadata, "modifiedBy");
444
                setShared(unmarshallBoolean(metadata, "shared"));
445
                if (metadata.get("version") != null)
446
                        version = new Integer(metadata.get("version").toString());
447

    
448
                deleted = unmarshallBoolean(metadata, "deleted");
449
                if (deleted)
450
                        GWT.log("FOUND A DELETED FILE:" + name, null);
451

    
452
                if (metadata.get("folder") != null) {
453
                        JSONObject folder = metadata.get("folder").isObject();
454
                        folderURI = unmarshallString(folder, "uri");
455
                        folderName = unmarshallString(folder, "name");
456
                        if(folderName != null)
457
                                folderName = URL.decodeComponent(folderName);
458
                }
459

    
460
                if (metadata.get("permissions") != null) {
461
                        JSONArray perm = metadata.get("permissions").isArray();
462
                        if (perm != null)
463
                                for (int i = 0; i < perm.size(); i++) {
464
                                        JSONObject obj = perm.get(i).isObject();
465
                                        if (obj != null) {
466
                                                PermissionHolder permission = new PermissionHolder();
467
                                                if (obj.get("user") != null)
468
                                                        permission.setUser(unmarshallString(obj, "user"));
469
                                                if (obj.get("group") != null) {
470
                                                        String group = unmarshallString(obj, "group");
471
                                                        group = URL.decodeComponent(group);
472
                                                        permission.setGroup(group);
473
                                                }
474
                                                permission.setRead(unmarshallBoolean(obj, "read"));
475
                                                permission.setWrite(unmarshallBoolean(obj, "write"));
476
                                                permission.setModifyACL(unmarshallBoolean(obj, "modifyACL"));
477
                                                permissions.add(permission);
478
                                        }
479
                                }
480

    
481
                }
482
                if (metadata.get("tags") != null) {
483
                        JSONArray perm = metadata.get("tags").isArray();
484
                        if (perm != null)
485
                                for (int i = 0; i < perm.size(); i++) {
486
                                        JSONString obj = perm.get(i).isString();
487
                                        if(obj != null)
488
                                                tags.add(URL.decodeComponent(obj.stringValue()));
489
                                }
490
                }
491
                if (metadata.get("creationDate") != null)
492
                        creationDate = new Date(new Long(metadata.get("creationDate").toString()));
493
                if (metadata.get("modificationDate") != null)
494
                        modificationDate = new Date(new Long(metadata.get("modificationDate").toString()));
495
                if (metadata.get("size") != null)
496
                        contentLength = Long.parseLong(metadata.get("size").toString());
497
        }
498

    
499
        /**
500
         * Return the file size in a humanly readable form, using SI units to denote
501
         * size information, e.g. 1 KB = 1000 B (bytes).
502
         *
503
         * @return the fileSize
504
         */
505
        public String getFileSizeAsString() {
506
                return getFileSizeAsString(contentLength);
507
        }
508

    
509
        /**
510
         * Return the given size in a humanly readable form, using SI units to denote
511
         * size information, e.g. 1 KB = 1000 B (bytes).
512
         *
513
         * @param size in bytes
514
         * @return the size in human readable string
515
         */
516
        public static String getFileSizeAsString(long size) {
517
                if (size < 1024)
518
                        return String.valueOf(size) + " B";
519
                else if (size < 1024 * 1024)
520
                        return getSize(size, 1024D) + " KB";
521
                else if (size < 1024 * 1024 * 1024)
522
                        return getSize(size, (1024D * 1024D)) + " MB";
523
                return getSize(size, (1024D * 1024D * 1024D)) + " GB";
524
        }
525

    
526
        private static String getSize(Long size, Double division) {
527
                Double res = Double.valueOf(size.toString()) / division;
528
                NumberFormat nf = NumberFormat.getFormat("######.#");
529
                return nf.format(res);
530
        }
531

    
532
        public boolean isShared(){
533
                return shared;
534
        }
535

    
536
        public boolean isShared(String ownerUser){
537
                GWT.log("OWNER USER:"+ownerUser, null);
538
                if (isReadForAll())
539
                        return true;
540
                if(permissions != null)
541
                        for(PermissionHolder perm : permissions){
542
                                if(perm.getUser() != null && !ownerUser.equals(perm.getUser()))
543
                                        return true;
544
                                if(perm.getGroup() != null)
545
                                        return true;
546
                        }
547
                return false;
548
        }
549

    
550
        @Override
551
        public String getLastModifiedSince() {
552
                if(modificationDate != null)
553
                        return getDate(modificationDate.getTime());
554
                return null;
555
        }
556
}
557