From 57f7b14b4939a244973725768ef4d819c7adcea4 Mon Sep 17 00:00:00 2001 From: Antonio Marrero Date: Mon, 24 Jun 2013 16:17:01 +0100 Subject: [PATCH] Assign lowest priority to `/**` pattern Update AntPathMatcher Comparator to treat `/**` in the same way as `null` paths. Prior to this commit the pattern `/**` would be picked in preference to patterns with 3 or more PathVariable (e.g. `/matches/{matchId}/ periods/{periodId}/teams/{teamId}/results`). Issue: SPR-10550 --- .../org/springframework/util/AntPathMatcher.java | 12 +++++++++--- .../springframework/util/AntPathMatcherTests.java | 5 +++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java b/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java index 76f4fe9467..2408a78bb8 100644 --- a/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java +++ b/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java @@ -391,15 +391,16 @@ public class AntPathMatcher implements PathMatcher { @Override public int compare(String pattern1, String pattern2) { - if (pattern1 == null && pattern2 == null) { + if (isNullOrCaptureAllPattern(pattern1) && isNullOrCaptureAllPattern(pattern2)) { return 0; } - else if (pattern1 == null) { + else if (isNullOrCaptureAllPattern(pattern1)) { return 1; } - else if (pattern2 == null) { + else if (isNullOrCaptureAllPattern(pattern2)) { return -1; } + boolean pattern1EqualsPath = pattern1.equals(path); boolean pattern2EqualsPath = pattern2.equals(path); if (pattern1EqualsPath && pattern2EqualsPath) { @@ -411,6 +412,7 @@ public class AntPathMatcher implements PathMatcher { else if (pattern2EqualsPath) { return 1; } + int wildCardCount1 = getWildCardCount(pattern1); int wildCardCount2 = getWildCardCount(pattern2); @@ -448,6 +450,10 @@ public class AntPathMatcher implements PathMatcher { return 0; } + private boolean isNullOrCaptureAllPattern(String pattern) { + return pattern == null || "/**".equals(pattern); + } + private int getWildCardCount(String pattern) { if (pattern.endsWith(".*")) { pattern = pattern.substring(0, pattern.length() - 2); diff --git a/spring-core/src/test/java/org/springframework/util/AntPathMatcherTests.java b/spring-core/src/test/java/org/springframework/util/AntPathMatcherTests.java index dba4f3fcd1..6a5e5684c8 100644 --- a/spring-core/src/test/java/org/springframework/util/AntPathMatcherTests.java +++ b/spring-core/src/test/java/org/springframework/util/AntPathMatcherTests.java @@ -437,6 +437,11 @@ public class AntPathMatcherTests { assertEquals(-1, comparator.compare("/hotels/{hotel}/booking", "/hotels/{hotel}/bookings/{booking}")); assertEquals(1, comparator.compare("/hotels/{hotel}/bookings/{booking}", "/hotels/{hotel}/booking")); + //SPR-10550 + assertEquals(-1, comparator.compare("/hotels/{hotel}/bookings/{booking}/cutomers/{customer}", "/**")); + assertEquals(1, comparator.compare("/**","/hotels/{hotel}/bookings/{booking}/cutomers/{customer}")); + assertEquals(0, comparator.compare("/**","/**")); + assertEquals(-1, comparator.compare("/hotels/{hotel}", "/hotels/*")); assertEquals(1, comparator.compare("/hotels/*", "/hotels/{hotel}"));