2 * Copyright 2011 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
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.
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.
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.
36 package gr.grnet.pithos.web.client.foldertree;
38 import gr.grnet.pithos.web.client.Pithos;
40 import java.util.Date;
41 import java.util.HashMap;
42 import java.util.Iterator;
43 import java.util.LinkedHashSet;
47 import com.google.gwt.http.client.Response;
48 import com.google.gwt.i18n.client.DateTimeFormat;
49 import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
50 import com.google.gwt.json.client.JSONArray;
51 import com.google.gwt.json.client.JSONObject;
52 import com.google.gwt.json.client.JSONParser;
53 import com.google.gwt.json.client.JSONValue;
55 public class Folder extends Resource {
57 * The name of the folder. If the folder is a container this is its name. If it is a virtual folder this is the
58 * last part of its path
60 private String name = null;
62 private Date lastModified = null;
64 private long bytesUsed = 0;
66 private Folder parent = null;
68 private Set<Folder> subfolders = new LinkedHashSet<Folder>();
70 * The name of the container that this folder belongs to. If this folder is container, this field equals name
72 private String container = null;
75 * This is the full path of the folder (prefix is a misnomer but it was named so because this is used as a prefix=
76 * parameter in the request that fetches its children). If the folder is a cointainer this is empty string
78 private String prefix = "";
80 private Set<File> files = new LinkedHashSet<File>();
82 private Set<String> tags = new LinkedHashSet<String>();
86 private Map<String, Boolean[]> permissions = new HashMap<String, Boolean[]>();
88 private String inheritedPermissionsFrom;
92 public Folder(String name) {
96 public String getName() {
100 public Date getLastModified() {
104 public long getBytesUsed() {
108 public void setLastModified(Date lastModified) {
109 this.lastModified = lastModified;
112 public Set<Folder> getSubfolders() {
116 public void setSubfolders(Set<Folder> subfolders) {
117 this.subfolders = subfolders;
120 public String getContainer() {
124 public String getPrefix() {
128 public void setPrefix(String prefix) {
129 this.prefix = prefix;
132 private void parsePermissions(String rawPermissions) {
133 String[] readwrite = rawPermissions.split(";");
134 for (String s : readwrite) {
135 String[] part = s.split("=");
136 String perm = part[0].trim();
137 String[] users = part[1].split(",");
138 for (String u : users) {
139 String user = u.trim();
140 Boolean[] userPerm = permissions.get(u);
141 if (userPerm == null) {
142 userPerm = new Boolean[2];
143 permissions.put(user, userPerm);
145 if (perm.equals("read")) {
146 userPerm[0] = Boolean.TRUE;
148 else if (perm.equals("write")) {
149 userPerm[1] = Boolean.TRUE;
155 public void populate(String _owner, Response response) {
157 String header = response.getHeader("Last-Modified");
159 lastModified = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).parse(header);
161 header = response.getHeader("X-Container-Bytes-Used");
163 bytesUsed = Long.valueOf(header);
165 header = response.getHeader("X-Container-Object-Meta");
166 if (header != null && header.length() > 0) {
167 for (String t : header.split(",")) {
168 tags.add(t.toLowerCase().trim());
172 inheritedPermissionsFrom = response.getHeader("X-Object-Shared-By");
173 String rawPermissions = response.getHeader("X-Object-Sharing");
174 if (rawPermissions != null)
175 parsePermissions(rawPermissions);
177 subfolders.clear(); //This is necessary in case we update a pre-existing Folder so that stale subfolders won't show up
179 JSONValue json = JSONParser.parseStrict(response.getText());
180 JSONArray array = json.isArray();
182 for (int i=0; i<array.size(); i++) {
183 JSONObject o = array.get(i).isObject();
185 String contentType = unmarshallString(o, "content_type");
186 if (o.containsKey("subdir") || (contentType != null && (contentType.startsWith("application/directory") || contentType.startsWith("application/folder")))) {
187 Folder f = new Folder();
188 f.populate(this, o, _owner, container);
192 File file = new File();
193 file.populate(this, o, _owner, container);
201 public void populate(Folder _parent, JSONObject o, String _owner, String aContainer) {
202 this.parent = _parent;
204 if (o.containsKey("subdir")) {
205 path = unmarshallString(o, "subdir");
208 path = unmarshallString(o, "name");
209 lastModified = unmarshallDate(o, "last_modified");
211 if (path.endsWith("/"))
212 path = path.substring(0, path.length() - 1);
213 if (path.contains("/"))
214 name = path.substring(path.lastIndexOf("/") + 1, path.length()); //strip the prefix
217 if (aContainer != null) {
218 container = aContainer;
227 inheritedPermissionsFrom = unmarshallString(o, "x_object_shared_by");
228 String rawPermissions = unmarshallString(o, "x_object_sharing");
229 if (rawPermissions != null)
230 parsePermissions(rawPermissions);
233 public static Folder createFromResponse(String owner, Response response, Folder result) {
240 f.populate(owner, response);
245 public boolean equals(Object other) {
246 if (other instanceof Folder) {
247 Folder o = (Folder) other;
248 return getUri().equals(o.getUri());
254 public int hashCode() {
255 return getUri().hashCode();
258 public Set<File> getFiles() {
262 public Folder getParent() {
266 public String getUri() {
267 return "/" + container + (prefix.length() == 0 ? "" : "/" + prefix);
270 public boolean isContainer() {
271 return parent == null;
274 public void setContainer(String container) {
275 this.container = container;
278 public Set<String> getTags() {
282 public String getInheritedPermissionsFrom() {
283 return inheritedPermissionsFrom;
286 public Map<String, Boolean[]> getPermissions() {
290 public String getOwner() {
294 public boolean existChildrenPermissions() {
296 if (!f.getPermissions().isEmpty() && f.getInheritedPermissionsFrom() == null)
299 for (Folder fo : subfolders)
300 if ((!fo.getPermissions().isEmpty() && fo.getInheritedPermissionsFrom() == null) || fo.existChildrenPermissions())
305 public boolean isShared() {
306 return !permissions.isEmpty();
314 public boolean isTrash() {
315 return isContainer() && name.equals(Pithos.TRASH_CONTAINER);
323 public boolean isInTrash() {
324 return container.equals(Pithos.TRASH_CONTAINER);
327 public boolean isHome() {
328 return isContainer() && name.equals(Pithos.HOME_CONTAINER);