use of org.osgi.service.event.Event in project ddf by codice.
the class PredicateTest method testMultipleContentTypes.
@Test
public void testMultipleContentTypes() throws IOException {
String methodName = "testMultipleContentTypes";
LOGGER.debug("*************** START: {} *****************", methodName);
String type1 = "type1";
String version1 = "version1";
String type2 = "type2";
String version2 = "version2";
String type3 = "type3";
String version4 = "version4";
MetacardImpl metacard = new MetacardImpl();
metacard.setMetadata(TestDataLibrary.getCatAndDogEntry());
List<MockTypeVersionsExtension> extensions = createContentTypeVersionList("type1,version1|type2,version2|type3,|,version4");
MockQuery query = new MockQuery();
query.addTypeFilter(extensions);
SubscriptionFilterVisitor visitor = new SubscriptionFilterVisitor();
Predicate pred = (Predicate) query.getFilter().accept(visitor, null);
LOGGER.debug("Resulting Predicate: {}", pred);
HashMap<String, Object> properties = new HashMap<>();
properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
Map<String, Object> contextualMap = constructContextualMap(metacard);
properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
// Above Pulled from PubSubProviderImpl
properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, type1 + "," + version1);
Event testEvent = new Event("topic", properties);
assertTrue(pred.matches(testEvent));
properties.clear();
properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, type2 + "," + version2);
testEvent = new Event("topic", properties);
assertTrue(pred.matches(testEvent));
properties.clear();
properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, type2 + "," + version1);
testEvent = new Event("topic", properties);
assertFalse(pred.matches(testEvent));
properties.clear();
properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, type1 + "," + version2);
testEvent = new Event("topic", properties);
assertFalse(pred.matches(testEvent));
properties.clear();
properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, type3 + "," + "random_version");
testEvent = new Event("topic", properties);
assertTrue(pred.matches(testEvent));
properties.clear();
properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, "random_type" + "," + "random_version");
testEvent = new Event("topic", properties);
assertFalse(pred.matches(testEvent));
properties.clear();
properties.put(PubSubConstants.HEADER_OPERATION_KEY, PubSubConstants.CREATE);
properties.put(PubSubConstants.HEADER_CONTEXTUAL_KEY, contextualMap);
properties.put(PubSubConstants.HEADER_CONTENT_TYPE_KEY, "random_type" + "," + version4);
testEvent = new Event("topic", properties);
assertFalse(pred.matches(testEvent));
LOGGER.debug("*************** END: {} *****************", methodName);
}
use of org.osgi.service.event.Event 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.osgi.service.event.Event in project ddf by codice.
the class CommandAuditerTest method testHandleBadEvent.
@Test
public void testHandleBadEvent() {
final String[] passedCommand = new String[1];
CommandAuditer commandAuditer = new CommandAuditer() {
void writeAudit(String command) {
passedCommand[0] = command;
}
};
Map<String, Object> props = new HashMap<>();
props.put("command", "command I'm trying to run");
Event event = new Event("not/the/event/you/are/looking/for", props);
commandAuditer.handleEvent(event);
assertNull(passedCommand[0]);
}
use of org.osgi.service.event.Event in project ddf by codice.
the class CommandAuditerTest method testHandleEvent.
@Test
public void testHandleEvent() {
final String[] passedCommand = new String[1];
CommandAuditer commandAuditer = new CommandAuditer() {
void writeAudit(String command) {
passedCommand[0] = command;
}
};
Map<String, Object> props = new HashMap<>();
props.put("command", "command I'm trying to run");
Event event = new Event("org/apache/karaf/shell/console/EXECUTING", props);
commandAuditer.handleEvent(event);
assertThat(passedCommand[0], is("command I'm trying to run"));
}
use of org.osgi.service.event.Event in project ddf by codice.
the class CommandAuditerTest method testHandleEventEmptyProps.
@Test
public void testHandleEventEmptyProps() {
final String[] passedCommand = new String[1];
CommandAuditer commandAuditer = new CommandAuditer() {
void writeAudit(String command) {
passedCommand[0] = command;
}
};
Event event = new Event("org/apache/karaf/shell/console/EXECUTING", new HashMap<>());
commandAuditer.handleEvent(event);
assertNull(passedCommand[0]);
}
Aggregations