Search in sources :

Example 6 with Listener

use of org.cometd.annotation.Listener in project ddf by codice.

the class ActivityController method deletePersistentActivity.

@Listener("/service/action")
public void deletePersistentActivity(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 activitiesPreCast = dataAsMap.get("data");
        Object[] activities = activitiesPreCast instanceof List ? ((List) activitiesPreCast).toArray() : (Object[]) activitiesPreCast;
        for (Object activityObject : activities) {
            Map activity = (Map) activityObject;
            String id = (String) activity.get("id");
            String action = (String) activity.get("action");
            if (action != null) {
                if (REMOVE_ACTION.equals(action)) {
                    //You can have a blank id for guest
                    if (id != null) {
                        try {
                            this.persistentStore.delete(PersistentStore.ACTIVITY_TYPE, "id = '" + id + "'");
                        } catch (PersistenceException e) {
                            throw new IllegalArgumentException("Unable to delete activity with id = " + id);
                        }
                    } else {
                        throw new IllegalArgumentException("Message id is null");
                    }
                } else if (CANCEL_ACTION.equals(action)) {
                    if (null == userId) {
                        throw new IllegalArgumentException("User ID is null");
                    }
                    if (null == id) {
                        throw new IllegalArgumentException("Metadata ID is null");
                    }
                    Map<String, Object> jsonPropMap = new HashMap<>();
                    jsonPropMap.put(ActivityEvent.DOWNLOAD_ID_KEY, id);
                    Event event = new Event(ActivityEvent.EVENT_TOPIC_DOWNLOAD_CANCEL, jsonPropMap);
                    eventAdmin.postEvent(event);
                }
            } else {
                throw new IllegalArgumentException("Message action is null.");
            }
        }
    } else {
        throw new IllegalArgumentException("Server Message is null.");
    }
}
Also used : PersistenceException(org.codice.ddf.persistence.PersistenceException) ActivityEvent(org.codice.ddf.activities.ActivityEvent) Event(org.osgi.service.event.Event) ArrayList(java.util.ArrayList) List(java.util.List) 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 7 with Listener

use of org.cometd.annotation.Listener in project ddf by codice.

the class ActivityController method getPersistedActivities.

@Listener('/' + ActivityEvent.EVENT_TOPIC)
public void getPersistedActivities(final ServerSession remote, Message message) {
    Map<String, Object> data = message.getDataAsMap();
    if (MapUtils.isEmpty(data)) {
        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");
        }
        List<Map<String, Object>> activities = getActivitiesForUser(userId);
        if (CollectionUtils.isNotEmpty(activities)) {
            queuePersistedMessages(remote, activities, ACTIVITY_TOPIC_COMETD_BROADCAST);
        }
    }
}
Also used : 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)

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