3a5c8e53d74555572be132a988bc36ae571582c2
[pithos] / web_client / src / gr / grnet / pithos / web / client / foldertree / File.java
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
36 package gr.grnet.pithos.web.client.foldertree;
37
38 import java.util.Date;
39 import java.util.HashMap;
40 import java.util.HashSet;
41 import java.util.Map;
42 import java.util.Set;
43
44 import com.google.gwt.http.client.Header;
45 import com.google.gwt.http.client.Response;
46 import com.google.gwt.http.client.URL;
47 import com.google.gwt.i18n.client.NumberFormat;
48 import com.google.gwt.json.client.JSONObject;
49
50 public class File extends Resource {
51     private String name;
52
53     private String hash;
54
55     private int version;
56
57     private long bytes;
58
59     private String contentType;
60
61     private Date lastModified;
62
63     private String modifiedBy;
64
65     private Date versionTimestamp;
66
67     private String path;
68
69     private String owner;
70
71     private String container;
72
73     private Folder parent;
74
75     private Set<String> tags = new HashSet<String>();
76
77     private boolean published;
78
79     private String publicUri;
80
81     private Map<String, Boolean[]> permissions = new HashMap<String, Boolean[]>();
82
83     private String inheritedPermissionsFrom;
84
85     public String getContentType() {
86         return contentType;
87     }
88
89     public String getHash() {
90         return hash;
91     }
92
93     @Override
94         public Date getLastModified() {
95         return lastModified;
96     }
97
98     public String getModifiedBy() {
99         return modifiedBy;
100     }
101
102     public String getName() {
103         return name;
104     }
105
106     public int getVersion() {
107         return version;
108     }
109
110     public Date getVersionTimestamp() {
111         return versionTimestamp;
112     }
113
114     public String getUri() {
115         return "/" + container + "/" + path;
116     }
117
118     public String getOwner() {
119         return owner;
120     }
121
122     public String getPath() {
123         return path;
124     }
125
126     public long getBytes() {
127         return bytes;
128     }
129
130     public String getSizeAsString() {
131         NumberFormat nf = NumberFormat.getFormat("######.#");
132         if (bytes < 1024)
133             return String.valueOf(bytes) + " B";
134         else if (bytes < 1024 * 1024)
135             return nf.format(Double.valueOf(bytes)/(1024)) + " KB";
136         else if (bytes < 1024 * 1024 * 1024)
137             return nf.format(Double.valueOf(bytes)/(1024 * 1024)) + " MB";
138         return nf.format(Double.valueOf(bytes)/(1024 * 1024 * 1024)) + " GB";
139     }
140
141     public boolean isShared() {
142         return !permissions.isEmpty();
143     }
144
145     public void populate(Folder _parent, JSONObject o, String _owner, String _container) {
146         this.parent = _parent;
147         path = unmarshallString(o, "name");
148         if (path.contains("/"))
149             name = path.substring(path.lastIndexOf("/") + 1, path.length()); //strip the prefix
150         else
151             name = path;
152         this.owner = _owner;
153         hash = unmarshallString(o, "hash");
154         bytes = unmarshallLong(o, "bytes");
155         version = unmarshallInt(o, "version");
156         contentType = unmarshallString(o, "content_type");
157         lastModified = unmarshallDate(o, "last_modified");
158         modifiedBy = unmarshallString(o, "modified_by");
159         versionTimestamp = unmarshallDate(o, "version_timestamp");
160         published = o.containsKey("x_object_public") ? true : false;
161         publicUri = unmarshallString(o, "x_object_public");
162         this.container = _container;
163
164         inheritedPermissionsFrom = unmarshallString(o, "x_object_shared_by");
165         String rawPermissions = unmarshallString(o, "x_object_sharing");
166         if (rawPermissions != null)
167             parsePermissions(rawPermissions);
168
169         for (String key : o.keySet())
170             if (key.startsWith("x_object_meta_"))
171                 tags.add(URL.decodePathSegment(key.substring("x_object_meta_".length())).trim().toLowerCase());
172
173         
174     }
175
176     private void parsePermissions(String rawPermissions) {
177         String[] readwrite = rawPermissions.split(";");
178         for (String s : readwrite) {
179             String[] part = s.split("=");
180             String perm = part[0].trim();
181             String[] users = part[1].split(",");
182             for (String u : users) {
183                 String user = u.trim();
184                 Boolean[] userPerm = permissions.get(u);
185                 if (userPerm == null) {
186                     userPerm = new Boolean[2];
187                     permissions.put(user, userPerm);
188                 }
189                 if (perm.equals("read")) {
190                     userPerm[0] = Boolean.TRUE;
191                 }
192                 else if (perm.equals("write")) {
193                     userPerm[1] = Boolean.TRUE;
194                 }
195             }
196         }
197     }
198
199     @Override
200         public boolean equals(Object other) {
201         if (other instanceof File) {
202             File o = (File) other;
203             return name.equals(o.getName());
204         }
205         return false;
206     }
207
208     @Override
209         public int hashCode() {
210         return name.hashCode();
211     }
212
213     public String getContainer() {
214         return container;
215     }
216
217     public static File createFromResponse(String owner, Response response, File result) {
218         result.populate(owner, response);
219         return result;
220     }
221
222     private void populate(String _owner, Response response) {
223         this.owner = _owner;
224         for (Header h : response.getHeaders()) {
225             String header = h.getName();
226             if (header.startsWith("X-Object-Meta-"))
227                 tags.add(URL.decodePathSegment(header.substring("X-Object-Meta-".length())).trim().toLowerCase());
228             else if (header.equals("X-Object-Sharing")) {
229                 String rawPermissions = h.getValue();
230                 parsePermissions(rawPermissions);
231             }
232             else if (header.equals("X-Object-Shared-By")) {
233                 inheritedPermissionsFrom = h.getValue().trim();
234             }
235         }
236     }
237
238     public Folder getParent() {
239         return parent;
240     }
241
242     public Set<String> getTags() {
243         return tags;
244     }
245
246     public boolean isPublished() {
247         return published;
248     }
249
250     public String getPublicUri() {
251         return publicUri;
252     }
253
254     public Map<String, Boolean[]> getPermissions() {
255         return permissions;
256     }
257
258     public String getInheritedPermissionsFrom() {
259         return inheritedPermissionsFrom;
260     }
261 }