use of org.cometd.annotation.Listener 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.annotation.Listener in project ddf by codice.
the class SearchService method processQuery.
/**
* Service method called by Cometd when something arrives on the service channel
*
* @param remote
* - Client session
* @param message
* - JSON message
*/
@Listener("/service/query")
public void processQuery(final ServerSession remote, Message message) {
ServerMessage.Mutable reply = new ServerMessageImpl();
Map<String, Object> queryMessage = message.getDataAsMap();
if (queryMessage != null && queryMessage.containsKey(Search.ID)) {
bayeux.createChannelIfAbsent("/" + queryMessage.get(Search.ID), new ConfigurableServerChannel.Initializer() {
public void configureChannel(ConfigurableServerChannel channel) {
channel.setPersistent(true);
}
});
BayeuxContext context = bayeux.getContext();
Subject subject = null;
if (context != null) {
subject = (Subject) context.getRequestAttribute(SecurityConstants.SECURITY_SUBJECT);
}
// kick off the query
executeQuery(queryMessage, subject);
reply.put(Search.SUCCESSFUL, true);
remote.deliver(serverSession, reply);
} else {
reply.put(Search.SUCCESSFUL, false);
reply.put("status", "ERROR: unable to return results, no id in query request");
remote.deliver(serverSession, reply);
}
}
use of org.cometd.annotation.Listener in project ddf by codice.
the class NotificationController method getPersistedNotifications.
@Listener('/' + Notification.NOTIFICATION_TOPIC_ROOT)
public void getPersistedNotifications(final ServerSession remote, Message message) {
Subject subject = null;
try {
subject = SecurityUtils.getSubject();
} catch (Exception e) {
LOGGER.debug("Couldn't grab user subject from Shiro.", e);
}
String userId = getUserId(remote, subject);
if (null == userId) {
throw new IllegalArgumentException("User ID is null");
}
Map<String, Object> data = message.getDataAsMap();
if (MapUtils.isEmpty(data)) {
List<Map<String, Object>> notifications = getNotificationsForUser(userId);
if (CollectionUtils.isNotEmpty(notifications)) {
queuePersistedMessages(remote, notifications, "/" + Notification.NOTIFICATION_TOPIC_BROADCAST);
}
} else {
String id = UUID.randomUUID().toString().replaceAll("-", "");
String sessionId = remote.getId();
Notification notification = new Notification(id, sessionId, (String) data.get(Notification.NOTIFICATION_KEY_APPLICATION), (String) data.get(Notification.NOTIFICATION_KEY_TITLE), (String) data.get(Notification.NOTIFICATION_KEY_MESSAGE), (Long) data.get(Notification.NOTIFICATION_KEY_TIMESTAMP), userId);
Event event = new Event(Notification.NOTIFICATION_TOPIC_PUBLISH, notification);
eventAdmin.postEvent(event);
}
}
use of org.cometd.annotation.Listener in project ddf by codice.
the class NotificationController method deletePersistentNotification.
@Listener("/notification/action")
public void deletePersistentNotification(ServerSession serverSession, ServerMessage serverMessage) {
LOGGER.debug("\nServerSession: {}\nServerMessage: {}", serverSession, serverMessage);
if (null == serverSession) {
throw new IllegalArgumentException("ServerSession is null");
}
if (null == serverMessage) {
throw new IllegalArgumentException("ServerMessage is null");
}
Subject subject = null;
try {
subject = SecurityUtils.getSubject();
} catch (Exception e) {
LOGGER.debug("Couldn't grab user subject from Shiro.", e);
}
String userId = getUserId(serverSession, subject);
Map<String, Object> dataAsMap = serverMessage.getDataAsMap();
if (dataAsMap != null) {
Object[] notifications = (Object[]) dataAsMap.get("data");
for (Object notificationObject : notifications) {
Map notification = (Map) notificationObject;
String id = (String) notification.get("id");
String action = (String) notification.get("action");
if (action != null) {
if ("remove".equals(action)) {
//You can have a blank id for guest
if (id != null) {
try {
this.persistentStore.delete(PersistentStore.NOTIFICATION_TYPE, "id = '" + id + "'");
} catch (PersistenceException e) {
throw new IllegalArgumentException("Unable to delete notification with id = " + id);
}
} else {
throw new IllegalArgumentException("Message id is null");
}
}
} else {
throw new IllegalArgumentException("Message action is null.");
}
}
} else {
throw new IllegalArgumentException("Server Message is null.");
}
}
use of org.cometd.annotation.Listener 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