Updated copyright notice
[pithos-web-client] / src / gr / grnet / pithos / web / client / rest / RestGetCallback.java
1 /*
2  *  Copyright (c) 2011 Greek Research and Technology Network
3  */
4 package gr.grnet.pithos.web.client.rest;
5
6 import com.google.gwt.http.client.Request;
7 import com.google.gwt.http.client.RequestCallback;
8 import com.google.gwt.http.client.Response;
9 import com.google.gwt.user.client.rpc.AsyncCallback;
10
11 public abstract class RestGetCallback implements RequestCallback {
12
13         private static final int HTTP_OK = 200;
14
15         private AsyncCallback callback;
16         private String path;
17         private int okcode = -1;
18
19         public RestGetCallback(String aPath, AsyncCallback aCallback) {
20                 callback = aCallback;
21                 path = aPath;
22         }
23
24         public RestGetCallback(String aPath, AsyncCallback aCallback, int okCode) {
25                 callback = aCallback;
26                 path = aPath;
27                 okcode = okCode;
28         }
29
30         @Override
31         public void onError(Request request, Throwable exception) {
32                 callback.onFailure(exception);
33         }
34
35         @Override
36         public void onResponseReceived(Request request, Response response) {
37                 try {
38                         if (okcode == -1 && response.getStatusCode() == HTTP_OK)
39                                 callback.onSuccess(deserialize(response));
40                         //this one is only used for trash handling where empty trash has 201 status code
41                         else if(okcode !=-1 && (response.getStatusCode() == okcode || response.getStatusCode() == HTTP_OK))
42                                 callback.onSuccess(deserialize(response));
43                         else
44                                 callback.onFailure(new RestException(path, response.getStatusCode(), response.getStatusText(), response.getText()));
45                 } catch (Exception e) {
46                         callback.onFailure(e);
47                 }
48         }
49
50         public abstract Object deserialize(Response response);
51
52 }