Search in sources :

Example 1 with Notification

use of org.codice.ddf.notifications.Notification 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 2 with Notification

use of org.codice.ddf.notifications.Notification in project ddf by codice.

the class DownloadsStatusEventPublisher method postRetrievalStatus.

/**
     * Based on the input parameters send notification and/or activity with current retrieval status.
     *
     * @param resourceResponse   the response from the product retrieval containing the actual @Resource
     * @param status             the status of the product retrieval, e.g., IN_PROGRESS, STARTED, etc.
     * @param metacard           the @Metacard associated with the product being downloaded
     * @param detail             detailed message to be displayed in the notification and/or activity
     * @param bytes              the number of bytes read thus far during the product download
     * @param downloadIdentifier unique ID for this product download
     * @param sendNotification   true indicates a notification with current retrieval status is to be sent
     * @param sendActivity       true indicates an activity with current retrieval status is to be sent
     */
public void postRetrievalStatus(final ResourceResponse resourceResponse, ProductRetrievalStatus status, Metacard metacard, String detail, Long bytes, String downloadIdentifier, boolean sendNotification, boolean sendActivity) {
    LOGGER.debug("ENTERING: postRetrievalStatus(...)");
    LOGGER.debug("sendNotification = {},   sendActivity = {}", sendNotification, sendActivity);
    LOGGER.debug("status: {}", status);
    LOGGER.debug("detail: {}", detail);
    LOGGER.debug("bytes: {}", bytes);
    if (bytes == null) {
        bytes = 0L;
    }
    Long sysTimeMillis = System.currentTimeMillis();
    // Add user information to the request properties.
    org.apache.shiro.subject.Subject shiroSubject = null;
    try {
        shiroSubject = SecurityUtils.getSubject();
    } catch (Exception e) {
        LOGGER.debug("Could not determine current user, using session id.");
    }
    String user = SubjectUtils.getName(shiroSubject, "");
    LOGGER.debug("User: {}, session: {}", user, getProperty(resourceResponse, ActivityEvent.SESSION_ID_KEY));
    if (notificationEnabled && sendNotification && (status != ProductRetrievalStatus.IN_PROGRESS) && (status != ProductRetrievalStatus.STARTED)) {
        String id = UUID.randomUUID().toString().replaceAll("-", "");
        Notification notification = new Notification(id, //get sessionId from resource request
        getProperty(resourceResponse, ActivityEvent.SESSION_ID_KEY), APPLICATION_NAME, resourceResponse.getResource().getName(), generateMessage(status, resourceResponse.getResource().getName(), bytes, sysTimeMillis, detail), sysTimeMillis, user);
        notification.put(STATUS, status.name().toLowerCase());
        notification.put(BYTES, String.valueOf(bytes));
        Event event = new Event(Notification.NOTIFICATION_TOPIC_DOWNLOADS, notification);
        eventAdmin.postEvent(event);
    } else {
        LOGGER.debug("Notifications have been disabled so this message will NOT be posted.");
    }
    if (activityEnabled && sendActivity) {
        // get Action
        Action downloadAction = null;
        if (null != actionProviders && !actionProviders.isEmpty()) {
            // take the first one
            downloadAction = actionProviders.get(0).getAction(metacard);
        }
        // send activity event
        // progress for downloads
        int progress = UNKNOWN_PROGRESS;
        Map<String, String> operations = new HashMap<String, String>();
        ActivityStatus type;
        switch(status) {
            case STARTED:
                type = ActivityStatus.STARTED;
                if (downloadAction != null) {
                    operations = ImmutableMap.of("cancel", "true");
                    progress = UNKNOWN_PROGRESS;
                }
                break;
            case COMPLETE:
                type = ActivityStatus.COMPLETE;
                progress = ONE_HUNDRED_PERCENT;
                if (downloadAction != null) {
                    operations = ImmutableMap.of("download", downloadAction.getUrl().toString());
                }
                break;
            case FAILED:
                type = ActivityStatus.FAILED;
                break;
            case CANCELLED:
                type = ActivityStatus.STOPPED;
                break;
            default:
                type = ActivityStatus.RUNNING;
                if (downloadAction != null) {
                    operations = ImmutableMap.of("cancel", "true");
                }
                progress = UNKNOWN_PROGRESS;
                if (metacard != null) {
                    String resourceSizeStr = metacard.getResourceSize();
                    if (org.apache.commons.lang.math.NumberUtils.isNumber(resourceSizeStr)) {
                        Long resourceSize = Long.parseLong(resourceSizeStr);
                        if (resourceSize > 0) {
                            progress = (int) (bytes * ONE_HUNDRED_PERCENT / resourceSize);
                        }
                    }
                }
                break;
        }
        ActivityEvent eventProperties = new ActivityEvent(downloadIdentifier, getProperty(resourceResponse, ActivityEvent.SESSION_ID_KEY), new Date(), "Product Retrieval", resourceResponse.getResource().getName(), generateMessage(status, resourceResponse.getResource().getName(), bytes, sysTimeMillis, detail), progress, operations, user, type, bytes);
        eventProperties.put(ActivityEvent.DOWNLOAD_ID_KEY, user + downloadIdentifier);
        Event event = new Event(ActivityEvent.EVENT_TOPIC, eventProperties);
        eventAdmin.postEvent(event);
    } else {
        LOGGER.debug("Activities have been disabled so this message will NOT be posted.");
    }
    LOGGER.debug("EXITING: postRetrievalStatus(...)");
}
Also used : ActivityStatus(org.codice.ddf.activities.ActivityEvent.ActivityStatus) Action(ddf.action.Action) ActivityEvent(org.codice.ddf.activities.ActivityEvent) HashMap(java.util.HashMap) Notification(org.codice.ddf.notifications.Notification) Date(java.util.Date) ActivityEvent(org.codice.ddf.activities.ActivityEvent) Event(org.osgi.service.event.Event)

