Browse Source

Add option to not trim path segments in AntPathMatch

Issue: SPR-8687
pull/214/merge
Rossen Stoyanchev 12 years ago
parent
commit
f185c3a3c1
  1. 16
      spring-core/src/main/java/org/springframework/util/AntPathMatcher.java
  2. 10
      spring-core/src/test/java/org/springframework/util/AntPathMatcherTests.java

16
spring-core/src/main/java/org/springframework/util/AntPathMatcher.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -60,12 +60,18 @@ public class AntPathMatcher implements PathMatcher { @@ -60,12 +60,18 @@ public class AntPathMatcher implements PathMatcher {
private final Map<String, AntPathStringMatcher> stringMatcherCache =
new ConcurrentHashMap<String, AntPathStringMatcher>(256);
private boolean trimTokens = true;
/** Set the path separator to use for pattern parsing. Default is "/", as in Ant. */
public void setPathSeparator(String pathSeparator) {
this.pathSeparator = (pathSeparator != null ? pathSeparator : DEFAULT_PATH_SEPARATOR);
}
/** Whether to trim tokenized paths and patterns. */
public void setTrimTokens(boolean trimTokens) {
this.trimTokens = trimTokens;
}
public boolean isPattern(String path) {
return (path.indexOf('*') != -1 || path.indexOf('?') != -1);
@ -95,8 +101,8 @@ public class AntPathMatcher implements PathMatcher { @@ -95,8 +101,8 @@ public class AntPathMatcher implements PathMatcher {
return false;
}
String[] pattDirs = StringUtils.tokenizeToStringArray(pattern, this.pathSeparator);
String[] pathDirs = StringUtils.tokenizeToStringArray(path, this.pathSeparator);
String[] pattDirs = StringUtils.tokenizeToStringArray(pattern, this.pathSeparator, this.trimTokens, true);
String[] pathDirs = StringUtils.tokenizeToStringArray(path, this.pathSeparator, this.trimTokens, true);
int pattIdxStart = 0;
int pattIdxEnd = pattDirs.length - 1;
@ -246,8 +252,8 @@ public class AntPathMatcher implements PathMatcher { @@ -246,8 +252,8 @@ public class AntPathMatcher implements PathMatcher {
* does <strong>not</strong> enforce this.
*/
public String extractPathWithinPattern(String pattern, String path) {
String[] patternParts = StringUtils.tokenizeToStringArray(pattern, this.pathSeparator);
String[] pathParts = StringUtils.tokenizeToStringArray(path, this.pathSeparator);
String[] patternParts = StringUtils.tokenizeToStringArray(pattern, this.pathSeparator, this.trimTokens, true);
String[] pathParts = StringUtils.tokenizeToStringArray(path, this.pathSeparator, this.trimTokens, true);
StringBuilder builder = new StringBuilder();

10
spring-core/src/test/java/org/springframework/util/AntPathMatcherTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -542,6 +542,14 @@ public class AntPathMatcherTests { @@ -542,6 +542,14 @@ public class AntPathMatcherTests {
paths.clear();
}
// SPR-8687
@Test
public void trimTokensOff() {
pathMatcher.setTrimTokens(false);
assertTrue(pathMatcher.match("/group/{groupName}/members", "/group/sales/members"));
assertTrue(pathMatcher.match("/group/{groupName}/members", "/group/ sales/members"));
}
}

Loading…
Cancel
Save