Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / rest / resource / FileResource.java @ 6dd67d1c

History | View | Annotate | Download (10.3 kB)

1
/*
2
 *  Copyright (c) 2011 Greek Research and Technology Network
3
 */
4
package gr.grnet.pithos.web.client.rest.resource;
5

    
6
import java.util.ArrayList;
7
import java.util.Date;
8
import java.util.HashSet;
9
import java.util.List;
10
import java.util.Set;
11

    
12
import com.google.gwt.core.client.GWT;
13
import com.google.gwt.http.client.URL;
14
import com.google.gwt.i18n.client.NumberFormat;
15
import com.google.gwt.json.client.JSONArray;
16
import com.google.gwt.json.client.JSONObject;
17
import com.google.gwt.json.client.JSONParser;
18
import com.google.gwt.json.client.JSONString;
19

    
20
public class FileResource extends RestResource {
21

    
22
        public FileResource(String aUri) {
23
                super(aUri);
24
        }
25

    
26
        String name;
27

    
28
        String owner;
29

    
30
        String createdBy;
31

    
32
        String modifiedBy;
33

    
34
        Date creationDate;
35

    
36
        Date modificationDate;
37

    
38
        String contentType;
39

    
40
        Long contentLength;
41

    
42
        boolean readForAll;
43

    
44
        boolean versioned;
45

    
46
        Integer version;
47

    
48
        String etag;
49

    
50
        boolean deleted = false;
51

    
52
        List<String> tags = new ArrayList<String>();
53

    
54
        Set<PermissionHolder> permissions = new HashSet<PermissionHolder>();
55

    
56
        String folderURI;
57

    
58
        String path;
59

    
60
        String folderName;
61
        Boolean shared;
62
        
63
        
64
        
65
        
66
        /**
67
         * Modify the shared.
68
         *
69
         * @param _shared the shared to set
70
         */
71
        public void setShared(Boolean _shared) {
72
                this.shared = _shared;
73
        }
74
        
75
        /**
76
         * Retrieve the folderName.
77
         *
78
         * @return the folderName
79
         */
80
        public String getFolderName() {
81
                return folderName;
82
        }
83

    
84
        /**
85
         * Modify the folderName.
86
         *
87
         * @param aFolderName the folderName to set
88
         */
89
        public void setFolderName(String aFolderName) {
90
                folderName = aFolderName;
91
        }
92

    
93
        /**
94
         * Retrieve the path.
95
         *
96
         * @return the path
97
         */
98
        public String getPath() {
99
                return path;
100
        }
101

    
102
        /**
103
         * Modify the path.
104
         *
105
         * @param aPath the path to set
106
         */
107
        public void setPath(String aPath) {
108
                path = aPath;
109
        }
110

    
111
        /**
112
         * Retrieve the name.
113
         *
114
         * @return the name
115
         */
116
        @Override
117
        public String getName() {
118
                return name;
119
        }
120

    
121
        /**
122
         * Modify the name.
123
         *
124
         * @param aName the name to set
125
         */
126
        public void setName(String aName) {
127
                name = aName;
128
        }
129

    
130
        /**
131
         * Retrieve the owner.
132
         *
133
         * @return the owner
134
         */
135
        public String getOwner() {
136
                return owner;
137
        }
138

    
139
        /**
140
         * Modify the owner.
141
         *
142
         * @param newOwner the owner to set
143
         */
144
        public void setOwner(String newOwner) {
145
                owner = newOwner;
146
        }
147

    
148
        /**
149
         * Retrieve the createdBy.
150
         *
151
         * @return the createdBy
152
         */
153
        public String getCreatedBy() {
154
                return createdBy;
155
        }
156

    
157
        /**
158
         * Modify the createdBy.
159
         *
160
         * @param aCreatedBy the createdBy to set
161
         */
162
        public void setCreatedBy(String aCreatedBy) {
163
                createdBy = aCreatedBy;
164
        }
165

    
166
        /**
167
         * Retrieve the modifiedBy.
168
         *
169
         * @return the modifiedBy
170
         */
171
        public String getModifiedBy() {
172
                return modifiedBy;
173
        }
174

    
175
        /**
176
         * Modify the modifiedBy.
177
         *
178
         * @param aModifiedBy the modifiedBy to set
179
         */
180
        public void setModifiedBy(String aModifiedBy) {
181
                modifiedBy = aModifiedBy;
182
        }
183

    
184
        /**
185
         * Retrieve the creationDate.
186
         *
187
         * @return the creationDate
188
         */
189
        public Date getCreationDate() {
190
                return creationDate;
191
        }
192

    
193
        /**
194
         * Modify the creationDate.
195
         *
196
         * @param aCreationDate the creationDate to set
197
         */
198
        public void setCreationDate(Date aCreationDate) {
199
                creationDate = aCreationDate;
200
        }
201

    
202
        /**
203
         * Retrieve the modificationDate.
204
         *
205
         * @return the modificationDate
206
         */
207
        public Date getModificationDate() {
208
                return modificationDate;
209
        }
210

    
211
        /**
212
         * Modify the modificationDate.
213
         *
214
         * @param aModificationDate the modificationDate to set
215
         */
216
        public void setModificationDate(Date aModificationDate) {
217
                modificationDate = aModificationDate;
218
        }
219

    
220
        /**
221
         * Retrieve the contentType.
222
         *
223
         * @return the contentType
224
         */
225
        public String getContentType() {
226
                return contentType;
227
        }
228

    
229
        /**
230
         * Modify the contentType.
231
         *
232
         * @param newContentType the contentType to set
233
         */
234
        public void setContentType(String newContentType) {
235
                contentType = newContentType;
236
        }
237

    
238
        /**
239
         * Retrieve the contentLength.
240
         *
241
         * @return the contentLength
242
         */
243
        public Long getContentLength() {
244
                return contentLength;
245
        }
246

    
247
        /**
248
         * Modify the contentLength.
249
         *
250
         * @param newContentLength the contentLength to set
251
         */
252
        public void setContentLength(Long newContentLength) {
253
                contentLength = newContentLength;
254
        }
255

    
256
        /**
257
         * Retrieve the readForAll.
258
         *
259
         * @return the readForAll
260
         */
261
        public boolean isReadForAll() {
262
                return readForAll;
263
        }
264

    
265
        /**
266
         * Modify the readForAll.
267
         *
268
         * @param newReadForAll the readForAll to set
269
         */
270
        public void setReadForAll(boolean newReadForAll) {
271
                readForAll = newReadForAll;
272
        }
273

    
274
        /**
275
         * Retrieve the versioned.
276
         *
277
         * @return the versioned
278
         */
279
        public boolean isVersioned() {
280
                return versioned;
281
        }
282

    
283
        /**
284
         * Modify the versioned.
285
         *
286
         * @param newVersioned the versioned to set
287
         */
288
        public void setVersioned(boolean newVersioned) {
289
                versioned = newVersioned;
290
        }
291

    
292
        /**
293
         * Retrieve the version.
294
         *
295
         * @return the version
296
         */
297
        public Integer getVersion() {
298
                return version;
299
        }
300

    
301
        /**
302
         * Modify the version.
303
         *
304
         * @param aVersion the version to set
305
         */
306
        public void setVersion(Integer aVersion) {
307
                version = aVersion;
308
        }
309

    
310
        /**
311
         * Retrieve the etag.
312
         *
313
         * @return the etag
314
         */
315
        public String getEtag() {
316
                return etag;
317
        }
318

    
319
        /**
320
         * Modify the etag.
321
         *
322
         * @param anEtag the etag to set
323
         */
324
        public void setEtag(String anEtag) {
325
                etag = anEtag;
326
        }
327

    
328
        /**
329
         * Retrieve the tags.
330
         *
331
         * @return the tags
332
         */
333
        public List<String> getTags() {
334
                return tags;
335
        }
336

    
337
        /**
338
         * Modify the tags.
339
         *
340
         * @param newTags the tags to set
341
         */
342
        public void setTags(List<String> newTags) {
343
                tags = newTags;
344
        }
345

    
346
        /**
347
         * Retrieve the permissions.
348
         *
349
         * @return the permissions
350
         */
351
        public Set<PermissionHolder> getPermissions() {
352
                return permissions;
353
        }
354

    
355
        /**
356
         * Modify the permissions.
357
         *
358
         * @param newPermissions the permissions to set
359
         */
360
        public void setPermissions(Set<PermissionHolder> newPermissions) {
361
                permissions = newPermissions;
362
        }
363

    
364
        /**
365
         * Retrieve the deleted.
366
         *
367
         * @return the deleted
368
         */
369
        public boolean isDeleted() {
370
                return deleted;
371
        }
372

    
373
        /**
374
         * Modify the deleted.
375
         *
376
         * @param newDeleted the deleted to set
377
         */
378
        public void setDeleted(boolean newDeleted) {
379
                deleted = newDeleted;
380
        }
381

    
382
        /**
383
         * Retrieve the folderURI.
384
         *
385
         * @return the folderURI
386
         */
387
        public String getFolderURI() {
388
                return folderURI;
389
        }
390

    
391
        /**
392
         * Modify the folderURI.
393
         *
394
         * @param aFolderURI the folderURI to set
395
         */
396
        public void setFolderURI(String aFolderURI) {
397
                folderURI = aFolderURI;
398
        }
399

    
400
        @Override
401
        public void createFromJSON(String text) {
402
                JSONObject metadata = (JSONObject) JSONParser.parse(text);
403
                name = unmarshallString(metadata, "name");
404
                name = URL.decodeComponent(name);
405
                path = unmarshallString(metadata, "path");
406
                path = URL.decodeComponent(path);
407
                owner = unmarshallString(metadata, "owner");
408
                contentType = unmarshallString(metadata, "content");
409
                readForAll = unmarshallBoolean(metadata, "readForAll");
410
                versioned = unmarshallBoolean(metadata, "versioned");
411
                createdBy = unmarshallString(metadata, "createdBy");
412
                modifiedBy = unmarshallString(metadata, "modifiedBy");
413
                setShared(unmarshallBoolean(metadata, "shared"));
414
                if (metadata.get("version") != null)
415
                        version = new Integer(metadata.get("version").toString());
416

    
417
                deleted = unmarshallBoolean(metadata, "deleted");
418
                if (deleted)
419
                        GWT.log("FOUND A DELETED FILE:" + name, null);
420

    
421
                if (metadata.get("folder") != null) {
422
                        JSONObject folder = metadata.get("folder").isObject();
423
                        folderURI = unmarshallString(folder, "uri");
424
                        folderName = unmarshallString(folder, "name");
425
                        if(folderName != null)
426
                                folderName = URL.decodeComponent(folderName);
427
                }
428

    
429
                if (metadata.get("permissions") != null) {
430
                        JSONArray perm = metadata.get("permissions").isArray();
431
                        if (perm != null)
432
                                for (int i = 0; i < perm.size(); i++) {
433
                                        JSONObject obj = perm.get(i).isObject();
434
                                        if (obj != null) {
435
                                                PermissionHolder permission = new PermissionHolder();
436
                                                if (obj.get("user") != null)
437
                                                        permission.setUser(unmarshallString(obj, "user"));
438
                                                if (obj.get("group") != null) {
439
                                                        String group = unmarshallString(obj, "group");
440
                                                        group = URL.decodeComponent(group);
441
                                                        permission.setGroup(group);
442
                                                }
443
                                                permission.setRead(unmarshallBoolean(obj, "read"));
444
                                                permission.setWrite(unmarshallBoolean(obj, "write"));
445
                                                permission.setModifyACL(unmarshallBoolean(obj, "modifyACL"));
446
                                                permissions.add(permission);
447
                                        }
448
                                }
449

    
450
                }
451
                if (metadata.get("tags") != null) {
452
                        JSONArray perm = metadata.get("tags").isArray();
453
                        if (perm != null)
454
                                for (int i = 0; i < perm.size(); i++) {
455
                                        JSONString obj = perm.get(i).isString();
456
                                        if(obj != null)
457
                                                tags.add(URL.decodeComponent(obj.stringValue()));
458
                                }
459
                }
460
                if (metadata.get("creationDate") != null)
461
                        creationDate = new Date(new Long(metadata.get("creationDate").toString()));
462
                if (metadata.get("modificationDate") != null)
463
                        modificationDate = new Date(new Long(metadata.get("modificationDate").toString()));
464
                if (metadata.get("size") != null)
465
                        contentLength = Long.parseLong(metadata.get("size").toString());
466
        }
467

    
468
        /**
469
         * Return the file size in a humanly readable form, using SI units to denote
470
         * size information, e.g. 1 KB = 1000 B (bytes).
471
         *
472
         * @return the fileSize
473
         */
474
        public String getFileSizeAsString() {
475
                return getFileSizeAsString(contentLength);
476
        }
477

    
478
        /**
479
         * Return the given size in a humanly readable form, using SI units to denote
480
         * size information, e.g. 1 KB = 1000 B (bytes).
481
         *
482
         * @param size in bytes
483
         * @return the size in human readable string
484
         */
485
        public static String getFileSizeAsString(long size) {
486
                if (size < 1024)
487
                        return String.valueOf(size) + " B";
488
                else if (size < 1024 * 1024)
489
                        return getSize(size, 1024D) + " KB";
490
                else if (size < 1024 * 1024 * 1024)
491
                        return getSize(size, (1024D * 1024D)) + " MB";
492
                return getSize(size, (1024D * 1024D * 1024D)) + " GB";
493
        }
494

    
495
        private static String getSize(Long size, Double division) {
496
                Double res = Double.valueOf(size.toString()) / division;
497
                NumberFormat nf = NumberFormat.getFormat("######.#");
498
                return nf.format(res);
499
        }
500

    
501
        public boolean isShared(){
502
                return shared;
503
        }
504

    
505
        public boolean isShared(String ownerUser){
506
                GWT.log("OWNER USER:"+ownerUser, null);
507
                if (isReadForAll())
508
                        return true;
509
                if(permissions != null)
510
                        for(PermissionHolder perm : permissions){
511
                                if(perm.getUser() != null && !ownerUser.equals(perm.getUser()))
512
                                        return true;
513
                                if(perm.getGroup() != null)
514
                                        return true;
515
                        }
516
                return false;
517
        }
518

    
519
        @Override
520
        public String getLastModifiedSince() {
521
                if(modificationDate != null)
522
                        return getDate(modificationDate.getTime());
523
                return null;
524
        }
525
}
526