Minor fix. In case of a bad typed url home folder is selected instead of Trash folder...
[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 theCached){
56                 this(theclass, pathToGet, true, theCached);
57         }
58
59         public GetCommand(Class<T> theclass, String pathToGet, boolean showLoading, T theCached){
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 = theCached;
69         }
70
71         public GetCommand(Class<T> theclass, String aUsername , String pathToGet, T theCached){
72                 this(theclass, aUsername, pathToGet, true, theCached);
73         }
74
75         public GetCommand(Class<T> theclass, String aUsername , String pathToGet, boolean showLoading, T theCached){
76                 setShowLoadingIndicator(showLoading);
77                 if(isShowLoadingIndicator())
78                         GSS.get().showLoadingIndicator();
79                 this.aclass = theclass;
80                 path = fixPath(pathToGet);
81                 this.username = aUsername;
82                 this.cached = theCached;
83         }
84
85         private void sendRequest(){
86                 if(requestSent)
87                         return;
88                 requestSent=true;
89                 RestRequestBuilder builder = new RestRequestBuilder("GET", path);
90                 if(cached!=null && cached.getLastModifiedSince() != null)
91                         builder.setHeader("If-Modified-Since", cached.getLastModifiedSince());
92                 try {
93                         if(username == null)
94                                 handleHeaders(builder, path);
95                         else
96                                 handleHeaders(username, builder, path);
97                         builder.sendRequest("", new RestCallback(path) {
98
99                                 @Override
100                                 public Object deserialize(Response response) {
101                                         return deserializeResponse(path, response);
102                                 }
103
104                                 @Override
105                                 public void handleError(Request request, Throwable exception) {
106                                         if(exception instanceof RestException)
107                                                 if(((RestException)exception).getHttpStatusCode() == 304 && cached != null){
108                                                         GWT.log("Using cache:"+cached.getUri(), null);
109                                                         handleSuccess(cached);
110                                                         return;
111                                                 }
112                                         complete = true;
113                                         GetCommand.this.onError(exception);
114                                 }
115
116                                 @Override
117                                 public void handleSuccess(Object object) {
118                                         result = (T) object;
119                                         complete = true;
120                                 }
121
122                         });
123                 } catch (Exception ex) {
124                         complete = true;
125                         onError(ex);
126                 }
127         }
128         public boolean isComplete() {
129                 return complete;
130         }
131
132         public T getResult(){
133                 return result;
134         }
135
136         public boolean execute() {
137                 if(!requestSent)
138                         sendRequest();
139                 boolean com = isComplete();
140                 if(com){
141                         if(isShowLoadingIndicator())
142                                 GSS.get().hideLoadingIndicator();
143                         if(getResult() != null)
144                                 onComplete();
145                         return false;
146                 }
147                 return true;
148         }
149
150         public Object deserializeResponse(String aPath, Response response) {
151                 RestResource result1 = null;
152                 if(aclass.equals(FolderResource.class)){
153                         result1 = new FolderResource(aPath);
154                         result1.createFromJSON(response.getText());
155                 }
156                 else if(aclass.equals(FileResource.class)){
157                         result1 = new FileResource(aPath);
158                         result1.createFromJSON(response.getHeader("X-GSS-Metadata"));
159                 }
160                 else if(aclass.equals(GroupsResource.class)){
161                         result1 = new GroupsResource(aPath);
162                         result1.createFromJSON(response.getText());
163                 }
164                 else if(aclass.equals(TrashResource.class)){
165                         result1 = new TrashResource(aPath);
166                         result1.createFromJSON(response.getText());
167                 }
168                 else if(aclass.equals(SharedResource.class)){
169                         result1 = new SharedResource(aPath);
170                         result1.createFromJSON(response.getText());
171                 }
172                 else if(aclass.equals(OthersResource.class)){
173                         result1 = new OthersResource(aPath);
174                         result1.createFromJSON(response.getText());
175                 }
176                 else if(aclass.equals(OtherUserResource.class)){
177                         result1 = new OtherUserResource(aPath);
178                         result1.createFromJSON(response.getText());
179                 }
180                 else if(aclass.equals(GroupResource.class)){
181                         result1 = new GroupResource(aPath);
182                         result1.createFromJSON(response.getText());
183                 }
184                 else if(aclass.equals(GroupUserResource.class)){
185                         result1 = new GroupUserResource(aPath);
186                         result1.createFromJSON(response.getText());
187                 }
188                 else if(aclass.equals(UserResource.class)){
189                         result1 = new UserResource(aPath);
190                         result1.createFromJSON(response.getText());
191                 }
192                 else if(aclass.equals(TagsResource.class)){
193                         result1 = new TagsResource(aPath);
194                         result1.createFromJSON(response.getText());
195                 }
196                 else if(aclass.equals(SearchResource.class)){
197                         result1 = new SearchResource(aPath);
198                         result1.createFromJSON(response.getText());
199                 }
200                 else if(aclass.equals(UserSearchResource.class)){
201                         result1 = new UserSearchResource(aPath);
202                         result1.createFromJSON(response.getText());
203                 }
204                 else if(aclass.equals(UploadStatusResource.class)){
205                         result1 = new UploadStatusResource(aPath);
206                         result1.createFromJSON(response.getText());
207                 }
208                 return result1;
209         }
210
211         public T getCached() {
212                 return cached;
213         }
214
215         public void setCached(T theCached) {
216                 this.cached = theCached;
217         }
218 }