Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / foldertree / File.java @ ff06bcd5

History | View | Annotate | Download (7.8 kB)

1
/*
2
 * Copyright 2011 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *
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.
16
 *
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.
29
 *
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.
34
 */
35

    
36
package gr.grnet.pithos.web.client.foldertree;
37

    
38
import com.google.gwt.http.client.Header;
39
import com.google.gwt.http.client.Response;
40
import com.google.gwt.i18n.client.NumberFormat;
41
import com.google.gwt.json.client.JSONObject;
42
import java.util.Date;
43
import java.util.HashMap;
44
import java.util.HashSet;
45
import java.util.Map;
46
import java.util.Set;
47

    
48
public class File extends Resource {
49
    private String name;
50

    
51
    private String hash;
52

    
53
    private int version;
54

    
55
    private long bytes;
56

    
57
    private String contentType;
58

    
59
    private Date lastModified;
60

    
61
    private String modifiedBy;
62

    
63
    private Date versionTimestamp;
64

    
65
    private String path;
66

    
67
    private String owner;
68

    
69
    private boolean inTrash;
70

    
71
    private String container;
72

    
73
    private Folder parent;
74

    
75
    private Set<String> tags = new HashSet<String>();
76

    
77
    private boolean published;
78

    
79
    private String publicUri;
80

    
81
    private Map<String, Boolean[]> permissions = new HashMap<String, Boolean[]>();
82

    
83
    private String inheritedPermissionsFrom;
84

    
85
    public String getContentType() {
86
        return contentType;
87
    }
88

    
89
    public String getHash() {
90
        return hash;
91
    }
92

    
93
    public Date getLastModified() {
94
        return lastModified;
95
    }
96

    
97
    public String getModifiedBy() {
98
        return modifiedBy;
99
    }
100

    
101
    public String getName() {
102
        return name;
103
    }
104

    
105
    public int getVersion() {
106
        return version;
107
    }
108

    
109
    public Date getVersionTimestamp() {
110
        return versionTimestamp;
111
    }
112

    
113
    public String getUri() {
114
        return "/" + container + "/" + path;
115
    }
116

    
117
    public String getOwner() {
118
        return owner;
119
    }
120

    
121
    public String getPath() {
122
        return path;
123
    }
124

    
125
    public long getBytes() {
126
        return bytes;
127
    }
128

    
129
    public String getSizeAsString() {
130
        NumberFormat nf = NumberFormat.getFormat("######.#");
131
        if (bytes < 1024)
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";
138
    }
139

    
140
    public boolean isShared() {
141
        return !permissions.isEmpty();
142
    }
143

    
144
    public boolean isInTrash() {
145
        return inTrash;
146
    }
147

    
148
    public void populate(Folder parent, JSONObject o, String owner, String container) {
149
        this.parent = parent;
150
        path = unmarshallString(o, "name");
151
        if (path.contains("/"))
152
            name = path.substring(path.lastIndexOf("/") + 1, path.length()); //strip the prefix
153
        else
154
            name = path;
155
        this.owner = owner;
156
        hash = unmarshallString(o, "hash");
157
        bytes = unmarshallLong(o, "bytes");
158
        version = unmarshallInt(o, "version");
159
        contentType = unmarshallString(o, "content_type");
160
        lastModified = unmarshallDate(o, "last_modified");
161
        modifiedBy = unmarshallString(o, "modified_by");
162
        versionTimestamp = unmarshallDate(o, "version_timestamp");
163
        published = o.containsKey("x_object_public") ? true : false;
164
        publicUri = unmarshallString(o, "x_object_public");
165
        this.container = container;
166

    
167
        inheritedPermissionsFrom = unmarshallString(o, "x_object_shared_by");
168
        String rawPermissions = unmarshallString(o, "x_object_sharing");
169
        if (rawPermissions != null)
170
            parsePermissions(rawPermissions);
171

    
172
        for (String key : o.keySet())
173
            if (key.startsWith("x_object_meta_") && !key.equals("x_object_meta_trash"))
174
                tags.add(key.substring("x_object_meta_".length()).trim().toLowerCase());
175

    
176
        
177
    }
178

    
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);
191
                }
192
                if (perm.equals("read")) {
193
                    userPerm[0] = Boolean.TRUE;
194
                }
195
                else if (perm.equals("write")) {
196
                    userPerm[1] = Boolean.TRUE;
197
                }
198
            }
199
        }
200
    }
201

    
202
    public boolean equals(Object other) {
203
        if (other instanceof File) {
204
            File o = (File) other;
205
            return name.equals(o.getName());
206
        }
207
        return false;
208
    }
209

    
210
    public int hashCode() {
211
        return name.hashCode();
212
    }
213

    
214
    public String getContainer() {
215
        return container;
216
    }
217

    
218
    public static File createFromResponse(String owner, Response response, File result) {
219
        result.populate(owner, response);
220
        return result;
221
    }
222

    
223
    private void populate(String owner, Response response) {
224
        this.owner = owner;
225
        for (Header h : response.getHeaders()) {
226
            String header = h.getName();
227
            if (header.startsWith("X-Object-Meta-") && !header.equals("X-Object-Meta-Trash"))
228
                tags.add(header.substring("X-Object-Meta-".length()).trim().toLowerCase());
229
            else if (header.equals("X-Object-Sharing")) {
230
                String rawPermissions = h.getValue();
231
                parsePermissions(rawPermissions);
232
            }
233
            else if (header.equals("X-Object-Shared-By")) {
234
                inheritedPermissionsFrom = h.getValue().trim();
235
            }
236
        }
237
        String header = response.getHeader("X-Object-Meta-Trash");
238
        if (header != null)
239
            inTrash = Boolean.valueOf(header);
240
        else
241
            inTrash = false;
242
    }
243

    
244
    public Folder getParent() {
245
        return parent;
246
    }
247

    
248
    public Set<String> getTags() {
249
        return tags;
250
    }
251

    
252
    public boolean isPublished() {
253
        return published;
254
    }
255

    
256
    public String getPublicUri() {
257
        return publicUri;
258
    }
259

    
260
    public Map<String, Boolean[]> getPermissions() {
261
        return permissions;
262
    }
263

    
264
    public String getInheritedPermissionsFrom() {
265
        return inheritedPermissionsFrom;
266
    }
267
}