root / src / gr / grnet / pithos / web / client / rest / GetRequest.java @ 45c76262
History | View | Annotate | Download (5.1 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 gr.grnet.pithos.web.client.Const; |
39 | 45c76262 | Christos KK Loverdos | import gr.grnet.pithos.web.client.Helpers; |
40 | 8ddb4627 | Christos KK Loverdos | import gr.grnet.pithos.web.client.Pithos; |
41 | cc0120ab | Christos KK Loverdos | import gr.grnet.pithos.web.client.Resource; |
42 | 7811b9d1 | Christos Stathis | |
43 | 7811b9d1 | Christos Stathis | import java.util.HashMap; |
44 | 7811b9d1 | Christos Stathis | import java.util.Map; |
45 | 7811b9d1 | Christos Stathis | |
46 | 6084aa02 | Christos Stathis | import com.google.gwt.core.client.GWT; |
47 | 6084aa02 | Christos Stathis | import com.google.gwt.core.client.Scheduler.ScheduledCommand; |
48 | 6084aa02 | Christos Stathis | import com.google.gwt.http.client.Request; |
49 | 6084aa02 | Christos Stathis | import com.google.gwt.http.client.RequestBuilder; |
50 | 6084aa02 | Christos Stathis | import com.google.gwt.http.client.RequestException; |
51 | 6084aa02 | Christos Stathis | import com.google.gwt.http.client.Response; |
52 | 6084aa02 | Christos Stathis | |
53 | 6084aa02 | Christos Stathis | public abstract class GetRequest<T extends Resource> implements ScheduledCommand { |
54 | 2f4ea617 | Christos Stathis | |
55 | 2f4ea617 | Christos Stathis | protected static final int MAX_RETRIES = 3; |
56 | 2f4ea617 | Christos Stathis | |
57 | dc8cc18a | Christos KK Loverdos | protected int retries = 0; |
58 | 6084aa02 | Christos Stathis | |
59 | cc0120ab | Christos KK Loverdos | protected Class<T> resourceClass; |
60 | 6084aa02 | Christos Stathis | |
61 | b51c628b | Christos Stathis | private String api; |
62 | b51c628b | Christos Stathis | |
63 | 7811b9d1 | Christos Stathis | protected String owner; |
64 | b51c628b | Christos Stathis | |
65 | 6084aa02 | Christos Stathis | private String path; |
66 | 6084aa02 | Christos Stathis | |
67 | 6084aa02 | Christos Stathis | private int okCode; |
68 | 6084aa02 | Christos Stathis | |
69 | 7811b9d1 | Christos Stathis | protected T result;
|
70 | 8e61880b | Christos Stathis | |
71 | 6084aa02 | Christos Stathis | private Map<String, String> headers = new HashMap<String, String>(); |
72 | 6084aa02 | Christos Stathis | |
73 | 7811b9d1 | Christos Stathis | public abstract void onSuccess(T _result); |
74 | 6084aa02 | Christos Stathis | |
75 | 6084aa02 | Christos Stathis | public abstract void onError(Throwable t); |
76 | 6084aa02 | Christos Stathis | |
77 | dc8cc18a | Christos KK Loverdos | public GetRequest(Class<T> resourceClass, String api, String owner, String path, int okCode, T result) { |
78 | cc0120ab | Christos KK Loverdos | this.resourceClass = resourceClass;
|
79 | b51c628b | Christos Stathis | this.api = api;
|
80 | b51c628b | Christos Stathis | this.owner = owner;
|
81 | 6084aa02 | Christos Stathis | this.path = path;
|
82 | 6084aa02 | Christos Stathis | this.okCode = okCode;
|
83 | 8e61880b | Christos Stathis | this.result = result;
|
84 | 6084aa02 | Christos Stathis | } |
85 | 6084aa02 | Christos Stathis | |
86 | dc8cc18a | Christos KK Loverdos | public GetRequest(Class<T> resourceClass, String api, String owner, String path) { |
87 | dc8cc18a | Christos KK Loverdos | this(resourceClass, api, owner, path, -1, null); |
88 | 8ddb4627 | Christos KK Loverdos | Pithos.LOG("GetRequest() resourceClass = ", resourceClass.getName(), ", api=", api, ", owner=", owner, ", path=", path); |
89 | 8e61880b | Christos Stathis | } |
90 | 8e61880b | Christos Stathis | |
91 | dc8cc18a | Christos KK Loverdos | public GetRequest(Class<T> resourceClass, String api, String owner, String path, T result) { |
92 | dc8cc18a | Christos KK Loverdos | this(resourceClass, api, owner, path, -1, result); |
93 | 6084aa02 | Christos Stathis | } |
94 | 6084aa02 | Christos Stathis | |
95 | 6084aa02 | Christos Stathis | @Override
|
96 | 6084aa02 | Christos Stathis | public void execute() { |
97 | 45c76262 | Christos KK Loverdos | if (path.contains(Const.QUESTION_MARK)) {
|
98 | f5023f13 | Christos Stathis | path += "&t=" + System.currentTimeMillis(); |
99 | 45c76262 | Christos KK Loverdos | } |
100 | 45c76262 | Christos KK Loverdos | else {
|
101 | f5023f13 | Christos Stathis | path += "?t=" + System.currentTimeMillis(); |
102 | 45c76262 | Christos KK Loverdos | } |
103 | 45c76262 | Christos KK Loverdos | |
104 | 45c76262 | Christos KK Loverdos | Pithos.LOG("GET ", api + owner + path);
|
105 | 45c76262 | Christos KK Loverdos | |
106 | b51c628b | Christos Stathis | RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, api + owner + path);
|
107 | 45c76262 | Christos KK Loverdos | Helpers.setHeaders(builder, headers); |
108 | cc0120ab | Christos KK Loverdos | |
109 | 6084aa02 | Christos Stathis | try {
|
110 | b51c628b | Christos Stathis | builder.sendRequest("", new RestRequestCallback<T>(api + owner + path, okCode) { |
111 | 6084aa02 | Christos Stathis | @Override
|
112 | 6084aa02 | Christos Stathis | public void onSuccess(T object) { |
113 | 6084aa02 | Christos Stathis | GetRequest.this.onSuccess(object); |
114 | 6084aa02 | Christos Stathis | } |
115 | 6084aa02 | Christos Stathis | |
116 | 6084aa02 | Christos Stathis | @Override
|
117 | 6084aa02 | Christos Stathis | public T deserialize(Response response) {
|
118 | dc8cc18a | Christos KK Loverdos | return Resource.createFromResponse(resourceClass, owner, response, result);
|
119 | 6084aa02 | Christos Stathis | } |
120 | 6084aa02 | Christos Stathis | |
121 | 6084aa02 | Christos Stathis | @Override
|
122 | ebead1b5 | Christos Stathis | public void onError(Request request, Throwable throwable) { |
123 | 6084aa02 | Christos Stathis | if (throwable instanceof RestException) { |
124 | f5023f13 | Christos Stathis | if (((RestException) throwable).getHttpStatusCode() == 304 && result != null){ |
125 | 8ddb4627 | Christos KK Loverdos | Pithos.LOG("Using cache: ", result.toString());
|
126 | f5023f13 | Christos Stathis | onSuccess(result); |
127 | 6084aa02 | Christos Stathis | return;
|
128 | 6084aa02 | Christos Stathis | } |
129 | 6084aa02 | Christos Stathis | } |
130 | 6084aa02 | Christos Stathis | GetRequest.this.onError(throwable); |
131 | 6084aa02 | Christos Stathis | } |
132 | 9539e23d | Christos Stathis | |
133 | 9539e23d | Christos Stathis | @Override
|
134 | 9539e23d | Christos Stathis | public void onUnauthorized(Response response) { |
135 | 9539e23d | Christos Stathis | GetRequest.this.onUnauthorized(response); |
136 | 9539e23d | Christos Stathis | } |
137 | 6084aa02 | Christos Stathis | }); |
138 | 2f4ea617 | Christos Stathis | retries++; |
139 | 6084aa02 | Christos Stathis | } |
140 | 6084aa02 | Christos Stathis | catch (RequestException e) {
|
141 | 8ddb4627 | Christos KK Loverdos | Pithos.LOG("Error in GetRequest", e);
|
142 | 6084aa02 | Christos Stathis | } |
143 | 6084aa02 | Christos Stathis | } |
144 | 6084aa02 | Christos Stathis | |
145 | 9539e23d | Christos Stathis | protected abstract void onUnauthorized(Response response); |
146 | 9539e23d | Christos Stathis | |
147 | 9539e23d | Christos Stathis | public void setHeader(String header, String value) { |
148 | 6084aa02 | Christos Stathis | headers.put(header, value); |
149 | 6084aa02 | Christos Stathis | } |
150 | 6084aa02 | Christos Stathis | } |