Change copyright notice
[pithos-web-client] / src / gr / grnet / pithos / web / client / catalog / GetUserCatalogs.java
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.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 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";
65
66     public GetUserCatalogs(Pithos app) {
67         this(app, null, null);
68     }
69
70     public GetUserCatalogs(Pithos app, String userID) {
71         this(app, Helpers.toList(userID), null);
72     }
73
74     public GetUserCatalogs(Pithos app, List<String> ids) {
75         this(app, ids, null);
76     }
77
78     public GetUserCatalogs(Pithos app, List<String> ids, List<String> names) {
79         assert app != null;
80
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, "/");
87
88         // https://server.com/user_catalogs
89         this.url = path + CallEndPoint;
90
91         this.ids = Helpers.safeList(ids);
92         this.names = Helpers.safeList(names);
93         this.userToken = app.getUserToken();
94     }
95
96     public String getURL() {
97         return url;
98     }
99
100     private JSONObject makeRequestData() {
101         final JSONObject root = new JSONObject();
102         final JSONArray uuids = Helpers.listToJSONArray(ids);
103         final JSONArray displaynames = Helpers.listToJSONArray(names);
104
105         root.put(RequestField_uuids, uuids);
106         root.put(RequestField_displaynames, displaynames);
107
108         return root;
109     }
110
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();
115
116         for(String uuid: uuid_catalog.keySet()) {
117             final String name = uuid_catalog.get(uuid).isString().stringValue();
118             userCatalogs.updateWithIDAndName(uuid, name);
119         }
120
121         for(String name: displayname_catalog.keySet()) {
122             final String uuid = displayname_catalog.get(name).isString().stringValue();
123             userCatalogs.updateWithIDAndName(uuid, name);
124         }
125
126         return userCatalogs;
127     }
128
129     public void onSuccess(Request request, Response response, JSONObject result, UserCatalogs userCatalogs) {
130     }
131
132     public void onBadStatusCode(Request request, Response response) {
133     }
134
135     public void onError(Request request, Throwable t) {
136         GWT.log("GetUserCatalogs", t);
137     }
138
139     @Override
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();
144
145         try {
146             rb.sendRequest(requestData, new RequestCallback() {
147                 @Override
148                 public void onResponseReceived(Request request, Response response) {
149                     final int statusCode = response.getStatusCode();
150
151                     if(statusCode != SuccessCode) {
152                         GetUserCatalogs.this.onBadStatusCode(request, response);
153                         return;
154                     }
155
156                     final String responseText = response.getText();
157                     final JSONValue jsonValue = JSONParser.parseStrict(responseText);
158                     final JSONObject result = jsonValue.isObject();
159
160                     if(result == null) {
161                         GetUserCatalogs.this.onError(request, new Exception("GetUserCatalogs: json result is not an object"));
162                         return;
163                     }
164
165                     final UserCatalogs userCatalogs = parseResult(result);
166
167                     GetUserCatalogs.this.onSuccess(request, response, result, userCatalogs);
168                 }
169
170                 @Override
171                 public void onError(Request request, Throwable exception) {
172                     GetUserCatalogs.this.onError(request, exception);
173                 }
174             });
175         }
176         catch(Exception e) {
177             GWT.log("GetUserCatalogs", e);
178         }
179     }
180
181     public void scheduleDeferred() {
182         Scheduler.get().scheduleDeferred(this);
183     }
184 }