Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / rest / RestRequestCallback.java @ 45c76262

History | View | Annotate | Download (4.2 kB)

1 6084aa02 Christos Stathis
/*
2 e6e9f6e6 Christos KK Loverdos
 * Copyright 2011-2013 GRNET S.A. All rights reserved.
3 63366925 Christos Stathis
 *
4 63366925 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 63366925 Christos Stathis
 * without modification, are permitted provided that the following
6 63366925 Christos Stathis
 * conditions are met:
7 63366925 Christos Stathis
 *
8 63366925 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 63366925 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 63366925 Christos Stathis
 *      disclaimer.
11 63366925 Christos Stathis
 *
12 63366925 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 63366925 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 63366925 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 63366925 Christos Stathis
 *      provided with the distribution.
16 63366925 Christos Stathis
 *
17 63366925 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 63366925 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 63366925 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 63366925 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 63366925 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 63366925 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 63366925 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 63366925 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 63366925 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 63366925 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 63366925 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 63366925 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 63366925 Christos Stathis
 *
30 63366925 Christos Stathis
 * The views and conclusions contained in the software and
31 63366925 Christos Stathis
 * documentation are those of the authors and should not be
32 63366925 Christos Stathis
 * interpreted as representing official policies, either expressed
33 63366925 Christos Stathis
 * or implied, of GRNET S.A.
34 6084aa02 Christos Stathis
 */
35 6084aa02 Christos Stathis
36 6084aa02 Christos Stathis
package gr.grnet.pithos.web.client.rest;
37 6084aa02 Christos Stathis
38 45c76262 Christos KK Loverdos
import com.google.gwt.http.client.Header;
39 6084aa02 Christos Stathis
import com.google.gwt.http.client.Request;
40 6084aa02 Christos Stathis
import com.google.gwt.http.client.RequestCallback;
41 6084aa02 Christos Stathis
import com.google.gwt.http.client.Response;
42 45c76262 Christos KK Loverdos
import gr.grnet.pithos.web.client.Helpers;
43 8ddb4627 Christos KK Loverdos
import gr.grnet.pithos.web.client.Pithos;
44 cc0120ab Christos KK Loverdos
import gr.grnet.pithos.web.client.Resource;
45 6084aa02 Christos Stathis
46 6084aa02 Christos Stathis
public abstract class RestRequestCallback<T extends Resource> implements RequestCallback {
47 6084aa02 Christos Stathis
48 6084aa02 Christos Stathis
    private static final int HTTP_OK = 200;
49 6084aa02 Christos Stathis
    private int okcode = -1;
50 6084aa02 Christos Stathis
    private String path;
51 6084aa02 Christos Stathis
52 6084aa02 Christos Stathis
    public RestRequestCallback(String path, int okCode) {
53 6084aa02 Christos Stathis
        this.path = path;
54 6084aa02 Christos Stathis
        this.okcode = okCode;
55 6084aa02 Christos Stathis
    }
56 6084aa02 Christos Stathis
57 6084aa02 Christos Stathis
    public RestRequestCallback(String path) {
58 6084aa02 Christos Stathis
        this(path, -1);
59 6084aa02 Christos Stathis
    }
60 6084aa02 Christos Stathis
61 6084aa02 Christos Stathis
    @Override
62 6084aa02 Christos Stathis
    public void onResponseReceived(Request request, Response response) {
63 45c76262 Christos KK Loverdos
        Helpers.LOGResponse(response);
64 45c76262 Christos KK Loverdos
65 6084aa02 Christos Stathis
        try {
66 6084aa02 Christos Stathis
            if (response.getStatusCode() == HTTP_OK || (okcode !=-1 && response.getStatusCode() == okcode))
67 6084aa02 Christos Stathis
                onSuccess(deserialize(response));
68 6c372d22 Christos Stathis
            else if (response.getStatusCode() == Response.SC_UNAUTHORIZED) {
69 6c372d22 Christos Stathis
                    log(request, response);
70 9539e23d Christos Stathis
                    onUnauthorized(response);
71 6c372d22 Christos Stathis
            }
72 6084aa02 Christos Stathis
            else {
73 6084aa02 Christos Stathis
                String statusText = "";
74 6084aa02 Christos Stathis
                String text = "";
75 6084aa02 Christos Stathis
                // Ignore JavaScript errors caused by non-existent text.
76 6084aa02 Christos Stathis
                try {
77 6084aa02 Christos Stathis
                    statusText = response.getStatusText();
78 6084aa02 Christos Stathis
                }
79 6084aa02 Christos Stathis
                catch (Exception e) {}
80 6084aa02 Christos Stathis
81 6084aa02 Christos Stathis
                try {
82 6084aa02 Christos Stathis
                    text = response.getText();
83 6084aa02 Christos Stathis
                }
84 6084aa02 Christos Stathis
                catch (Exception e) {}
85 6084aa02 Christos Stathis
86 6084aa02 Christos Stathis
                onError(request, new RestException(path, response.getStatusCode(), statusText, text));
87 6084aa02 Christos Stathis
            }
88 6084aa02 Christos Stathis
        } catch (Exception e) {
89 8ddb4627 Christos KK Loverdos
            Pithos.LOG(e);
90 6084aa02 Christos Stathis
            onError(request, e);
91 6084aa02 Christos Stathis
        }
92 6084aa02 Christos Stathis
    }
93 6084aa02 Christos Stathis
94 6c372d22 Christos Stathis
    private native void log(Request req, Response resp)/*-{
95 6c372d22 Christos Stathis
            if ($wnd.console && $wnd.console.log) {
96 6c372d22 Christos Stathis
                    $wnd.console.log(req.@com.google.gwt.http.client.Request::toString()());
97 6c372d22 Christos Stathis
                    $wnd.console.log(resp.@com.google.gwt.http.client.Response::toString()());
98 6c372d22 Christos Stathis
                    $wnd.console.log(resp.@com.google.gwt.http.client.Response::getStatusCode()());
99 6c372d22 Christos Stathis
                    $wnd.console.log(resp.@com.google.gwt.http.client.Response::getStatusText()());
100 6c372d22 Christos Stathis
                    $wnd.console.log(resp.@com.google.gwt.http.client.Response::getStatusCode()());
101 6c372d22 Christos Stathis
                    $wnd.console.log(resp.@com.google.gwt.http.client.Response::getText()());
102 6c372d22 Christos Stathis
                    $wnd.console.log(resp.@com.google.gwt.http.client.Response::getHeadersAsString()());
103 6c372d22 Christos Stathis
            }
104 6c372d22 Christos Stathis
    }-*/;
105 6c372d22 Christos Stathis
    
106 6084aa02 Christos Stathis
    public abstract void onSuccess(T result);
107 6084aa02 Christos Stathis
108 6084aa02 Christos Stathis
    public abstract T deserialize(Response response);
109 9539e23d Christos Stathis
    
110 9539e23d Christos Stathis
    public abstract void onUnauthorized(Response response);
111 6084aa02 Christos Stathis
}