Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / rest / GetCommand.java @ ac4f68ab

History | View | Annotate | Download (8.7 kB)

1
/*
2
 * Copyright 2009 Electronic Business Systems Ltd.
3
 *
4
 * This file is part of GSS.
5
 *
6
 * GSS is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * GSS is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
package gr.ebs.gss.client.rest;
20

    
21
import gr.ebs.gss.client.GSS;
22
import gr.ebs.gss.client.rest.resource.FileResource;
23
import gr.ebs.gss.client.rest.resource.FolderResource;
24
import gr.ebs.gss.client.rest.resource.GroupResource;
25
import gr.ebs.gss.client.rest.resource.GroupUserResource;
26
import gr.ebs.gss.client.rest.resource.GroupsResource;
27
import gr.ebs.gss.client.rest.resource.OtherUserResource;
28
import gr.ebs.gss.client.rest.resource.OthersResource;
29
import gr.ebs.gss.client.rest.resource.RestResource;
30
import gr.ebs.gss.client.rest.resource.SearchResource;
31
import gr.ebs.gss.client.rest.resource.SharedResource;
32
import gr.ebs.gss.client.rest.resource.TagsResource;
33
import gr.ebs.gss.client.rest.resource.TrashResource;
34
import gr.ebs.gss.client.rest.resource.UploadStatusResource;
35
import gr.ebs.gss.client.rest.resource.UserResource;
36
import gr.ebs.gss.client.rest.resource.UserSearchResource;
37

    
38
import java.util.HashMap;
39
import java.util.Map;
40

    
41
import com.google.gwt.core.client.GWT;
42
import com.google.gwt.http.client.Request;
43
import com.google.gwt.http.client.RequestBuilder;
44
import com.google.gwt.http.client.Response;
45

    
46
/**
47
 * @author kman
48
 */
