Search in sources :

Example 66 with Event

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);
}
Also used : SubscriptionFilterVisitor(ddf.catalog.pubsub.internal.SubscriptionFilterVisitor) HashMap(java.util.HashMap) Event(org.osgi.service.event.Event) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) GeospatialPredicate(ddf.catalog.pubsub.predicate.GeospatialPredicate) ContentTypePredicate(ddf.catalog.pubsub.predicate.ContentTypePredicate) Predicate(ddf.catalog.pubsub.predicate.Predicate) Test(org.junit.Test)

Example 67 with Event

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(...)");
}
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 68 with Event

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]);
}
Also used : HashMap(java.util.HashMap) Event(org.osgi.service.event.Event) Test(org.junit.Test)

Example 69 with Event

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"));
}
Also used : HashMap(java.util.HashMap) Event(org.osgi.service.event.Event) Test(org.junit.Test)

Example 70 with Event

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]);
}
Also used : Event(org.osgi.service.event.Event) Test(org.junit.Test)

Aggregations

Event (org.osgi.service.event.Event)142 Test (org.junit.Test)79 HashMap (java.util.HashMap)48 Hashtable (java.util.Hashtable)44 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)23 ContentTypePredicate (ddf.catalog.pubsub.predicate.ContentTypePredicate)20 EventAdmin (org.osgi.service.event.EventAdmin)19 GeospatialPredicate (ddf.catalog.pubsub.predicate.GeospatialPredicate)18 Predicate (ddf.catalog.pubsub.predicate.Predicate)16 ArrayList (java.util.ArrayList)16 ActivityEvent (org.codice.ddf.activities.ActivityEvent)13 EventHandler (org.osgi.service.event.EventHandler)13 SubscriptionFilterVisitor (ddf.catalog.pubsub.internal.SubscriptionFilterVisitor)11 Date (java.util.Date)11 Job (org.apache.sling.event.jobs.Job)9 Map (java.util.Map)8 List (java.util.List)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 JobManager (org.apache.sling.event.jobs.JobManager)7 JobConsumer (org.apache.sling.event.jobs.consumer.JobConsumer)7