Example 3 with Notification

use of org.codice.ddf.notifications.Notification in project ddf by codice.

the class SendCommand method sendNotification.

private void sendNotification() throws Exception {
    Long sysTimeMillis = System.currentTimeMillis();
    String id = UUID.randomUUID().toString().replaceAll("-", "");
    String sessionId = "mockSessionId";
    Notification notification = new Notification(id, sessionId, application, title, message, sysTimeMillis, userId);
    notification.put("status", "Started");
    notification.put("bytes", "12345");
    Event event = new Event(Notification.NOTIFICATION_TOPIC_DOWNLOADS, notification);
    // Get OSGi Event Admin service
    EventAdmin eventAdmin;
    @SuppressWarnings("rawtypes") ServiceReference[] serviceReferences = bundleContext.getServiceReferences(SERVICE_PID, null);
    if (serviceReferences == null || serviceReferences.length != 1) {
        LOGGER.debug("Found no service references for {}", SERVICE_PID);
    } else {
        LOGGER.debug("Found " + serviceReferences.length + " service references for " + SERVICE_PID);
        eventAdmin = (EventAdmin) bundleContext.getService(serviceReferences[0]);
        if (eventAdmin != null) {
            eventAdmin.postEvent(event);
        }
    }
}
Also used : EventAdmin(org.osgi.service.event.EventAdmin) Event(org.osgi.service.event.Event) Notification(org.codice.ddf.notifications.Notification) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

Notification (org.codice.ddf.notifications.Notification)3 Event (org.osgi.service.event.Event)3 HashMap (java.util.HashMap)2 Action (ddf.action.Action)1 Date (java.util.Date)1 Map (java.util.Map)1 Subject (org.apache.shiro.subject.Subject)1 ActivityEvent (org.codice.ddf.activities.ActivityEvent)1 ActivityStatus (org.codice.ddf.activities.ActivityEvent.ActivityStatus)1 PersistenceException (org.codice.ddf.persistence.PersistenceException)1 Listener (org.cometd.annotation.Listener)1 ServiceReference (org.osgi.framework.ServiceReference)1 EventAdmin (org.osgi.service.event.EventAdmin)1