Search in sources :

Example 6 with ActivityEvent

use of org.codice.ddf.activities.ActivityEvent in project ddf by codice.

the class ActivityControllerTest method testHandleEventThrowsIllegalArgumentExceptionOnNullUserAndSessionId.

/**
     * Test method for {@link ActivityController#handleEvent(org.osgi.service.event.Event)}
     *
     * Verifies that {@code IllegalArgumentException} is thrown when
     * {@code Event}'s {@link ActivityEvent#USER_ID_KEY} and
     * {@link ActivityEvent#SESSION_ID_KEY} properties are null.
     */
@Test(expected = IllegalArgumentException.class)
public void testHandleEventThrowsIllegalArgumentExceptionOnNullUserAndSessionId() {
    testEventProperties.put(ActivityEvent.USER_ID_KEY, null);
    testEventProperties.put(ActivityEvent.SESSION_ID_KEY, null);
    activityController.handleEvent(new Event(ActivityEvent.EVENT_TOPIC, testEventProperties));
}
Also used : ActivityEvent(org.codice.ddf.activities.ActivityEvent) Event(org.osgi.service.event.Event) Test(org.junit.Test)

Example 7 with ActivityEvent

use of org.codice.ddf.activities.ActivityEvent 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 8 with ActivityEvent

use of org.codice.ddf.activities.ActivityEvent in project ddf by codice.

the class SendCommand method sendActivity.

private void sendActivity() throws Exception {
    String id = UUID.randomUUID().toString().replaceAll("-", "");
    String sessionId = "mockSessionId";
    Map<String, String> operations = new HashMap<>();
    operations.put("cancel", "true");
    ActivityEvent eventProperties = new ActivityEvent(id, sessionId, new Date(), "Activity category", "Activity title", "Activity message", UNKNOWN_PROGRESS, operations, userId, ActivityStatus.RUNNING, 100L);
    Event event = new Event(ActivityEvent.EVENT_TOPIC, eventProperties);
    // 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 : ActivityEvent(org.codice.ddf.activities.ActivityEvent) HashMap(java.util.HashMap) EventAdmin(org.osgi.service.event.EventAdmin) ActivityEvent(org.codice.ddf.activities.ActivityEvent) Event(org.osgi.service.event.Event) Date(java.util.Date) ServiceReference(org.osgi.framework.ServiceReference)

Example 9 with ActivityEvent

use of org.codice.ddf.activities.ActivityEvent in project ddf by codice.

the class ActivityControllerTest method testHandleEventThrowsIllegalArgumentExceptionOnNullMessage.

/**
     * Test method for {@link ActivityController#handleEvent(org.osgi.service.event.Event)}
     *
     * Verifies that {@code IllegalArgumentException} is thrown when
     * {@code Event}'s {@link ActivityEvent#MESSAGE_KEY} property is
     * null.
     */
@Test(expected = IllegalArgumentException.class)
public void testHandleEventThrowsIllegalArgumentExceptionOnNullMessage() {
    testEventProperties.put(ActivityEvent.MESSAGE_KEY, null);
    activityController.handleEvent(new Event(ActivityEvent.EVENT_TOPIC, testEventProperties));
}
Also used : ActivityEvent(org.codice.ddf.activities.ActivityEvent) Event(org.osgi.service.event.Event) Test(org.junit.Test)

Aggregations

ActivityEvent (org.codice.ddf.activities.ActivityEvent)9 Event (org.osgi.service.event.Event)9 Test (org.junit.Test)7 Date (java.util.Date)2 HashMap (java.util.HashMap)2 Action (ddf.action.Action)1 ActivityStatus (org.codice.ddf.activities.ActivityEvent.ActivityStatus)1 Notification (org.codice.ddf.notifications.Notification)1 ServiceReference (org.osgi.framework.ServiceReference)1 EventAdmin (org.osgi.service.event.EventAdmin)1