2 * Copyright 2011 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
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.
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.
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.
35 package gr.grnet.pithos.web.client.rest;
37 import gr.grnet.pithos.web.client.Pithos;
38 import gr.grnet.pithos.web.client.rest.resource.FileResource;
39 import gr.grnet.pithos.web.client.rest.resource.FolderResource;
40 import gr.grnet.pithos.web.client.rest.resource.GroupResource;
41 import gr.grnet.pithos.web.client.rest.resource.GroupUserResource;
42 import gr.grnet.pithos.web.client.rest.resource.GroupsResource;
43 import gr.grnet.pithos.web.client.rest.resource.OtherUserResource;
44 import gr.grnet.pithos.web.client.rest.resource.OthersResource;
45 import gr.grnet.pithos.web.client.rest.resource.RestResource;
46 import gr.grnet.pithos.web.client.rest.resource.SearchResource;
47 import gr.grnet.pithos.web.client.rest.resource.SharedResource;
48 import gr.grnet.pithos.web.client.rest.resource.TagsResource;
49 import gr.grnet.pithos.web.client.rest.resource.TrashResource;
50 import gr.grnet.pithos.web.client.rest.resource.UploadStatusResource;
51 import gr.grnet.pithos.web.client.rest.resource.UserResource;
52 import gr.grnet.pithos.web.client.rest.resource.UserSearchResource;
54 import java.util.HashMap;
57 import com.google.gwt.core.client.GWT;
58 import com.google.gwt.http.client.Request;
59 import com.google.gwt.http.client.RequestBuilder;
60 import com.google.gwt.http.client.Response;
62 public abstract class GetCommand<T extends RestResource> extends RestCommand{
64 boolean complete = false;
66 Throwable exception = null;
68 private final String path;
69 private String username;
70 private boolean requestSent = false;
73 private static final long MAX_CACHE_AGE = 1000;
75 private static class RequestData {
77 public String username;
79 public RequestData(String _path, String _username) {
85 public int hashCode() {
88 result = prime * result + ((path == null) ? 0 : path.hashCode());
89 result = prime * result + ((username == null) ? 0 : username.hashCode());
94 public boolean equals(Object obj) {
99 if (getClass() != obj.getClass())
101 RequestData other = (RequestData) obj;
103 if (other.path != null)
105 } else if (!path.equals(other.path))
107 if (username == null) {
108 if (other.username != null)
110 } else if (!username.equals(other.username))
116 private static class ResponseData {
117 public long timestamp;
118 public Object result;
119 public ResponseData(long _timestamp, Object _result) {
120 timestamp = _timestamp;
125 private static Map<RequestData, ResponseData> cache = new HashMap<RequestData, ResponseData>();
128 public GetCommand(Class<T> theclass, String pathToGet, T theCached){
129 this(theclass, pathToGet, true, theCached);
132 public GetCommand(Class<T> theclass, String pathToGet, boolean showLoading, T theCached){
133 setShowLoadingIndicator(showLoading);
134 if(isShowLoadingIndicator())
135 Pithos.get().showLoadingIndicator("Getting ",pathToGet);
136 this.aclass = theclass;
137 if(pathToGet.indexOf("?") != -1)
140 path =fixPath(pathToGet);
141 this.cached = theCached;
144 public GetCommand(Class<T> theclass, String aUsername , String pathToGet, T theCached){
145 this(theclass, aUsername, pathToGet, true, theCached);
148 public GetCommand(Class<T> theclass, String aUsername , String pathToGet, boolean showLoading, T theCached){
149 setShowLoadingIndicator(showLoading);
150 if(isShowLoadingIndicator())
151 Pithos.get().showLoadingIndicator("Getting ",pathToGet);
152 this.aclass = theclass;
153 path = fixPath(pathToGet);
154 this.username = aUsername;
155 this.cached = theCached;
158 private void sendRequest(){
162 RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, path);
163 if(cached!=null && cached.getLastModifiedSince() != null)
164 builder.setHeader("If-Modified-Since", cached.getLastModifiedSince());
167 handleHeaders(builder, path);
169 handleHeaders(username, builder, path);
170 builder.sendRequest("", new RestCallback(path) {
173 public Object deserialize(Response response) {
174 return deserializeResponse(path, response);
178 public void handleError(Request request, Throwable _exception) {
181 exception = _exception;
182 if(_exception instanceof RestException)
183 if(((RestException)_exception).getHttpStatusCode() == 304 && cached != null){
184 GWT.log("Using cache:"+cached.getUri(), null);
185 handleSuccess(cached);
192 public void handleSuccess(Object object) {
198 } catch (Exception ex) {
203 public boolean isComplete() {
207 public T getResult(){
212 public boolean execute() {
213 boolean com = isComplete();
214 RequestData key = new RequestData(path, username);
216 if (cache.containsKey(key)) {
217 ResponseData resp = cache.get(key);
223 if (System.currentTimeMillis()-resp.timestamp>MAX_CACHE_AGE) {
224 // Cache stale, remove
229 if(isShowLoadingIndicator())
230 Pithos.get().hideLoadingIndicator();
231 if (resp.result instanceof Throwable) {
232 // Error to be handled
233 Throwable ex = (Throwable) resp.result;
237 result = (T) resp.result;
238 if (result != null) {
253 if(isShowLoadingIndicator())
254 Pithos.get().hideLoadingIndicator();
255 if(getResult() != null) {
257 cache.put(key, new ResponseData(System.currentTimeMillis(), getResult()));
261 cache.put(key, new ResponseData(System.currentTimeMillis(), exception));
269 public Object deserializeResponse(String aPath, Response response) {
270 RestResource result1 = null;
271 if(aclass.equals(FolderResource.class)){
272 result1 = new FolderResource(aPath);
273 result1.createFromJSON(response.getText());
275 else if(aclass.equals(FileResource.class)){
276 result1 = new FileResource(aPath);
277 result1.createFromJSON(response.getHeader("X-Pithos-Metadata"));
279 else if(aclass.equals(GroupsResource.class)){
280 result1 = new GroupsResource(aPath);
281 result1.createFromJSON(response.getText());
283 else if(aclass.equals(TrashResource.class)){
284 result1 = new TrashResource(aPath);
285 result1.createFromJSON(response.getText());
287 else if(aclass.equals(SharedResource.class)){
288 result1 = new SharedResource(aPath);
289 result1.createFromJSON(response.getText());
291 else if(aclass.equals(OthersResource.class)){
292 result1 = new OthersResource(aPath);
293 result1.createFromJSON(response.getText());
295 else if(aclass.equals(OtherUserResource.class)){
296 result1 = new OtherUserResource(aPath);
297 result1.createFromJSON(response.getText());
299 else if(aclass.equals(GroupResource.class)){
300 result1 = new GroupResource(aPath);
301 result1.createFromJSON(response.getText());
303 else if(aclass.equals(GroupUserResource.class)){
304 result1 = new GroupUserResource(aPath);
305 result1.createFromJSON(response.getText());
307 else if(aclass.equals(UserResource.class)){
308 result1 = new UserResource(aPath);
309 result1.createFromJSON(response.getText());
311 else if(aclass.equals(TagsResource.class)){
312 result1 = new TagsResource(aPath);
313 result1.createFromJSON(response.getText());
315 else if(aclass.equals(SearchResource.class)){
316 result1 = new SearchResource(aPath);
317 result1.createFromJSON(response.getText());
319 else if(aclass.equals(UserSearchResource.class)){
320 result1 = new UserSearchResource(aPath);
321 result1.createFromJSON(response.getText());
323 else if(aclass.equals(UploadStatusResource.class)){
324 result1 = new UploadStatusResource(aPath);
325 result1.createFromJSON(response.getText());
330 public T getCached() {
334 public void setCached(T theCached) {
335 this.cached = theCached;
338 public boolean usedCachedVersion(){
339 if(exception !=null && exception instanceof RestException)
340 if(((RestException)exception).getHttpStatusCode() == 304){