Search in sources :

Example 1 with Listener

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);
        }
    }
}
Also used : PersistentItem(org.codice.ddf.persistence.PersistentItem) ServerMessage(org.cometd.bayeux.server.ServerMessage) ArrayList(java.util.ArrayList) Subject(ddf.security.Subject) JSONContext(org.cometd.common.JSONContext) ServerMessageImpl(org.cometd.server.ServerMessageImpl) StringReader(java.io.StringReader) PersistenceException(org.codice.ddf.persistence.PersistenceException) ParseException(java.text.ParseException) Jackson1JSONContextServer(org.cometd.server.Jackson1JSONContextServer) Jackson1JSONContextClient(org.cometd.common.Jackson1JSONContextClient) Map(java.util.Map) Listener(org.cometd.annotation.Listener)

Example 2 with Listener

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);
    }
}
Also used : ServerMessageImpl(org.cometd.server.ServerMessageImpl) ServerMessage(org.cometd.bayeux.server.ServerMessage) ConfigurableServerChannel(org.cometd.bayeux.server.ConfigurableServerChannel) BayeuxContext(org.cometd.bayeux.server.BayeuxContext) Subject(ddf.security.Subject) Listener(org.cometd.annotation.Listener)

Example 3 with Listener

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);
    }
}
Also used : Event(org.osgi.service.event.Event) HashMap(java.util.HashMap) Map(java.util.Map) Subject(org.apache.shiro.subject.Subject) PersistenceException(org.codice.ddf.persistence.PersistenceException) Notification(org.codice.ddf.notifications.Notification) Listener(org.cometd.annotation.Listener)

Example 4 with Listener

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.");
    }
}
Also used : PersistenceException(org.codice.ddf.persistence.PersistenceException) HashMap(java.util.HashMap) Map(java.util.Map) Subject(org.apache.shiro.subject.Subject) PersistenceException(org.codice.ddf.persistence.PersistenceException) Listener(org.cometd.annotation.Listener)

Example 5 with Listener

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);
    }
}
Also used : PersistentItem(org.codice.ddf.persistence.PersistentItem) HashMap(java.util.HashMap) ServerMessage(org.cometd.bayeux.server.ServerMessage) Subject(ddf.security.Subject) JSONContext(org.cometd.common.JSONContext) ServerMessageImpl(org.cometd.server.ServerMessageImpl) StringReader(java.io.StringReader) PersistenceException(org.codice.ddf.persistence.PersistenceException) ParseException(java.text.ParseException) Jackson1JSONContextServer(org.cometd.server.Jackson1JSONContextServer) Jackson1JSONContextClient(org.cometd.common.Jackson1JSONContextClient) HashMap(java.util.HashMap) Map(java.util.Map) Listener(org.cometd.annotation.Listener)

Aggregations

Listener (org.cometd.annotation.Listener)7 Map (java.util.Map)6 PersistenceException (org.codice.ddf.persistence.PersistenceException)6 HashMap (java.util.HashMap)5 Subject (org.apache.shiro.subject.Subject)4 Subject (ddf.security.Subject)3 ServerMessage (org.cometd.bayeux.server.ServerMessage)3 ServerMessageImpl (org.cometd.server.ServerMessageImpl)3 StringReader (java.io.StringReader)2 ParseException (java.text.ParseException)2 ArrayList (java.util.ArrayList)2 PersistentItem (org.codice.ddf.persistence.PersistentItem)2 JSONContext (org.cometd.common.JSONContext)2 Jackson1JSONContextClient (org.cometd.common.Jackson1JSONContextClient)2 Jackson1JSONContextServer (org.cometd.server.Jackson1JSONContextServer)2 Event (org.osgi.service.event.Event)2 List (java.util.List)1 ActivityEvent (org.codice.ddf.activities.ActivityEvent)1 Notification (org.codice.ddf.notifications.Notification)1 BayeuxContext (org.cometd.bayeux.server.BayeuxContext)1