public boolean matches(String path) { return antMatcher.match(pattern, path); } }
public boolean match(String basePackage) { if (pathMatcher.isPattern(basePackage)) { return pathMatcher.match(basePackage, this.packageName); } else { return this.type.startsWith(basePackage); } }
/** * Find a {@code VersionStrategy} for the request path of the requested resource. * @return an instance of a {@code VersionStrategy} or null if none matches that request path */ @Nullable protected VersionStrategy getStrategyForPath(String requestPath) { String path = "/".concat(requestPath); List<String> matchingPatterns = new ArrayList<>(); for (String pattern : this.versionStrategyMap.keySet()) { if (this.pathMatcher.match(pattern, path)) { matchingPatterns.add(pattern); } } if (!matchingPatterns.isEmpty()) { Comparator<String> comparator = this.pathMatcher.getPatternComparator(path); matchingPatterns.sort(comparator); return this.versionStrategyMap.get(matchingPatterns.get(0)); } return null; }
/** * Find a {@code VersionStrategy} for the request path of the requested resource. * @return an instance of a {@code VersionStrategy} or null if none matches that request path */ @Nullable protected VersionStrategy getStrategyForPath(String requestPath) { String path = "/".concat(requestPath); List<String> matchingPatterns = new ArrayList<>(); for (String pattern : this.versionStrategyMap.keySet()) { if (this.pathMatcher.match(pattern, path)) { matchingPatterns.add(pattern); } } if (!matchingPatterns.isEmpty()) { Comparator<String> comparator = this.pathMatcher.getPatternComparator(path); matchingPatterns.sort(comparator); return this.versionStrategyMap.get(matchingPatterns.get(0)); } return null; }
public boolean match(String basePackage) { if (pathMatcher.isPattern(basePackage)) { return pathMatcher.match(basePackage, this.packageName); } else { return this.type.startsWith(basePackage); } }
@Override public Strategy createStrategyIfMatch(Class target, Method method) { if (switcher.isEmpty()) { return null; } String text = target.getName().concat(".").concat(method.getName()); return switcher.entrySet().stream() .filter(entry -> antPathMatcher.match(entry.getValue().getExpression(), text)) .peek(entry -> entry.getValue().setId(entry.getKey())) .map(Map.Entry::getValue) .findFirst() .orElse(null); }
@Test public void creatingStringMatchersIfPatternPrefixCannotDetermineIfPathMatch() { pathMatcher.setCachePatterns(true); assertEquals(0, pathMatcher.stringMatcherCache.size()); pathMatcher.match("test", "testian"); pathMatcher.match("test?", "testFf"); pathMatcher.match("test/*", "test/dir/name.jpg"); pathMatcher.match("test/{name}.jpg", "test/lorem.jpg"); pathMatcher.match("bla/**/test.jpg", "bla/test.jpg"); pathMatcher.match("**/{name}.jpg", "test/lorem.jpg"); pathMatcher.match("/**/{name}.jpg", "/test/lorem.jpg"); pathMatcher.match("/*/dir/{name}.jpg", "/*/dir/lorem.jpg"); assertEquals(7, pathMatcher.stringMatcherCache.size()); }
private boolean isBranchIncluded(String branchName) { AntPathMatcher matcher = new AntPathMatcher(); for (String includePattern : includedBranches) { if (matcher.match(includePattern, branchName)) { return true; } } return includedBranches.isEmpty(); }
private boolean isBranchNotExcluded(String branchName) { AntPathMatcher matcher = new AntPathMatcher(); for (String excludePattern : excludedBranches) { if (matcher.match(excludePattern, branchName)) { return false; } } return true; }
@Test public void preventCreatingStringMatchersIfPathDoesNotStartsWithPatternPrefix() { pathMatcher.setCachePatterns(true); assertEquals(0, pathMatcher.stringMatcherCache.size()); pathMatcher.match("test?", "test"); assertEquals(1, pathMatcher.stringMatcherCache.size()); pathMatcher.match("test?", "best"); pathMatcher.match("test/*", "view/test.jpg"); pathMatcher.match("test/**/test.jpg", "view/test.jpg"); pathMatcher.match("test/{name}.jpg", "view/test.jpg"); assertEquals(1, pathMatcher.stringMatcherCache.size()); }
if (!pattern1.equals(pattern2) && !pattern1ContainsUriVar && match(pattern1, pattern2)) { // /* + /hotel -> /hotel ; "/*.*" + "/*.html" -> /*.html
@Test // SPR-8687 public void trimTokensOff() { pathMatcher.setTrimTokens(false); assertTrue(pathMatcher.match("/group/{groupName}/members", "/group/sales/members")); assertTrue(pathMatcher.match("/group/{groupName}/members", "/group/ sales/members")); assertFalse(pathMatcher.match("/group/{groupName}/members", "/Group/ Sales/Members")); }
@Test // SPR-13286 public void caseInsensitive() { pathMatcher.setCaseSensitive(false); assertTrue(pathMatcher.match("/group/{groupName}/members", "/group/sales/members")); assertTrue(pathMatcher.match("/group/{groupName}/members", "/Group/Sales/Members")); assertTrue(pathMatcher.match("/Group/{groupName}/Members", "/group/Sales/members")); }
/** * Asserts the request was forwarded to the given URL. * <p>This method accepts {@link org.springframework.util.AntPathMatcher} * patterns. * @param urlPattern an AntPath pattern to match against * @since 4.0 * @see org.springframework.util.AntPathMatcher */ public static ResultMatcher forwardedUrlPattern(String urlPattern) { return result -> { assertTrue("AntPath pattern", pathMatcher.isPattern(urlPattern)); String url = result.getResponse().getForwardedUrl(); assertTrue("Forwarded URL does not match the expected URL pattern", (url != null && pathMatcher.match(urlPattern, url))); }; }
@Test public void defaultCacheSetting() { match(); assertTrue(pathMatcher.stringMatcherCache.size() > 20); for (int i = 0; i < 65536; i++) { pathMatcher.match("test" + i, "test"); } // Cache turned off because it went beyond the threshold assertTrue(pathMatcher.stringMatcherCache.isEmpty()); }
assertTrue(pathMatcher.match("test", "test")); assertTrue(pathMatcher.match(".test", ".test")); assertFalse(pathMatcher.match(".test/jpg", "test/jpg")); assertFalse(pathMatcher.match("test", ".test")); assertFalse(pathMatcher.match(".test", "test")); assertTrue(pathMatcher.match("t?st", "test")); assertTrue(pathMatcher.match("??st", "test")); assertTrue(pathMatcher.match("tes?", "test")); assertTrue(pathMatcher.match("te??", "test")); assertTrue(pathMatcher.match("?es?", "test")); assertFalse(pathMatcher.match("tes?", "tes")); assertFalse(pathMatcher.match("tes?", "testt")); assertFalse(pathMatcher.match("tes?", "tsst")); assertTrue(pathMatcher.match("*", "test")); assertTrue(pathMatcher.match("test*", "test")); assertTrue(pathMatcher.match("test*", "testTest")); assertTrue(pathMatcher.match("*test*", "AnothertestTest")); assertTrue(pathMatcher.match("*test", "Anothertest")); assertTrue(pathMatcher.match("*/*", "test/")); assertTrue(pathMatcher.match("*/*", "test/test")); assertTrue(pathMatcher.match("*/*", "test/test/test")); assertTrue(pathMatcher.match("test*aaa", "testblaaaa")); assertFalse(pathMatcher.match("test*", "tst")); assertFalse(pathMatcher.match("test*", "tsttest")); assertFalse(pathMatcher.match("*test*", "tsttst")); assertFalse(pathMatcher.match("*test", "tsttst")); assertFalse(pathMatcher.match("*/*", "tsttst"));
/** * Asserts the request was redirected to the given URL. * <p>This method accepts {@link org.springframework.util.AntPathMatcher} * patterns. * @param urlPattern an AntPath pattern to match against * @since 4.0 * @see org.springframework.util.AntPathMatcher */ public static ResultMatcher redirectedUrlPattern(String urlPattern) { return result -> { assertTrue("No Ant-style path pattern", pathMatcher.isPattern(urlPattern)); String url = result.getResponse().getRedirectedUrl(); assertTrue("Redirected URL does not match the expected URL pattern", (url != null && pathMatcher.match(urlPattern, url))); }; }
@Test public void matchWithTrimTokensEnabled() throws Exception { pathMatcher.setTrimTokens(true); assertTrue(pathMatcher.match("/foo/bar", "/foo /bar")); }
@Test public void cachePatternsSetToTrue() { pathMatcher.setCachePatterns(true); match(); assertTrue(pathMatcher.stringMatcherCache.size() > 20); for (int i = 0; i < 65536; i++) { pathMatcher.match("test" + i, "test" + i); } // Cache keeps being alive due to the explicit cache setting assertTrue(pathMatcher.stringMatcherCache.size() > 65536); }
assertFalse(new AntPathMatcher().match("/{foo}", "/")); assertTrue(new AntPathMatcher().match("/{foo}", "/a")); assertTrue(new AntPathMatcher().match("/{foo}{bar}", "/a")); assertFalse(new AntPathMatcher().match("/{foo}*", "/")); assertTrue(new AntPathMatcher().match("/*", "/")); assertFalse(new AntPathMatcher().match("/*{foo}", "/")); Map<String, String> vars = new AntPathMatcher().extractUriTemplateVariables("/{foo}{bar}", "/a"); assertEquals("a",vars.get("foo"));