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