display message while image loads on the client viewer
[pithos] / src / gr / ebs / gss / client / rest / HeadCommand.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.exceptions.ObjectNotFoundException;
23 import gr.ebs.gss.client.rest.resource.FileResource;
24 import gr.ebs.gss.client.rest.resource.FolderResource;
25 import gr.ebs.gss.client.rest.resource.GroupResource;
26 import gr.ebs.gss.client.rest.resource.GroupUserResource;
27 import gr.ebs.gss.client.rest.resource.GroupsResource;
28 import gr.ebs.gss.client.rest.resource.RestResource;
29 import gr.ebs.gss.client.rest.resource.SharedResource;
30 import gr.ebs.gss.client.rest.resource.TrashResource;
31 import gr.ebs.gss.client.rest.resource.UserResource;
32
33 import com.google.gwt.http.client.Request;
34 import com.google.gwt.http.client.Response;
35
36
37 /**
38  * @author kman
39  *
40  */
41 public  abstract class HeadCommand <T extends RestResource> extends RestCommand{
42
43         boolean complete = false;
44         T result = null;
45         Class<T> aclass;
46
47         public HeadCommand(Class<T> aclass, String pathToGet){
48                 this(aclass, pathToGet, true);
49         }
50         public HeadCommand(Class<T> aclass, String pathToGet, boolean showLoading){
51                 setShowLoadingIndicator(showLoading);
52                 this.aclass = aclass;
53                 if(isShowLoadingIndicator())
54                         GSS.get().showLoadingIndicator();
55                 final String path;
56                 if(aclass.equals(FileResource.class))
57                         path = pathToGet;
58                 else
59                         path = fixPath(pathToGet);
60                 RestRequestBuilder builder = new RestRequestBuilder("HEAD", path);
61
62                 try {
63                         handleHeaders(builder, path);
64                         builder.sendRequest("", new RestCallback(path) {
65
66                                 public Object deserialize(Response response) {
67                                         return deserializeResponse(path, response);
68                                 }
69
70                                 public void handleError(Request request, Throwable exception) {
71                                         complete = true;
72                                         HeadCommand.this.onError(exception);
73                                 }
74
75                                 public void handleSuccess(Object object) {
76                                         result = (T) object;
77                                         complete = true;
78                                 }
79
80                         });
81                 } catch (Exception ex) {
82                         complete = true;
83                         onError(ex);
84                 }
85         }
86
87
88         public boolean isComplete() {
89                 return complete;
90         }
91
92         public T getResult(){
93                 return result;
94         }
95
96         public boolean execute() {
97                 boolean com = isComplete();
98                 if(com){
99                         if(isShowLoadingIndicator())
100                                 GSS.get().hideLoadingIndicator();
101                         if(getResult() != null)
102                                 onComplete();
103                         else
104                                 onError(new ObjectNotFoundException("Resource Not Found"));
105                         return false;
106                 }
107                 return true;
108         }
109
110         public  Object deserializeResponse(String path, Response response){
111                 RestResource result1 = null;
112                 if(aclass.equals(FolderResource.class)){
113                         result1 = new FolderResource(path);
114                         result1.createFromJSON(response.getText());
115
116                 }
117                 else if(aclass.equals(FileResource.class)){
118                         result1 = new FileResource(path);
119                         result1.createFromJSON(response.getHeader("X-GSS-Metadata"));
120                 }
121                 else if(aclass.equals(GroupsResource.class)){
122                         result1 = new GroupsResource(path);
123                         result1.createFromJSON(response.getText());
124                 }
125                 else if(aclass.equals(TrashResource.class)){
126                         result1 = new TrashResource(path);
127                         result1.createFromJSON(response.getText());
128
129                 }
130                 else if(aclass.equals(SharedResource.class)){
131                         result1 = new SharedResource(path);
132                         result1.createFromJSON(response.getText());
133
134                 }
135                 else if(aclass.equals(GroupResource.class)){
136                         result1 = new GroupResource(path);
137                         result1.createFromJSON(response.getText());
138
139                 }
140                 else if(aclass.equals(GroupUserResource.class)){
141                         result1 = new GroupUserResource(path);
142                         result1.createFromJSON(response.getText());
143
144                 }
145                 else if(aclass.equals(UserResource.class)){
146                         result1 = new UserResource(path);
147                         result1.createFromJSON(response.getText());
148
149                 }
150                 return result1;
151
152         }
153
154 }