Statistics
| Branch: | Tag: | Revision:

root / src / org / gss_project / gss / web / client / rest / RestCallback.java @ 1206:292dec4eae08

History | View | Annotate | Download (2.4 kB)

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 org.gss_project.gss.web.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

    
25

    
26
/**
27
 * @author kman
28
 *
29
 */
30
public abstract class RestCallback  implements RequestCallback {
31

    
32
        private static final int HTTP_OK = 200;
33
        private String path;
34
        private int okcode = -1;
35

    
36
        public RestCallback(String aPath) {
37
                path = aPath;
38
        }
39

    
40
        public RestCallback(String aPath, int okCode) {
41
                path = aPath;
42
                okcode = okCode;
43
        }
44

    
45
        @Override
46
        public void onError(Request request, Throwable exception) {
47
                handleError(request, exception);
48
        }
49

    
50
        @Override
51
        public void onResponseReceived(Request request, Response response) {
52
                try {
53
                        if (okcode == -1 && response.getStatusCode() == HTTP_OK)
54
                                handleSuccess(deserialize(response));
55
                        //this one is only used for trash handling where empty trash has 201 status code
56
                        else if(okcode !=-1 && (response.getStatusCode() == okcode || response.getStatusCode() == HTTP_OK))
57
                                handleSuccess(deserialize(response));
58
                        else if(response.getStatusCode() == 403)
59
                                RestCommand.sessionExpired();
60
                        else {
61
                                String statusText = "";
62
                                String text = "";
63
                                // Ignore JavaScript errors caused by non-existent text.
64
                                try {
65
                                        statusText = response.getStatusText();
66
                                } catch (Exception e) {        }
67
                                try {
68
                                        text = response.getText();
69
                                } catch (Exception e) {        }
70
                                handleError(request, new RestException(path, response.getStatusCode(), statusText, text));
71
                        }
72
                } catch (Exception e) {
73
                        handleError(request,e);
74
                }
75
        }
76

    
77
        public abstract void handleSuccess(Object object);
78

    
79
        public abstract void handleError(Request request, Throwable exception);
80

    
81
        public abstract Object deserialize(Response response);
82

    
83
}