6c16e3102966deb7d14362acf67a5c0f13e97647
[pithos-web-client] / src / gr / grnet / pithos / web / client / rest / HeadCommand.java
1 /*
2  * Copyright 2011 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above
13  *      copyright notice, this list of conditions and the following
14  *      disclaimer in the documentation and/or other materials
15  *      provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * The views and conclusions contained in the software and
31  * documentation are those of the authors and should not be
32  * interpreted as representing official policies, either expressed
33  * or implied, of GRNET S.A.
34  */
35 package gr.grnet.pithos.web.client.rest;
36
37 import gr.grnet.pithos.web.client.Pithos;
38 import gr.grnet.pithos.web.client.ObjectNotFoundException;
39 import gr.grnet.pithos.web.client.rest.resource.FileResource;
40 import gr.grnet.pithos.web.client.rest.resource.FolderResource;
41 import gr.grnet.pithos.web.client.rest.resource.GroupResource;
42 import gr.grnet.pithos.web.client.rest.resource.GroupUserResource;
43 import gr.grnet.pithos.web.client.rest.resource.GroupsResource;
44 import gr.grnet.pithos.web.client.rest.resource.RestResource;
45 import gr.grnet.pithos.web.client.rest.resource.SharedResource;
46 import gr.grnet.pithos.web.client.rest.resource.TrashResource;
47 import gr.grnet.pithos.web.client.rest.resource.UserResource;
48
49 import com.google.gwt.core.client.GWT;
50 import com.google.gwt.http.client.Request;
51 import com.google.gwt.http.client.RequestBuilder;
52 import com.google.gwt.http.client.Response;
53
54
55 public  abstract class HeadCommand<T extends RestResource> extends RestCommand{
56
57         boolean complete = false;
58         T result = null;
59         Class<T> aclass;
60         private boolean requestSent = false;
61         T cached;
62         final String path;
63
64         public HeadCommand(Class<T> theclass, String pathToGet, T theCached){
65                 this(theclass, pathToGet, true, theCached);
66         }
67
68         public HeadCommand(Class<T> theClass, String pathToGet, boolean showLoading, T theCached){
69                 setShowLoadingIndicator(showLoading);
70                 this.aclass = theClass;
71                 if(isShowLoadingIndicator())
72                         Pithos.get().showLoadingIndicator("Getting ",pathToGet);
73
74                 if(theClass.equals(FileResource.class))
75                         path = pathToGet;
76                 else
77                         path = fixPath(pathToGet);
78                 this.cached = theCached;
79
80         }
81
82         private void sendRequest(){
83                 if(requestSent)
84                         return;
85                 requestSent=true;
86                 RequestBuilder builder = new RequestBuilder(RequestBuilder.HEAD, path);
87                 if(cached!=null && cached.getLastModifiedSince() != null){
88                         GWT.log("ADDING IF MODIFIED HEADERS", null);
89                         builder.setHeader("If-Modified-Since", cached.getLastModifiedSince());
90                 }
91                 try {
92                         handleHeaders(builder, path);
93                         builder.sendRequest("", new RestCallback(path) {
94
95                                 @Override
96                                 public Object deserialize(Response response) {
97                                         return deserializeResponse(path, response);
98                                 }
99
100                                 @Override
101                                 public void handleError(Request request, Throwable exception) {
102                                         if(exception instanceof RestException)
103                                                 if(((RestException)exception).getHttpStatusCode() == 304 && cached != null){
104                                                         GWT.log("Using cache:"+cached.getUri(), null);
105                                                         handleSuccess(cached);
106                                                         return;
107                                                 }
108                                         complete = true;
109                                         HeadCommand.this.onError(exception);
110                                 }
111
112                                 @Override
113                                 public void handleSuccess(Object object) {
114                                         result = (T) object;
115                                         complete = true;
116                                 }
117
118                         });
119                 } catch (Exception ex) {
120                         complete = true;
121                         onError(ex);
122                 }
123         }
124
125         public boolean isComplete() {
126                 return complete;
127         }
128
129         public T getResult(){
130                 return result;
131         }
132
133         @Override
134         public boolean execute() {
135                 if(!requestSent)
136                         sendRequest();
137                 boolean com = isComplete();
138                 if(com){
139                         if(isShowLoadingIndicator())
140                                 Pithos.get().hideLoadingIndicator();
141                         if(getResult() != null)
142                                 onComplete();
143                         else
144                                 onError(new ObjectNotFoundException("Resource Not Found"));
145                         return false;
146                 }
147                 return true;
148         }
149
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());
155
156                 }
157                 else if(aclass.equals(FileResource.class)){
158                         result1 = new FileResource(aPath);
159                         result1.createFromJSON(response.getHeader("X-Pithos-Metadata"));
160                 }
161                 else if(aclass.equals(GroupsResource.class)){
162                         result1 = new GroupsResource(aPath);
163                         result1.createFromJSON(response.getText());
164                 }
165                 else if(aclass.equals(TrashResource.class)){
166                         result1 = new TrashResource(aPath);
167                         result1.createFromJSON(response.getText());
168
169                 }
170                 else if(aclass.equals(SharedResource.class)){
171                         result1 = new SharedResource(aPath);
172                         result1.createFromJSON(response.getText());
173
174                 }
175                 else if(aclass.equals(GroupResource.class)){
176                         result1 = new GroupResource(aPath);
177                         result1.createFromJSON(response.getText());
178
179                 }
180                 else if(aclass.equals(GroupUserResource.class)){
181                         result1 = new GroupUserResource(aPath);
182                         result1.createFromJSON(response.getText());
183
184                 }
185                 else if(aclass.equals(UserResource.class)){
186                         result1 = new UserResource(aPath);
187                         result1.createFromJSON(response.getText());
188
189                 }
190                 return result1;
191         }
192 }