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 cached){
56 this(theclass,pathToGet,true,cached);
59 public GetCommand(Class<T> theclass, String pathToGet, boolean showLoading, T cached){
60 setShowLoadingIndicator(showLoading);
61 if(isShowLoadingIndicator())
62 GSS.get().showLoadingIndicator();
63 this.aclass = theclass;
64 if(pathToGet.indexOf("?") != -1)
67 path =fixPath(pathToGet);
72 public GetCommand(Class<T> theclass, String username , String pathToGet, T cached){
73 this(theclass,username, pathToGet, true, cached);
76 public GetCommand(Class<T> theclass, String username , String pathToGet, boolean showLoading, T cached){
77 setShowLoadingIndicator(showLoading);
78 if(isShowLoadingIndicator())
79 GSS.get().showLoadingIndicator();
80 this.aclass = theclass;
81 path = fixPath(pathToGet);
82 this.username = username;
86 private void sendRequest(){
90 RestRequestBuilder builder = new RestRequestBuilder("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(){
137 public boolean execute() {
140 boolean com = isComplete();
142 if(isShowLoadingIndicator())
143 GSS.get().hideLoadingIndicator();
144 if(getResult() != null)
151 public Object deserializeResponse(String path, Response response) {
152 RestResource result1 = null;
153 if(aclass.equals(FolderResource.class)){
154 result1 = new FolderResource(path);
155 result1.createFromJSON(response.getText());
157 else if(aclass.equals(FileResource.class)){
158 result1 = new FileResource(path);
159 result1.createFromJSON(response.getHeader("X-GSS-Metadata"));
161 else if(aclass.equals(GroupsResource.class)){
162 result1 = new GroupsResource(path);
163 result1.createFromJSON(response.getText());
165 else if(aclass.equals(TrashResource.class)){
166 result1 = new TrashResource(path);
167 result1.createFromJSON(response.getText());
169 else if(aclass.equals(SharedResource.class)){
170 result1 = new SharedResource(path);
171 result1.createFromJSON(response.getText());
173 else if(aclass.equals(OthersResource.class)){
174 result1 = new OthersResource(path);
175 result1.createFromJSON(response.getText());
177 else if(aclass.equals(OtherUserResource.class)){
178 result1 = new OtherUserResource(path);
179 result1.createFromJSON(response.getText());
181 else if(aclass.equals(GroupResource.class)){
182 result1 = new GroupResource(path);
183 result1.createFromJSON(response.getText());
185 else if(aclass.equals(GroupUserResource.class)){
186 result1 = new GroupUserResource(path);
187 result1.createFromJSON(response.getText());
189 else if(aclass.equals(UserResource.class)){
190 result1 = new UserResource(path);
191 result1.createFromJSON(response.getText());
193 else if(aclass.equals(TagsResource.class)){
194 result1 = new TagsResource(path);
195 result1.createFromJSON(response.getText());
197 else if(aclass.equals(SearchResource.class)){
198 result1 = new SearchResource(path);
199 result1.createFromJSON(response.getText());
201 else if(aclass.equals(UserSearchResource.class)){
202 result1 = new UserSearchResource(path);
203 result1.createFromJSON(response.getText());
205 else if(aclass.equals(UploadStatusResource.class)){
206 result1 = new UploadStatusResource(path);
207 result1.createFromJSON(response.getText());
213 public T getCached() {
218 public void setCached(T cached) {
219 this.cached = cached;