RegexFilteringInterceptor implements Interceptor { @Override public Event intercept(Event event) { if (!excludeEvents) { if (regex.matcher(new String(event.getBody())).find()) { return event; } else { return null; } } else { if (regex.matcher(new String(event.getBody())).find()) { return null; } else { return event; } } } private  RegexFilteringInterceptor(Pattern regex, boolean excludeEvents); @Override void initialize(); @Override /** * Returns the event if it passes the regular expression filter and null * otherwise. */ Event intercept(Event event); @Override List<Event> intercept(List<Event> events); @Override void close();  }
@Test public void testDefaultBehavior() throws ClassNotFoundException, InstantiationException, IllegalAccessException { Interceptor.Builder builder = InterceptorBuilderFactory.newInstance( InterceptorType.REGEX_FILTER.toString()); builder.configure(new Context()); Interceptor interceptor = builder.build(); Event event = EventBuilder.withBody("test", Charsets.UTF_8); Event filteredEvent = interceptor.intercept(event); Assert.assertNotNull(filteredEvent); Assert.assertEquals(event, filteredEvent); }
@Test public void testInclusion() throws ClassNotFoundException, InstantiationException, IllegalAccessException { Interceptor.Builder builder = InterceptorBuilderFactory.newInstance( InterceptorType.REGEX_FILTER.toString()); Context ctx = new Context(); ctx.put(Constants.REGEX, "(INFO.*)|(WARNING.*)"); ctx.put(Constants.EXCLUDE_EVENTS, "false"); builder.configure(ctx); Interceptor interceptor = builder.build(); Event shouldPass1 = EventBuilder.withBody("INFO: some message", Charsets.UTF_8); Assert.assertNotNull(interceptor.intercept(shouldPass1)); Event shouldPass2 = EventBuilder.withBody("WARNING: some message", Charsets.UTF_8); Assert.assertNotNull(interceptor.intercept(shouldPass2)); Event shouldNotPass = EventBuilder.withBody("DEBUG: some message", Charsets.UTF_8); Assert.assertNull(interceptor.intercept(shouldNotPass)); builder.configure(ctx); }
@Test public void testExclusion() throws ClassNotFoundException, InstantiationException, IllegalAccessException { Interceptor.Builder builder = InterceptorBuilderFactory.newInstance( InterceptorType.REGEX_FILTER.toString()); Context ctx = new Context(); ctx.put(Constants.REGEX, ".*DEBUG.*"); ctx.put(Constants.EXCLUDE_EVENTS, "true"); builder.configure(ctx); Interceptor interceptor = builder.build(); Event shouldPass1 = EventBuilder.withBody("INFO: some message", Charsets.UTF_8); Assert.assertNotNull(interceptor.intercept(shouldPass1)); Event shouldPass2 = EventBuilder.withBody("WARNING: some message", Charsets.UTF_8); Assert.assertNotNull(interceptor.intercept(shouldPass2)); Event shouldNotPass = EventBuilder.withBody("this message has DEBUG in it", Charsets.UTF_8); Assert.assertNull(interceptor.intercept(shouldNotPass)); builder.configure(ctx); }