DynamicJsonFilter implements ContainerRequestFilter { @Override public void filter(ContainerRequestContext requestContext) { Map<String, List<String>> queryParams = requestContext.getUriInfo().getQueryParameters(); boolean containsExcludes = queryParams.containsKey("exclude"); boolean containsIncludes = queryParams.containsKey("include"); DynamicFilterData filterData = new DynamicFilterData(); if (queryParams.containsKey("filtermode")) { List<String> values = queryParams.get("filtermode"); filterData.setWhitelistMode("whitelist".equalsIgnoreCase(values.get(0))); } else { filterData.setWhitelistMode(containsIncludes && !containsExcludes); } if (containsIncludes) { for (String path : queryParams.get("include")) { filterData.includeAttribute(path); } } if (containsExcludes) { for (String path : queryParams.get("exclude")) { filterData.excludeAttribute(path); } } if (containsIncludes || containsExcludes) { ResteasyContext.pushContext(DynamicFilterData.class, filterData); } }  @Override void filter(ContainerRequestContext requestContext);  }
@Test public void testNoFilters() throws Exception { mockReq = MockHttpRequest.create("GET", new URI("/candlepin/status"), new URI("http: when(mockRequestContext.getUriInfo()).thenReturn(mockReq.getUri()); interceptor.filter(mockRequestContext); DynamicFilterData filterData = ResteasyContext.getContextData(DynamicFilterData.class); assertNull(filterData); }
@Test public void testSimpleBlacklist() throws Exception { mockReq = MockHttpRequest.create("GET", new URI("/candlepin/status?exclude=a2"), new URI("http: when(mockRequestContext.getUriInfo()).thenReturn(mockReq.getUri()); interceptor.filter(mockRequestContext); DynamicFilterData filterData = ResteasyContext.getContextData(DynamicFilterData.class); assertNotNull(filterData); assertFalse(filterData.isAttributeExcluded("a1")); assertFalse(filterData.isAttributeExcluded("a1.c1")); assertFalse(filterData.isAttributeExcluded("a1.c2")); assertTrue(filterData.isAttributeExcluded("a2")); assertTrue(filterData.isAttributeExcluded("a2.c1")); assertTrue(filterData.isAttributeExcluded("a2.c2")); assertFalse(filterData.isAttributeExcluded("a3")); assertFalse(filterData.isAttributeExcluded("a3.c1")); assertFalse(filterData.isAttributeExcluded("a3.c2")); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a1"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a1", "c1"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a1", "c2"))); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a2"))); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a2", "c1"))); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a2", "c2"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a3"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a3", "c1"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a3", "c2"))); }
@Test public void testSimpleMultiBlacklist() throws Exception { mockReq = MockHttpRequest.create("GET", new URI("/candlepin/status?exclude=a2&exclude=a3"), new URI("http: when(mockRequestContext.getUriInfo()).thenReturn(mockReq.getUri()); interceptor.filter(mockRequestContext); DynamicFilterData filterData = ResteasyContext.getContextData(DynamicFilterData.class); assertNotNull(filterData); assertFalse(filterData.isAttributeExcluded("a1")); assertFalse(filterData.isAttributeExcluded("a1.c1")); assertFalse(filterData.isAttributeExcluded("a1.c2")); assertTrue(filterData.isAttributeExcluded("a2")); assertTrue(filterData.isAttributeExcluded("a2.c1")); assertTrue(filterData.isAttributeExcluded("a2.c2")); assertTrue(filterData.isAttributeExcluded("a3")); assertTrue(filterData.isAttributeExcluded("a3.c1")); assertTrue(filterData.isAttributeExcluded("a3.c2")); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a1"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a1", "c1"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a1", "c2"))); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a2"))); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a2", "c1"))); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a2", "c2"))); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a3"))); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a3", "c1"))); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a3", "c2"))); }
@Test public void testNestedAttributeBlacklist() throws Exception { mockReq = MockHttpRequest.create("GET", new URI("/candlepin/status?exclude=a2&exclude=a3.c1"), new URI("http: when(mockRequestContext.getUriInfo()).thenReturn(mockReq.getUri()); interceptor.filter(mockRequestContext); DynamicFilterData filterData = ResteasyContext.getContextData(DynamicFilterData.class); assertNotNull(filterData); assertFalse(filterData.isAttributeExcluded("a1")); assertFalse(filterData.isAttributeExcluded("a1.c1")); assertFalse(filterData.isAttributeExcluded("a1.c2")); assertTrue(filterData.isAttributeExcluded("a2")); assertTrue(filterData.isAttributeExcluded("a2.c1")); assertTrue(filterData.isAttributeExcluded("a2.c2")); assertFalse(filterData.isAttributeExcluded("a3")); assertTrue(filterData.isAttributeExcluded("a3.c1")); assertFalse(filterData.isAttributeExcluded("a3.c2")); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a1"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a1", "c1"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a1", "c2"))); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a2"))); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a2", "c1"))); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a2", "c2"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a3"))); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a3", "c1"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a3", "c2"))); }
@Test public void testSimpleWhitelist() throws Exception { mockReq = MockHttpRequest.create("GET", new URI("/candlepin/status?include=a2"), new URI("http: when(mockRequestContext.getUriInfo()).thenReturn(mockReq.getUri()); interceptor.filter(mockRequestContext); DynamicFilterData filterData = ResteasyContext.getContextData(DynamicFilterData.class); assertNotNull(filterData); assertTrue(filterData.isAttributeExcluded("a1")); assertTrue(filterData.isAttributeExcluded("a1.c1")); assertTrue(filterData.isAttributeExcluded("a1.c2")); assertFalse(filterData.isAttributeExcluded("a2")); assertFalse(filterData.isAttributeExcluded("a2.c1")); assertFalse(filterData.isAttributeExcluded("a2.c2")); assertTrue(filterData.isAttributeExcluded("a3")); assertTrue(filterData.isAttributeExcluded("a3.c1")); assertTrue(filterData.isAttributeExcluded("a3.c2")); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a1"))); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a1", "c1"))); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a1", "c2"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a2"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a2", "c1"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a2", "c2"))); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a3"))); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a3", "c1"))); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a3", "c2"))); }
@Test public void testSimpleMultiWhitelist() throws Exception { mockReq = MockHttpRequest.create("GET", new URI("/candlepin/status?include=a1&include=a3"), new URI("http: when(mockRequestContext.getUriInfo()).thenReturn(mockReq.getUri()); interceptor.filter(mockRequestContext); DynamicFilterData filterData = ResteasyContext.getContextData(DynamicFilterData.class); assertNotNull(filterData); assertFalse(filterData.isAttributeExcluded("a1")); assertFalse(filterData.isAttributeExcluded("a1.c1")); assertFalse(filterData.isAttributeExcluded("a1.c2")); assertTrue(filterData.isAttributeExcluded("a2")); assertTrue(filterData.isAttributeExcluded("a2.c1")); assertTrue(filterData.isAttributeExcluded("a2.c2")); assertFalse(filterData.isAttributeExcluded("a3")); assertFalse(filterData.isAttributeExcluded("a3.c1")); assertFalse(filterData.isAttributeExcluded("a3.c2")); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a1"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a1", "c1"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a1", "c2"))); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a2"))); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a2", "c1"))); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a2", "c2"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a3"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a3", "c1"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a3", "c2"))); }
@Test public void testNestedAttributeWhitelist() throws Exception { mockReq = MockHttpRequest.create("GET", new URI("/candlepin/status?include=a2&include=a3.c1"), new URI("http: when(mockRequestContext.getUriInfo()).thenReturn(mockReq.getUri()); interceptor.filter(mockRequestContext); DynamicFilterData filterData = ResteasyContext.getContextData(DynamicFilterData.class); assertNotNull(filterData); assertTrue(filterData.isAttributeExcluded("a1")); assertTrue(filterData.isAttributeExcluded("a1.c1")); assertTrue(filterData.isAttributeExcluded("a1.c2")); assertFalse(filterData.isAttributeExcluded("a2")); assertFalse(filterData.isAttributeExcluded("a2.c1")); assertFalse(filterData.isAttributeExcluded("a2.c2")); assertFalse(filterData.isAttributeExcluded("a3")); assertFalse(filterData.isAttributeExcluded("a3.c1")); assertTrue(filterData.isAttributeExcluded("a3.c2")); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a1"))); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a1", "c1"))); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a1", "c2"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a2"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a2", "c1"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a2", "c2"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a3"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a3", "c1"))); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a3", "c2"))); }
@Test public void testIncludesWithExcludes() throws Exception { mockReq = MockHttpRequest.create("GET", new URI("/candlepin/status?include=a.b1&exclude=a.b1.c2&exclude=a.b2&include=a.b2.d2"), new URI("http: when(mockRequestContext.getUriInfo()).thenReturn(mockReq.getUri()); interceptor.filter(mockRequestContext); DynamicFilterData filterData = ResteasyContext.getContextData(DynamicFilterData.class); assertNotNull(filterData); assertFalse(filterData.isAttributeExcluded("a")); assertFalse(filterData.isAttributeExcluded("a.b1")); assertFalse(filterData.isAttributeExcluded("a.b1.c1")); assertTrue(filterData.isAttributeExcluded("a.b1.c2")); assertFalse(filterData.isAttributeExcluded("a.b1.c3")); assertFalse(filterData.isAttributeExcluded("a.b2")); assertTrue(filterData.isAttributeExcluded("a.b2.d1")); assertFalse(filterData.isAttributeExcluded("a.b2.d2")); assertTrue(filterData.isAttributeExcluded("a.b2.d3")); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a", "b1"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a", "b1", "c1"))); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a", "b1", "c2"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a", "b1", "c3"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a", "b2"))); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a", "b2", "d1"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a", "b2", "d2"))); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a", "b2", "d3"))); }
@Test public void testIncludesWithExcludesUsingWhitelist() throws Exception { mockReq = MockHttpRequest.create("GET", new URI("/candlepin/status?include=a.b1&exclude=a.b1.c2&include=a.b2.d2&filtermode=whitelist"), new URI("http: when(mockRequestContext.getUriInfo()).thenReturn(mockReq.getUri()); interceptor.filter(mockRequestContext); DynamicFilterData filterData = ResteasyContext.getContextData(DynamicFilterData.class); assertNotNull(filterData); assertFalse(filterData.isAttributeExcluded("a")); assertFalse(filterData.isAttributeExcluded("a.b1")); assertFalse(filterData.isAttributeExcluded("a.b1.c1")); assertTrue(filterData.isAttributeExcluded("a.b1.c2")); assertFalse(filterData.isAttributeExcluded("a.b1.c3")); assertFalse(filterData.isAttributeExcluded("a.b2")); assertTrue(filterData.isAttributeExcluded("a.b2.d1")); assertFalse(filterData.isAttributeExcluded("a.b2.d2")); assertTrue(filterData.isAttributeExcluded("a.b2.d3")); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a", "b1"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a", "b1", "c1"))); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a", "b1", "c2"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a", "b1", "c3"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a", "b2"))); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a", "b2", "d1"))); assertFalse(filterData.isAttributeExcluded(Arrays.asList("a", "b2", "d2"))); assertTrue(filterData.isAttributeExcluded(Arrays.asList("a", "b2", "d3"))); }