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 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.NumberFormat;
46 import com.google.gwt.json.client.JSONObject;
47 import com.google.gwt.json.client.JSONValue;
49 public class File extends Resource {
58 private String contentType;
60 private Date lastModified;
62 private String modifiedBy;
64 private Date versionTimestamp;
70 private String container;
72 private Folder parent;
74 private Map<String, String> meta = new HashMap<String, String>();
76 private boolean published;
78 private String publicUri;
80 private Map<String, Boolean[]> permissions = new HashMap<String, Boolean[]>();
82 private String inheritedPermissionsFrom;
84 public String getContentType() {
88 public String getHash() {
93 public Date getLastModified() {
97 public String getModifiedBy() {
101 public String getName() {
105 public int getVersion() {
109 public Date getVersionTimestamp() {
110 return versionTimestamp;
113 public String getUri() {
114 return "/" + container + "/" + path;
117 public String getOwner() {
121 public String getPath() {
125 public long getBytes() {
129 public String getSizeAsString() {
130 NumberFormat nf = NumberFormat.getFormat("######.#");
132 return String.valueOf(bytes) + " B";
133 else if (bytes < 1024 * 1024)
134 return nf.format(Double.valueOf(bytes)/(1024)) + " KB";
135 else if (bytes < 1024 * 1024 * 1024)
136 return nf.format(Double.valueOf(bytes)/(1024 * 1024)) + " MB";
137 return nf.format(Double.valueOf(bytes)/(1024 * 1024 * 1024)) + " GB";
140 public boolean isShared() {
141 return !permissions.isEmpty() || published;
144 public void populate(Folder _parent, JSONObject o, String _owner, String _container) {
145 this.parent = _parent;
146 path = unmarshallString(o, "name");
147 if (path.contains("/"))
148 name = path.substring(path.lastIndexOf("/") + 1, path.length()); //strip the prefix
152 hash = unmarshallString(o, "hash");
153 bytes = unmarshallLong(o, "bytes");
154 version = unmarshallInt(o, "version");
155 contentType = unmarshallString(o, "content_type");
156 lastModified = unmarshallDate(o, "last_modified");
157 modifiedBy = unmarshallString(o, "modified_by");
158 versionTimestamp = unmarshallDate(o, "version_timestamp");
159 published = o.containsKey("x_object_public") ? true : false;
160 publicUri = unmarshallString(o, "x_object_public");
161 this.container = _container;
163 inheritedPermissionsFrom = unmarshallString(o, "x_object_shared_by");
164 String rawPermissions = unmarshallString(o, "x_object_sharing");
165 if (rawPermissions != null)
166 parsePermissions(rawPermissions);
168 JSONValue metaValue = o.get("x_object_meta");
169 if (metaValue != null) {
170 JSONObject metaObj = metaValue.isObject();
171 if (metaObj != null) {
172 for (String key: metaObj.keySet()) {
173 meta.put(key, unmarshallString(metaObj, key));
179 private void parsePermissions(String rawPermissions) {
180 String[] readwrite = rawPermissions.split(";");
181 for (String s : readwrite) {
182 String[] part = s.split("=");
183 String perm = part[0].trim();
184 String[] users = part[1].split(",");
185 for (String u : users) {
186 String user = u.trim();
187 Boolean[] userPerm = permissions.get(u);
188 if (userPerm == null) {
189 userPerm = new Boolean[2];
190 permissions.put(user, userPerm);
192 if (perm.equals("read")) {
193 userPerm[0] = Boolean.TRUE;
195 else if (perm.equals("write")) {
196 userPerm[1] = Boolean.TRUE;
203 public boolean equals(Object other) {
204 if (other instanceof File) {
205 File o = (File) other;
206 return name.equals(o.getName());
212 public int hashCode() {
213 return name.hashCode();
216 public String getContainer() {
220 public static File createFromResponse(String owner, Response response, File result) {
221 result.populate(owner, response);
225 private void populate(String _owner, Response response) {
227 for (Header h : response.getHeaders()) {
228 String header = h.getName();
229 if (header.startsWith("X-Object-Meta-"))
230 meta.put(URL.decodePathSegment(header.substring("X-Object-Meta-".length())), URL.decodePathSegment(h.getValue()));
231 else if (header.equals("X-Object-Sharing")) {
232 String rawPermissions = h.getValue();
233 parsePermissions(URL.decodePathSegment(rawPermissions));
235 else if (header.equals("X-Object-Shared-By")) {
236 inheritedPermissionsFrom = URL.decodePathSegment(h.getValue());
241 public Folder getParent() {
245 public Map<String, String> getMeta() {
249 public boolean isPublished() {
253 public String getPublicUri() {
257 public Map<String, Boolean[]> getPermissions() {
261 public String getInheritedPermissionsFrom() {
262 return inheritedPermissionsFrom;