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