Implemented download (with the authentication passed as parameter):
[pithos] / web_client / src / gr / grnet / pithos / web / client / foldertree / AccountResource.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.Header;
39 import com.google.gwt.http.client.Response;
40 import com.google.gwt.i18n.client.DateTimeFormat;
41 import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
42 import com.google.gwt.i18n.client.NumberFormat;
43 import com.google.gwt.json.client.JSONArray;
44 import com.google.gwt.json.client.JSONObject;
45 import com.google.gwt.json.client.JSONParser;
46 import com.google.gwt.json.client.JSONValue;
47 import gr.grnet.pithos.web.client.rest.resource.RestResource;
48 import java.util.ArrayList;
49 import java.util.Date;
50 import java.util.LinkedHashSet;
51 import java.util.List;
52 import java.util.Set;
53
54 /**
55  * Created by IntelliJ IDEA. User: chstath Date: 5/19/11 Time: 2:55 PM To change this template use File | Settings |
56  * File Templates.
57  */
58 public class AccountResource extends Resource {
59
60     private long numberOfContainers = 0;
61
62     private long numberOfObjects = 0;
63
64     private long bytesUsed = 0;
65
66     private long bytesRemaining = 0;
67
68     private Date lastLogin = null;
69
70     private Date lastModified = null;
71     
72     private Set<Folder> containers = new LinkedHashSet<Folder>();
73
74     private Date currentLogin = null;
75
76     public long getBytesRemaining() {
77         return bytesRemaining;
78     }
79
80     public void setBytesRemaining(long bytesRemaining) {
81         this.bytesRemaining = bytesRemaining;
82     }
83
84     public long getBytesUsed() {
85         return bytesUsed;
86     }
87
88     public void setBytesUsed(long bytesUsed) {
89         this.bytesUsed = bytesUsed;
90     }
91
92     public Date getLastLogin() {
93         return lastLogin;
94     }
95
96     public void setLastLogin(Date lastLogin) {
97         this.lastLogin = lastLogin;
98     }
99
100     public Date getLastModified() {
101         return lastModified;
102     }
103
104     public void setLastModified(Date lastModified) {
105         this.lastModified = lastModified;
106     }
107
108     public long getNumberOfContainers() {
109         return numberOfContainers;
110     }
111
112     public void setNumberOfContainers(long numberOfContainers) {
113         this.numberOfContainers = numberOfContainers;
114     }
115
116     public long getNumberOfObjects() {
117         return numberOfObjects;
118     }
119
120     public void setNumberOfObjects(long numberOfObjects) {
121         this.numberOfObjects = numberOfObjects;
122     }
123
124     @Override
125     public String getLastModifiedSince() {
126         return null;
127     }
128
129     public Set<Folder> getContainers() {
130         return containers;
131     }
132
133     public Date getCurrentLogin() {
134         return currentLogin;
135     }
136
137     public void setCurrentLogin(Date currentLogin) {
138         this.currentLogin = currentLogin;
139     }
140
141     public void populate(Response response) {
142         String header = response.getHeader("X-Account-Container-Count");
143         if (header != null)
144             numberOfContainers = Long.valueOf(header);
145
146         header = response.getHeader("X-Account-Object-Count");
147         if (header != null)
148             numberOfObjects = Long.valueOf(header);
149
150         header = response.getHeader("X-Account-Bytes-Used");
151         if (header != null)
152             bytesUsed = Long.valueOf(header);
153
154         header = response.getHeader("X-Account-Bytes-Remaining");
155         if (header != null)
156             bytesRemaining = Long.valueOf(header);
157
158         DateTimeFormat df = DateTimeFormat.getFormat(PredefinedFormat.RFC_2822);
159         header = response.getHeader("X-Account-Last-Login");
160         if (header != null)
161             lastLogin = df.parse(header);
162
163         header = response.getHeader("Last-Modified");
164         if (header != null)
165             lastModified = df.parse(header);
166
167         JSONValue json = JSONParser.parseStrict(response.getText());
168         JSONArray array = json.isArray();
169         if (array != null) {
170             for (int i=0; i<array.size(); i++) {
171                 JSONObject o = array.get(i).isObject();
172                 if (o != null) {
173                     Folder f = new Folder();
174                     f.populate(null, o, null);
175                     containers.add(f);
176                 }
177             }
178         }
179     }
180
181     public static AccountResource createFromResponse(Response response) {
182         AccountResource a = new AccountResource();
183         a.populate(response);
184         return a;
185     }
186
187     private String getSize(Long size, Double division){
188         Double res = Double.valueOf(size.toString())/division;
189         NumberFormat nf = NumberFormat.getFormat("######.#");
190         return nf.format(res);
191     }
192
193     public String getFileSizeAsString() {
194         if (bytesUsed < 1024)
195             return String.valueOf(bytesUsed) + " B";
196         else if (bytesUsed < 1024*1024)
197             return getSize(bytesUsed, 1024D) + " KB";
198         else if (bytesUsed < 1024*1024*1024)
199             return getSize(bytesUsed,(1024D*1024D)) + " MB";
200         return getSize(bytesUsed , (1024D*1024D*1024D)) + " GB";
201     }
202
203     public String getQuotaLeftAsString() {
204         if (bytesRemaining < 1024)
205             return String.valueOf(bytesRemaining) + " B";
206         else if (bytesRemaining < 1024 * 1024)
207             return getSize(bytesRemaining, 1024D) + " KB";
208         else if (bytesRemaining < 1024 * 1024 * 1024)
209             return getSize(bytesRemaining,(1024D * 1024D)) + " MB";
210         return getSize(bytesRemaining , (1024D * 1024D * 1024D)) + " GB";
211     }
212 }