UriCspDirectiveCollapser extends AbstractPathCollapser implements StringCollapser { @Override public <T> Map<String, Collection<T>> collapse(Map<String, Collection<T>> input) { Map<PathName, Collection<T>> inputMap = new HashMap<PathName, Collection<T>>(input.size()); for (Map.Entry<String, Collection<T>> entry : input.entrySet()) { String uri = entry.getKey(); if (!uri.startsWith("/")) { throw new IllegalArgumentException("Expected URI to start with leading slash."); } inputMap.put(new PathName(uri.substring(1).split("/", -1), null), entry.getValue()); } Map<PathName, Collection<T>> outputMap = collapsePaths(inputMap); Map<String, Collection<T>> output = new HashMap<String, Collection<T>>(outputMap.size()); for (Map.Entry<PathName, Collection<T>> pathNameEntry : outputMap.entrySet()) { output.put("/" + StringUtil.join("/", pathNameEntry.getKey().getPathComponents()), pathNameEntry.getValue()); } return output; } UriCspDirectiveCollapser(int collapseThreshold); @Override Map<String, Collection<T>> collapse(Map<String, Collection<T>> input); boolean pathNameMatches(String matcher, String matchee);  }
@Test public void testBasicCollapse() { final UriCspDirectiveCollapser collapser = new UriCspDirectiveCollapser(2); List<String> output = new ArrayList<String>(collapser.collapse(nullMap( "/a/b/c", "/a/b/d", "/c/d/e", "/c/d/f", "/c/g/h", "/c/g/j")).keySet()); Collections.sort(output); Assert.assertEquals(output, Arrays.asList("/a/b/*", "/c/**")); }
@Test public void testDoNotCollapseToRoot() { final UriCspDirectiveCollapser collapser = new UriCspDirectiveCollapser(2); List<String> output = new ArrayList<String>(collapser.collapse(nullMap("/**", "/a/a", "/a/b")).keySet()); Collections.sort(output); Assert.assertEquals(output, Arrays.asList("/**", "/a/*")); }