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;
40 import java.util.HashSet;
44 import com.google.gwt.http.client.Header;
45 import com.google.gwt.http.client.Response;
46 import com.google.gwt.http.client.URL;
47 import com.google.gwt.i18n.client.NumberFormat;
48 import com.google.gwt.json.client.JSONObject;
50 public class File extends Resource {
59 private String contentType;
61 private Date lastModified;
63 private String modifiedBy;
65 private Date versionTimestamp;
71 private String container;
73 private Folder parent;
75 private Set<String> tags = new HashSet<String>();
77 private boolean published;
79 private String publicUri;
81 private Map<String, Boolean[]> permissions = new HashMap<String, Boolean[]>();
83 private String inheritedPermissionsFrom;
85 public String getContentType() {
89 public String getHash() {
94 public Date getLastModified() {
98 public String getModifiedBy() {
102 public String getName() {
106 public int getVersion() {
110 public Date getVersionTimestamp() {
111 return versionTimestamp;
114 public String getUri() {
115 return "/" + container + "/" + path;
118 public String getOwner() {
122 public String getPath() {
126 public long getBytes() {
130 public String getSizeAsString() {
131 NumberFormat nf = NumberFormat.getFormat("######.#");
133 return String.valueOf(bytes) + " B";
134 else if (bytes < 1024 * 1024)
135 return nf.format(Double.valueOf(bytes)/(1024)) + " KB";
136 else if (bytes < 1024 * 1024 * 1024)
137 return nf.format(Double.valueOf(bytes)/(1024 * 1024)) + " MB";
138 return nf.format(Double.valueOf(bytes)/(1024 * 1024 * 1024)) + " GB";
141 public boolean isShared() {
142 return !permissions.isEmpty();
145 public void populate(Folder _parent, JSONObject o, String _owner, String _container) {
146 this.parent = _parent;
147 path = unmarshallString(o, "name");
148 if (path.contains("/"))
149 name = path.substring(path.lastIndexOf("/") + 1, path.length()); //strip the prefix
153 hash = unmarshallString(o, "hash");
154 bytes = unmarshallLong(o, "bytes");
155 version = unmarshallInt(o, "version");
156 contentType = unmarshallString(o, "content_type");
157 lastModified = unmarshallDate(o, "last_modified");
158 modifiedBy = unmarshallString(o, "modified_by");
159 versionTimestamp = unmarshallDate(o, "version_timestamp");
160 published = o.containsKey("x_object_public") ? true : false;
161 publicUri = unmarshallString(o, "x_object_public");
162 this.container = _container;
164 inheritedPermissionsFrom = unmarshallString(o, "x_object_shared_by");
165 String rawPermissions = unmarshallString(o, "x_object_sharing");
166 if (rawPermissions != null)
167 parsePermissions(rawPermissions);
169 for (String key : o.keySet())
170 if (key.startsWith("x_object_meta_"))
171 tags.add(URL.decodePathSegment(key.substring("x_object_meta_".length())).trim().toLowerCase());
176 private void parsePermissions(String rawPermissions) {
177 String[] readwrite = rawPermissions.split(";");
178 for (String s : readwrite) {
179 String[] part = s.split("=");
180 String perm = part[0].trim();
181 String[] users = part[1].split(",");
182 for (String u : users) {
183 String user = u.trim();
184 Boolean[] userPerm = permissions.get(u);
185 if (userPerm == null) {
186 userPerm = new Boolean[2];
187 permissions.put(user, userPerm);
189 if (perm.equals("read")) {
190 userPerm[0] = Boolean.TRUE;
192 else if (perm.equals("write")) {
193 userPerm[1] = Boolean.TRUE;
200 public boolean equals(Object other) {
201 if (other instanceof File) {
202 File o = (File) other;
203 return name.equals(o.getName());
209 public int hashCode() {
210 return name.hashCode();
213 public String getContainer() {
217 public static File createFromResponse(String owner, Response response, File result) {
218 result.populate(owner, response);
222 private void populate(String _owner, Response response) {
224 for (Header h : response.getHeaders()) {
225 String header = h.getName();
226 if (header.startsWith("X-Object-Meta-"))
227 tags.add(URL.decodePathSegment(header.substring("X-Object-Meta-".length())).trim().toLowerCase());
228 else if (header.equals("X-Object-Sharing")) {
229 String rawPermissions = h.getValue();
230 parsePermissions(rawPermissions);
232 else if (header.equals("X-Object-Shared-By")) {
233 inheritedPermissionsFrom = h.getValue().trim();
238 public Folder getParent() {
242 public Set<String> getTags() {
246 public boolean isPublished() {
250 public String getPublicUri() {
254 public Map<String, Boolean[]> getPermissions() {
258 public String getInheritedPermissionsFrom() {
259 return inheritedPermissionsFrom;