Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / rest / RestGetCallback.java @ 2147acc8

History | View | Annotate | Download (2.1 kB)

1 a52ea5e4 pastith
/*
2 a52ea5e4 pastith
 * Copyright 2009 Electronic Business Systems Ltd.
3 a52ea5e4 pastith
 *
4 a52ea5e4 pastith
 * This file is part of GSS.
5 a52ea5e4 pastith
 *
6 a52ea5e4 pastith
 * GSS is free software: you can redistribute it and/or modify
7 a52ea5e4 pastith
 * it under the terms of the GNU General Public License as published by
8 a52ea5e4 pastith
 * the Free Software Foundation, either version 3 of the License, or
9 a52ea5e4 pastith
 * (at your option) any later version.
10 a52ea5e4 pastith
 *
11 a52ea5e4 pastith
 * GSS is distributed in the hope that it will be useful,
12 a52ea5e4 pastith
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 a52ea5e4 pastith
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 a52ea5e4 pastith
 * GNU General Public License for more details.
15 a52ea5e4 pastith
 *
16 a52ea5e4 pastith
 * You should have received a copy of the GNU General Public License
17 a52ea5e4 pastith
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18 a52ea5e4 pastith
 */
19 a52ea5e4 pastith
package gr.ebs.gss.client.rest;
20 a52ea5e4 pastith
21 a52ea5e4 pastith
import com.google.gwt.http.client.Request;
22 a52ea5e4 pastith
import com.google.gwt.http.client.RequestCallback;
23 a52ea5e4 pastith
import com.google.gwt.http.client.Response;
24 a52ea5e4 pastith
import com.google.gwt.user.client.rpc.AsyncCallback;
25 a52ea5e4 pastith
26 a52ea5e4 pastith
/**
27 a52ea5e4 pastith
 * @author kman
28 a52ea5e4 pastith
 */
29 a52ea5e4 pastith
public abstract class RestGetCallback implements RequestCallback {
30 a52ea5e4 pastith
31 a52ea5e4 pastith
        private static final int HTTP_OK = 200;
32 a52ea5e4 pastith
33 a52ea5e4 pastith
        private AsyncCallback callback;
34 a52ea5e4 pastith
        private String path;
35 a52ea5e4 pastith
        private int okcode = -1;
36 a52ea5e4 pastith
        public RestGetCallback(String path, AsyncCallback callback) {
37 a52ea5e4 pastith
                this.callback = callback;
38 a52ea5e4 pastith
                this.path = path;
39 a52ea5e4 pastith
        }
40 a52ea5e4 pastith
41 a52ea5e4 pastith
        public RestGetCallback(String path, AsyncCallback callback, int okCode) {
42 a52ea5e4 pastith
                this.callback = callback;
43 a52ea5e4 pastith
                this.path = path;
44 a52ea5e4 pastith
                okcode = okCode;
45 a52ea5e4 pastith
        }
46 a52ea5e4 pastith
47 a52ea5e4 pastith
        public void onError(Request request, Throwable exception) {
48 a52ea5e4 pastith
                callback.onFailure(exception);
49 a52ea5e4 pastith
        }
50 a52ea5e4 pastith
51 a52ea5e4 pastith
        public void onResponseReceived(Request request, Response response) {
52 a52ea5e4 pastith
                try {
53 a52ea5e4 pastith
                        if (okcode == -1 && response.getStatusCode() == HTTP_OK)
54 a52ea5e4 pastith
                                callback.onSuccess(deserialize(response));
55 a52ea5e4 pastith
                        //this one is only used for trash handling where empty trash has 201 status code
56 a52ea5e4 pastith
                        else if(okcode !=-1 && (response.getStatusCode() == okcode || response.getStatusCode() == HTTP_OK))
57 a52ea5e4 pastith
                                callback.onSuccess(deserialize(response));
58 a52ea5e4 pastith
                        else
59 a52ea5e4 pastith
                                callback.onFailure(new RestException(path, response.getStatusCode(), response.getStatusText(), response.getText()));
60 a52ea5e4 pastith
                } catch (Exception e) {
61 a52ea5e4 pastith
                        callback.onFailure(e);
62 a52ea5e4 pastith
                }
63 a52ea5e4 pastith
        }
64 a52ea5e4 pastith
65 a52ea5e4 pastith
        public abstract Object deserialize(Response response);
66 a52ea5e4 pastith
67 a52ea5e4 pastith
}