Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / rest / RestRequestCallback.java @ 2acd47a8

History | View | Annotate | Download (3.3 kB)

1 6084aa02 Christos Stathis
/*
2 cae2a8db Christos Stathis
 * Copyright 2011-2012 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 6084aa02 Christos Stathis
import com.google.gwt.http.client.Request;
39 6084aa02 Christos Stathis
import com.google.gwt.http.client.RequestCallback;
40 6084aa02 Christos Stathis
import com.google.gwt.http.client.Response;
41 6084aa02 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Resource;
42 6084aa02 Christos Stathis
43 6084aa02 Christos Stathis
public abstract class RestRequestCallback<T extends Resource> implements RequestCallback {
44 6084aa02 Christos Stathis
45 6084aa02 Christos Stathis
    private static final int HTTP_OK = 200;
46 6084aa02 Christos Stathis
    private int okcode = -1;
47 6084aa02 Christos Stathis
    private String path;
48 6084aa02 Christos Stathis
49 6084aa02 Christos Stathis
    public RestRequestCallback(String path, int okCode) {
50 6084aa02 Christos Stathis
        this.path = path;
51 6084aa02 Christos Stathis
        this.okcode = okCode;
52 6084aa02 Christos Stathis
    }
53 6084aa02 Christos Stathis
54 6084aa02 Christos Stathis
    public RestRequestCallback(String path) {
55 6084aa02 Christos Stathis
        this(path, -1);
56 6084aa02 Christos Stathis
    }
57 6084aa02 Christos Stathis
58 6084aa02 Christos Stathis
    @Override
59 6084aa02 Christos Stathis
    public void onResponseReceived(Request request, Response response) {
60 6084aa02 Christos Stathis
        try {
61 6084aa02 Christos Stathis
            if (response.getStatusCode() == HTTP_OK || (okcode !=-1 && response.getStatusCode() == okcode))
62 6084aa02 Christos Stathis
                onSuccess(deserialize(response));
63 9539e23d Christos Stathis
            else if (response.getStatusCode() == Response.SC_UNAUTHORIZED)
64 9539e23d Christos Stathis
                    onUnauthorized(response);
65 6084aa02 Christos Stathis
            else {
66 6084aa02 Christos Stathis
                String statusText = "";
67 6084aa02 Christos Stathis
                String text = "";
68 6084aa02 Christos Stathis
                // Ignore JavaScript errors caused by non-existent text.
69 6084aa02 Christos Stathis
                try {
70 6084aa02 Christos Stathis
                    statusText = response.getStatusText();
71 6084aa02 Christos Stathis
                }
72 6084aa02 Christos Stathis
                catch (Exception e) {}
73 6084aa02 Christos Stathis
74 6084aa02 Christos Stathis
                try {
75 6084aa02 Christos Stathis
                    text = response.getText();
76 6084aa02 Christos Stathis
                }
77 6084aa02 Christos Stathis
                catch (Exception e) {}
78 6084aa02 Christos Stathis
79 6084aa02 Christos Stathis
                onError(request, new RestException(path, response.getStatusCode(), statusText, text));
80 6084aa02 Christos Stathis
            }
81 6084aa02 Christos Stathis
        } catch (Exception e) {
82 6084aa02 Christos Stathis
            onError(request, e);
83 6084aa02 Christos Stathis
        }
84 6084aa02 Christos Stathis
    }
85 6084aa02 Christos Stathis
86 6084aa02 Christos Stathis
    public abstract void onSuccess(T result);
87 6084aa02 Christos Stathis
88 6084aa02 Christos Stathis
    public abstract T deserialize(Response response);
89 9539e23d Christos Stathis
    
90 9539e23d Christos Stathis
    public abstract void onUnauthorized(Response response);
91 6084aa02 Christos Stathis
}