Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (8.5 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

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

    
54
    private String hash;
55

    
56
    private int version;
57

    
58
    private long bytes;
59

    
60
    private String contentType;
61

    
62
    private Date lastModified;
63

    
64
    private String modifiedBy;
65

    
66
    private String path;
67

    
68
    private String owner;
69

    
70
    private String container;
71

    
72
    private Folder parent;
73

    
74
    private Map<String, String> meta = new HashMap<String, String>();
75

    
76
    private boolean published;
77

    
78
    private String publicUri;
79

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

    
82
    private String inheritedPermissionsFrom;
83

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

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

    
92
    @Override
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 String getUri() {
110
        return "/" + container + "/" + path;
111
    }
112

    
113
    public String getOwner() {
114
        return owner;
115
    }
116

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

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

    
125
    public String getSizeAsString() {
126
        NumberFormat nf = NumberFormat.getFormat("######.#");
127
        if (bytes < 1024)
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";
134
    }
135

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

    
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);
149
        else
150
            name = path;
151
        this.owner = _owner;
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;
161

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

    
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));
173
                        }
174
                }
175
        }
176
    }
177

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

    
201
    @Override
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
    @Override
211
        public int hashCode() {
212
        return name.hashCode();
213
    }
214

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

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

    
224
    private void populate(String _owner, Response response) {
225
        this.owner = _owner;
226
        published = false;
227
        publicUri = null;
228
        permissions.clear();
229
        for (Header h : response.getHeaders()) {
230
                if (h == null)
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));
238
            }
239
            else if (header.equals("X-Object-Shared-By")) {
240
                inheritedPermissionsFrom = URL.decodePathSegment(h.getValue());
241
            }
242
            else if (header.equals("Content-Length")) {
243
                bytes = Long.parseLong(h.getValue());
244
            }
245
            else if (header.equals("Content-Type")) {
246
                    contentType = h.getValue();
247
            }
248
            else if (header.equals("Last-Modified")) {
249
                    lastModified = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822).parse(h.getValue());
250
            }
251
            else if (header.equals("X-Object-Public")) {
252
                    published = true;
253
                    publicUri = h.getValue();
254
            }
255
        }
256
    }
257

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

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

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

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

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

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

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