Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (8.6 kB)

1
/*
2
 * Copyright 2011-2012 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 java.util.Date;
39
import java.util.HashMap;
40
import java.util.Map;
41

    
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;
50
import gr.grnet.pithos.web.client.Resource;
51

    
52
public class File extends Resource {
53
    private String name;
54

    
55
    private String hash;
56

    
57
    private int version;
58

    
59
    private long bytes;
60

    
61
    private String contentType;
62

    
63
    private Date lastModified;
64

    
65
    private String modifiedBy;
66

    
67
    private String path;
68

    
69
    private String ownerID;
70

    
71
    private String container;
72

    
73
    private Folder parent;
74

    
75
    private Map<String, String> meta = new HashMap<String, 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
    @Override
94
        public Date getLastModified() {
95
        return lastModified;
96
    }
97

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

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

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

    
110
    public String getUri() {
111
        return "/" + container + "/" + path;
112
    }
113

    
114
    public String getOwnerID() {
115
        return ownerID;
116
    }
117

    
118
    public String getPath() {
119
        return path;
120
    }
121

    
122
    public long getBytes() {
123
        return bytes;
124
    }
125

    
126
    public String getSizeAsString() {
127
        NumberFormat nf = NumberFormat.getFormat("######.#");
128
        if (bytes < 1024)
129
            return String.valueOf(bytes) + " B";
130
        else if (bytes < 1024 * 1024)
131
            return nf.format(Double.valueOf(bytes)/(1024)) + " KB";
132
        else if (bytes < 1024 * 1024 * 1024)
133
            return nf.format(Double.valueOf(bytes)/(1024 * 1024)) + " MB";
134
        return nf.format(Double.valueOf(bytes)/(1024 * 1024 * 1024)) + " GB";
135
    }
136

    
137
    public boolean isSharedOrPublished() {
138
        return !permissions.isEmpty() || published;
139
    }
140
    
141
    public boolean isShared() {
142
            return !permissions.isEmpty();
143
    }
144

    
145
    public void populate(Folder _parent, JSONObject o, String _owner, String _container) {
146
        this.parent = _parent;
147
        path = unmarshallString(o, "name");
148
        if (parent != null && parent.getPrefix().length() > 0)
149
                name = path.substring(parent.getPrefix().length() + 1);
150
        else
151
            name = path;
152
        this.ownerID = _owner;
153
        hash = unmarshallString(o, "hash");
154
        bytes = unmarshallLong(o, "bytes");
155
        version = unmarshallInt(o, "x_object_version");
156
        contentType = unmarshallString(o, "content_type");
157
        lastModified = unmarshallDate(o, "last_modified");
158
        modifiedBy = unmarshallString(o, "x_object_modified_by");
159
        published = o.containsKey("x_object_public") ? true : false;
160
        publicUri = unmarshallString(o, "x_object_public");
161
        this.container = _container;
162

    
163
        inheritedPermissionsFrom = unmarshallString(o, "x_object_shared_by");
164
        String rawPermissions = unmarshallString(o, "x_object_sharing");
165
        if (rawPermissions != null)
166
            parsePermissions(rawPermissions);
167

    
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));
174
                        }
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
    @Override
203
        public boolean equals(Object other) {
204
        if (other instanceof File) {
205
            File o = (File) other;
206
            return name.equals(o.getName());
207
        }
208
        return false;
209
    }
210

    
211
    @Override
212
        public int hashCode() {
213
        return name.hashCode();
214
    }
215

    
216
    public String getContainer() {
217
        return container;
218
    }
219

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

    
225
    private void populate(String _owner, Response response) {
226
        this.ownerID = _owner;
227
        published = false;
228
        publicUri = null;
229
        permissions.clear();
230
        for (Header h : response.getHeaders()) {
231
                if (h == null)
232
                        continue; //IE bug. h cannot be null in the general case
233
            String header = h.getName();
234
            if (header.startsWith("X-Object-Meta-"))
235
                meta.put(URL.decodePathSegment(header.substring("X-Object-Meta-".length())), URL.decodePathSegment(h.getValue()));
236
            else if (header.equals("X-Object-Sharing")) {
237
                String rawPermissions = h.getValue();
238
                parsePermissions(URL.decodePathSegment(rawPermissions));
239
            }
240
            else if (header.equals("X-Object-Shared-By")) {
241
                inheritedPermissionsFrom = URL.decodePathSegment(h.getValue());
242
            }
243
            else if (header.equals("Content-Length")) {
244
                bytes = Long.parseLong(h.getValue());
245
            }
246
            else if (header.equals("Content-Type")) {
247
                    contentType = h.getValue();
248
            }
249
            else if (header.equals("Last-Modified")) {
250
                    lastModified = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).parse(h.getValue());
251
            }
252
            else if (header.equals("X-Object-Public")) {
253
                    published = true;
254
                    publicUri = h.getValue();
255
            }
256
        }
257
    }
258

    
259
    public Folder getParent() {
260
        return parent;
261
    }
262

    
263
    public Map<String, String> getMeta() {
264
        return meta;
265
    }
266

    
267
    public boolean isPublished() {
268
        return published;
269
    }
270

    
271
    public String getPublicUri() {
272
        return publicUri;
273
    }
274

    
275
    public Map<String, Boolean[]> getPermissions() {
276
        return permissions;
277
    }
278

    
279
    public String getInheritedPermissionsFrom() {
280
        return inheritedPermissionsFrom;
281
    }
282

    
283
        public void setName(String _name) {
284
                name = _name;
285
        }
286
}