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 com.google.gwt.http.client.Response;
39 import com.google.gwt.i18n.client.DateTimeFormat;
40 import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
41 import com.google.gwt.json.client.JSONArray;
42 import com.google.gwt.json.client.JSONObject;
43 import com.google.gwt.json.client.JSONParser;
44 import com.google.gwt.json.client.JSONValue;
45 import java.util.ArrayList;
46 import java.util.Date;
47 import java.util.LinkedHashSet;
48 import java.util.List;
51 public class Folder extends Resource {
53 * The name of the folder. If the folder is a container this is its name. If it is a virtual folder this is the
54 * last part of its path
56 private String name = null;
58 private Date lastModified = null;
60 private long bytesUsed = 0;
62 private Folder parent = null;
64 private Set<Folder> subfolders = new LinkedHashSet<Folder>();
66 * The name of the container that this folder belongs to. If this folder is container, this field equals name
68 private String container = null;
71 * This is the full path of the folder (prefix is a misnomer but it was named so because this is used as a prefix=
72 * parameter in the request that fetches its children). If the folder is a cointainer this is empty string
74 private String prefix = "";
76 private Set<File> files = new LinkedHashSet<File>();
80 public Folder(String name) {
84 public String getName() {
88 public Date getLastModified() {
92 public long getBytesUsed() {
96 public void setLastModified(Date lastModified) {
97 this.lastModified = lastModified;
100 public Set<Folder> getSubfolders() {
104 public void setSubfolders(Set<Folder> subfolders) {
105 this.subfolders = subfolders;
108 public String getContainer() {
112 public String getPrefix() {
116 public void setPrefix(String prefix) {
117 this.prefix = prefix;
120 public void populate(Response response) {
121 String header = response.getHeader("Last-Modified");
123 lastModified = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).parse(header);
125 header = response.getHeader("X-Container-Bytes-Used");
127 bytesUsed = Long.valueOf(header);
129 subfolders.clear(); //This is necessary in case we update a pre-existing Folder so that stale subfolders won't show up
130 JSONValue json = JSONParser.parseStrict(response.getText());
131 JSONArray array = json.isArray();
133 for (int i=0; i<array.size(); i++) {
134 JSONObject o = array.get(i).isObject();
136 String contentType = unmarshallString(o, "content_type");
137 if (o.containsKey("subdir") || (contentType != null && (contentType.startsWith("application/directory") || contentType.startsWith("application/folder")))) {
138 Folder f = new Folder();
139 f.populate(this, o, container);
143 File file = new File();
144 file.populate(this, o, container);
152 public void populate(Folder parent, JSONObject o, String aContainer) {
153 this.parent = parent;
155 if (o.containsKey("subdir")) {
156 path = unmarshallString(o, "subdir");
159 path = unmarshallString(o, "name");
160 lastModified = unmarshallDate(o, "last_modified");
162 if (path.endsWith("/"))
163 path = path.substring(0, path.length() - 1);
164 if (path.contains("/"))
165 name = path.substring(path.lastIndexOf("/") + 1, path.length()); //strip the prefix
168 if (aContainer != null) {
169 container = aContainer;
179 public String getLastModifiedSince() {
180 return null; //To change body of implemented methods use File | Settings | File Templates.
183 public static Folder createFromResponse(Response response, Folder result) {
190 f.populate(response);
195 public boolean equals(Object other) {
196 if (other instanceof Folder) {
197 Folder o = (Folder) other;
198 return (container + prefix).equals(o.getContainer() + o.getPrefix());
204 public int hashCode() {
205 return (container + prefix).hashCode();
208 public Set<File> getFiles() {
212 public Folder getParent() {