2 * Copyright 2011-2012 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.LinkedHashSet;
46 import com.google.gwt.core.client.GWT;
47 import com.google.gwt.http.client.Response;
48 import com.google.gwt.http.client.URL;
49 import com.google.gwt.i18n.client.DateTimeFormat;
50 import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
51 import com.google.gwt.json.client.JSONArray;
52 import com.google.gwt.json.client.JSONObject;
53 import com.google.gwt.json.client.JSONParser;
54 import com.google.gwt.json.client.JSONValue;
56 public class Folder extends Resource {
58 * The name of the folder. If the folder is a container this is its name. If it is a virtual folder this is the
59 * last part of its path
61 private String name = null;
63 private Date lastModified = null;
65 private long bytesUsed = 0;
67 private Folder parent = null;
69 private Set<Folder> subfolders = new LinkedHashSet<Folder>();
71 * The name of the container that this folder belongs to. If this folder is container, this field equals name
73 private String container = null;
76 * This is the full path of the folder (prefix is a misnomer but it was named so because this is used as a prefix=
77 * parameter in the request that fetches its children). If the folder is a cointainer this is empty string
79 private String prefix = "";
81 private Set<File> files = new LinkedHashSet<File>();
85 private Map<String, Boolean[]> permissions = new HashMap<String, Boolean[]>();
87 private String inheritedPermissionsFrom;
91 public Folder(String name) {
95 public String getName() {
100 public Date getLastModified() {
104 public long getBytesUsed() {
108 public Set<Folder> getSubfolders() {
112 public void setSubfolders(Set<Folder> subfolders) {
113 this.subfolders = subfolders;
116 public String getContainer() {
120 public String getPrefix() {
124 private void parsePermissions(String rawPermissions) {
125 String[] readwrite = rawPermissions.split(";");
126 for (String s : readwrite) {
127 String[] part = s.split("=");
128 String perm = part[0].trim();
129 String[] users = part[1].split(",");
130 for (String u : users) {
131 String user = u.trim();
132 Boolean[] userPerm = permissions.get(u);
133 if (userPerm == null) {
134 userPerm = new Boolean[2];
135 permissions.put(user, userPerm);
137 if (perm.equals("read")) {
138 userPerm[0] = Boolean.TRUE;
140 else if (perm.equals("write")) {
141 userPerm[1] = Boolean.TRUE;
147 public void populate(String _owner, Response response) {
149 String header = response.getHeader("Last-Modified");
152 lastModified = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).parse(header);
153 } catch (IllegalArgumentException e) {
154 GWT.log("Last-Modified will be set to null", e);
158 header = response.getHeader("X-Container-Bytes-Used");
159 if (header != null && header.length() > 0)
160 bytesUsed = Long.valueOf(header);
162 String rawPermissions = response.getHeader("X-Object-Sharing");
163 if (rawPermissions != null && rawPermissions.length() > 0) {
164 parsePermissions(URL.decodePathSegment(rawPermissions));
167 if (response.getText() == null || response.getText().isEmpty())
169 JSONValue json = JSONParser.parseStrict(response.getText());
170 JSONArray array = json.isArray();
172 subfolders.clear(); //This is necessary in case we update a pre-existing Folder so that stale subfolders won't show up
174 for (int i=0; i<array.size(); i++) {
175 JSONObject o = array.get(i).isObject();
177 String contentType = unmarshallString(o, "content_type");
178 if (o.containsKey("subdir") || (contentType != null && (contentType.startsWith("application/directory") || contentType.startsWith("application/folder")))) {
179 Folder f = new Folder();
180 f.populate(this, o, _owner, container);
181 if (f.getName().length() > 0)
185 File file = new File();
186 file.populate(this, o, _owner, container);
187 if (file.getName().length() > 0)
195 public void populate(Folder _parent, JSONObject o, String _owner, String aContainer) {
196 this.parent = _parent;
198 if (o.containsKey("subdir")) {
199 path = unmarshallString(o, "subdir");
200 if (path.endsWith("/")) { //Always true for "subdir"
201 path = path.substring(0, path.length() - 1);
203 if (parent != null && parent.getPrefix().length() > 0)
204 name = path.substring(parent.getPrefix().length() + 1);
207 if (name.equals("/"))
211 path = unmarshallString(o, "name");
212 lastModified = unmarshallDate(o, "last_modified");
213 if (parent != null && parent.getPrefix().length() > 0)
214 name = path.substring(parent.getPrefix().length() + 1);
218 if (aContainer != null) {
219 container = aContainer;
228 inheritedPermissionsFrom = unmarshallString(o, "x_object_shared_by");
229 String rawPermissions = unmarshallString(o, "x_object_sharing");
230 if (rawPermissions != null)
231 parsePermissions(rawPermissions);
234 public static Folder createFromResponse(String owner, Response response, Folder result) {
241 f.populate(owner, response);
246 public boolean equals(Object other) {
247 if (other instanceof Folder) {
248 Folder o = (Folder) other;
249 return owner.equals(o.getOwner()) && getUri().equals(o.getUri());
255 public int hashCode() {
256 return getUri().hashCode();
259 public Set<File> getFiles() {
263 public Folder getParent() {
267 public String getUri() {
268 return "/" + container + (prefix.length() == 0 ? "" : "/" + prefix);
271 public boolean isContainer() {
272 return parent == null;
275 public void setContainer(String container) {
276 this.container = container;
279 public String getInheritedPermissionsFrom() {
280 return inheritedPermissionsFrom;
283 public Map<String, Boolean[]> getPermissions() {
287 public String getOwner() {
291 public boolean isShared() {
292 return !permissions.isEmpty();
300 public boolean isTrash() {
301 return isContainer() && name.equals(Pithos.TRASH_CONTAINER);
309 public boolean isInTrash() {
310 return container.equals(Pithos.TRASH_CONTAINER);
313 public boolean isHome() {
314 return isContainer() && name.equals(Pithos.HOME_CONTAINER);
317 public boolean contains(Folder folder) {
318 for (Folder f : subfolders)
319 if (f.equals(folder) || f.contains(folder))