49
public abstract class GetCommand<T extends RestResource> extends RestCommand{
50

    
51
        boolean complete = false;
52
        T result = null;
53
        Class<T> aclass;
54
        private final String path;
55
        private String username;
56
        private boolean requestSent = false;
57
        T cached;
58
        
59
        private static final long MAX_CACHE_AGE = 1000;
60
        
61
        private static class RequestData {
62
                public String path;
63
                public String username;
64
                
65
                public RequestData(String _path, String _username) {
66
                        path = _path;
67
                        username = _username;
68
                }
69

    
70
                @Override
71
                public int hashCode() {
72
                        final int prime = 31;
73
                        int result = 1;
74
                        result = prime * result + ((path == null) ? 0 : path.hashCode());
75
                        result = prime * result + ((username == null) ? 0 : username.hashCode());
76
                        return result;
77
                }
78

    
79
                @Override
80
                public boolean equals(Object obj) {
81
                        if (this == obj)
82
                                return true;
83
                        if (obj == null)
84
                                return false;
85
                        if (getClass() != obj.getClass())
86
                                return false;
87
                        RequestData other = (RequestData) obj;
88
                        if (path == null) {
89
                                if (other.path != null)
90
                                        return false;
91
                        } else if (!path.equals(other.path))
92
                                return false;
93
                        if (username == null) {
94
                                if (other.username != null)
95
                                        return false;
96
                        } else if (!username.equals(other.username))
97
                                return false;
98
                        return true;
99
                }
100
        }
101
        
102
        private static class ResponseData {
103
                public long timestamp;
104
                public Object result;
105
                public ResponseData(long _timestamp, Object _result) {
106
                        timestamp = _timestamp;
107
                        result = _result;
108
                }
109
        }
110
        
111
        private static Map<RequestData, ResponseData> cache = new HashMap<RequestData, ResponseData>();
112
        
113

    
114
        public GetCommand(Class<T> theclass, String pathToGet, T theCached){
115
                this(theclass, pathToGet, true, theCached);
116
        }
117

    
118
        public GetCommand(Class<T> theclass, String pathToGet, boolean showLoading, T theCached){
119
                setShowLoadingIndicator(showLoading);
120
                if(isShowLoadingIndicator())
121
                        GSS.get().showLoadingIndicator();
122
                this.aclass = theclass;
123
                if(pathToGet.indexOf("?") != -1)
124
                        path = pathToGet;
125
                else
126
                        path =fixPath(pathToGet);
127
                this.cached = theCached;
128
        }
129

    
130
        public GetCommand(Class<T> theclass, String aUsername , String pathToGet, T theCached){
131
                this(theclass, aUsername, pathToGet, true, theCached);
132
        }
133

    
134
        public GetCommand(Class<T> theclass, String aUsername , String pathToGet, boolean showLoading, T theCached){
135
                setShowLoadingIndicator(showLoading);
136
                if(isShowLoadingIndicator())
137
                        GSS.get().showLoadingIndicator();
138
                this.aclass = theclass;
139
                path = fixPath(pathToGet);
140
                this.username = aUsername;
141
                this.cached = theCached;
142
        }
143

    
144
        private void sendRequest(){
145
                if(requestSent)
146
                        return;
147
                requestSent=true;
148
                RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
149
                if(cached!=null && cached.getLastModifiedSince() != null)
150
                        builder.setHeader("If-Modified-Since", cached.getLastModifiedSince());
151
                try {
152
                        if(username == null)
153
                                handleHeaders(builder, path);
154
                        else
155
                                handleHeaders(username, builder, path);
156
                        builder.sendRequest("", new RestCallback(path) {
157

    
158
                                @Override
159
                                public Object deserialize(Response response) {
160
                                        return deserializeResponse(path, response);
161
                                }
162

    
163
                                @Override
164
                                public void handleError(Request request, Throwable exception) {
165
                                        if(exception instanceof RestException)
166
                                                if(((RestException)exception).getHttpStatusCode() == 304 && cached != null){
167
                                                        GWT.log("Using cache:"+cached.getUri(), null);
168
                                                        handleSuccess(cached);
169
                                                        return;
170
                                                }
171
                                        complete = true;
172
                                        GetCommand.this.onError(exception);
173
                                }
174

    
175
                                @Override
176
                                public void handleSuccess(Object object) {
177
                                        result = (T) object;
178
                                        complete = true;
179
                                }
180

    
181
                        });
182
                } catch (Exception ex) {
183
                        complete = true;
184
                        onError(ex);
185
                }
186
        }
187
        public boolean isComplete() {
188
                return complete;
189
        }
190

    
191
        public T getResult(){
192
                return result;
193
        }
194

    
195
        @Override
196
        public boolean execute() {
197
                boolean com = isComplete();
198
                RequestData key = new RequestData(path, username);
199
                if (!com) {
200
                        if (cache.containsKey(key)) {
201
                                ResponseData resp = cache.get(key);
202
                                if (resp==null) {
203
                                        return true;
204
                                }
205
                                GWT.log("Cache hit: "+key.username+", "+key.path, null);
206
                                if (System.currentTimeMillis()-resp.timestamp>MAX_CACHE_AGE) {
207
                                        // Cache stale, remove
208
                                        cache.put(key,null);
209
                                }
210
                                else {
211
                                        // Use cache data
212
                                        if(isShowLoadingIndicator())
213
                                                GSS.get().hideLoadingIndicator();
214
                                        result = (T) resp.result;
215
                                        if (result != null) {
216
                                                onComplete();
217
                                        }
218
                                        complete = true;
219
                                        return false;
220
                                }
221
                        }
222
                
223
                        if(!requestSent) {
224
                                cache.put(key,null);
225
                                sendRequest();
226
                        }
227
                }
228
                
229
                if(com){
230
                        if(isShowLoadingIndicator())
231
                                GSS.get().hideLoadingIndicator();
232
                        if(getResult() != null) {
233
                                // Add to cache
234
                                cache.put(key, new ResponseData(System.currentTimeMillis(), getResult()));
235
                                onComplete();
236
                        }
237
                        return false;
238
                }
239
                return true;
240
        }
241

    
242
        public Object deserializeResponse(String aPath, Response response) {
243
                RestResource result1 = null;
244
                if(aclass.equals(FolderResource.class)){
245
                        result1 = new FolderResource(aPath);
246
                        result1.createFromJSON(response.getText());
247
                }
248
                else if(aclass.equals(FileResource.class)){
249
                        result1 = new FileResource(aPath);
250
                        result1.createFromJSON(response.getHeader("X-GSS-Metadata"));
251
                }
252
                else if(aclass.equals(GroupsResource.class)){
253
                        result1 = new GroupsResource(aPath);
254
                        result1.createFromJSON(response.getText());
255
                }
256
                else if(aclass.equals(TrashResource.class)){
257
                        result1 = new TrashResource(aPath);
258
                        result1.createFromJSON(response.getText());
259
                }
260
                else if(aclass.equals(SharedResource.class)){
261
                        result1 = new SharedResource(aPath);
262
                        result1.createFromJSON(response.getText());
263
                }
264
                else if(aclass.equals(OthersResource.class)){
265
                        result1 = new OthersResource(aPath);
266
                        result1.createFromJSON(response.getText());
267
                }
268
                else if(aclass.equals(OtherUserResource.class)){
269
                        result1 = new OtherUserResource(aPath);
270
                        result1.createFromJSON(response.getText());
271
                }
272
                else if(aclass.equals(GroupResource.class)){
273
                        result1 = new GroupResource(aPath);
274
                        result1.createFromJSON(response.getText());
275
                }
276
                else if(aclass.equals(GroupUserResource.class)){
277
                        result1 = new GroupUserResource(aPath);
278
                        result1.createFromJSON(response.getText());
279
                }
280
                else if(aclass.equals(UserResource.class)){
281
                        result1 = new UserResource(aPath);
282
                        result1.createFromJSON(response.getText());
283
                }
284
                else if(aclass.equals(TagsResource.class)){
285
                        result1 = new TagsResource(aPath);
286
                        result1.createFromJSON(response.getText());
287
                }
288
                else if(aclass.equals(SearchResource.class)){
289
                        result1 = new SearchResource(aPath);
290
                        result1.createFromJSON(response.getText());
291
                }
292
                else if(aclass.equals(UserSearchResource.class)){
293
                        result1 = new UserSearchResource(aPath);
294
                        result1.createFromJSON(response.getText());
295
                }
296
                else if(aclass.equals(UploadStatusResource.class)){
297
                        result1 = new UploadStatusResource(aPath);
298
                        result1.createFromJSON(response.getText());
299
                }
300
                return result1;
301
        }
302

    
303
        public T getCached() {
304
                return cached;
305
        }
306

    
307
        public void setCached(T theCached) {
308
                this.cached = theCached;
309
        }
310
}