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.Response;
45 public abstract class GetCommand<T extends RestResource> extends RestCommand{
47 boolean complete = false;
50 private final String path;
51 private String username;
52 private boolean requestSent = false;
55 public GetCommand(Class<T> theclass, String pathToGet, T theCached){
56 this(theclass, pathToGet, true, theCached);
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)
67 path =fixPath(pathToGet);
68 this.cached = theCached;
71 public GetCommand(Class<T> theclass, String aUsername , String pathToGet, T theCached){
72 this(theclass, aUsername, pathToGet, true, theCached);
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;
85 private void sendRequest(){
89 RestRequestBuilder builder = new RestRequestBuilder("GET", path);
90 if(cached!=null && cached.getLastModifiedSince() != null)
91 builder.setHeader("If-Modified-Since", cached.getLastModifiedSince());
94 handleHeaders(builder, path);
96 handleHeaders(username, builder, path);
97 builder.sendRequest("", new RestCallback(path) {
100 public Object deserialize(Response response) {
101 return deserializeResponse(path, response);
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);
113 GetCommand.this.onError(exception);
117 public void handleSuccess(Object object) {
123 } catch (Exception ex) {
128 public boolean isComplete() {
132 public T getResult(){
136 public boolean execute() {
139 boolean com = isComplete();
141 if(isShowLoadingIndicator())
142 GSS.get().hideLoadingIndicator();
143 if(getResult() != null)
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());
156 else if(aclass.equals(FileResource.class)){
157 result1 = new FileResource(aPath);
158 result1.createFromJSON(response.getHeader("X-GSS-Metadata"));
160 else if(aclass.equals(GroupsResource.class)){
161 result1 = new GroupsResource(aPath);
162 result1.createFromJSON(response.getText());
164 else if(aclass.equals(TrashResource.class)){
165 result1 = new TrashResource(aPath);
166 result1.createFromJSON(response.getText());
168 else if(aclass.equals(SharedResource.class)){
169 result1 = new SharedResource(aPath);
170 result1.createFromJSON(response.getText());
172 else if(aclass.equals(OthersResource.class)){
173 result1 = new OthersResource(aPath);
174 result1.createFromJSON(response.getText());
176 else if(aclass.equals(OtherUserResource.class)){
177 result1 = new OtherUserResource(aPath);
178 result1.createFromJSON(response.getText());
180 else if(aclass.equals(GroupResource.class)){
181 result1 = new GroupResource(aPath);
182 result1.createFromJSON(response.getText());
184 else if(aclass.equals(GroupUserResource.class)){
185 result1 = new GroupUserResource(aPath);
186 result1.createFromJSON(response.getText());
188 else if(aclass.equals(UserResource.class)){
189 result1 = new UserResource(aPath);
190 result1.createFromJSON(response.getText());
192 else if(aclass.equals(TagsResource.class)){
193 result1 = new TagsResource(aPath);
194 result1.createFromJSON(response.getText());
196 else if(aclass.equals(SearchResource.class)){
197 result1 = new SearchResource(aPath);
198 result1.createFromJSON(response.getText());
200 else if(aclass.equals(UserSearchResource.class)){
201 result1 = new UserSearchResource(aPath);
202 result1.createFromJSON(response.getText());
204 else if(aclass.equals(UploadStatusResource.class)){
205 result1 = new UploadStatusResource(aPath);
206 result1.createFromJSON(response.getText());
211 public T getCached() {
215 public void setCached(T theCached) {
216 this.cached = theCached;