fetch file data also for MyShare and Other User Shared
[pithos] / src / gr / ebs / gss / client / rest / GetCommand.java
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 com.google.gwt.core.client.GWT;
39 import com.google.gwt.http.client.Request;
40 import com.google.gwt.http.client.Response;
41
42 /**
43  * @author kman
44  */
45 public abstract class GetCommand<T extends RestResource> extends RestCommand{
46
47         boolean complete = false;
48         T result = null;
49         Class<T> aclass;
50         private final String path;
51         private String username;
52         private boolean requestSent = false;
53         T cached;
54
55         public GetCommand(Class<T> theclass, String pathToGet, T cached){
56                 this(theclass,pathToGet,true,cached);
57         }
58
59         public GetCommand(Class<T> theclass, String pathToGet, boolean showLoading, T cached){
60                 setShowLoadingIndicator(showLoading);
61                 if(isShowLoadingIndicator())
62                         GSS.get().showLoadingIndicator();
63                 this.aclass = theclass;
64                 if(pathToGet.indexOf("?") != -1)
65                         path = pathToGet;
66                 else
67                         path =fixPath(pathToGet);
68                 this.cached = cached;
69
70         }
71
72         public GetCommand(Class<T> theclass, String username , String pathToGet, T cached){
73                 this(theclass,username, pathToGet, true, cached);
74         }
75
76         public GetCommand(Class<T> theclass, String username , String pathToGet, boolean showLoading, T cached){
77                 setShowLoadingIndicator(showLoading);
78                 if(isShowLoadingIndicator())
79                         GSS.get().showLoadingIndicator();
80                 this.aclass = theclass;
81                 path = fixPath(pathToGet);
82                 this.username = username;
83                 this.cached = cached;
84         }
85
86         private void sendRequest(){
87                 if(requestSent)
88                         return;
89                 requestSent=true;
90                 RestRequestBuilder builder = new RestRequestBuilder("GET", path);
91                 if(cached!=null && cached.getLastModifiedSince() != null)
92                         builder.setHeader("If-Modified-Since", cached.getLastModifiedSince());
93                 try {
94                         if(username == null)
95                                 handleHeaders(builder, path);
96                         else
97                                 handleHeaders(username, builder, path);
98                         builder.sendRequest("", new RestCallback(path) {
99
100                                 @Override
101                                 public Object deserialize(Response response) {
102                                         return deserializeResponse(path, response);
103                                 }
104
105                                 @Override
106                                 public void handleError(Request request, Throwable exception) {
107                                         if(exception instanceof RestException)
108                                                 if(((RestException)exception).getHttpStatusCode() == 304 && cached != null){
109                                                         GWT.log("Using cache:"+cached.getUri(), null);
110                                                         handleSuccess(cached);
111                                                         return;
112                                                 }
113                                         complete = true;
114                                         GetCommand.this.onError(exception);
115                                 }
116
117                                 @Override
118                                 public void handleSuccess(Object object) {
119                                         result = (T) object;
120                                         complete = true;
121                                 }
122
123                         });
124                 } catch (Exception ex) {
125                         complete = true;
126                         onError(ex);
127                 }
128         }
129         public boolean isComplete() {
130                 return complete;
131         }
132
133         public T getResult(){
134                 return result;
135         }
136
137         public boolean execute() {
138                 if(!requestSent)
139                         sendRequest();
140                 boolean com = isComplete();
141                 if(com){
142                         if(isShowLoadingIndicator())
143                                 GSS.get().hideLoadingIndicator();
144                         if(getResult() != null)
145                                 onComplete();
146                         return false;
147                 }
148                 return true;
149         }
150
151         public Object deserializeResponse(String path, Response response) {
152                 RestResource result1 = null;
153                 if(aclass.equals(FolderResource.class)){
154                         result1 = new FolderResource(path);
155                         result1.createFromJSON(response.getText());
156                 }
157                 else if(aclass.equals(FileResource.class)){
158                         result1 = new FileResource(path);
159                         result1.createFromJSON(response.getHeader("X-GSS-Metadata"));
160                 }
161                 else if(aclass.equals(GroupsResource.class)){
162                         result1 = new GroupsResource(path);
163                         result1.createFromJSON(response.getText());
164                 }
165                 else if(aclass.equals(TrashResource.class)){
166                         result1 = new TrashResource(path);
167                         result1.createFromJSON(response.getText());
168                 }
169                 else if(aclass.equals(SharedResource.class)){
170                         result1 = new SharedResource(path);
171                         result1.createFromJSON(response.getText());
172                 }
173                 else if(aclass.equals(OthersResource.class)){
174                         result1 = new OthersResource(path);
175                         result1.createFromJSON(response.getText());
176                 }
177                 else if(aclass.equals(OtherUserResource.class)){
178                         result1 = new OtherUserResource(path);
179                         result1.createFromJSON(response.getText());
180                 }
181                 else if(aclass.equals(GroupResource.class)){
182                         result1 = new GroupResource(path);
183                         result1.createFromJSON(response.getText());
184                 }
185                 else if(aclass.equals(GroupUserResource.class)){
186                         result1 = new GroupUserResource(path);
187                         result1.createFromJSON(response.getText());
188                 }
189                 else if(aclass.equals(UserResource.class)){
190                         result1 = new UserResource(path);
191                         result1.createFromJSON(response.getText());
192                 }
193                 else if(aclass.equals(TagsResource.class)){
194                         result1 = new TagsResource(path);
195                         result1.createFromJSON(response.getText());
196                 }
197                 else if(aclass.equals(SearchResource.class)){
198                         result1 = new SearchResource(path);
199                         result1.createFromJSON(response.getText());
200                 }
201                 else if(aclass.equals(UserSearchResource.class)){
202                         result1 = new UserSearchResource(path);
203                         result1.createFromJSON(response.getText());
204                 }
205                 else if(aclass.equals(UploadStatusResource.class)){
206                         result1 = new UploadStatusResource(path);
207                         result1.createFromJSON(response.getText());
208                 }
209                 return result1;
210         }
211
212
213         public T getCached() {
214                 return cached;
215         }
216
217
218         public void setCached(T cached) {
219                 this.cached = cached;
220         }
221
222
223
224 }