Simon Hunt | a29c87b | 2015-05-21 09:56:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | package org.onosproject.cord.gui; |
| 19 | |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.JsonNode; |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 21 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 22 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 23 | import com.google.common.collect.ImmutableList; |
| 24 | import org.onosproject.cord.gui.model.Bundle; |
| 25 | import org.onosproject.cord.gui.model.BundleDescriptor; |
| 26 | import org.onosproject.cord.gui.model.BundleFactory; |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 27 | import org.onosproject.cord.gui.model.JsonFactory; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 28 | import org.onosproject.cord.gui.model.SubscriberUser; |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 29 | import org.onosproject.cord.gui.model.UserFactory; |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 30 | import org.onosproject.cord.gui.model.XosFunction; |
| 31 | import org.onosproject.cord.gui.model.XosFunctionDescriptor; |
Simon Hunt | b124641 | 2015-06-01 13:37:26 -0700 | [diff] [blame] | 32 | import org.slf4j.Logger; |
| 33 | import org.slf4j.LoggerFactory; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 34 | |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 35 | import java.util.HashMap; |
| 36 | import java.util.Iterator; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 37 | import java.util.List; |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 38 | import java.util.Map; |
| 39 | import java.util.TreeMap; |
| 40 | |
| 41 | import static com.google.common.base.Preconditions.checkNotNull; |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 42 | import static org.onosproject.cord.gui.model.XosFunctionDescriptor.URL_FILTER; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 43 | |
Simon Hunt | a29c87b | 2015-05-21 09:56:19 -0700 | [diff] [blame] | 44 | /** |
| 45 | * In memory cache of the model of the subscriber's account. |
| 46 | */ |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 47 | public class CordModelCache extends JsonFactory { |
| 48 | |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 49 | private static final String KEY_SSID_MAP = "ssidmap"; |
Simon Hunt | 2739e6f8 | 2015-06-05 16:27:45 -0700 | [diff] [blame] | 50 | private static final String KEY_SSID = "service_specific_id"; |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 51 | private static final String KEY_SUB_ID = "subscriber_id"; |
| 52 | |
| 53 | private static final int DEMO_SSID = 1234; |
| 54 | |
| 55 | private static final String EMAIL_0 = "john@smith.org"; |
| 56 | private static final String EMAIL_1 = "john@doe.org"; |
| 57 | |
| 58 | private static final String EMAIL = "email"; |
| 59 | private static final String SSID = "ssid"; |
| 60 | private static final String SUB_ID = "subId"; |
| 61 | |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 62 | private static final String BUNDLE = "bundle"; |
| 63 | private static final String USERS = "users"; |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 64 | private static final String LEVEL = "level"; |
Simon Hunt | c296d2c | 2015-06-08 12:54:57 -0700 | [diff] [blame] | 65 | private static final String LOGOUT = "logout"; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 66 | |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 67 | private static final Map<Integer, Integer> LOOKUP = new HashMap<>(); |
| 68 | |
Simon Hunt | c296d2c | 2015-06-08 12:54:57 -0700 | [diff] [blame] | 69 | private String email = null; |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 70 | private int subscriberId; |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 71 | private int ssid; |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 72 | private Bundle currentBundle; |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 73 | |
Simon Hunt | b124641 | 2015-06-01 13:37:26 -0700 | [diff] [blame] | 74 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 75 | |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 76 | // NOTE: use a tree map to maintain sorted order by user ID |
| 77 | private final Map<Integer, SubscriberUser> userMap = |
| 78 | new TreeMap<Integer, SubscriberUser>(); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 79 | |
| 80 | /** |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 81 | * Constructs a model cache, retrieving a mapping of SSID to XOS Subscriber |
| 82 | * IDs from the XOS server. |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 83 | */ |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 84 | CordModelCache() { |
Simon Hunt | b124641 | 2015-06-01 13:37:26 -0700 | [diff] [blame] | 85 | log.info("Initialize model cache"); |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 86 | ObjectNode map = XosManager.INSTANCE.initXosSubscriberLookups(); |
| 87 | initLookupMap(map); |
| 88 | log.info("{} entries in SSID->SubID lookup map", LOOKUP.size()); |
| 89 | } |
| 90 | |
| 91 | private void initLookupMap(ObjectNode map) { |
| 92 | ArrayNode array = (ArrayNode) map.get(KEY_SSID_MAP); |
| 93 | Iterator<JsonNode> iter = array.elements(); |
Simon Hunt | 2739e6f8 | 2015-06-05 16:27:45 -0700 | [diff] [blame] | 94 | StringBuilder msg = new StringBuilder(); |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 95 | while (iter.hasNext()) { |
| 96 | ObjectNode node = (ObjectNode) iter.next(); |
Simon Hunt | 4236608 | 2015-06-08 09:57:05 -0700 | [diff] [blame] | 97 | String ssidStr = node.get(KEY_SSID).asText(); |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 98 | int ssid = Integer.valueOf(ssidStr); |
| 99 | int subId = node.get(KEY_SUB_ID).asInt(); |
| 100 | LOOKUP.put(ssid, subId); |
Simon Hunt | 2739e6f8 | 2015-06-05 16:27:45 -0700 | [diff] [blame] | 101 | msg.append(String.format("\n..binding SSID %s to sub-id %s", ssid, subId)); |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 102 | } |
Simon Hunt | 2739e6f8 | 2015-06-05 16:27:45 -0700 | [diff] [blame] | 103 | log.info(msg.toString()); |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | private int lookupSubId(int ssid) { |
| 107 | Integer subId = LOOKUP.get(ssid); |
| 108 | if (subId == null) { |
| 109 | log.error("Unmapped SSID: {}", ssid); |
| 110 | return 0; |
| 111 | } |
| 112 | return subId; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Initializes the model for the subscriber account associated with |
| 117 | * the given email address. |
| 118 | * |
| 119 | * @param email the email address |
| 120 | */ |
| 121 | void init(String email) { |
| 122 | // defaults to the demo account |
| 123 | int ssid = DEMO_SSID; |
| 124 | |
Simon Hunt | c296d2c | 2015-06-08 12:54:57 -0700 | [diff] [blame] | 125 | this.email = email; |
| 126 | |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 127 | // obviously not scalable, but good enough for demo code... |
| 128 | if (EMAIL_0.equals(email)) { |
| 129 | ssid = 0; |
| 130 | } else if (EMAIL_1.equals(email)) { |
| 131 | ssid = 1; |
| 132 | } |
| 133 | |
| 134 | this.ssid = ssid; |
| 135 | subscriberId = lookupSubId(ssid); |
| 136 | XosManager.INSTANCE.setXosUtilsForSubscriber(subscriberId); |
| 137 | |
| 138 | // if we are using the demo account, tell XOS to reset it... |
| 139 | if (ssid == DEMO_SSID) { |
| 140 | XosManager.INSTANCE.initDemoSubscriber(); |
| 141 | } |
| 142 | |
| 143 | // NOTE: I think the following should work for non-DEMO account... |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 144 | currentBundle = new Bundle(BundleFactory.BASIC_BUNDLE); |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 145 | initUsers(); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 148 | private void initUsers() { |
Simon Hunt | c296d2c | 2015-06-08 12:54:57 -0700 | [diff] [blame] | 149 | // start with a clean slate |
| 150 | userMap.clear(); |
| 151 | |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 152 | ArrayNode users = XosManager.INSTANCE.getUserList(); |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 153 | if (users == null) { |
| 154 | log.warn("no user list for SSID {} (subid {})", ssid, subscriberId); |
| 155 | return; |
| 156 | } |
| 157 | |
Simon Hunt | c296d2c | 2015-06-08 12:54:57 -0700 | [diff] [blame] | 158 | StringBuilder sb = new StringBuilder(); |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 159 | for (JsonNode u: users) { |
| 160 | ObjectNode user = (ObjectNode) u; |
| 161 | |
| 162 | int id = user.get("id").asInt(); |
| 163 | String name = user.get("name").asText(); |
| 164 | String mac = user.get("mac").asText(); |
| 165 | String level = user.get("level").asText(); |
| 166 | |
| 167 | // NOTE: We are just storing the current "url-filter" level. |
| 168 | // Since we are starting with the BASIC bundle, (that does |
| 169 | // not include URL_FILTER), we don't yet have the URL_FILTER |
| 170 | // memento in which to store the level. |
| 171 | SubscriberUser su = createUser(id, name, mac, level); |
| 172 | userMap.put(id, su); |
Simon Hunt | c296d2c | 2015-06-08 12:54:57 -0700 | [diff] [blame] | 173 | sb.append(String.format("\n..cache user %s [%d], %s, %s", |
| 174 | name, id, mac, level)); |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 175 | } |
Simon Hunt | c296d2c | 2015-06-08 12:54:57 -0700 | [diff] [blame] | 176 | log.info(sb.toString()); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 179 | private SubscriberUser createUser(int uid, String name, String mac, |
| 180 | String level) { |
| 181 | SubscriberUser user = new SubscriberUser(uid, name, mac, level); |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 182 | for (XosFunction f: currentBundle.functions()) { |
| 183 | user.setMemento(f.descriptor(), f.createMemento()); |
| 184 | } |
| 185 | return user; |
| 186 | } |
| 187 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 188 | /** |
| 189 | * Returns the currently selected bundle. |
| 190 | * |
| 191 | * @return current bundle |
| 192 | */ |
| 193 | public Bundle getCurrentBundle() { |
| 194 | return currentBundle; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Sets a new bundle. |
| 199 | * |
| 200 | * @param bundleId bundle identifier |
| 201 | * @throws IllegalArgumentException if bundle ID is unknown |
| 202 | */ |
| 203 | public void setCurrentBundle(String bundleId) { |
Simon Hunt | b124641 | 2015-06-01 13:37:26 -0700 | [diff] [blame] | 204 | log.info("set new bundle : {}", bundleId); |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 205 | BundleDescriptor bd = BundleFactory.bundleFromId(bundleId); |
| 206 | currentBundle = new Bundle(bd); |
| 207 | // update the user mementos |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 208 | for (SubscriberUser user: userMap.values()) { |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 209 | user.clearMementos(); |
| 210 | for (XosFunction f: currentBundle.functions()) { |
| 211 | user.setMemento(f.descriptor(), f.createMemento()); |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 212 | if (f.descriptor().equals(URL_FILTER)) { |
| 213 | applyUrlFilterLevel(user, user.urlFilterLevel()); |
| 214 | } |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 218 | XosManager.INSTANCE.setNewBundle(currentBundle); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 221 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 222 | /** |
| 223 | * Returns the list of current users for this subscriber account. |
| 224 | * |
| 225 | * @return the list of users |
| 226 | */ |
| 227 | public List<SubscriberUser> getUsers() { |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 228 | return ImmutableList.copyOf(userMap.values()); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 229 | } |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 230 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 231 | /** |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 232 | * Applies a function parameter change for a user, pushing that |
| 233 | * change through to XOS. |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 234 | * |
| 235 | * @param userId user identifier |
| 236 | * @param funcId function identifier |
| 237 | * @param param function parameter to change |
| 238 | * @param value new value for function parameter |
| 239 | */ |
| 240 | public void applyPerUserParam(String userId, String funcId, |
| 241 | String param, String value) { |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 242 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 243 | int uid = Integer.parseInt(userId); |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 244 | SubscriberUser user = userMap.get(uid); |
| 245 | checkNotNull(user, "unknown user id: " + uid); |
| 246 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 247 | XosFunctionDescriptor xfd = |
| 248 | XosFunctionDescriptor.valueOf(funcId.toUpperCase()); |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 249 | |
| 250 | XosFunction func = currentBundle.findFunction(xfd); |
| 251 | checkNotNull(func, "function not part of bundle: " + funcId); |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 252 | applyParam(func, user, param, value, true); |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | // ============= |
| 256 | |
Simon Hunt | 7d02c08 | 2015-05-29 12:17:09 -0700 | [diff] [blame] | 257 | private void applyUrlFilterLevel(SubscriberUser user, String level) { |
| 258 | XosFunction urlFilter = currentBundle.findFunction(URL_FILTER); |
| 259 | if (urlFilter != null) { |
| 260 | applyParam(urlFilter, user, LEVEL, level, false); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | private void applyParam(XosFunction func, SubscriberUser user, |
| 265 | String param, String value, boolean punchThrough) { |
| 266 | func.applyParam(user, param, value); |
| 267 | if (punchThrough) { |
| 268 | XosManager.INSTANCE.apply(func, user); |
| 269 | } |
| 270 | } |
| 271 | |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 272 | private ArrayNode userJsonArray() { |
| 273 | ArrayNode userList = arrayNode(); |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 274 | for (SubscriberUser user: userMap.values()) { |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 275 | userList.add(UserFactory.toObjectNode(user)); |
| 276 | } |
| 277 | return userList; |
| 278 | } |
| 279 | |
| 280 | // ============= generate JSON for GUI rest calls.. |
| 281 | |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 282 | private void addSubId(ObjectNode root) { |
| 283 | root.put(SUB_ID, subscriberId); |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 284 | root.put(SSID, ssid); |
Simon Hunt | c296d2c | 2015-06-08 12:54:57 -0700 | [diff] [blame] | 285 | root.put(EMAIL, email); |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | |
| 289 | /** |
| 290 | * Returns response JSON for login request. |
| 291 | * <p> |
| 292 | * Depending on which email is used, will bind the GUI to the |
| 293 | * appropriate XOS Subscriber ID. |
| 294 | * |
| 295 | * @param email the supplied email |
| 296 | * @return JSON acknowledgement |
| 297 | */ |
Simon Hunt | 6f0afae | 2015-06-08 21:40:41 -0700 | [diff] [blame] | 298 | public synchronized String jsonLogin(String email) { |
Simon Hunt | c296d2c | 2015-06-08 12:54:57 -0700 | [diff] [blame] | 299 | log.info("jsonLogin(\"{}\")", email); |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 300 | init(email); |
| 301 | ObjectNode root = objectNode(); |
Simon Hunt | c686c6a | 2015-06-05 14:33:30 -0700 | [diff] [blame] | 302 | addSubId(root); |
| 303 | return root.toString(); |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 306 | /** |
| 307 | * Returns the dashboard page data as JSON. |
| 308 | * |
| 309 | * @return dashboard page JSON data |
| 310 | */ |
Simon Hunt | 6f0afae | 2015-06-08 21:40:41 -0700 | [diff] [blame] | 311 | public synchronized String jsonDashboard() { |
Simon Hunt | c296d2c | 2015-06-08 12:54:57 -0700 | [diff] [blame] | 312 | log.info("jsonDashboard()"); |
| 313 | |
| 314 | if (email == null) { |
| 315 | return jsonLogout(); |
| 316 | } |
| 317 | |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 318 | ObjectNode root = objectNode(); |
| 319 | root.put(BUNDLE, currentBundle.descriptor().displayName()); |
| 320 | root.set(USERS, userJsonArray()); |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 321 | addSubId(root); |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 322 | return root.toString(); |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Returns the bundle page data as JSON. |
| 327 | * |
| 328 | * @return bundle page JSON data |
| 329 | */ |
Simon Hunt | 6f0afae | 2015-06-08 21:40:41 -0700 | [diff] [blame] | 330 | public synchronized String jsonBundle() { |
Simon Hunt | c296d2c | 2015-06-08 12:54:57 -0700 | [diff] [blame] | 331 | log.info("jsonBundle()"); |
| 332 | |
| 333 | if (email == null) { |
| 334 | return jsonLogout(); |
| 335 | } |
| 336 | |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 337 | ObjectNode root = BundleFactory.toObjectNode(currentBundle); |
| 338 | addSubId(root); |
| 339 | return root.toString(); |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Returns the users page data as JSON. |
| 344 | * |
| 345 | * @return users page JSON data |
| 346 | */ |
Simon Hunt | 6f0afae | 2015-06-08 21:40:41 -0700 | [diff] [blame] | 347 | public synchronized String jsonUsers() { |
Simon Hunt | c296d2c | 2015-06-08 12:54:57 -0700 | [diff] [blame] | 348 | log.info("jsonUsers()"); |
| 349 | |
| 350 | if (email == null) { |
| 351 | return jsonLogout(); |
| 352 | } |
| 353 | |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 354 | ObjectNode root = objectNode(); |
| 355 | root.set(USERS, userJsonArray()); |
Simon Hunt | ee6a737 | 2015-05-28 14:04:24 -0700 | [diff] [blame] | 356 | addSubId(root); |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 357 | return root.toString(); |
| 358 | } |
| 359 | |
| 360 | /** |
Simon Hunt | c296d2c | 2015-06-08 12:54:57 -0700 | [diff] [blame] | 361 | * Returns logout acknowledgement as JSON. |
| 362 | * |
| 363 | * @return logout acknowledgement |
| 364 | */ |
Simon Hunt | 6f0afae | 2015-06-08 21:40:41 -0700 | [diff] [blame] | 365 | public synchronized String jsonLogout() { |
Simon Hunt | c296d2c | 2015-06-08 12:54:57 -0700 | [diff] [blame] | 366 | log.info("jsonLogout()"); |
| 367 | ObjectNode root = objectNode().put(LOGOUT, true); |
| 368 | addSubId(root); |
| 369 | |
| 370 | email = null; // signifies no one logged in |
| 371 | |
| 372 | return root.toString(); |
| 373 | } |
| 374 | |
| 375 | /** |
Simon Hunt | 09a32db | 2015-05-21 15:00:42 -0700 | [diff] [blame] | 376 | * Singleton instance. |
| 377 | */ |
| 378 | public static final CordModelCache INSTANCE = new CordModelCache(); |
Simon Hunt | a29c87b | 2015-05-21 09:56:19 -0700 | [diff] [blame] | 379 | } |