public boolean matches(String path) { return antMatcher.match(pattern, path); } }
private DestinationPatternsMessageCondition(Collection<String> patterns, @Nullable PathMatcher pathMatcher) { this.pathMatcher = (pathMatcher != null ? pathMatcher : new AntPathMatcher()); this.patterns = Collections.unmodifiableSet(prependLeadingSlash(patterns, this.pathMatcher)); }
protected String getEndpointLocalPath(String pathWithinApplication) { return new AntPathMatcher().extractPathWithinPattern(this.realRequestMappingPath, pathWithinApplication); }
private boolean isBranchNotExcluded(String branchName) { AntPathMatcher matcher = new AntPathMatcher(); for (String excludePattern : excludedBranches) { if (matcher.match(excludePattern, branchName)) { return false; } } return true; }
protected boolean applyPath(String path) { if (paths==null || paths.size()==0 || path == null) { return false; } AntPathMatcher matcher = new AntPathMatcher(); for (String pattern : paths) { if (matcher.isPattern(pattern)) { if (matcher.match(pattern, path)) { return true; } } else { //exact match if (pattern.equals(path)) { return true; } } } return false; }
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")); assertEquals("",vars.get("bar"));
/** * 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; }
String format = "location/{state}/{city}"; String actualUrl = "location/washington/seattle"; AntPathMatcher pathMatcher = new AntPathMatcher(); Map<String, String> variables = pathMatcher.extractUriTemplateVariables(format, actualUrl); assertThat(variables.get("state"), is("washington")); assertThat(variables.get("city"), is("seattle"));
@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()); }
if (!pattern1.equals(pattern2) && !pattern1ContainsUriVar && match(pattern1, pattern2)) { // /* + /hotel -> /hotel ; "/*.*" + "/*.html" -> /*.html return concat(pattern1.substring(0, pattern1.length() - 2), pattern2); return concat(pattern1, pattern2); if (pattern1ContainsUriVar || starDotPos1 == -1 || this.pathSeparator.equals(".")) { return concat(pattern1, pattern2);
@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")); }
@Configuration public class WebConfig extends WebMvcConfigurerAdapter { @Override public void configurePathMatch(PathMatchConfigurer configurer) { AntPathMatcher matcher = new AntPathMatcher(); matcher.setCaseSensitive(false); configurer.setPathMatcher(matcher); } }
@Test public void uniqueDeliminator() { pathMatcher.setPathSeparator("."); 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"));
@Test public void extractUriTemplateVariablesRegex() { Map<String, String> result = pathMatcher .extractUriTemplateVariables("{symbolicName:[\\w\\.]+}-{version:[\\w\\.]+}.jar", "com.example-1.0.0.jar"); assertEquals("com.example", result.get("symbolicName")); assertEquals("1.0.0", result.get("version")); result = pathMatcher.extractUriTemplateVariables("{symbolicName:[\\w\\.]+}-sources-{version:[\\w\\.]+}.jar", "com.example-sources-1.0.0.jar"); assertEquals("com.example", result.get("symbolicName")); assertEquals("1.0.0", result.get("version")); }
@Override public boolean isPattern(String path) { return this.delegate.isPattern(path); }
private boolean isBranchIncluded(String branchName) { AntPathMatcher matcher = new AntPathMatcher(); for (String includePattern : includedBranches) { if (matcher.match(includePattern, branchName)) { return true; } } return includedBranches.isEmpty(); }
/** * 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; }
@Test // INT-1677 public void withExpressions() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.setMethod("POST"); request.setContentType("text/plain"); request.setParameter("foo", "bar"); request.setContent("hello".getBytes()); String requestURI = "/fname/bill/lname/clinton"; //See org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping#handleMatch Map<String, String> uriTemplateVariables = new AntPathMatcher().extractUriTemplateVariables("/fname/{f}/lname/{l}", requestURI); request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVariables); request.setRequestURI(requestURI); MockHttpServletResponse response = new MockHttpServletResponse(); inboundAdapterWithExpressions.handleRequest(request, response); assertEquals(HttpServletResponse.SC_OK, response.getStatus()); Message<?> message = requests.receive(0); assertNotNull(message); Object payload = message.getPayload(); assertTrue(payload instanceof String); assertEquals("bill", payload); assertEquals("clinton", message.getHeaders().get("lname")); }