root / src / gr / grnet / pithos / web / client / catalog / GetUserCatalogs.java @ 01337c33
History | View | Annotate | Download (7.1 kB)
1 |
/*
|
---|---|
2 |
* Copyright 2011-2013 GRNET S.A. All rights reserved.
|
3 |
*
|
4 |
* Redistribution and use in source and binary forms, with or
|
5 |
* without modification, are permitted provided that the following
|
6 |
* conditions are met:
|
7 |
*
|
8 |
* 1. Redistributions of source code must retain the above
|
9 |
* copyright notice, this list of conditions and the following
|
10 |
* disclaimer.
|
11 |
*
|
12 |
* 2. Redistributions in binary form must reproduce the above
|
13 |
* copyright notice, this list of conditions and the following
|
14 |
* disclaimer in the documentation and/or other materials
|
15 |
* provided with the distribution.
|
16 |
*
|
17 |
* THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
18 |
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
20 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
21 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
22 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
23 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
24 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
25 |
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
26 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
27 |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
28 |
* POSSIBILITY OF SUCH DAMAGE.
|
29 |
*
|
30 |
* The views and conclusions contained in the software and
|
31 |
* documentation are those of the authors and should not be
|
32 |
* interpreted as representing official policies, either expressed
|
33 |
* or implied, of GRNET S.A.
|
34 |
*/
|
35 |
|
36 |
package gr.grnet.pithos.web.client.catalog; |
37 |
|
38 |
import com.google.gwt.core.client.GWT; |
39 |
import com.google.gwt.core.client.Scheduler; |
40 |
import com.google.gwt.http.client.*; |
41 |
import com.google.gwt.json.client.JSONArray; |
42 |
import com.google.gwt.json.client.JSONObject; |
43 |
import com.google.gwt.json.client.JSONParser; |
44 |
import com.google.gwt.json.client.JSONValue; |
45 |
import gr.grnet.pithos.web.client.Const; |
46 |
import gr.grnet.pithos.web.client.Helpers; |
47 |
import gr.grnet.pithos.web.client.Pithos; |
48 |
|
49 |
import java.util.List; |
50 |
|
51 |
/**
|
52 |
* The request via which we obtain user catalog info.
|
53 |
*/
|
54 |
public class GetUserCatalogs implements Scheduler.ScheduledCommand { |
55 |
private final Pithos app; |
56 |
private final String url; |
57 |
private final String userToken; |
58 |
private final List<String> ids; |
59 |
private final List<String> names; |
60 |
|
61 |
public static final int SuccessCode = 200; |
62 |
public static final String CallEndPoint = "/user_catalogs"; |
63 |
public static final String RequestField_uuids = "uuids"; |
64 |
public static final String RequestField_displaynames = "displaynames"; |
65 |
public static final String ResponseField_displayname_catalog = "displayname_catalog"; |
66 |
public static final String ResponseField_uuid_catalog = "uuid_catalog"; |
67 |
|
68 |
public GetUserCatalogs(Pithos app) {
|
69 |
this(app, null, null); |
70 |
} |
71 |
|
72 |
public GetUserCatalogs(Pithos app, String userID) { |
73 |
this(app, Helpers.toList(userID), null); |
74 |
} |
75 |
|
76 |
public GetUserCatalogs(Pithos app, List<String> ids) { |
77 |
this(app, ids, null); |
78 |
} |
79 |
|
80 |
public GetUserCatalogs(Pithos app, List<String> ids, List<String> names) { |
81 |
assert app != null; |
82 |
|
83 |
this.app = app;
|
84 |
|
85 |
// FIXME: Probably use Window.Location.getHost()
|
86 |
// https://server.com/v1/ --> https://server.com
|
87 |
String path = app.getApiPath();
|
88 |
path = Helpers.stripTrailing(path, "/");
|
89 |
path = Helpers.upToIncludingLastPart(path, "/");
|
90 |
path = Helpers.stripTrailing(path, "/");
|
91 |
|
92 |
// https://server.com/user_catalogs
|
93 |
this.url = path + CallEndPoint;
|
94 |
|
95 |
this.ids = Helpers.safeList(ids);
|
96 |
this.names = Helpers.safeList(names);
|
97 |
this.userToken = app.getUserToken();
|
98 |
} |
99 |
|
100 |
public String getURL() { |
101 |
return url;
|
102 |
} |
103 |
|
104 |
private JSONObject makeRequestData() {
|
105 |
final JSONObject root = new JSONObject(); |
106 |
final JSONArray uuids = Helpers.listToJSONArray(ids);
|
107 |
final JSONArray displaynames = Helpers.listToJSONArray(names);
|
108 |
|
109 |
root.put(RequestField_uuids, uuids); |
110 |
root.put(RequestField_displaynames, displaynames); |
111 |
|
112 |
return root;
|
113 |
} |
114 |
|
115 |
public UserCatalogs parseResult(JSONObject result) {
|
116 |
final UserCatalogs userCatalogs = new UserCatalogs(); |
117 |
final JSONObject uuid_catalog = result.get(ResponseField_uuid_catalog).isObject();
|
118 |
final JSONObject displayname_catalog = result.get(ResponseField_displayname_catalog).isObject();
|
119 |
|
120 |
for(String uuid: uuid_catalog.keySet()) { |
121 |
final String name = uuid_catalog.get(uuid).isString().stringValue(); |
122 |
userCatalogs.updateWithIDAndName(uuid, name); |
123 |
} |
124 |
|
125 |
for(String name: displayname_catalog.keySet()) { |
126 |
final String uuid = displayname_catalog.get(name).isString().stringValue(); |
127 |
userCatalogs.updateWithIDAndName(uuid, name); |
128 |
} |
129 |
|
130 |
return userCatalogs;
|
131 |
} |
132 |
|
133 |
public void onSuccess(Request request, Response response, JSONObject result, UserCatalogs userCatalogs) { |
134 |
} |
135 |
|
136 |
public void onBadStatusCode(Request request, Response response) { |
137 |
} |
138 |
|
139 |
public void onError(Request request, Throwable t) { |
140 |
app.LOG("GetUserCatalogs ", t);
|
141 |
} |
142 |
|
143 |
@Override
|
144 |
public void execute() { |
145 |
final RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, getURL()); |
146 |
rb.setHeader(Const.X_AUTH_TOKEN, userToken); |
147 |
final String requestData = makeRequestData().toString(); |
148 |
app.LOG("GetUserCatalog => ", requestData);
|
149 |
app.LOG(new Exception()); |
150 |
|
151 |
try {
|
152 |
rb.sendRequest(requestData, new RequestCallback() {
|
153 |
@Override
|
154 |
public void onResponseReceived(Request request, Response response) { |
155 |
final int statusCode = response.getStatusCode(); |
156 |
|
157 |
if(statusCode != SuccessCode) {
|
158 |
app.LOG("GetUserCatalog <= [", statusCode, " ", response.getStatusText(), "]"); |
159 |
GetUserCatalogs.this.onBadStatusCode(request, response); |
160 |
return;
|
161 |
} |
162 |
|
163 |
final String responseText = response.getText(); |
164 |
final JSONValue jsonValue = JSONParser.parseStrict(responseText);
|
165 |
app.LOG("GetUserCatalog <= ", jsonValue.toString());
|
166 |
final JSONObject result = jsonValue.isObject();
|
167 |
|
168 |
if(result == null) { |
169 |
GetUserCatalogs.this.onError(request, new Exception("GetUserCatalogs: json result is not an object")); |
170 |
return;
|
171 |
} |
172 |
|
173 |
final UserCatalogs userCatalogs = parseResult(result);
|
174 |
|
175 |
GetUserCatalogs.this.onSuccess(request, response, result, userCatalogs); |
176 |
} |
177 |
|
178 |
@Override
|
179 |
public void onError(Request request, Throwable exception) { |
180 |
GetUserCatalogs.this.onError(request, exception); |
181 |
} |
182 |
}); |
183 |
} |
184 |
catch(Exception e) { |
185 |
app.LOG("GetUserCatalogs. ", e);
|
186 |
} |
187 |
} |
188 |
|
189 |
public void scheduleDeferred() { |
190 |
Scheduler.get().scheduleDeferred(this);
|
191 |
} |
192 |
} |