Fix of Revision: ec7b8d0b2c after code review. Added a check condition before remov...
[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.RequestBuilder;
41 import com.google.gwt.http.client.Response;
42
43 /**
44  * @author kman
45  */
46 public abstract class GetCommand<T extends RestResource> extends RestCommand{
47
48         boolean complete = false;
49         T result = null;
50         Class<T> aclass;
51         private final String path;
52         private String username;
53         private boolean requestSent = false;
54         T cached;
55
56         public GetCommand(Class<T> theclass, String pathToGet, T theCached){
57                 this(theclass, pathToGet, true, theCached);
58         }
59
60         public GetCommand(Class<T> theclass, String pathToGet, boolean showLoading, T theCached){
61                 setShowLoadingIndicator(showLoading);
62                 if(isShowLoadingIndicator())
63                         GSS.get().showLoadingIndicator();
64                 this.aclass = theclass;
65                 if(pathToGet.indexOf("?") != -1)
66                         path = pathToGet;
67                 else
68                         path =fixPath(pathToGet);
69                 this.cached = theCached;
70         }
71
72         public GetCommand(Class<T> theclass, String aUsername , String pathToGet, T theCached){
73                 this(theclass, aUsername, pathToGet, true, theCached);
74         }
75
76         public GetCommand(Class<T> theclass, String aUsername , String pathToGet, boolean showLoading, T theCached){
77                 setShowLoadingIndicator(showLoading);
78                 if(isShowLoadingIndicator())
79                         GSS.get().showLoadingIndicator();
80                 this.aclass = theclass;
81                 path = fixPath(pathToGet);
82                 this.username = aUsername;
83                 this.cached = theCached;
84         }
85
86         private void sendRequest(){
87                 if(requestSent)
88                         return;
89                 requestSent=true;
90                 RequestBuilder builder = new RequestBuilder(RequestBuilder.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         @Override
138         public boolean execute() {
139                 if(!requestSent)
140                         sendRequest();
141                 boolean com = isComplete();
142                 if(com){
143                         if(isShowLoadingIndicator())
144                                 GSS.get().hideLoadingIndicator();
145                         if(getResult() != null)
146                                 onComplete();
147                         return false;
148                 }
149                 return true;
150         }
151
152         public Object deserializeResponse(String aPath, Response response) {
153                 RestResource result1 = null;
154                 if(aclass.equals(FolderResource.class)){
155                         result1 = new FolderResource(aPath);
156                         result1.createFromJSON(response.getText());
157                 }
158                 else if(aclass.equals(FileResource.class)){
159                         result1 = new FileResource(aPath);
160                         result1.createFromJSON(response.getHeader("X-GSS-Metadata"));
161                 }
162                 else if(aclass.equals(GroupsResource.class)){
163                         result1 = new GroupsResource(aPath);
164                         result1.createFromJSON(response.getText());
165                 }
166                 else if(aclass.equals(TrashResource.class)){
167                         result1 = new TrashResource(aPath);
168                         result1.createFromJSON(response.getText());
169                 }
170                 else if(aclass.equals(SharedResource.class)){
171                         result1 = new SharedResource(aPath);
172                         result1.createFromJSON(response.getText());
173                 }
174                 else if(aclass.equals(OthersResource.class)){
175                         result1 = new OthersResource(aPath);
176                         result1.createFromJSON(response.getText());
177                 }
178                 else if(aclass.equals(OtherUserResource.class)){
179                         result1 = new OtherUserResource(aPath);
180                         result1.createFromJSON(response.getText());
181                 }
182                 else if(aclass.equals(GroupResource.class)){
183                         result1 = new GroupResource(aPath);
184                         result1.createFromJSON(response.getText());
185                 }
186                 else if(aclass.equals(GroupUserResource.class)){
187                         result1 = new GroupUserResource(aPath);
188                         result1.createFromJSON(response.getText());
189                 }
190                 else if(aclass.equals(UserResource.class)){
191                         result1 = new UserResource(aPath);
192                         result1.createFromJSON(response.getText());
193                 }
194                 else if(aclass.equals(TagsResource.class)){
195                         result1 = new TagsResource(aPath);
196                         result1.createFromJSON(response.getText());
197                 }
198                 else if(aclass.equals(SearchResource.class)){
199                         result1 = new SearchResource(aPath);
200                         result1.createFromJSON(response.getText());
201                 }
202                 else if(aclass.equals(UserSearchResource.class)){
203                         result1 = new UserSearchResource(aPath);
204                         result1.createFromJSON(response.getText());
205                 }
206                 else if(aclass.equals(UploadStatusResource.class)){
207                         result1 = new UploadStatusResource(aPath);
208                         result1.createFromJSON(response.getText());
209                 }
210                 return result1;
211         }
212
213         public T getCached() {
214                 return cached;
215         }
216
217         public void setCached(T theCached) {
218                 this.cached = theCached;
219         }
220 }