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;
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 String getUri() {
110 return "/" + container + "/" + path;
113 public String getOwner() {
117 public String getPath() {
121 public long getBytes() {
125 public String getSizeAsString() {
126 NumberFormat nf = NumberFormat.getFormat("######.#");
128 return String.valueOf(bytes) + " B";
129 else if (bytes < 1024 * 1024)
130 return nf.format(Double.valueOf(bytes)/(1024)) + " KB";
131 else if (bytes < 1024 * 1024 * 1024)
132 return nf.format(Double.valueOf(bytes)/(1024 * 1024)) + " MB";
133 return nf.format(Double.valueOf(bytes)/(1024 * 1024 * 1024)) + " GB";
136 public boolean isSharedOrPublished() {
137 return !permissions.isEmpty() || published;
140 public boolean isShared() {
141 return !permissions.isEmpty();
144 public void populate(Folder _parent, JSONObject o, String _owner, String _container) {
145 this.parent = _parent;
146 path = unmarshallString(o, "name");
147 if (parent != null && parent.getPrefix().length() > 0)
148 name = path.substring(parent.getPrefix().length() + 1);
152 hash = unmarshallString(o, "hash");
153 bytes = unmarshallLong(o, "bytes");
154 version = unmarshallInt(o, "x_object_version");
155 contentType = unmarshallString(o, "content_type");
156 lastModified = unmarshallDate(o, "last_modified");
157 modifiedBy = unmarshallString(o, "x_object_modified_by");
158 published = o.containsKey("x_object_public") ? true : false;
159 publicUri = unmarshallString(o, "x_object_public");
160 this.container = _container;
162 inheritedPermissionsFrom = unmarshallString(o, "x_object_shared_by");
163 String rawPermissions = unmarshallString(o, "x_object_sharing");
164 if (rawPermissions != null)
165 parsePermissions(rawPermissions);
167 JSONValue metaValue = o.get("x_object_meta");
168 if (metaValue != null) {
169 JSONObject metaObj = metaValue.isObject();
170 if (metaObj != null) {
171 for (String key: metaObj.keySet()) {
172 meta.put(key, unmarshallString(metaObj, key));
178 private void parsePermissions(String rawPermissions) {
179 String[] readwrite = rawPermissions.split(";");
180 for (String s : readwrite) {
181 String[] part = s.split("=");
182 String perm = part[0].trim();
183 String[] users = part[1].split(",");
184 for (String u : users) {
185 String user = u.trim();
186 Boolean[] userPerm = permissions.get(u);
187 if (userPerm == null) {
188 userPerm = new Boolean[2];
189 permissions.put(user, userPerm);
191 if (perm.equals("read")) {
192 userPerm[0] = Boolean.TRUE;
194 else if (perm.equals("write")) {
195 userPerm[1] = Boolean.TRUE;
202 public boolean equals(Object other) {
203 if (other instanceof File) {
204 File o = (File) other;
205 return name.equals(o.getName());
211 public int hashCode() {
212 return name.hashCode();
215 public String getContainer() {
219 public static File createFromResponse(String owner, Response response, File result) {
220 result.populate(owner, response);
224 private void populate(String _owner, Response response) {
229 for (Header h : response.getHeaders()) {
231 continue; //IE bug. h cannot be null in the general case
232 String header = h.getName();
233 if (header.startsWith("X-Object-Meta-"))
234 meta.put(URL.decodePathSegment(header.substring("X-Object-Meta-".length())), URL.decodePathSegment(h.getValue()));
235 else if (header.equals("X-Object-Sharing")) {
236 String rawPermissions = h.getValue();
237 parsePermissions(URL.decodePathSegment(rawPermissions));
239 else if (header.equals("X-Object-Shared-By")) {
240 inheritedPermissionsFrom = URL.decodePathSegment(h.getValue());
242 else if (header.equals("Content-Length")) {
243 bytes = Long.parseLong(h.getValue());
245 else if (header.equals("Content-Type")) {
246 contentType = h.getValue();
248 else if (header.equals("Last-Modified")) {
249 lastModified = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).parse(h.getValue());
251 else if (header.equals("X-Object-Public")) {
253 publicUri = h.getValue();
258 public Folder getParent() {
262 public Map<String, String> getMeta() {
266 public boolean isPublished() {
270 public String getPublicUri() {
274 public Map<String, Boolean[]> getPermissions() {
278 public String getInheritedPermissionsFrom() {
279 return inheritedPermissionsFrom;