root / src / gr / grnet / pithos / web / client / catalog / GetUserCatalogs.java @ 28e270f9
History | View | Annotate | Download (6.3 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.Scheduler; |
39 |
import com.google.gwt.http.client.*; |
40 |
import com.google.gwt.json.client.JSONArray; |
41 |
import com.google.gwt.json.client.JSONObject; |
42 |
import com.google.gwt.json.client.JSONParser; |
43 |
import com.google.gwt.json.client.JSONValue; |
44 |
import gr.grnet.pithos.web.client.Const; |
45 |
import gr.grnet.pithos.web.client.Helpers; |
46 |
import gr.grnet.pithos.web.client.Pithos; |
47 |
|
48 |
import java.util.List; |
49 |
|
50 |
/**
|
51 |
* The request via which we obtain user catalog info.
|
52 |
*/
|
53 |
public class GetUserCatalogs implements Scheduler.ScheduledCommand { |
54 |
private final String url; |
55 |
private final String userToken; |
56 |
private final List<String> ids; |
57 |
private final List<String> names; |
58 |
|
59 |
public static final int SuccessCode = 200; |
60 |
public static final String RequestField_uuids = "uuids"; |
61 |
public static final String RequestField_displaynames = "displaynames"; |
62 |
public static final String ResponseField_displayname_catalog = "displayname_catalog"; |
63 |
public static final String ResponseField_uuid_catalog = "uuid_catalog"; |
64 |
|
65 |
public GetUserCatalogs(String userToken, List<String> ids, List<String> names) { |
66 |
this.url = Pithos.getUserCatalogsURL();
|
67 |
this.userToken = userToken;
|
68 |
this.ids = Helpers.safeList(ids);
|
69 |
this.names = Helpers.safeList(names);
|
70 |
} |
71 |
|
72 |
public String getURL() { |
73 |
return url;
|
74 |
} |
75 |
|
76 |
private JSONObject makeRequestData() {
|
77 |
final JSONObject root = new JSONObject(); |
78 |
final JSONArray uuids = Helpers.listToJSONArray(ids);
|
79 |
final JSONArray displaynames = Helpers.listToJSONArray(names);
|
80 |
|
81 |
root.put(RequestField_uuids, uuids); |
82 |
root.put(RequestField_displaynames, displaynames); |
83 |
|
84 |
return root;
|
85 |
} |
86 |
|
87 |
public UserCatalogs parseResult(JSONObject result) {
|
88 |
final UserCatalogs userCatalogs = new UserCatalogs(); |
89 |
final JSONObject uuid_catalog = result.get(ResponseField_uuid_catalog).isObject();
|
90 |
final JSONObject displayname_catalog = result.get(ResponseField_displayname_catalog).isObject();
|
91 |
|
92 |
for(String uuid: uuid_catalog.keySet()) { |
93 |
final String name = uuid_catalog.get(uuid).isString().stringValue(); |
94 |
userCatalogs.updateWithIDAndName(uuid, name); |
95 |
} |
96 |
|
97 |
for(String name: displayname_catalog.keySet()) { |
98 |
final String uuid = displayname_catalog.get(name).isString().stringValue(); |
99 |
userCatalogs.updateWithIDAndName(uuid, name); |
100 |
} |
101 |
|
102 |
return userCatalogs;
|
103 |
} |
104 |
|
105 |
public void onSuccess(Request request, Response response, JSONObject result, UserCatalogs userCatalogs) { |
106 |
} |
107 |
|
108 |
public void onBadStatusCode(Request request, Response response) { |
109 |
} |
110 |
|
111 |
public void onError(Request request, Throwable t) { |
112 |
Pithos.LOG("GetUserCatalogs", t);
|
113 |
} |
114 |
|
115 |
@Override
|
116 |
public void execute() { |
117 |
final RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, getURL()); |
118 |
rb.setHeader(Const.X_AUTH_TOKEN, userToken); |
119 |
final String requestData = makeRequestData().toString(); |
120 |
Pithos.LOG("GetUserCatalogs => ", requestData);
|
121 |
Pithos.LOG("POST ", getURL());
|
122 |
|
123 |
try {
|
124 |
rb.sendRequest(requestData, new RequestCallback() {
|
125 |
@Override
|
126 |
public void onResponseReceived(Request request, Response response) { |
127 |
final int statusCode = response.getStatusCode(); |
128 |
|
129 |
if(statusCode != SuccessCode) {
|
130 |
Pithos.LOG("GetUserCatalogs <= [", statusCode, " ", response.getStatusText(), "]"); |
131 |
GetUserCatalogs.this.onBadStatusCode(request, response); |
132 |
return;
|
133 |
} |
134 |
|
135 |
final String responseText = response.getText(); |
136 |
final JSONValue jsonValue = JSONParser.parseStrict(responseText);
|
137 |
Pithos.LOG("GetUserCatalogs <= ", jsonValue.toString());
|
138 |
final JSONObject result = jsonValue.isObject();
|
139 |
|
140 |
if(result == null) { |
141 |
GetUserCatalogs.this.onError(request, new Exception("GetUserCatalogs: json result is not an object")); |
142 |
return;
|
143 |
} |
144 |
|
145 |
final UserCatalogs userCatalogs = parseResult(result);
|
146 |
|
147 |
GetUserCatalogs.this.onSuccess(request, response, result, userCatalogs); |
148 |
} |
149 |
|
150 |
@Override
|
151 |
public void onError(Request request, Throwable exception) { |
152 |
GetUserCatalogs.this.onError(request, exception); |
153 |
} |
154 |
}); |
155 |
} |
156 |
catch(Exception e) { |
157 |
Pithos.LOG("GetUserCatalogs", e);
|
158 |
} |
159 |
} |
160 |
|
161 |
public void scheduleDeferred() { |
162 |
Scheduler.get().scheduleDeferred(this);
|
163 |
} |
164 |
} |