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 java.util.Date;
39 import java.util.HashMap;
42 import com.google.gwt.http.client.Header;
43 import com.google.gwt.http.client.Response;
44 import com.google.gwt.http.client.URL;
45 import com.google.gwt.i18n.client.DateTimeFormat;
46 import com.google.gwt.i18n.client.NumberFormat;
47 import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
48 import com.google.gwt.json.client.JSONObject;
49 import com.google.gwt.json.client.JSONValue;
51 public class File extends Resource {
60 private String contentType;
62 private Date lastModified;
64 private String modifiedBy;
66 private Date versionTimestamp;
72 private String container;
74 private Folder parent;
76 private Map<String, String> meta = new HashMap<String, String>();
78 private boolean published;
80 private String publicUri;
82 private Map<String, Boolean[]> permissions = new HashMap<String, Boolean[]>();
84 private String inheritedPermissionsFrom;
86 public String getContentType() {
90 public String getHash() {
95 public Date getLastModified() {
99 public String getModifiedBy() {
103 public String getName() {
107 public int getVersion() {
111 public Date getVersionTimestamp() {
112 return versionTimestamp;
115 public String getUri() {
116 return "/" + container + "/" + path;
119 public String getOwner() {
123 public String getPath() {
127 public long getBytes() {
131 public String getSizeAsString() {
132 NumberFormat nf = NumberFormat.getFormat("######.#");
134 return String.valueOf(bytes) + " B";
135 else if (bytes < 1024 * 1024)
136 return nf.format(Double.valueOf(bytes)/(1024)) + " KB";
137 else if (bytes < 1024 * 1024 * 1024)
138 return nf.format(Double.valueOf(bytes)/(1024 * 1024)) + " MB";
139 return nf.format(Double.valueOf(bytes)/(1024 * 1024 * 1024)) + " GB";
142 public boolean isSharedOrPublished() {
143 return !permissions.isEmpty() || published;
146 public boolean isShared() {
147 return !permissions.isEmpty();
150 public void populate(Folder _parent, JSONObject o, String _owner, String _container) {
151 this.parent = _parent;
152 path = unmarshallString(o, "name");
153 if (parent != null && parent.getPrefix().length() > 0)
154 name = path.substring(parent.getPrefix().length() + 1);
158 hash = unmarshallString(o, "hash");
159 bytes = unmarshallLong(o, "bytes");
160 version = unmarshallInt(o, "version");
161 contentType = unmarshallString(o, "content_type");
162 lastModified = unmarshallDate(o, "last_modified");
163 modifiedBy = unmarshallString(o, "modified_by");
164 versionTimestamp = unmarshallDate(o, "version_timestamp");
165 published = o.containsKey("x_object_public") ? true : false;
166 publicUri = unmarshallString(o, "x_object_public");
167 this.container = _container;
169 inheritedPermissionsFrom = unmarshallString(o, "x_object_shared_by");
170 String rawPermissions = unmarshallString(o, "x_object_sharing");
171 if (rawPermissions != null)
172 parsePermissions(rawPermissions);
174 JSONValue metaValue = o.get("x_object_meta");
175 if (metaValue != null) {
176 JSONObject metaObj = metaValue.isObject();
177 if (metaObj != null) {
178 for (String key: metaObj.keySet()) {
179 meta.put(key, unmarshallString(metaObj, key));
185 private void parsePermissions(String rawPermissions) {
186 String[] readwrite = rawPermissions.split(";");
187 for (String s : readwrite) {
188 String[] part = s.split("=");
189 String perm = part[0].trim();
190 String[] users = part[1].split(",");
191 for (String u : users) {
192 String user = u.trim();
193 Boolean[] userPerm = permissions.get(u);
194 if (userPerm == null) {
195 userPerm = new Boolean[2];
196 permissions.put(user, userPerm);
198 if (perm.equals("read")) {
199 userPerm[0] = Boolean.TRUE;
201 else if (perm.equals("write")) {
202 userPerm[1] = Boolean.TRUE;
209 public boolean equals(Object other) {
210 if (other instanceof File) {
211 File o = (File) other;
212 return name.equals(o.getName());
218 public int hashCode() {
219 return name.hashCode();
222 public String getContainer() {
226 public static File createFromResponse(String owner, Response response, File result) {
227 result.populate(owner, response);
231 private void populate(String _owner, Response response) {
236 for (Header h : response.getHeaders()) {
238 continue; //IE bug. h cannot be null in the general case
239 String header = h.getName();
240 if (header.startsWith("X-Object-Meta-"))
241 meta.put(URL.decodePathSegment(header.substring("X-Object-Meta-".length())), URL.decodePathSegment(h.getValue()));
242 else if (header.equals("X-Object-Sharing")) {
243 String rawPermissions = h.getValue();
244 parsePermissions(URL.decodePathSegment(rawPermissions));
246 else if (header.equals("X-Object-Shared-By")) {
247 inheritedPermissionsFrom = URL.decodePathSegment(h.getValue());
249 else if (header.equals("Content-Length")) {
250 bytes = Long.parseLong(h.getValue());
252 else if (header.equals("Content-Type")) {
253 contentType = h.getValue();
255 else if (header.equals("Last-Modified")) {
256 lastModified = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).parse(h.getValue());
258 else if (header.equals("X-Object-Public")) {
260 publicUri = h.getValue();
265 public Folder getParent() {
269 public Map<String, String> getMeta() {
273 public boolean isPublished() {
277 public String getPublicUri() {
281 public Map<String, Boolean[]> getPermissions() {
285 public String getInheritedPermissionsFrom() {
286 return inheritedPermissionsFrom;