2 * Copyright 2009 Electronic Business Systems Ltd.
4 * This file is part of GSS.
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.
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.
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/>.
19 package gr.ebs.gss.client.rest;
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;
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;
46 public abstract class GetCommand<T extends RestResource> extends RestCommand{
48 boolean complete = false;
51 private final String path;
52 private String username;
53 private boolean requestSent = false;
56 public GetCommand(Class<T> theclass, String pathToGet, T theCached){
57 this(theclass, pathToGet, true, theCached);
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)
68 path =fixPath(pathToGet);
69 this.cached = theCached;
72 public GetCommand(Class<T> theclass, String aUsername , String pathToGet, T theCached){
73 this(theclass, aUsername, pathToGet, true, theCached);
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;
86 private void sendRequest(){
90 RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
91 if(cached!=null && cached.getLastModifiedSince() != null)
92 builder.setHeader("If-Modified-Since", cached.getLastModifiedSince());
95 handleHeaders(builder, path);
97 handleHeaders(username, builder, path);
98 builder.sendRequest("", new RestCallback(path) {
101 public Object deserialize(Response response) {
102 return deserializeResponse(path, response);
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);
114 GetCommand.this.onError(exception);
118 public void handleSuccess(Object object) {
124 } catch (Exception ex) {
129 public boolean isComplete() {
133 public T getResult(){
138 public boolean execute() {
141 boolean com = isComplete();
143 if(isShowLoadingIndicator())
144 GSS.get().hideLoadingIndicator();
145 if(getResult() != null)
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());
158 else if(aclass.equals(FileResource.class)){
159 result1 = new FileResource(aPath);
160 result1.createFromJSON(response.getHeader("X-GSS-Metadata"));
162 else if(aclass.equals(GroupsResource.class)){
163 result1 = new GroupsResource(aPath);
164 result1.createFromJSON(response.getText());
166 else if(aclass.equals(TrashResource.class)){
167 result1 = new TrashResource(aPath);
168 result1.createFromJSON(response.getText());
170 else if(aclass.equals(SharedResource.class)){
171 result1 = new SharedResource(aPath);
172 result1.createFromJSON(response.getText());
174 else if(aclass.equals(OthersResource.class)){
175 result1 = new OthersResource(aPath);
176 result1.createFromJSON(response.getText());
178 else if(aclass.equals(OtherUserResource.class)){
179 result1 = new OtherUserResource(aPath);
180 result1.createFromJSON(response.getText());
182 else if(aclass.equals(GroupResource.class)){
183 result1 = new GroupResource(aPath);
184 result1.createFromJSON(response.getText());
186 else if(aclass.equals(GroupUserResource.class)){
187 result1 = new GroupUserResource(aPath);
188 result1.createFromJSON(response.getText());
190 else if(aclass.equals(UserResource.class)){
191 result1 = new UserResource(aPath);
192 result1.createFromJSON(response.getText());
194 else if(aclass.equals(TagsResource.class)){
195 result1 = new TagsResource(aPath);
196 result1.createFromJSON(response.getText());
198 else if(aclass.equals(SearchResource.class)){
199 result1 = new SearchResource(aPath);
200 result1.createFromJSON(response.getText());
202 else if(aclass.equals(UserSearchResource.class)){
203 result1 = new UserSearchResource(aPath);
204 result1.createFromJSON(response.getText());
206 else if(aclass.equals(UploadStatusResource.class)){
207 result1 = new UploadStatusResource(aPath);
208 result1.createFromJSON(response.getText());
213 public T getCached() {
217 public void setCached(T theCached) {
218 this.cached = theCached;