use of org.cometd.server.Jackson1JSONContextServer 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.server.Jackson1JSONContextServer 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