diff --git a/org.springframework.core/src/main/java/org/springframework/util/AntPathMatcher.java b/org.springframework.core/src/main/java/org/springframework/util/AntPathMatcher.java index 9602d7cc1b0..367e5b843d9 100644 --- a/org.springframework.core/src/main/java/org/springframework/util/AntPathMatcher.java +++ b/org.springframework.core/src/main/java/org/springframework/util/AntPathMatcher.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2009 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. @@ -290,18 +290,96 @@ public class AntPathMatcher implements PathMatcher { return variables; } + /** + * Combines two patterns into a new pattern that is returned. + *
This implementation simply concatenates the two patterns, unless the + * first pattern contains a file extension match (such as {@code *.html}. In + * that case, the second pattern should be included in the first, or an + * {@code IllegalArgumentException} is thrown. + *
For example: + *
| Pattern 1 | Pattern 2 | Result |
|---|---|---|
| /hotels | {@code null} | /hotels |
| {@code null} | /hotels | /hotels |
| /hotels | /bookings | /hotels/bookings |
| /hotels | bookings | /hotels/bookings |
| /hotels/* | /bookings | /hotels/bookings |
| /hotels/** | /bookings | /hotels/**/bookings |
| /hotels | {hotel} | /hotels/{hotel} |
| /hotels/* | {hotel} | /hotels/{hotel} |
| /hotels/** | {hotel} | /hotels/**/{hotel} |
| /*.html | /hotels.html | /hotels.html |
| /*.html | /hotels | IllegalArgumentException |
The returned Comparator will {@linkplain java.util.Collections#sort(java.util.List,
* java.util.Comparator) sort} a list so that more specific patterns (without uri templates or wild cards) come before
- * generic patterns. So given a list with the following patterns:
- *
/hotels/new/hotels/{hotel}/hotels/*/hotels/new/hotels/{hotel}/hotels/*For example: For pattern "/hotels/{hotel}" and path "/hotels/1", this method will * return a map containing "hotel"->"1". * @@ -106,7 +105,6 @@ public interface PathMatcher { /** * Given a full path, returns a {@link Comparator} suitable for sorting patterns in order of explicitness. - * *
The full algorithm used depends on the underlying implementation, but generally, the returned
* The full algorithm used for combining the two pattern depends on the underlying implementation.
+ *
+ * @param pattern1 the first pattern
+ * @param pattern2 the second pattern
+ * @return the combination of the two patterns
+ * @throws IllegalArgumentException when the two patterns cannot be combined
+ */
+ String combine(String pattern1, String pattern2);
}
diff --git a/org.springframework.core/src/test/java/org/springframework/util/AntPathMatcherTests.java b/org.springframework.core/src/test/java/org/springframework/util/AntPathMatcherTests.java
index 1491299ec65..72bfbeed5ad 100644
--- a/org.springframework.core/src/test/java/org/springframework/util/AntPathMatcherTests.java
+++ b/org.springframework.core/src/test/java/org/springframework/util/AntPathMatcherTests.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2007 the original author or authors.
+ * Copyright 2002-2009 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.
@@ -334,6 +334,31 @@ public class AntPathMatcherTests {
assertEquals(expected, result);
}
+ @Test
+ public void combine() {
+ assertEquals("", pathMatcher.combine(null, null));
+ assertEquals("/hotels", pathMatcher.combine("/hotels", null));
+ assertEquals("/hotels", pathMatcher.combine(null, "/hotels"));
+ assertEquals("/hotels/booking", pathMatcher.combine("/hotels/*", "booking"));
+ assertEquals("/hotels/booking", pathMatcher.combine("/hotels/*", "/booking"));
+ assertEquals("/hotels/**/booking", pathMatcher.combine("/hotels/**", "booking"));
+ assertEquals("/hotels/**/booking", pathMatcher.combine("/hotels/**", "/booking"));
+ assertEquals("/hotels/booking", pathMatcher.combine("/hotels", "/booking"));
+ assertEquals("/hotels/booking", pathMatcher.combine("/hotels", "booking"));
+ assertEquals("/hotels/{hotel}", pathMatcher.combine("/hotels/*", "{hotel}"));
+ assertEquals("/hotels/**/{hotel}", pathMatcher.combine("/hotels/**", "{hotel}"));
+ assertEquals("/hotels/{hotel}", pathMatcher.combine("/hotels", "{hotel}"));
+ assertEquals("/hotels/*/booking/{booking}", pathMatcher.combine("/hotels/*/booking", "{booking}"));
+ assertEquals("/hotel.html", pathMatcher.combine("/*.html", "/hotel.html"));
+ try {
+ assertEquals("/hotel.html", pathMatcher.combine("/*.html", "/hotel"));
+ fail("IllegalArgumentException expected");
+ }
+ catch (IllegalArgumentException ex) {
+ // expected
+ }
+ }
+
@Test
public void patternComparator() {
ComparatorComparator will {@linkplain java.util.Collections#sort(java.util.List, java.util.Comparator) sort} a
* list so that more specific patterns come before generic patterns.
@@ -115,4 +113,15 @@ public interface PathMatcher {
* @return a comparator capable of sorting patterns in order of explicitness
*/
Comparator