Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (9.1 kB)

1
/*
2
 * Copyright 2011-2013 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.http.client.URL;
41
import com.google.gwt.i18n.client.DateTimeFormat;
42
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
43
import com.google.gwt.i18n.client.NumberFormat;
44
import com.google.gwt.json.client.JSONObject;
45
import com.google.gwt.json.client.JSONValue;
46
import gr.grnet.pithos.web.client.Pithos;
47
import gr.grnet.pithos.web.client.Resource;
48

    
49
import java.util.Date;
50
import java.util.HashMap;
51
import java.util.Map;
52

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

    
56
    private String hash;
57

    
58
    private int version;
59

    
60
    private long bytes;
61

    
62
    private String contentType;
63

    
64
    private Date lastModified;
65

    
66
    private String modifiedBy;
67

    
68
    private String path;
69

    
70
    private String ownerID;
71

    
72
    private String container;
73

    
74
    private Folder parent;
75

    
76
    private Map<String, String> meta = new HashMap<String, String>();
77

    
78
    private boolean published;
79

    
80
    private String publicUri;
81

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

    
84
    private String inheritedPermissionsFrom;
85

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

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

    
94
    @Override
95
        public Date getLastModified() {
96
        return lastModified;
97
    }
98

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

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

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

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

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

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

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

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

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

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

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

    
170
        JSONValue metaValue = o.get("x_object_meta");
171
        if (metaValue != null) {
172
                JSONObject metaObj = metaValue.isObject();
173
                if (metaObj != null) {
174
                        for (String key: metaObj.keySet()) {
175
                    meta.put(key, unmarshallString(metaObj, key));
176
                        }
177
                }
178
        }
179
    }
180

    
181
    private void parsePermissions(String rawPermissions) {
182
        String[] readwrite = rawPermissions.split(";");
183
        for (String s : readwrite) {
184
            String[] part = s.split("=");
185
            String perm = part[0].trim();
186
            String[] users = part[1].split(",");
187
            for (String u : users) {
188
                String user = u.trim();
189
                Boolean[] userPerm = permissions.get(u);
190
                if (userPerm == null) {
191
                    userPerm = new Boolean[2];
192
                    permissions.put(user, userPerm);
193
                }
194
                if (perm.equals("read")) {
195
                    userPerm[0] = Boolean.TRUE;
196
                }
197
                else if (perm.equals("write")) {
198
                    userPerm[1] = Boolean.TRUE;
199
                }
200
            }
201
        }
202
    }
203

    
204
    @Override
205
        public boolean equals(Object other) {
206
        if (other instanceof File) {
207
            File o = (File) other;
208
            return name.equals(o.getName());
209
        }
210
        return false;
211
    }
212

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

    
218
    public String getContainer() {
219
        return container;
220
    }
221

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

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

    
262
    public Folder getParent() {
263
        return parent;
264
    }
265

    
266
    public Map<String, String> getMeta() {
267
        return meta;
268
    }
269

    
270
    public boolean isPublished() {
271
        return published;
272
    }
273

    
274
    public String getPublicUri() {
275
        return publicUri;
276
    }
277

    
278
    public Map<String, Boolean[]> getPermissions() {
279
        return permissions;
280
    }
281

    
282
    public String getInheritedPermissionsFrom() {
283
        return inheritedPermissionsFrom;
284
    }
285

    
286
        public void setName(String _name) {
287
                name = _name;
288
        }
289

    
290
    @Override
291
    public String toString() {
292
        final StringBuilder sb = new StringBuilder();
293
        sb.append("File(").append(getUri()).append(")");
294
        if(published) {
295
            sb.append("::[");
296
            sb.append(getPublicUri());
297
            sb.append("]");
298
        }
299
        return sb.toString();
300
    }
301
}