2 * Copyright 2011-2013 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
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.
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.
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.
36 package gr.grnet.pithos.web.client.catalog;
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;
48 import java.util.List;
51 * The request via which we obtain user catalog info.
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;
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";
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);
72 public String getURL() {
76 private JSONObject makeRequestData() {
77 final JSONObject root = new JSONObject();
78 final JSONArray uuids = Helpers.listToJSONArray(ids);
79 final JSONArray displaynames = Helpers.listToJSONArray(names);
81 root.put(RequestField_uuids, uuids);
82 root.put(RequestField_displaynames, displaynames);
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();
92 for(String uuid: uuid_catalog.keySet()) {
93 final String name = uuid_catalog.get(uuid).isString().stringValue();
94 userCatalogs.updateWithIDAndName(uuid, name);
97 for(String name: displayname_catalog.keySet()) {
98 final String uuid = displayname_catalog.get(name).isString().stringValue();
99 userCatalogs.updateWithIDAndName(uuid, name);
105 public void onSuccess(Request request, Response response, JSONObject result, UserCatalogs userCatalogs) {
108 public void onBadStatusCode(Request request, Response response) {
111 public void onError(Request request, Throwable t) {
112 Pithos.LOG("GetUserCatalogs", t);
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());
124 rb.sendRequest(requestData, new RequestCallback() {
126 public void onResponseReceived(Request request, Response response) {
127 final int statusCode = response.getStatusCode();
129 if(statusCode != SuccessCode) {
130 Pithos.LOG("GetUserCatalogs <= [", statusCode, " ", response.getStatusText(), "]");
131 GetUserCatalogs.this.onBadStatusCode(request, response);
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();
141 GetUserCatalogs.this.onError(request, new Exception("GetUserCatalogs: json result is not an object"));
145 final UserCatalogs userCatalogs = parseResult(result);
147 GetUserCatalogs.this.onSuccess(request, response, result, userCatalogs);
151 public void onError(Request request, Throwable exception) {
152 GetUserCatalogs.this.onError(request, exception);
157 Pithos.LOG("GetUserCatalogs", e);
161 public void scheduleDeferred() {
162 Scheduler.get().scheduleDeferred(this);