use of org.cometd.common.JSONContext in project ddf by codice.
the class WorkspaceService method getWorkspaces.
@SuppressWarnings("unchecked")
@Listener("/service/workspaces")
public void getWorkspaces(final ServerSession remote, Message message) {
ServerMessage.Mutable reply = new ServerMessageImpl();
Map<String, Object> data = message.getDataAsMap();
Subject subject = (Subject) bayeux.getContext().getRequestAttribute(SecurityConstants.SECURITY_SUBJECT);
String username = SubjectUtils.getName(subject);
// No workspaces persisted for a guest user (whose username="")
if (StringUtils.isNotBlank(username)) {
if (data == null || data.isEmpty() || data.get("workspaces") == null) {
List<Map<String, Object>> workspacesList = new ArrayList<Map<String, Object>>();
try {
workspacesList = persistentStore.get(PersistentStore.WORKSPACE_TYPE, "user = '" + username + "'");
if (workspacesList.size() == 1) {
// Convert workspace's JSON representation back to nested maps of Map<String, Object>
Map<String, Object> workspaces = (Map<String, Object>) workspacesList.get(0);
JSONContext.Client jsonContext = new Jackson1JSONContextClient();
String json = (String) workspaces.get("workspaces_json_txt");
LOGGER.debug("workspaces extracted JSON text:\n {}", json);
Map<String, Object> workspacesMap;
try {
workspacesMap = jsonContext.getParser().parse(new StringReader(json), Map.class);
reply.putAll(workspacesMap);
} catch (ParseException e) {
LOGGER.info("ParseException while trying to convert persisted workspaces's for user {} from JSON", username, e);
}
}
} catch (PersistenceException e) {
LOGGER.info("PersistenceException while trying to retrieve persisted workspaces for user {}", username, e);
}
reply.put(Search.SUCCESSFUL, true);
remote.deliver(serverSession, "/service/workspaces", reply);
} else {
LOGGER.debug("Persisting workspaces for username = {}", username);
// Use JSON serializer so that only "data" component is serialized, not entire Message
JSONContext.Server jsonContext = new Jackson1JSONContextServer();
String json = jsonContext.getGenerator().generate(data);
LOGGER.debug("workspaces JSON text:\n {}", json);
PersistentItem item = new PersistentItem();
item.addIdProperty(username);
item.addProperty("user", username);
item.addProperty("workspaces_json", json);
try {
persistentStore.add(PersistentStore.WORKSPACE_TYPE, item);
} catch (PersistenceException e) {
LOGGER.info("PersistenceException while trying to persist workspaces for user {}", username, e);
}
reply.put(Search.SUCCESSFUL, true);
remote.deliver(serverSession, "/service/workspaces", reply);
}
}
}
use of org.cometd.common.JSONContext in project ddf by codice.
the class WorkspaceServiceTest method test.
@Test
public void test() throws Exception {
// JSONContext.Client jsonContext = (JSONContext.Client)getClass().getClassLoader().loadClass(JacksonJSONContextClient.class).newInstance();
// Data data1 = new Data("data");
// Extra extra1 = new Extra(42L);
// Map<String, Object> map1 = new HashMap<>();
// map1.put("data", data1);
// map1.put("extra", extra1);
// String json = jsonContext.getGenerator().generate(map1);
JSONContext.Client jsonContext = new Jackson1JSONContextClient();
String json = "{\"id\":\"16\",\"data\":{\"workspaces\":[{\"name\":\"admin_ws\",\"searches\":[{\"radiusUnits\":\"meters\",\"result\":{},\"startIndex\":1,\"count\":250,\"federation\":\"enterprise\",\"q\":\"admin_phrase\",\"name\":\"admin_search\",\"radiusValue\":0,\"radius\":0,\"offsetTimeUnits\":\"hours\",\"src\":\"ddf.distribution\",\"format\":\"geojson\",\"timeType\":\"modified\"}],\"metacards\":[]}],\"successful\":true},\"channel\":\"/service/workspaces\"}";
Map map2 = jsonContext.getParser().parse(new StringReader(json), Map.class);
int x = 1;
// Data data2 = (Data)map2.get("data");
// Extra extra2 = (Extra)map2.get("extra");
// Assert.assertEquals(data1.content, data2.content);
// Assert.assertEquals(extra1.content, extra2.content);
}
use of org.cometd.common.JSONContext in project ddf by codice.
the class UserService method getUser.
@Listener("/service/user")
public void getUser(final ServerSession remote, Message message) {
ServerMessage.Mutable reply = new ServerMessageImpl();
Map<String, Object> data = message.getDataAsMap();
Subject subject = (Subject) bayeux.getContext().getRequestAttribute(SecurityConstants.SECURITY_SUBJECT);
if (subject != null) {
if (data == null || data.isEmpty()) {
Map<String, Object> userMap = new HashMap<>();
String username = SubjectUtils.getName(subject);
userMap.put("username", username);
userMap.put("isGuest", String.valueOf(subject.isGuest()));
List<Map<String, Object>> preferencesList;
try {
preferencesList = persistentStore.get(PersistentStore.PREFERENCES_TYPE, "user = '" + username + "'");
if (preferencesList.size() == 1) {
Map<String, Object> preferences = preferencesList.get(0);
JSONContext.Client jsonContext = new Jackson1JSONContextClient();
String json = (String) preferences.get("preferences_json_txt");
LOGGER.debug("preferences extracted JSON text:\n {}", json);
Map preferencesMap;
try {
preferencesMap = jsonContext.getParser().parse(new StringReader(json), Map.class);
userMap.put("preferences", preferencesMap);
} catch (ParseException e) {
LOGGER.info("ParseException while trying to convert persisted preferences for user {} from JSON", username, e);
}
}
} catch (PersistenceException e) {
LOGGER.info("PersistenceException while trying to retrieve persisted preferences for user {}", username, e);
}
reply.put("user", userMap);
reply.put(Search.SUCCESSFUL, true);
remote.deliver(serverSession, "/service/user", reply);
} else {
JSONContext.Server jsonContext = new Jackson1JSONContextServer();
String json = jsonContext.getGenerator().generate(data);
LOGGER.debug("preferences JSON text:\n {}", json);
String username = SubjectUtils.getName(subject);
PersistentItem item = new PersistentItem();
item.addIdProperty(username);
item.addProperty("user", username);
item.addProperty("preferences_json", json);
try {
persistentStore.add(PersistentStore.PREFERENCES_TYPE, item);
} catch (PersistenceException e) {
LOGGER.info("PersistenceException while trying to persist preferences for user {}", username, e);
}
}
} else {
reply.put(Search.SUCCESSFUL, false);
remote.deliver(serverSession, "/service/user", reply);
}
}
Aggregations