use of org.codice.ddf.activities.ActivityEvent.ActivityStatus 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(...)");
}
use of org.codice.ddf.activities.ActivityEvent.ActivityStatus in project ddf by codice.
the class ActivityEventTest method testCreateActivityEvent.
@Test
public void testCreateActivityEvent() {
String id = "12345";
String sessionId = "09876";
Date timestamp = new Date();
String category = "Product Retrieval";
String title = "Download 12345";
String message = "Downloading a file.";
int progress = 55;
Map<String, String> operations = new HashMap<String, String>();
operations.put("cancel", "true");
String user = UUID.randomUUID().toString();
ActivityStatus type = ActivityStatus.RUNNING;
Long bytes = 1024000000L;
ActivityEvent event = new ActivityEvent(id, sessionId, timestamp, category, title, message, progress, operations, user, type, bytes);
// id
assertEquals(id, event.getActivityId());
assertEquals(id, event.get(ActivityEvent.ID_KEY));
// session id
assertEquals(sessionId, event.getSessionId());
assertEquals(sessionId, event.get(ActivityEvent.SESSION_ID_KEY));
// time stamp
assertEquals(timestamp, event.getTimestamp());
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(ISODateTimeFormat.dateTime().parseDateTime(event.get(ActivityEvent.TIMESTAMP_KEY).toString()).toDate().getTime());
assertEquals(timestamp, cal.getTime());
// category
assertEquals(category, event.getCategory());
assertEquals(category, event.get(ActivityEvent.CATEGORY_KEY));
// title
assertEquals(title, event.getTitle());
assertEquals(title, event.get(ActivityEvent.TITLE_KEY));
// message
assertEquals(message, event.getMessage());
assertEquals(message, event.get(ActivityEvent.MESSAGE_KEY));
// progress
assertEquals(progress, event.get(ActivityEvent.PROGRESS_KEY));
// operations
assertEquals(operations, event.get(ActivityEvent.OPERATIONS_KEY));
// user
assertEquals(user, event.getUserId());
assertEquals(user, event.get(ActivityEvent.USER_ID_KEY));
// type
assertEquals(type, ActivityStatus.valueOf(event.getActivityType()));
assertEquals(type, ActivityStatus.valueOf(event.get(ActivityEvent.STATUS_KEY).toString()));
// bytes
assertEquals(bytes, event.getBytesRead());
assertEquals(bytes, event.get(ActivityEvent.BYTES_READ_KEY));
}
use of org.codice.ddf.activities.ActivityEvent.ActivityStatus in project ddf by codice.
the class ActivityEventTest method testCreateActivityEventForGuestUser.
/**
* Guest user will have empty string as their user ID - verify can create
* an ActivityEvent with an empty string for the user ID.
*/
@Test
public void testCreateActivityEventForGuestUser() {
String id = "12345";
String sessionId = "09876";
Date timestamp = new Date();
String category = "Product Retrieval";
String title = "Download 12345";
String message = "Downloading a file.";
int progress = 55;
Map<String, String> operations = new HashMap<String, String>();
operations.put("cancel", "true");
String user = "";
ActivityStatus type = ActivityStatus.RUNNING;
Long bytes = 1024000000L;
ActivityEvent event = new ActivityEvent(id, sessionId, timestamp, category, title, message, progress, operations, user, type, bytes);
// id
assertEquals(id, event.getActivityId());
assertEquals(id, event.get(ActivityEvent.ID_KEY));
// session id
assertEquals(sessionId, event.getSessionId());
assertEquals(sessionId, event.get(ActivityEvent.SESSION_ID_KEY));
// time stamp
assertEquals(timestamp, event.getTimestamp());
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(ISODateTimeFormat.dateTime().parseDateTime(event.get(ActivityEvent.TIMESTAMP_KEY).toString()).toDate().getTime());
assertEquals(timestamp, cal.getTime());
// category
assertEquals(category, event.getCategory());
assertEquals(category, event.get(ActivityEvent.CATEGORY_KEY));
// title
assertEquals(title, event.getTitle());
assertEquals(title, event.get(ActivityEvent.TITLE_KEY));
// message
assertEquals(message, event.getMessage());
assertEquals(message, event.get(ActivityEvent.MESSAGE_KEY));
// progress
assertEquals(progress, event.get(ActivityEvent.PROGRESS_KEY));
// operations
assertEquals(operations, event.get(ActivityEvent.OPERATIONS_KEY));
// user
assertEquals(user, event.getUserId());
assertEquals(user, event.get(ActivityEvent.USER_ID_KEY));
// type
assertEquals(type, ActivityStatus.valueOf(event.getActivityType()));
assertEquals(type, ActivityStatus.valueOf(event.get(ActivityEvent.STATUS_KEY).toString()));
// bytes
assertEquals(bytes, event.getBytesRead());
assertEquals(bytes, event.get(ActivityEvent.BYTES_READ_KEY));
}
Aggregations