Skip to content

Commit

Permalink
Add option to not trim path segments in AntPathMatch
Browse files Browse the repository at this point in the history
Issue: SPR-8687
  • Loading branch information
rstoyanchev committed Jan 8, 2013
1 parent 68d4a70 commit f185c3a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -95,8 +101,8 @@ protected boolean doMatch(String pattern, String path, boolean fullMatch,
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;
Expand Down Expand Up @@ -246,8 +252,8 @@ private boolean matchStrings(String pattern, String str, Map<String, String> uri
* 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();

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -542,6 +542,14 @@ public void patternComparatorSort() {
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"));
}

}

0 comments on commit f185c3a

Please sign in to comment.