Fix of Revision: ec7b8d0b2c after code review. Added a check condition before remov...
[pithos] / src / gr / ebs / gss / client / rest / RestGetCallback.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 com.google.gwt.http.client.Request;
22 import com.google.gwt.http.client.RequestCallback;
23 import com.google.gwt.http.client.Response;
24 import com.google.gwt.user.client.rpc.AsyncCallback;
25
26 /**
27  * @author kman
28  */
29 public abstract class RestGetCallback implements RequestCallback {
30
31         private static final int HTTP_OK = 200;
32
33         private AsyncCallback callback;
34         private String path;
35         private int okcode = -1;
36
37         public RestGetCallback(String aPath, AsyncCallback aCallback) {
38                 callback = aCallback;
39                 path = aPath;
40         }
41
42         public RestGetCallback(String aPath, AsyncCallback aCallback, int okCode) {
43                 callback = aCallback;
44                 path = aPath;
45                 okcode = okCode;
46         }
47
48         @Override
49         public void onError(Request request, Throwable exception) {
50                 callback.onFailure(exception);
51         }
52
53         @Override
54         public void onResponseReceived(Request request, Response response) {
55                 try {
56                         if (okcode == -1 && response.getStatusCode() == HTTP_OK)
57                                 callback.onSuccess(deserialize(response));
58                         //this one is only used for trash handling where empty trash has 201 status code
59                         else if(okcode !=-1 && (response.getStatusCode() == okcode || response.getStatusCode() == HTTP_OK))
60                                 callback.onSuccess(deserialize(response));
61                         else
62                                 callback.onFailure(new RestException(path, response.getStatusCode(), response.getStatusText(), response.getText()));
63                 } catch (Exception e) {
64                         callback.onFailure(e);
65                 }
66         }
67
68         public abstract Object deserialize(Response response);
69
70 }