EventFilter { public static boolean match(Event event, Dictionary<String, Object> config) { if (config == null) { return true; } String nameExcludeRegex = (config.get(PROPERTY_NAME_EXCLUDE_CONFIG) != null) ? (String) config.get(PROPERTY_NAME_EXCLUDE_CONFIG) : null; String nameIncludeRegex = (config.get(PROPERTY_NAME_INCLUDE_CONFIG) != null) ? (String) config.get(PROPERTY_NAME_INCLUDE_CONFIG) : null; String valueExcludeRegex = (config.get(PROPERTY_VALUE_EXCLUDE_CONFIG) != null) ? (String) config.get(PROPERTY_VALUE_EXCLUDE_CONFIG) : null; String valueIncludeRegex = (config.get(PROPERTY_VALUE_INCLUDE_CONFIG) != null) ? (String) config.get(PROPERTY_VALUE_INCLUDE_CONFIG) : null; for (String name : event.getPropertyNames()) { if (nameExcludeRegex != null && name.matches(nameExcludeRegex)) { return false; } if (nameIncludeRegex != null && name.matches(nameIncludeRegex)) { return true; } if (event.getProperty(name) != null && event.getProperty(name) instanceof String) { if (valueExcludeRegex != null && ((String) event.getProperty(name)).matches(valueExcludeRegex)) { return false; } if (valueIncludeRegex != null && ((String) event.getProperty(name)).matches(valueIncludeRegex)) { return true; } } } return true; }  static boolean match(Event event, Dictionary<String, Object> config); static final String PROPERTY_NAME_EXCLUDE_CONFIG; static final String PROPERTY_NAME_INCLUDE_CONFIG; static final String PROPERTY_VALUE_EXCLUDE_CONFIG; static final String PROPERTY_VALUE_INCLUDE_CONFIG; }
@Test public void noFilter() { Assert.assertTrue(EventFilter.match(prepareTestEvent(), null)); Dictionary<String, Object> config = new Hashtable<>(); Assert.assertTrue(EventFilter.match(prepareTestEvent(), config)); }
@Test public void propertyNameFilter() { Dictionary<String, Object> config = new Hashtable<>(); config.put(EventFilter.PROPERTY_NAME_EXCLUDE_CONFIG, "key.*"); Assert.assertFalse(EventFilter.match(prepareTestEvent(), config)); config.put(EventFilter.PROPERTY_NAME_INCLUDE_CONFIG, "other"); Assert.assertFalse(EventFilter.match(prepareTestEvent(), config)); config.remove(EventFilter.PROPERTY_NAME_EXCLUDE_CONFIG); Assert.assertTrue(EventFilter.match(prepareTestEvent(), config)); }
@Test public void propertyValueFilter() { Dictionary<String, Object> config = new Hashtable<>(); config.put(EventFilter.PROPERTY_VALUE_EXCLUDE_CONFIG, "value.*"); Assert.assertFalse(EventFilter.match(prepareTestEvent(), config)); config.put(EventFilter.PROPERTY_VALUE_INCLUDE_CONFIG, "other"); Assert.assertFalse(EventFilter.match(prepareTestEvent(), config)); config.remove(EventFilter.PROPERTY_VALUE_EXCLUDE_CONFIG); Assert.assertTrue(EventFilter.match(prepareTestEvent(), config)); }