root / src / gr / grnet / pithos / web / client / foldertree / Folder.java @ d0efa1ee
History | View | Annotate | Download (7.7 kB)
1 | 6084aa02 | Christos Stathis | /*
|
---|---|---|---|
2 | 63366925 | Christos Stathis | * Copyright 2011 GRNET S.A. All rights reserved.
|
3 | 63366925 | Christos Stathis | *
|
4 | 63366925 | Christos Stathis | * Redistribution and use in source and binary forms, with or
|
5 | 63366925 | Christos Stathis | * without modification, are permitted provided that the following
|
6 | 63366925 | Christos Stathis | * conditions are met:
|
7 | 63366925 | Christos Stathis | *
|
8 | 63366925 | Christos Stathis | * 1. Redistributions of source code must retain the above
|
9 | 63366925 | Christos Stathis | * copyright notice, this list of conditions and the following
|
10 | 63366925 | Christos Stathis | * disclaimer.
|
11 | 63366925 | Christos Stathis | *
|
12 | 63366925 | Christos Stathis | * 2. Redistributions in binary form must reproduce the above
|
13 | 63366925 | Christos Stathis | * copyright notice, this list of conditions and the following
|
14 | 63366925 | Christos Stathis | * disclaimer in the documentation and/or other materials
|
15 | 63366925 | Christos Stathis | * provided with the distribution.
|
16 | 63366925 | Christos Stathis | *
|
17 | 63366925 | Christos Stathis | * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
18 | 63366925 | Christos Stathis | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19 | 63366925 | Christos Stathis | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
20 | 63366925 | Christos Stathis | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
21 | 63366925 | Christos Stathis | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
22 | 63366925 | Christos Stathis | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
23 | 63366925 | Christos Stathis | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
24 | 63366925 | Christos Stathis | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
25 | 63366925 | Christos Stathis | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
26 | 63366925 | Christos Stathis | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
27 | 63366925 | Christos Stathis | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
28 | 63366925 | Christos Stathis | * POSSIBILITY OF SUCH DAMAGE.
|
29 | 63366925 | Christos Stathis | *
|
30 | 63366925 | Christos Stathis | * The views and conclusions contained in the software and
|
31 | 63366925 | Christos Stathis | * documentation are those of the authors and should not be
|
32 | 63366925 | Christos Stathis | * interpreted as representing official policies, either expressed
|
33 | 63366925 | Christos Stathis | * or implied, of GRNET S.A.
|
34 | 6084aa02 | Christos Stathis | */
|
35 | 6084aa02 | Christos Stathis | |
36 | 6084aa02 | Christos Stathis | package gr.grnet.pithos.web.client.foldertree; |
37 | 6084aa02 | Christos Stathis | |
38 | 8e61880b | Christos Stathis | import com.google.gwt.http.client.Response; |
39 | 8e61880b | Christos Stathis | import com.google.gwt.i18n.client.DateTimeFormat; |
40 | 8e61880b | Christos Stathis | import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat; |
41 | 8e61880b | Christos Stathis | import com.google.gwt.json.client.JSONArray; |
42 | 8e61880b | Christos Stathis | import com.google.gwt.json.client.JSONObject; |
43 | 8e61880b | Christos Stathis | import com.google.gwt.json.client.JSONParser; |
44 | 8e61880b | Christos Stathis | import com.google.gwt.json.client.JSONValue; |
45 | 875a0179 | Christos Stathis | import java.util.ArrayList; |
46 | 8e61880b | Christos Stathis | import java.util.Date; |
47 | 2637c9cd | Christos Stathis | import java.util.Iterator; |
48 | 8e61880b | Christos Stathis | import java.util.LinkedHashSet; |
49 | 875a0179 | Christos Stathis | import java.util.List; |
50 | 8e61880b | Christos Stathis | import java.util.Set; |
51 | 6084aa02 | Christos Stathis | |
52 | 8e61880b | Christos Stathis | public class Folder extends Resource { |
53 | 7c818c14 | Christos Stathis | /*
|
54 | 7c818c14 | Christos Stathis | * 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 | 7c818c14 | Christos Stathis | * last part of its path
|
56 | 7c818c14 | Christos Stathis | */
|
57 | 8e61880b | Christos Stathis | private String name = null; |
58 | 6084aa02 | Christos Stathis | |
59 | 8e61880b | Christos Stathis | private Date lastModified = null; |
60 | 6084aa02 | Christos Stathis | |
61 | 8e61880b | Christos Stathis | private long bytesUsed = 0; |
62 | 8e61880b | Christos Stathis | |
63 | 74ebb3a3 | Christos Stathis | private Folder parent = null; |
64 | 74ebb3a3 | Christos Stathis | |
65 | 8e61880b | Christos Stathis | private Set<Folder> subfolders = new LinkedHashSet<Folder>(); |
66 | 7c818c14 | Christos Stathis | /*
|
67 | 7c818c14 | Christos Stathis | * The name of the container that this folder belongs to. If this folder is container, this field equals name
|
68 | 7c818c14 | Christos Stathis | */
|
69 | 8e61880b | Christos Stathis | private String container = null; |
70 | 8e61880b | Christos Stathis | |
71 | 7c818c14 | Christos Stathis | /*
|
72 | 7c818c14 | Christos Stathis | * 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 | 7c818c14 | Christos Stathis | * parameter in the request that fetches its children). If the folder is a cointainer this is empty string
|
74 | 7c818c14 | Christos Stathis | */
|
75 | 8e61880b | Christos Stathis | private String prefix = ""; |
76 | 8e61880b | Christos Stathis | |
77 | 875a0179 | Christos Stathis | private Set<File> files = new LinkedHashSet<File>(); |
78 | 875a0179 | Christos Stathis | |
79 | 2637c9cd | Christos Stathis | private boolean inTrash = false; |
80 | 2637c9cd | Christos Stathis | |
81 | 8e61880b | Christos Stathis | public Folder() {};
|
82 | 6084aa02 | Christos Stathis | |
83 | 6084aa02 | Christos Stathis | public Folder(String name) { |
84 | 6084aa02 | Christos Stathis | this.name = name;
|
85 | 6084aa02 | Christos Stathis | } |
86 | 8e61880b | Christos Stathis | |
87 | 8e61880b | Christos Stathis | public String getName() { |
88 | 8e61880b | Christos Stathis | return name;
|
89 | 8e61880b | Christos Stathis | } |
90 | 6084aa02 | Christos Stathis | |
91 | 8e61880b | Christos Stathis | public Date getLastModified() { |
92 | 8e61880b | Christos Stathis | return lastModified;
|
93 | 6084aa02 | Christos Stathis | } |
94 | 6084aa02 | Christos Stathis | |
95 | 8e61880b | Christos Stathis | public long getBytesUsed() { |
96 | 8e61880b | Christos Stathis | return bytesUsed;
|
97 | 6084aa02 | Christos Stathis | } |
98 | 6084aa02 | Christos Stathis | |
99 | 8e61880b | Christos Stathis | public void setLastModified(Date lastModified) { |
100 | 8e61880b | Christos Stathis | this.lastModified = lastModified;
|
101 | 8e61880b | Christos Stathis | } |
102 | 8e61880b | Christos Stathis | |
103 | 8e61880b | Christos Stathis | public Set<Folder> getSubfolders() { |
104 | 6084aa02 | Christos Stathis | return subfolders;
|
105 | 6084aa02 | Christos Stathis | } |
106 | 6084aa02 | Christos Stathis | |
107 | 8e61880b | Christos Stathis | public void setSubfolders(Set<Folder> subfolders) { |
108 | 6084aa02 | Christos Stathis | this.subfolders = subfolders;
|
109 | 6084aa02 | Christos Stathis | } |
110 | 8e61880b | Christos Stathis | |
111 | 8e61880b | Christos Stathis | public String getContainer() { |
112 | 8e61880b | Christos Stathis | return container;
|
113 | 8e61880b | Christos Stathis | } |
114 | 8e61880b | Christos Stathis | |
115 | 8e61880b | Christos Stathis | public String getPrefix() { |
116 | 8e61880b | Christos Stathis | return prefix;
|
117 | 8e61880b | Christos Stathis | } |
118 | 8e61880b | Christos Stathis | |
119 | 8e61880b | Christos Stathis | public void setPrefix(String prefix) { |
120 | 8e61880b | Christos Stathis | this.prefix = prefix;
|
121 | 8e61880b | Christos Stathis | } |
122 | 8e61880b | Christos Stathis | |
123 | 8e61880b | Christos Stathis | public void populate(Response response) { |
124 | 8e61880b | Christos Stathis | String header = response.getHeader("Last-Modified"); |
125 | 8e61880b | Christos Stathis | if (header != null) |
126 | 8e61880b | Christos Stathis | lastModified = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).parse(header); |
127 | 8e61880b | Christos Stathis | |
128 | 8e61880b | Christos Stathis | header = response.getHeader("X-Container-Bytes-Used");
|
129 | 8e61880b | Christos Stathis | if (header != null) |
130 | 8e61880b | Christos Stathis | bytesUsed = Long.valueOf(header);
|
131 | 8e61880b | Christos Stathis | |
132 | 2637c9cd | Christos Stathis | header = response.getHeader("X-Object-Meta-Trash");
|
133 | 2637c9cd | Christos Stathis | if (header != null && header.equals("true")) |
134 | 2637c9cd | Christos Stathis | inTrash = true;
|
135 | 2637c9cd | Christos Stathis | |
136 | 74ebb3a3 | Christos Stathis | subfolders.clear(); //This is necessary in case we update a pre-existing Folder so that stale subfolders won't show up
|
137 | d4374639 | Christos Stathis | files.clear(); |
138 | 8e61880b | Christos Stathis | JSONValue json = JSONParser.parseStrict(response.getText()); |
139 | 8e61880b | Christos Stathis | JSONArray array = json.isArray(); |
140 | 8e61880b | Christos Stathis | if (array != null) { |
141 | 8e61880b | Christos Stathis | for (int i=0; i<array.size(); i++) { |
142 | 8e61880b | Christos Stathis | JSONObject o = array.get(i).isObject(); |
143 | 8e61880b | Christos Stathis | if (o != null) { |
144 | 7c818c14 | Christos Stathis | String contentType = unmarshallString(o, "content_type"); |
145 | 07a1b5fe | Christos Stathis | if (o.containsKey("subdir") || (contentType != null && (contentType.startsWith("application/directory") || contentType.startsWith("application/folder")))) { |
146 | 8e61880b | Christos Stathis | Folder f = new Folder();
|
147 | 74ebb3a3 | Christos Stathis | f.populate(this, o, container);
|
148 | 8e61880b | Christos Stathis | subfolders.add(f); |
149 | 8e61880b | Christos Stathis | } |
150 | d0efa1ee | Christos Stathis | else if (!(o.containsKey("x_object_meta_trash") && o.get("x_object_meta_trash").isString().stringValue().equals("true"))) { |
151 | 875a0179 | Christos Stathis | File file = new File(); |
152 | 74ebb3a3 | Christos Stathis | file.populate(this, o, container);
|
153 | 875a0179 | Christos Stathis | files.add(file); |
154 | 8e61880b | Christos Stathis | } |
155 | 8e61880b | Christos Stathis | } |
156 | 8e61880b | Christos Stathis | } |
157 | d0efa1ee | Christos Stathis | //This step is necessary to remove the trashed folders. Trashed folders are added initially because we need to
|
158 | d0efa1ee | Christos Stathis | //avoid having in the list the virtual folders of the form {"subdir":"folder1"} which have no indication of thrash
|
159 | 2637c9cd | Christos Stathis | Iterator<Folder> iter = subfolders.iterator();
|
160 | 2637c9cd | Christos Stathis | while (iter.hasNext()) {
|
161 | 2637c9cd | Christos Stathis | Folder f = iter.next(); |
162 | 2637c9cd | Christos Stathis | if (f.isInTrash())
|
163 | 2637c9cd | Christos Stathis | iter.remove(); |
164 | 2637c9cd | Christos Stathis | } |
165 | 8e61880b | Christos Stathis | } |
166 | 8e61880b | Christos Stathis | } |
167 | 8e61880b | Christos Stathis | |
168 | 74ebb3a3 | Christos Stathis | public void populate(Folder parent, JSONObject o, String aContainer) { |
169 | 74ebb3a3 | Christos Stathis | this.parent = parent;
|
170 | 7c818c14 | Christos Stathis | String path = null; |
171 | 8e61880b | Christos Stathis | if (o.containsKey("subdir")) { |
172 | 7c818c14 | Christos Stathis | path = unmarshallString(o, "subdir");
|
173 | 8e61880b | Christos Stathis | } |
174 | 8e61880b | Christos Stathis | else {
|
175 | 7c818c14 | Christos Stathis | path = unmarshallString(o, "name");
|
176 | 8e61880b | Christos Stathis | lastModified = unmarshallDate(o, "last_modified");
|
177 | 8e61880b | Christos Stathis | } |
178 | 7c818c14 | Christos Stathis | if (path.endsWith("/")) |
179 | 7c818c14 | Christos Stathis | path = path.substring(0, path.length() - 1); |
180 | 7c818c14 | Christos Stathis | if (path.contains("/")) |
181 | 7c818c14 | Christos Stathis | name = path.substring(path.lastIndexOf("/") + 1, path.length()); //strip the prefix |
182 | 7c818c14 | Christos Stathis | else
|
183 | 7c818c14 | Christos Stathis | name = path; |
184 | 7c818c14 | Christos Stathis | if (aContainer != null) { |
185 | 7c818c14 | Christos Stathis | container = aContainer; |
186 | 7c818c14 | Christos Stathis | prefix = path; |
187 | 7c818c14 | Christos Stathis | } |
188 | 7c818c14 | Christos Stathis | else {
|
189 | 7c818c14 | Christos Stathis | container = name; |
190 | 7c818c14 | Christos Stathis | prefix = "";
|
191 | 7c818c14 | Christos Stathis | } |
192 | 2637c9cd | Christos Stathis | if (o.containsKey("x_object_meta_trash") && o.get("x_object_meta_trash").isString().stringValue().equals("true")) |
193 | 2637c9cd | Christos Stathis | inTrash = true;
|
194 | 8e61880b | Christos Stathis | } |
195 | 8e61880b | Christos Stathis | |
196 | 8e61880b | Christos Stathis | public static Folder createFromResponse(Response response, Folder result) { |
197 | 8e61880b | Christos Stathis | Folder f = null;
|
198 | 8e61880b | Christos Stathis | if (result == null) |
199 | 8e61880b | Christos Stathis | f = new Folder();
|
200 | 8e61880b | Christos Stathis | else
|
201 | 8e61880b | Christos Stathis | f = result; |
202 | 8e61880b | Christos Stathis | |
203 | 8e61880b | Christos Stathis | f.populate(response); |
204 | 8e61880b | Christos Stathis | return f;
|
205 | 8e61880b | Christos Stathis | } |
206 | 8e61880b | Christos Stathis | |
207 | 8e61880b | Christos Stathis | @Override
|
208 | 8e61880b | Christos Stathis | public boolean equals(Object other) { |
209 | 8e61880b | Christos Stathis | if (other instanceof Folder) { |
210 | 8e61880b | Christos Stathis | Folder o = (Folder) other; |
211 | b651e67f | Christos Stathis | return (container + prefix).equals(o.getContainer() + o.getPrefix());
|
212 | 8e61880b | Christos Stathis | } |
213 | 8e61880b | Christos Stathis | return false; |
214 | 8e61880b | Christos Stathis | } |
215 | 8e61880b | Christos Stathis | |
216 | 8e61880b | Christos Stathis | @Override
|
217 | 8e61880b | Christos Stathis | public int hashCode() { |
218 | b651e67f | Christos Stathis | return (container + prefix).hashCode();
|
219 | 8e61880b | Christos Stathis | } |
220 | 875a0179 | Christos Stathis | |
221 | 875a0179 | Christos Stathis | public Set<File> getFiles() { |
222 | 875a0179 | Christos Stathis | return files;
|
223 | 875a0179 | Christos Stathis | } |
224 | 74ebb3a3 | Christos Stathis | |
225 | 74ebb3a3 | Christos Stathis | public Folder getParent() {
|
226 | 74ebb3a3 | Christos Stathis | return parent;
|
227 | 74ebb3a3 | Christos Stathis | } |
228 | bdda6b2f | Christos Stathis | |
229 | bdda6b2f | Christos Stathis | public String getUri() { |
230 | bdda6b2f | Christos Stathis | return "/" + container + (prefix.length() == 0 ? "" : "/" + prefix); |
231 | bdda6b2f | Christos Stathis | } |
232 | 2637c9cd | Christos Stathis | |
233 | 2637c9cd | Christos Stathis | public boolean isInTrash() { |
234 | 2637c9cd | Christos Stathis | return inTrash;
|
235 | 2637c9cd | Christos Stathis | } |
236 | 6084aa02 | Christos Stathis | } |