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.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.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 CallEndPoint = "/user_catalogs";
61 public static final String RequestField_uuids = "uuids";
62 public static final String RequestField_displaynames = "displaynames";
63 public static final String ResponseField_displayname_catalog = "displayname_catalog";
64 public static final String ResponseField_uuid_catalog = "uuid_catalog";
66 public GetUserCatalogs(Pithos app) {
67 this(app, null, null);
70 public GetUserCatalogs(Pithos app, String userID) {
71 this(app, Helpers.toList(userID), null);
74 public GetUserCatalogs(Pithos app, List<String> ids) {
78 public GetUserCatalogs(Pithos app, List<String> ids, List<String> names) {
81 // FIXME: Probably use Window.Location.getHost()
82 // https://server.com/v1/ --> https://server.com
83 String path = app.getApiPath();
84 path = Helpers.stripTrailing(path, "/");
85 path = Helpers.upToIncludingLastPart(path, "/");
86 path = Helpers.stripTrailing(path, "/");
88 // https://server.com/user_catalogs
89 this.url = path + CallEndPoint;
91 this.ids = Helpers.safeList(ids);
92 this.names = Helpers.safeList(names);
93 this.userToken = app.getUserToken();
96 public String getURL() {
100 private JSONObject makeRequestData() {
101 final JSONObject root = new JSONObject();
102 final JSONArray uuids = Helpers.listToJSONArray(ids);
103 final JSONArray displaynames = Helpers.listToJSONArray(names);
105 root.put(RequestField_uuids, uuids);
106 root.put(RequestField_displaynames, displaynames);
111 public UserCatalogs parseResult(JSONObject result) {
112 final UserCatalogs userCatalogs = new UserCatalogs();
113 final JSONObject uuid_catalog = result.get(ResponseField_uuid_catalog).isObject();
114 final JSONObject displayname_catalog = result.get(ResponseField_displayname_catalog).isObject();
116 for(String uuid: uuid_catalog.keySet()) {
117 final String name = uuid_catalog.get(uuid).isString().stringValue();
118 userCatalogs.updateWithIDAndName(uuid, name);
121 for(String name: displayname_catalog.keySet()) {
122 final String uuid = displayname_catalog.get(name).isString().stringValue();
123 userCatalogs.updateWithIDAndName(uuid, name);
129 public void onSuccess(Request request, Response response, JSONObject result, UserCatalogs userCatalogs) {
132 public void onBadStatusCode(Request request, Response response) {
135 public void onError(Request request, Throwable t) {
136 GWT.log("GetUserCatalogs", t);
140 public void execute() {
141 final RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, getURL());
142 rb.setHeader("X-Auth-Token", userToken);
143 final String requestData = makeRequestData().toString();
146 rb.sendRequest(requestData, new RequestCallback() {
148 public void onResponseReceived(Request request, Response response) {
149 final int statusCode = response.getStatusCode();
151 if(statusCode != SuccessCode) {
152 GetUserCatalogs.this.onBadStatusCode(request, response);
156 final String responseText = response.getText();
157 final JSONValue jsonValue = JSONParser.parseStrict(responseText);
158 final JSONObject result = jsonValue.isObject();
161 GetUserCatalogs.this.onError(request, new Exception("GetUserCatalogs: json result is not an object"));
165 final UserCatalogs userCatalogs = parseResult(result);
167 GetUserCatalogs.this.onSuccess(request, response, result, userCatalogs);
171 public void onError(Request request, Throwable exception) {
172 GetUserCatalogs.this.onError(request, exception);
177 GWT.log("GetUserCatalogs", e);
181 public void scheduleDeferred() {
182 Scheduler.get().scheduleDeferred(this);