root / src / gr / grnet / pithos / web / client / foldertree / Folder.java @ 9a849658
History | View | Annotate | Download (10 kB)
1 |
/*
|
---|---|
2 |
* Copyright 2011-2013 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 com.google.gwt.core.client.GWT; |
39 |
import com.google.gwt.http.client.Response; |
40 |
import com.google.gwt.http.client.URL; |
41 |
import com.google.gwt.i18n.client.DateTimeFormat; |
42 |
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat; |
43 |
import com.google.gwt.json.client.JSONArray; |
44 |
import com.google.gwt.json.client.JSONObject; |
45 |
import com.google.gwt.json.client.JSONParser; |
46 |
import com.google.gwt.json.client.JSONValue; |
47 |
import gr.grnet.pithos.web.client.Const; |
48 |
import gr.grnet.pithos.web.client.Resource; |
49 |
|
50 |
import java.util.*; |
51 |
|
52 |
public class Folder extends Resource { |
53 |
/*
|
54 |
* The name of the folder. If the folder is a container this is its name. If it is a virtual folder this is the
|
55 |
* last part of its path
|
56 |
*/
|
57 |
private String name = null; |
58 |
|
59 |
private Date lastModified = null; |
60 |
|
61 |
private long bytesUsed = 0; |
62 |
|
63 |
private Folder parent = null; |
64 |
|
65 |
private Set<Folder> subfolders = new LinkedHashSet<Folder>(); |
66 |
/*
|
67 |
* The name of the container that this folder belongs to. If this folder is container, this field equals name
|
68 |
*/
|
69 |
private String container = null; |
70 |
|
71 |
/*
|
72 |
* This is the full path of the folder (prefix is a misnomer but it was named so because this is used as a prefix=
|
73 |
* parameter in the request that fetches its children). If the folder is a cointainer this is empty string
|
74 |
*/
|
75 |
private String prefix = ""; |
76 |
|
77 |
private Set<File> files = new LinkedHashSet<File>(); |
78 |
|
79 |
private String ownerID; |
80 |
|
81 |
private Map<String, Boolean[]> permissions = new HashMap<String, Boolean[]>(); |
82 |
|
83 |
private String inheritedPermissionsFrom; |
84 |
|
85 |
public Folder() {};
|
86 |
|
87 |
public Folder(String name) { |
88 |
this.name = name;
|
89 |
} |
90 |
|
91 |
public String getName() { |
92 |
return name;
|
93 |
} |
94 |
|
95 |
@Override
|
96 |
public Date getLastModified() { |
97 |
return lastModified;
|
98 |
} |
99 |
|
100 |
public long getBytesUsed() { |
101 |
return bytesUsed;
|
102 |
} |
103 |
|
104 |
public Set<Folder> getSubfolders() { |
105 |
return subfolders;
|
106 |
} |
107 |
|
108 |
public void setSubfolders(Set<Folder> subfolders) { |
109 |
this.subfolders = subfolders;
|
110 |
} |
111 |
|
112 |
public String getContainer() { |
113 |
return container;
|
114 |
} |
115 |
|
116 |
public String getPrefix() { |
117 |
return prefix;
|
118 |
} |
119 |
|
120 |
private void parsePermissions(String rawPermissions) { |
121 |
String[] readwrite = rawPermissions.split(";"); |
122 |
for (String s : readwrite) { |
123 |
String[] part = s.split("="); |
124 |
String perm = part[0].trim(); |
125 |
String[] users = part[1].split(","); |
126 |
for (String u : users) { |
127 |
String user = u.trim();
|
128 |
Boolean[] userPerm = permissions.get(u); |
129 |
if (userPerm == null) { |
130 |
userPerm = new Boolean[2]; |
131 |
permissions.put(user, userPerm); |
132 |
} |
133 |
if (perm.equals("read")) { |
134 |
userPerm[0] = Boolean.TRUE; |
135 |
} |
136 |
else if (perm.equals("write")) { |
137 |
userPerm[1] = Boolean.TRUE; |
138 |
} |
139 |
} |
140 |
} |
141 |
} |
142 |
|
143 |
public void populate(String _owner, Response response) { |
144 |
this.ownerID = _owner;
|
145 |
String header = response.getHeader("Last-Modified"); |
146 |
if (header != null) |
147 |
try {
|
148 |
lastModified = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).parse(header); |
149 |
} catch (IllegalArgumentException e) { |
150 |
GWT.log("Last-Modified will be set to null", e);
|
151 |
lastModified = null;
|
152 |
} |
153 |
|
154 |
header = response.getHeader("X-Container-Bytes-Used");
|
155 |
if (header != null && header.length() > 0) |
156 |
bytesUsed = Long.valueOf(header);
|
157 |
|
158 |
String rawPermissions = response.getHeader("X-Object-Sharing"); |
159 |
if (rawPermissions != null && rawPermissions.length() > 0) { |
160 |
parsePermissions(URL.decodePathSegment(rawPermissions));
|
161 |
} |
162 |
|
163 |
if (response.getText() == null || response.getText().isEmpty()) |
164 |
return;
|
165 |
JSONValue json = JSONParser.parseStrict(response.getText()); |
166 |
JSONArray array = json.isArray(); |
167 |
if (array != null) { |
168 |
subfolders.clear(); //This is necessary in case we update a pre-existing Folder so that stale subfolders won't show up
|
169 |
files.clear(); |
170 |
for (int i=0; i<array.size(); i++) { |
171 |
JSONObject o = array.get(i).isObject(); |
172 |
if (o != null) { |
173 |
String contentType = unmarshallString(o, "content_type"); |
174 |
if (o.containsKey("subdir") || (contentType != null && (contentType.startsWith("application/directory") || contentType.startsWith("application/folder")))) { |
175 |
Folder f = new Folder();
|
176 |
f.populate(this, o, _owner, container);
|
177 |
if (f.getName().length() > 0) |
178 |
subfolders.add(f); |
179 |
} |
180 |
else {
|
181 |
File file = new File(); |
182 |
file.populate(this, o, _owner, container);
|
183 |
if (file.getName().length() > 0) |
184 |
files.add(file); |
185 |
} |
186 |
} |
187 |
} |
188 |
} |
189 |
} |
190 |
|
191 |
public void populate(Folder _parent, JSONObject o, String _owner, String aContainer) { |
192 |
this.parent = _parent;
|
193 |
String path = null; |
194 |
if (o.containsKey("subdir")) { |
195 |
path = unmarshallString(o, "subdir");
|
196 |
if (path.endsWith("/")) { //Always true for "subdir" |
197 |
path = path.substring(0, path.length() - 1); |
198 |
} |
199 |
if (parent != null && parent.getPrefix().length() > 0) |
200 |
name = path.substring(parent.getPrefix().length() + 1);
|
201 |
else
|
202 |
name = path; |
203 |
if (name.equals("/")) |
204 |
name = "";
|
205 |
} |
206 |
else {
|
207 |
path = unmarshallString(o, "name");
|
208 |
lastModified = unmarshallDate(o, "last_modified");
|
209 |
if (parent != null && parent.getPrefix().length() > 0) |
210 |
name = path.substring(parent.getPrefix().length() + 1);
|
211 |
else
|
212 |
name = path; |
213 |
} |
214 |
if (aContainer != null) { |
215 |
container = aContainer; |
216 |
prefix = path; |
217 |
} |
218 |
else {
|
219 |
container = name; |
220 |
prefix = "";
|
221 |
} |
222 |
this.ownerID = _owner;
|
223 |
|
224 |
inheritedPermissionsFrom = unmarshallString(o, "x_object_shared_by");
|
225 |
String rawPermissions = unmarshallString(o, "x_object_sharing"); |
226 |
if (rawPermissions != null) |
227 |
parsePermissions(rawPermissions); |
228 |
} |
229 |
|
230 |
public static Folder createFromResponse(String owner, Response response, Folder result) { |
231 |
Folder f = null;
|
232 |
if (result == null) |
233 |
f = new Folder();
|
234 |
else
|
235 |
f = result; |
236 |
|
237 |
f.populate(owner, response); |
238 |
return f;
|
239 |
} |
240 |
|
241 |
@Override
|
242 |
public boolean equals(Object other) { |
243 |
if (other instanceof Folder) { |
244 |
Folder o = (Folder) other; |
245 |
return (ownerID == null ? true : ownerID.equals(o.getOwnerID())) |
246 |
&& (getUri().equals(o.getUri())); |
247 |
} |
248 |
return false; |
249 |
} |
250 |
|
251 |
@Override
|
252 |
public int hashCode() { |
253 |
return getUri().hashCode();
|
254 |
} |
255 |
|
256 |
public Set<File> getFiles() { |
257 |
return files;
|
258 |
} |
259 |
|
260 |
public Folder getParent() {
|
261 |
return parent;
|
262 |
} |
263 |
|
264 |
public String getUri() { |
265 |
return "/" + container + (prefix.length() == 0 ? "" : "/" + prefix); |
266 |
} |
267 |
|
268 |
public boolean isContainer() { |
269 |
return parent == null; |
270 |
} |
271 |
|
272 |
public void setContainer(String container) { |
273 |
this.container = container;
|
274 |
} |
275 |
|
276 |
public String getInheritedPermissionsFrom() { |
277 |
return inheritedPermissionsFrom;
|
278 |
} |
279 |
|
280 |
public Map<String, Boolean[]> getPermissions() { |
281 |
return permissions;
|
282 |
} |
283 |
|
284 |
public String getOwnerID() { |
285 |
return ownerID;
|
286 |
} |
287 |
|
288 |
public boolean isShared() { |
289 |
return !permissions.isEmpty();
|
290 |
} |
291 |
|
292 |
/**
|
293 |
* I am THE trash
|
294 |
*
|
295 |
* @return
|
296 |
*/
|
297 |
public boolean isTrash() { |
298 |
return isContainer() && name.equals(Const.TRASH_CONTAINER);
|
299 |
} |
300 |
|
301 |
/**
|
302 |
* I am IN THE trash
|
303 |
*
|
304 |
* @return
|
305 |
*/
|
306 |
public boolean isInTrash() { |
307 |
return container.equals(Const.TRASH_CONTAINER);
|
308 |
} |
309 |
|
310 |
public boolean isHome() { |
311 |
return isContainer() && name.equals(Const.HOME_CONTAINER);
|
312 |
} |
313 |
|
314 |
public boolean contains(Folder folder) { |
315 |
for (Folder f : subfolders)
|
316 |
if (f.equals(folder) || f.contains(folder))
|
317 |
return true; |
318 |
return false; |
319 |
} |
320 |
|
321 |
@Override
|
322 |
public String toString() { |
323 |
return "Folder(container="+container+", name="+name+", parent="+(parent == null ? parent : parent.getName())+", prefix="+prefix+")"; |
324 |
} |
325 |
} |