root / web_client / src / gr / grnet / pithos / web / client / rest / RestRequestCallback.java @ 9e8e14e4
History | View | Annotate | Download (1.6 kB)
1 |
/*
|
---|---|
2 |
* Copyright (c) 2011 Greek Research and Technology Network
|
3 |
*/
|
4 |
|
5 |
package gr.grnet.pithos.web.client.rest; |
6 |
|
7 |
import com.google.gwt.http.client.Request; |
8 |
import com.google.gwt.http.client.RequestCallback; |
9 |
import com.google.gwt.http.client.Response; |
10 |
import gr.grnet.pithos.web.client.foldertree.Resource; |
11 |
|
12 |
public abstract class RestRequestCallback<T extends Resource> implements RequestCallback { |
13 |
|
14 |
private static final int HTTP_OK = 200; |
15 |
private int okcode = -1; |
16 |
private String path; |
17 |
|
18 |
public RestRequestCallback(String path, int okCode) { |
19 |
this.path = path;
|
20 |
this.okcode = okCode;
|
21 |
} |
22 |
|
23 |
public RestRequestCallback(String path) { |
24 |
this(path, -1); |
25 |
} |
26 |
|
27 |
@Override
|
28 |
public void onResponseReceived(Request request, Response response) { |
29 |
try {
|
30 |
if (response.getStatusCode() == HTTP_OK || (okcode !=-1 && response.getStatusCode() == okcode)) |
31 |
onSuccess(deserialize(response)); |
32 |
else {
|
33 |
String statusText = ""; |
34 |
String text = ""; |
35 |
// Ignore JavaScript errors caused by non-existent text.
|
36 |
try {
|
37 |
statusText = response.getStatusText(); |
38 |
} |
39 |
catch (Exception e) {} |
40 |
|
41 |
try {
|
42 |
text = response.getText(); |
43 |
} |
44 |
catch (Exception e) {} |
45 |
|
46 |
onError(request, new RestException(path, response.getStatusCode(), statusText, text));
|
47 |
} |
48 |
} catch (Exception e) { |
49 |
onError(request, e); |
50 |
} |
51 |
} |
52 |
|
53 |
public abstract void onSuccess(T result); |
54 |
|
55 |
public abstract T deserialize(Response response); |
56 |
} |