Implemented download (with the authentication passed as parameter):
[pithos] / web_client / src / gr / grnet / pithos / web / client / foldertree / File.java
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.Response;
39 import com.google.gwt.i18n.client.DateTimeFormat;
40 import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
41 import com.google.gwt.i18n.client.NumberFormat;
42 import com.google.gwt.json.client.JSONObject;
43 import com.google.gwt.json.client.JSONParser;
44 import com.google.gwt.json.client.JSONValue;
45 import java.util.Date;
46
47 public class File extends Resource {
48     private String name;
49
50     private String hash;
51
52     private int version;
53
54     private long bytes;
55
56     private String contentType;
57
58     private Date lastModified;
59
60     private String modifiedBy;
61
62     private Date versionTimestamp;
63
64     private String path;
65
66     private String owner;
67
68     private boolean inTrash;
69
70     private String container;
71
72     private Folder parent;
73
74     public String getContentType() {
75         return contentType;
76     }
77
78     public String getHash() {
79         return hash;
80     }
81
82     public Date getLastModified() {
83         return lastModified;
84     }
85
86     public String getModifiedBy() {
87         return modifiedBy;
88     }
89
90     public String getName() {
91         return name;
92     }
93
94     public int getVersion() {
95         return version;
96     }
97
98     public Date getVersionTimestamp() {
99         return versionTimestamp;
100     }
101
102     @Override
103     public String getLastModifiedSince() {
104         return null;  //To change body of implemented methods use File | Settings | File Templates.
105     }
106
107     public String getUri() {
108         return "/" + container + "/" + path;
109     }
110
111     public String getOwner() {
112         return owner;
113     }
114
115     public String getPath() {
116         return path;
117     }
118
119     public long getBytes() {
120         return bytes;
121     }
122
123     public String getSizeAsString() {
124         NumberFormat nf = NumberFormat.getFormat("######.#");
125         if (bytes < 1024)
126             return String.valueOf(bytes) + " B";
127         else if (bytes < 1024 * 1024)
128             return nf.format(Double.valueOf(bytes)/(1024)) + " KB";
129         else if (bytes < 1024 * 1024 * 1024)
130             return nf.format(Double.valueOf(bytes)/(1024 * 1024)) + " MB";
131         return nf.format(Double.valueOf(bytes)/(1024 * 1024 * 1024)) + " GB";
132     }
133
134     public boolean isShared() {
135         return false;
136     }
137
138     public boolean isInTrash() {
139         return inTrash;
140     }
141
142     public void populate(Folder parent, JSONObject o, String container) {
143         this.parent = parent;
144         path = unmarshallString(o, "name");
145         if (path.contains("/"))
146             name = path.substring(path.lastIndexOf("/") + 1, path.length()); //strip the prefix
147         else
148             name = path;
149         hash = unmarshallString(o, "hash");
150         bytes = unmarshallLong(o, "bytes");
151         version = unmarshallInt(o, "version");
152         contentType = unmarshallString(o, "content_type");
153         lastModified = unmarshallDate(o, "last_modified");
154         modifiedBy = unmarshallString(o, "modified_by");
155         versionTimestamp = unmarshallDate(o, "version_timestamp");
156         this.container = container;
157     }
158
159     public boolean equals(Object other) {
160         if (other instanceof File) {
161             File o = (File) other;
162             return name.equals(o.getName());
163         }
164         return false;
165     }
166
167     public int hashCode() {
168         return name.hashCode();
169     }
170
171     public String getContainer() {
172         return container;
173     }
174
175     public static File createFromResponse(Response response, File result) {
176         result.populate(response);
177         return result;
178     }
179
180     private void populate(Response response) {
181         String header = response.getHeader("X-Object-Meta-Trash");
182         if (header != null)
183             inTrash = Boolean.valueOf(header);
184         else
185             inTrash = false;
186
187         JSONValue json = JSONParser.parseStrict(response.getText());
188         JSONObject o = json.isObject();
189     }
190
191     public Folder getParent() {
192         return parent;
193     }
194 }