@@ -419,12 +419,10 @@ public Map<String, String> extractUriTemplateVariables(String pattern, String pa
419
419
420
420
/**
421
421
* Combine two patterns into a new pattern.
422
- *
423
422
* <p>This implementation simply concatenates the two patterns, unless
424
423
* the first pattern contains a file extension match (e.g., {@code *.html}).
425
424
* In that case, the second pattern will be merged into the first. Otherwise,
426
425
* an {@code IllegalArgumentException} will be thrown.
427
- *
428
426
* <h3>Examples</h3>
429
427
* <table border="1">
430
428
* <tr><th>Pattern 1</th><th>Pattern 2</th><th>Result</th></tr>
@@ -442,7 +440,6 @@ public Map<String, String> extractUriTemplateVariables(String pattern, String pa
442
440
* <tr><td>/*.html</td><td>/hotels</td><td>/hotels.html</td></tr>
443
441
* <tr><td>/*.html</td><td>/*.txt</td><td>{@code IllegalArgumentException}</td></tr>
444
442
* </table>
445
- *
446
443
* @param pattern1 the first pattern
447
444
* @param pattern2 the second pattern
448
445
* @return the combination of the two patterns
@@ -460,7 +457,7 @@ public String combine(String pattern1, String pattern2) {
460
457
return pattern1 ;
461
458
}
462
459
463
- boolean pattern1ContainsUriVar = pattern1 .indexOf ('{' ) != -1 ;
460
+ boolean pattern1ContainsUriVar = ( pattern1 .indexOf ('{' ) != -1 ) ;
464
461
if (!pattern1 .equals (pattern2 ) && !pattern1ContainsUriVar && match (pattern1 , pattern2 )) {
465
462
// /* + /hotel -> /hotel ; "/*.*" + "/*.html" -> /*.html
466
463
// However /user + /user -> /usr/user ; /{foo} + /bar -> /{foo}/bar
@@ -484,12 +481,18 @@ public String combine(String pattern1, String pattern2) {
484
481
// simply concatenate the two patterns
485
482
return concat (pattern1 , pattern2 );
486
483
}
487
- String extension1 = pattern1 .substring (starDotPos1 + 1 );
484
+
485
+ String ext1 = pattern1 .substring (starDotPos1 + 1 );
488
486
int dotPos2 = pattern2 .indexOf ('.' );
489
- String fileName2 = (dotPos2 == -1 ? pattern2 : pattern2 .substring (0 , dotPos2 ));
490
- String extension2 = (dotPos2 == -1 ? "" : pattern2 .substring (dotPos2 ));
491
- String extension = extension1 .startsWith ("*" ) ? extension2 : extension1 ;
492
- return fileName2 + extension ;
487
+ String file2 = (dotPos2 == -1 ? pattern2 : pattern2 .substring (0 , dotPos2 ));
488
+ String ext2 = (dotPos2 == -1 ? "" : pattern2 .substring (dotPos2 ));
489
+ boolean ext1All = (ext1 .equals (".*" ) || ext1 .equals ("" ));
490
+ boolean ext2All = (ext2 .equals (".*" ) || ext2 .equals ("" ));
491
+ if (!ext1All && !ext2All ) {
492
+ throw new IllegalArgumentException ("Cannot combine patterns: " + pattern1 + " vs " + pattern2 );
493
+ }
494
+ String ext = (ext1All ? ext2 : ext1 );
495
+ return file2 + ext ;
493
496
}
494
497
495
498
private String concat (String path1 , String path2 ) {
@@ -508,14 +511,18 @@ else if (path1EndsWithSeparator || path2StartsWithSeparator) {
508
511
}
509
512
510
513
/**
511
- * Given a full path, returns a {@link Comparator} suitable for sorting patterns in order of explicitness.
512
- * <p>The returned {@code Comparator} will {@linkplain java.util.Collections#sort(java.util.List,
513
- * java.util.Comparator) sort} a list so that more specific patterns (without uri templates or wild cards) come before
514
- * generic patterns. So given a list with the following patterns: <ol> <li>{@code /hotels/new}</li>
515
- * <li>{@code /hotels/{hotel}}</li> <li>{@code /hotels/*}</li> </ol> the returned comparator will sort this
516
- * list so that the order will be as indicated.
517
- * <p>The full path given as parameter is used to test for exact matches. So when the given path is {@code /hotels/2},
518
- * the pattern {@code /hotels/2} will be sorted before {@code /hotels/1}.
514
+ * Given a full path, returns a {@link Comparator} suitable for sorting patterns in order of
515
+ * explicitness.
516
+ * <p>This{@code Comparator} will {@linkplain java.util.Collections#sort(List, Comparator) sort}
517
+ * a list so that more specific patterns (without uri templates or wild cards) come before
518
+ * generic patterns. So given a list with the following patterns:
519
+ * <ol>
520
+ * <li>{@code /hotels/new}</li>
521
+ * <li>{@code /hotels/{hotel}}</li> <li>{@code /hotels/*}</li>
522
+ * </ol>
523
+ * the returned comparator will sort this list so that the order will be as indicated.
524
+ * <p>The full path given as parameter is used to test for exact matches. So when the given path
525
+ * is {@code /hotels/2}, the pattern {@code /hotels/2} will be sorted before {@code /hotels/1}.
519
526
* @param path the full path to use for comparison
520
527
* @return a comparator capable of sorting patterns in order of explicitness
521
528
*/
0 commit comments