Search in sources :

Example 6 with PersistentItem

use of org.codice.ddf.persistence.PersistentItem 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)

Example 7 with PersistentItem

use of org.codice.ddf.persistence.PersistentItem in project ddf by codice.

the class AttributesStoreImpl method toPersistentItem.

private PersistentItem toPersistentItem(final String username, final long dataUsage, final long dataLimit) throws PersistenceException {
    // add to usage and store
    PersistentItem item = new PersistentItem();
    item.addIdProperty(username);
    item.addProperty(USER_KEY, username);
    item.addProperty(DATA_USAGE_KEY, dataUsage);
    item.addProperty(DATA_USAGE_LIMIT_KEY, dataLimit);
    LOGGER.debug("Created PersistentItem : User {} Usage {} Limit {}", username, dataUsage, dataLimit);
    return item;
}
Also used : PersistentItem(org.codice.ddf.persistence.PersistentItem)

Example 8 with PersistentItem

use of org.codice.ddf.persistence.PersistentItem in project ddf by codice.

the class ActivityListener method handleEvent.

@Override
public void handleEvent(Event event) throws IllegalArgumentException {
    LOGGER.debug("Received activity on topic {}", event.getTopic());
    String id = (String) event.getProperty(ActivityEvent.ID_KEY);
    String session = (String) event.getProperty(ActivityEvent.SESSION_ID_KEY);
    String status = (String) event.getProperty(ActivityEvent.STATUS_KEY);
    if (status.equals(ActivityEvent.ActivityStatus.RUNNING.toString())) {
        return;
    }
    String title = (String) event.getProperty(ActivityEvent.TITLE_KEY);
    String message = (String) event.getProperty(ActivityEvent.MESSAGE_KEY);
    String timestamp = (String) event.getProperty(ActivityEvent.TIMESTAMP_KEY);
    Map<String, String> operations = (Map<String, String>) event.getProperty(ActivityEvent.OPERATIONS_KEY);
    String progress = event.getProperty(ActivityEvent.PROGRESS_KEY).toString();
    String user = (String) event.getProperty(ActivityEvent.USER_ID_KEY);
    String category = (String) event.getProperty(ActivityEvent.CATEGORY_KEY);
    Long bytes = (Long) event.getProperty(ActivityEvent.BYTES_READ_KEY);
    String downloadId = (String) event.getProperty(ActivityEvent.DOWNLOAD_ID_KEY);
    PersistentItem activityToStore = new PersistentItem();
    activityToStore.addIdProperty(id);
    activityToStore.addProperty(ActivityEvent.SESSION_ID_KEY, session);
    activityToStore.addProperty(ActivityEvent.STATUS_KEY, status);
    activityToStore.addProperty(ActivityEvent.TITLE_KEY, title);
    activityToStore.addProperty(ActivityEvent.MESSAGE_KEY, message);
    activityToStore.addProperty(ActivityEvent.TIMESTAMP_KEY, timestamp);
    for (Map.Entry<String, String> entry : operations.entrySet()) {
        activityToStore.addProperty(ActivityEvent.OPERATIONS_KEY + "_" + entry.getKey(), entry.getValue());
    }
    activityToStore.addProperty(ActivityEvent.PROGRESS_KEY, progress);
    activityToStore.addProperty(ActivityEvent.USER_ID_KEY, user);
    activityToStore.addProperty(ActivityEvent.CATEGORY_KEY, category);
    activityToStore.addProperty(ActivityEvent.BYTES_READ_KEY, bytes);
    activityToStore.addProperty(ActivityEvent.DOWNLOAD_ID_KEY, downloadId);
    try {
        persistentStore.add(PersistentStore.ACTIVITY_TYPE, activityToStore);
    } catch (PersistenceException e) {
        LOGGER.info("Error while handling activity event", e);
    }
}
Also used : PersistentItem(org.codice.ddf.persistence.PersistentItem) PersistenceException(org.codice.ddf.persistence.PersistenceException) Map(java.util.Map)

Example 9 with PersistentItem

use of org.codice.ddf.persistence.PersistentItem in project ddf by codice.

the class NotificationListener method handleEvent.

@Override
public void handleEvent(Event event) throws IllegalArgumentException {
    LOGGER.debug("Received notification on topic {}", event.getTopic());
    String id = (String) event.getProperty(Notification.NOTIFICATION_KEY_ID);
    String application = (String) event.getProperty(Notification.NOTIFICATION_KEY_APPLICATION);
    String message = (String) event.getProperty(Notification.NOTIFICATION_KEY_MESSAGE);
    String timestamp = (String) event.getProperty(Notification.NOTIFICATION_KEY_TIMESTAMP);
    String title = (String) event.getProperty(Notification.NOTIFICATION_KEY_TITLE);
    String userId = (String) event.getProperty(Notification.NOTIFICATION_KEY_USER_ID);
    if (StringUtils.isEmpty(userId)) {
        throw new IllegalArgumentException("Event \"" + Notification.NOTIFICATION_KEY_USER_ID + "\" property is blank");
    }
    //TODO: Do we need to get extra properties out of event for Notification, i.e., STATUS and BYTES?
    PersistentItem item = new PersistentItem();
    item.addIdProperty(id);
    item.addProperty(Notification.NOTIFICATION_KEY_USER_ID, userId);
    item.addProperty(Notification.NOTIFICATION_KEY_TIMESTAMP, timestamp);
    item.addProperty(Notification.NOTIFICATION_KEY_APPLICATION, application);
    item.addProperty(Notification.NOTIFICATION_KEY_TITLE, title);
    item.addProperty(Notification.NOTIFICATION_KEY_MESSAGE, message);
    try {
        persistentStore.add(PersistentStore.NOTIFICATION_TYPE, item);
    } catch (PersistenceException e) {
    }
}
Also used : PersistentItem(org.codice.ddf.persistence.PersistentItem) PersistenceException(org.codice.ddf.persistence.PersistenceException)

Example 10 with PersistentItem

use of org.codice.ddf.persistence.PersistentItem in project ddf by codice.

the class DataUsage method setPersistentCronTime.

private void setPersistentCronTime() {
    PersistentItem persistentItem = new PersistentItem();
    persistentItem.addIdProperty(CRON_TIME_ID_KEY);
    persistentItem.addProperty(CRON_TIME_KEY, this.cronTime);
    try {
        readWriteLock.readLock().lock();
        persistentStore.add(PREFERENCES_TYPE, persistentItem);
    } catch (PersistenceException e) {
        LOGGER.error("Error adding Cron Time to Persistent Store.", e);
    } finally {
        readWriteLock.readLock().unlock();
    }
}
Also used : PersistentItem(org.codice.ddf.persistence.PersistentItem) PersistenceException(org.codice.ddf.persistence.PersistenceException)

Aggregations

PersistentItem (org.codice.ddf.persistence.PersistentItem)13 PersistenceException (org.codice.ddf.persistence.PersistenceException)9 Map (java.util.Map)6 PersistentStore (org.codice.ddf.persistence.PersistentStore)3 Subject (ddf.security.Subject)2 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 ParseException (java.text.ParseException)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Listener (org.cometd.annotation.Listener)2 ServerMessage (org.cometd.bayeux.server.ServerMessage)2 JSONContext (org.cometd.common.JSONContext)2 Jackson1JSONContextClient (org.cometd.common.Jackson1JSONContextClient)2 Jackson1JSONContextServer (org.cometd.server.Jackson1JSONContextServer)2 ServerMessageImpl (org.cometd.server.ServerMessageImpl)2 ImmutableMap (com.google.common.collect.ImmutableMap)1