use http caching by using IF-MODIFIED-SINCE HEADERS and handling response accordingly
[pithos] / src / gr / ebs / gss / client / rest / resource / FileResource.java
1 /*
2  * Copyright 2009 Electronic Business Systems Ltd.
3  *
4  * This file is part of GSS.
5  *
6  * GSS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GSS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 package gr.ebs.gss.client.rest.resource;
20
21 import java.util.ArrayList;
22 import java.util.Date;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Set;
26
27 import com.google.gwt.core.client.GWT;
28 import com.google.gwt.http.client.URL;
29 import com.google.gwt.i18n.client.NumberFormat;
30 import com.google.gwt.json.client.JSONArray;
31 import com.google.gwt.json.client.JSONObject;
32 import com.google.gwt.json.client.JSONParser;
33 import com.google.gwt.json.client.JSONString;
34
35 /**
36  * @author kman
37  */
38 public class FileResource extends RestResource {
39
40         public FileResource(String aUri) {
41                 super(aUri);
42         }
43
44         String name;
45
46         String owner;
47
48         String createdBy;
49
50         String modifiedBy;
51
52         Date creationDate;
53
54         Date modificationDate;
55
56         String contentType;
57
58         Long contentLength;
59
60         boolean readForAll;
61
62         boolean versioned;
63
64         Integer version;
65
66         String etag;
67
68         boolean deleted = false;
69
70         List<String> tags = new ArrayList<String>();
71
72         Set<PermissionHolder> permissions = new HashSet<PermissionHolder>();
73
74         String folderURI;
75
76         String path;
77
78         String folderName;
79
80         /**
81          * Retrieve the folderName.
82          *
83          * @return the folderName
84          */
85         public String getFolderName() {
86                 return folderName;
87         }
88
89         /**
90          * Modify the folderName.
91          *
92          * @param aFolderName the folderName to set
93          */
94         public void setFolderName(String aFolderName) {
95                 folderName = aFolderName;
96         }
97
98         /**
99          * Retrieve the path.
100          *
101          * @return the path
102          */
103         public String getPath() {
104                 return path;
105         }
106
107         /**
108          * Modify the path.
109          *
110          * @param aPath the path to set
111          */
112         public void setPath(String aPath) {
113                 path = aPath;
114         }
115
116         /**
117          * Retrieve the name.
118          *
119          * @return the name
120          */
121         public String getName() {
122                 return name;
123         }
124
125         /**
126          * Modify the name.
127          *
128          * @param aName the name to set
129          */
130         public void setName(String aName) {
131                 name = aName;
132         }
133
134         /**
135          * Retrieve the owner.
136          *
137          * @return the owner
138          */
139         public String getOwner() {
140                 return owner;
141         }
142
143         /**
144          * Modify the owner.
145          *
146          * @param newOwner the owner to set
147          */
148         public void setOwner(String newOwner) {
149                 owner = newOwner;
150         }
151
152         /**
153          * Retrieve the createdBy.
154          *
155          * @return the createdBy
156          */
157         public String getCreatedBy() {
158                 return createdBy;
159         }
160
161         /**
162          * Modify the createdBy.
163          *
164          * @param aCreatedBy the createdBy to set
165          */
166         public void setCreatedBy(String aCreatedBy) {
167                 createdBy = aCreatedBy;
168         }
169
170         /**
171          * Retrieve the modifiedBy.
172          *
173          * @return the modifiedBy
174          */
175         public String getModifiedBy() {
176                 return modifiedBy;
177         }
178
179         /**
180          * Modify the modifiedBy.
181          *
182          * @param aModifiedBy the modifiedBy to set
183          */
184         public void setModifiedBy(String aModifiedBy) {
185                 modifiedBy = aModifiedBy;
186         }
187
188         /**
189          * Retrieve the creationDate.
190          *
191          * @return the creationDate
192          */
193         public Date getCreationDate() {
194                 return creationDate;
195         }
196
197         /**
198          * Modify the creationDate.
199          *
200          * @param aCreationDate the creationDate to set
201          */
202         public void setCreationDate(Date aCreationDate) {
203                 creationDate = aCreationDate;
204         }
205
206         /**
207          * Retrieve the modificationDate.
208          *
209          * @return the modificationDate
210          */
211         public Date getModificationDate() {
212                 return modificationDate;
213         }
214
215         /**
216          * Modify the modificationDate.
217          *
218          * @param aModificationDate the modificationDate to set
219          */
220         public void setModificationDate(Date aModificationDate) {
221                 modificationDate = aModificationDate;
222         }
223
224         /**
225          * Retrieve the contentType.
226          *
227          * @return the contentType
228          */
229         public String getContentType() {
230                 return contentType;
231         }
232
233         /**
234          * Modify the contentType.
235          *
236          * @param newContentType the contentType to set
237          */
238         public void setContentType(String newContentType) {
239                 contentType = newContentType;
240         }
241
242         /**
243          * Retrieve the contentLength.
244          *
245          * @return the contentLength
246          */
247         public Long getContentLength() {
248                 return contentLength;
249         }
250
251         /**
252          * Modify the contentLength.
253          *
254          * @param newContentLength the contentLength to set
255          */
256         public void setContentLength(Long newContentLength) {
257                 contentLength = newContentLength;
258         }
259
260         /**
261          * Retrieve the readForAll.
262          *
263          * @return the readForAll
264          */
265         public boolean isReadForAll() {
266                 return readForAll;
267         }
268
269         /**
270          * Modify the readForAll.
271          *
272          * @param newReadForAll the readForAll to set
273          */
274         public void setReadForAll(boolean newReadForAll) {
275                 readForAll = newReadForAll;
276         }
277
278         /**
279          * Retrieve the versioned.
280          *
281          * @return the versioned
282          */
283         public boolean isVersioned() {
284                 return versioned;
285         }
286
287         /**
288          * Modify the versioned.
289          *
290          * @param newVersioned the versioned to set
291          */
292         public void setVersioned(boolean newVersioned) {
293                 versioned = newVersioned;
294         }
295
296         /**
297          * Retrieve the version.
298          *
299          * @return the version
300          */
301         public Integer getVersion() {
302                 return version;
303         }
304
305         /**
306          * Modify the version.
307          *
308          * @param aVersion the version to set
309          */
310         public void setVersion(Integer aVersion) {
311                 version = aVersion;
312         }
313
314         /**
315          * Retrieve the etag.
316          *
317          * @return the etag
318          */
319         public String getEtag() {
320                 return etag;
321         }
322
323         /**
324          * Modify the etag.
325          *
326          * @param anEtag the etag to set
327          */
328         public void setEtag(String anEtag) {
329                 etag = anEtag;
330         }
331
332         /**
333          * Retrieve the tags.
334          *
335          * @return the tags
336          */
337         public List<String> getTags() {
338                 return tags;
339         }
340
341         /**
342          * Modify the tags.
343          *
344          * @param newTags the tags to set
345          */
346         public void setTags(List<String> newTags) {
347                 tags = newTags;
348         }
349
350         /**
351          * Retrieve the permissions.
352          *
353          * @return the permissions
354          */
355         public Set<PermissionHolder> getPermissions() {
356                 return permissions;
357         }
358
359         /**
360          * Modify the permissions.
361          *
362          * @param newPermissions the permissions to set
363          */
364         public void setPermissions(Set<PermissionHolder> newPermissions) {
365                 permissions = newPermissions;
366         }
367
368         /**
369          * Retrieve the deleted.
370          *
371          * @return the deleted
372          */
373         public boolean isDeleted() {
374                 return deleted;
375         }
376
377         /**
378          * Modify the deleted.
379          *
380          * @param newDeleted the deleted to set
381          */
382         public void setDeleted(boolean newDeleted) {
383                 deleted = newDeleted;
384         }
385
386         /**
387          * Retrieve the folderURI.
388          *
389          * @return the folderURI
390          */
391         public String getFolderURI() {
392                 return folderURI;
393         }
394
395         /**
396          * Modify the folderURI.
397          *
398          * @param aFolderURI the folderURI to set
399          */
400         public void setFolderURI(String aFolderURI) {
401                 folderURI = aFolderURI;
402         }
403
404         @Override
405         public void createFromJSON(String text) {
406                 JSONObject metadata = (JSONObject) JSONParser.parse(text);
407                 name = unmarshallString(metadata, "name");
408                 name = URL.decodeComponent(name);
409                 path = unmarshallString(metadata, "path");
410                 path = URL.decodeComponent(path);
411                 owner = unmarshallString(metadata, "owner");
412                 contentType = unmarshallString(metadata, "content");
413                 readForAll = unmarshallBoolean(metadata, "readForAll");
414                 versioned = unmarshallBoolean(metadata, "versioned");
415
416                 if (metadata.get("version") != null)
417                         version = new Integer(metadata.get("version").toString());
418
419                 deleted = unmarshallBoolean(metadata, "deleted");
420                 if (deleted)
421                         GWT.log("FOUND A DELETED FILE:" + name, null);
422
423                 if (metadata.get("folder") != null) {
424                         JSONObject folder = metadata.get("folder").isObject();
425                         folderURI = unmarshallString(folder, "uri");
426                         folderName = unmarshallString(folder, "name");
427                         if(folderName != null)
428                                 folderName = URL.decodeComponent(folderName);
429                 }
430
431                 if (metadata.get("permissions") != null) {
432                         JSONArray perm = metadata.get("permissions").isArray();
433                         if (perm != null)
434                                 for (int i = 0; i < perm.size(); i++) {
435                                         JSONObject obj = perm.get(i).isObject();
436                                         if (obj != null) {
437                                                 PermissionHolder permission = new PermissionHolder();
438                                                 if (obj.get("user") != null)
439                                                         permission.setUser(unmarshallString(obj, "user"));
440                                                 if (obj.get("group") != null) {
441                                                         String group = unmarshallString(obj, "group");
442                                                         group = URL.decodeComponent(group);
443                                                         permission.setGroup(group);
444                                                 }
445                                                 permission.setRead(unmarshallBoolean(obj, "read"));
446                                                 permission.setWrite(unmarshallBoolean(obj, "write"));
447                                                 permission.setModifyACL(unmarshallBoolean(obj, "modifyACL"));
448                                                 permissions.add(permission);
449                                         }
450                                 }
451
452                 }
453                 if (metadata.get("tags") != null) {
454                         JSONArray perm = metadata.get("tags").isArray();
455                         if (perm != null)
456                                 for (int i = 0; i < perm.size(); i++) {
457                                         JSONString obj = perm.get(i).isString();
458                                         if(obj != null)
459                                                 tags.add(URL.decodeComponent(obj.stringValue()));
460                                 }
461                 }
462                 if (metadata.get("creationDate") != null)
463                         creationDate = new Date(new Long(metadata.get("creationDate").toString()));
464                 if (metadata.get("modificationDate") != null)
465                         modificationDate = new Date(new Long(metadata.get("modificationDate").toString()));
466                 if (metadata.get("size") != null)
467                         contentLength = Long.parseLong(metadata.get("size").toString());
468         }
469
470         /**
471          * Return the file size in a humanly readable form, using SI units to denote
472          * size information, e.g. 1 KB = 1000 B (bytes).
473          *
474          * @return the fileSize
475          */
476         public String getFileSizeAsString() {
477                 return getFileSizeAsString(contentLength);
478         }
479
480         /**
481          * Return the given size in a humanly readable form, using SI units to denote
482          * size information, e.g. 1 KB = 1000 B (bytes).
483          *
484          * @param size in bytes
485          * @return the size in human readable string
486          */
487         public static String getFileSizeAsString(long size) {
488                 if (size < 1024)
489                         return String.valueOf(size) + " B";
490                 else if (size < 1024 * 1024)
491                         return getSize(size, 1024D) + " KB";
492                 else if (size < 1024 * 1024 * 1024)
493                         return getSize(size, (1024D * 1024D)) + " MB";
494                 return getSize(size, (1024D * 1024D * 1024D)) + " GB";
495         }
496
497         private static String getSize(Long size, Double division) {
498                 Double res = Double.valueOf(size.toString()) / division;
499                 NumberFormat nf = NumberFormat.getFormat("######.#");
500                 return nf.format(res);
501         }
502
503         public boolean isShared(){
504                 if (isReadForAll())
505                         return true;
506                 for(PermissionHolder perm : permissions){
507                         if(perm.getUser() != null && !owner.equals(perm.getUser()))
508                                 return true;
509                         if(perm.getGroup() != null)
510                                 return true;
511                 }
512                 return false;
513         }
514
515         @Override
516         public String getLastModifiedSince() {
517                 if(modificationDate != null)
518                         return getDate(modificationDate.getTime());
519                 return null;
520         }
521 }