2 * Copyright 2009 Electronic Business Systems Ltd.
4 * This file is part of GSS.
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.
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.
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/>.
19 package gr.ebs.gss.client.rest;
21 import com.google.gwt.http.client.Request;
22 import com.google.gwt.http.client.RequestCallback;
23 import com.google.gwt.http.client.Response;
30 public abstract class RestCallback implements RequestCallback {
32 private static final int HTTP_OK = 200;
34 private int okcode = -1;
36 public RestCallback(String aPath) {
40 public RestCallback(String aPath, int okCode) {
45 public void onError(Request request, Throwable exception) {
46 handleError(request, exception);
49 public void onResponseReceived(Request request, Response response) {
51 if (okcode == -1 && response.getStatusCode() == HTTP_OK)
52 handleSuccess(deserialize(response));
53 //this one is only used for trash handling where empty trash has 201 status code
54 else if(okcode !=-1 && (response.getStatusCode() == okcode || response.getStatusCode() == HTTP_OK))
55 handleSuccess(deserialize(response));
56 else if(response.getStatusCode() == 403)
57 RestCommand.sessionExpired();
59 String statusText = "";
61 // Ignore JavaScript errors caused by non-existent text.
63 statusText = response.getStatusText();
64 } catch (Exception e) { }
66 text = response.getText();
67 } catch (Exception e) { }
68 handleError(request, new RestException(path, response.getStatusCode(), statusText, text));
70 } catch (Exception e) {
71 handleError(request,e);
75 public abstract void handleSuccess(Object object);
77 public abstract void handleError(Request request, Throwable exception);
79 public abstract Object deserialize(Response response);