Skip to content

Commit ab1ea4a

Browse files
bdemersfpapon
authored andcommitted
Improving tests for path matching logic
1 parent e35a669 commit ab1ea4a

File tree

7 files changed

+574
-54
lines changed

7 files changed

+574
-54
lines changed

NOTICE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ on initial ideas from Dr. Heinz Kabutz's publicly posted version
99
available at http://www.javaspecialists.eu/archive/Issue015.html,
1010
with continued modifications.
1111

12-
Certain parts (StringUtils etc.) of the source code for this
12+
Certain parts (StringUtils, AntPathMatcherTests, etc.) of the source code for this
1313
product was copied for simplicity and to reduce dependencies
1414
from the source code developed by the Spring Framework Project
1515
(http://www.springframework.org).

core/src/main/java/org/apache/shiro/util/AntPathMatcher.java

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,15 @@ public void setPathSeparator(String pathSeparator) {
7777
this.pathSeparator = (pathSeparator != null ? pathSeparator : DEFAULT_PATH_SEPARATOR);
7878
}
7979

80-
80+
/**
81+
* Checks if {@code path} is a pattern (i.e. contains a '*', or '?'). For example the {@code /foo/**} would return {@code true}, while {@code /bar/} would return {@code false}.
82+
* @param path the string to check
83+
* @return this method returns {@code true} if {@code path} contains a '*' or '?', otherwise, {@code false}
84+
*/
8185
public boolean isPattern(String path) {
86+
if (path == null) {
87+
return false;
88+
}
8289
return (path.indexOf('*') != -1 || path.indexOf('?') != -1);
8390
}
8491

@@ -106,12 +113,12 @@ public boolean matchStart(String pattern, String path) {
106113
* <code>false</code> if it didn't
107114
*/
108115
protected boolean doMatch(String pattern, String path, boolean fullMatch) {
109-
if (path.startsWith(this.pathSeparator) != pattern.startsWith(this.pathSeparator)) {
116+
if (path == null || path.startsWith(this.pathSeparator) != pattern.startsWith(this.pathSeparator)) {
110117
return false;
111118
}
112119

113-
String[] pattDirs = StringUtils.tokenizeToStringArray(pattern, this.pathSeparator);
114-
String[] pathDirs = StringUtils.tokenizeToStringArray(path, this.pathSeparator);
120+
String[] pattDirs = StringUtils.tokenizeToStringArray(pattern, this.pathSeparator, false, true);
121+
String[] pathDirs = StringUtils.tokenizeToStringArray(path, this.pathSeparator, false, true);
115122

116123
int pattIdxStart = 0;
117124
int pattIdxEnd = pattDirs.length - 1;
@@ -393,33 +400,26 @@ private boolean matchStrings(String pattern, String str) {
393400
* and '<code>path</code>', but does <strong>not</strong> enforce this.
394401
*/
395402
public String extractPathWithinPattern(String pattern, String path) {
396-
String[] patternParts = StringUtils.tokenizeToStringArray(pattern, this.pathSeparator);
397-
String[] pathParts = StringUtils.tokenizeToStringArray(path, this.pathSeparator);
398-
399-
StringBuilder buffer = new StringBuilder();
400-
401-
// Add any path parts that have a wildcarded pattern part.
402-
int puts = 0;
403-
for (int i = 0; i < patternParts.length; i++) {
404-
String patternPart = patternParts[i];
405-
if ((patternPart.indexOf('*') > -1 || patternPart.indexOf('?') > -1) && pathParts.length >= i + 1) {
406-
if (puts > 0 || (i == 0 && !pattern.startsWith(this.pathSeparator))) {
407-
buffer.append(this.pathSeparator);
403+
String[] patternParts = StringUtils.tokenizeToStringArray(pattern, this.pathSeparator, false, true);
404+
String[] pathParts = StringUtils.tokenizeToStringArray(path, this.pathSeparator, false, true);
405+
StringBuilder builder = new StringBuilder();
406+
boolean pathStarted = false;
407+
408+
for (int segment = 0; segment < patternParts.length; segment++) {
409+
String patternPart = patternParts[segment];
410+
if (patternPart.indexOf('*') > -1 || patternPart.indexOf('?') > -1) {
411+
for (; segment < pathParts.length; segment++) {
412+
if (pathStarted || (segment == 0 && !pattern.startsWith(this.pathSeparator))) {
413+
builder.append(this.pathSeparator);
414+
}
415+
builder.append(pathParts[segment]);
416+
pathStarted = true;
408417
}
409-
buffer.append(pathParts[i]);
410-
puts++;
411418
}
412419
}
413420

414-
// Append any trailing path parts.
415-
for (int i = patternParts.length; i < pathParts.length; i++) {
416-
if (puts > 0 || i > 0) {
417-
buffer.append(this.pathSeparator);
418-
}
419-
buffer.append(pathParts[i]);
420-
}
421-
422-
return buffer.toString();
421+
return builder.toString();
423422
}
424423

424+
425425
}

0 commit comments

Comments
 (0)