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