@@ -79,6 +79,8 @@ public class AntPathMatcher implements PathMatcher {
79
79
80
80
private PathSeparatorPatternCache pathSeparatorPatternCache ;
81
81
82
+ private boolean caseSensitive = true ;
83
+
82
84
private boolean trimTokens = true ;
83
85
84
86
private volatile Boolean cachePatterns ;
@@ -117,6 +119,15 @@ public void setPathSeparator(String pathSeparator) {
117
119
this .pathSeparatorPatternCache = new PathSeparatorPatternCache (this .pathSeparator );
118
120
}
119
121
122
+ /**
123
+ * Specify whether to perform pattern matching in a case-sensitive fashion.
124
+ * <p>Default is {@code true}. Switch this to {@code false} for case-insensitive matching.
125
+ * @since 4.2
126
+ */
127
+ public void setCaseSensitive (boolean caseSensitive ) {
128
+ this .caseSensitive = caseSensitive ;
129
+ }
130
+
120
131
/**
121
132
* Specify whether to trim tokenized paths and patterns.
122
133
* <p>Default is {@code true}.
@@ -134,6 +145,7 @@ public void setTrimTokens(boolean trimTokens) {
134
145
* turn it off when encountering too many patterns to cache at runtime
135
146
* (the threshold is 65536), assuming that arbitrary permutations of patterns
136
147
* are coming in, with little chance for encountering a recurring pattern.
148
+ * @since 4.0.1
137
149
* @see #getStringMatcher(String)
138
150
*/
139
151
public void setCachePatterns (boolean cachePatterns ) {
@@ -363,7 +375,7 @@ protected AntPathStringMatcher getStringMatcher(String pattern) {
363
375
matcher = this .stringMatcherCache .get (pattern );
364
376
}
365
377
if (matcher == null ) {
366
- matcher = new AntPathStringMatcher (pattern );
378
+ matcher = new AntPathStringMatcher (pattern , this . caseSensitive );
367
379
if (cachePatterns == null && this .stringMatcherCache .size () >= CACHE_TURNOFF_THRESHOLD ) {
368
380
// Try to adapt to the runtime situation that we're encountering:
369
381
// There are obviously too many different patterns coming in here...
@@ -418,7 +430,9 @@ public String extractPathWithinPattern(String pattern, String path) {
418
430
public Map <String , String > extractUriTemplateVariables (String pattern , String path ) {
419
431
Map <String , String > variables = new LinkedHashMap <String , String >();
420
432
boolean result = doMatch (pattern , path , true , variables );
421
- Assert .state (result , "Pattern \" " + pattern + "\" is not a match for \" " + path + "\" " );
433
+ if (!result ) {
434
+ throw new IllegalStateException ("Pattern \" " + pattern + "\" is not a match for \" " + path + "\" " );
435
+ }
422
436
return variables ;
423
437
}
424
438
@@ -553,12 +567,16 @@ protected static class AntPathStringMatcher {
553
567
private final List <String > variableNames = new LinkedList <String >();
554
568
555
569
public AntPathStringMatcher (String pattern ) {
570
+ this (pattern , true );
571
+ }
572
+
573
+ public AntPathStringMatcher (String pattern , boolean caseSensitive ) {
556
574
StringBuilder patternBuilder = new StringBuilder ();
557
- Matcher m = GLOB_PATTERN .matcher (pattern );
575
+ Matcher matcher = GLOB_PATTERN .matcher (pattern );
558
576
int end = 0 ;
559
- while (m .find ()) {
560
- patternBuilder .append (quote (pattern , end , m .start ()));
561
- String match = m .group ();
577
+ while (matcher .find ()) {
578
+ patternBuilder .append (quote (pattern , end , matcher .start ()));
579
+ String match = matcher .group ();
562
580
if ("?" .equals (match )) {
563
581
patternBuilder .append ('.' );
564
582
}
@@ -569,7 +587,7 @@ else if (match.startsWith("{") && match.endsWith("}")) {
569
587
int colonIdx = match .indexOf (':' );
570
588
if (colonIdx == -1 ) {
571
589
patternBuilder .append (DEFAULT_VARIABLE_PATTERN );
572
- this .variableNames .add (m .group (1 ));
590
+ this .variableNames .add (matcher .group (1 ));
573
591
}
574
592
else {
575
593
String variablePattern = match .substring (colonIdx + 1 , match .length () - 1 );
@@ -580,10 +598,11 @@ else if (match.startsWith("{") && match.endsWith("}")) {
580
598
this .variableNames .add (variableName );
581
599
}
582
600
}
583
- end = m .end ();
601
+ end = matcher .end ();
584
602
}
585
603
patternBuilder .append (quote (pattern , end , pattern .length ()));
586
- this .pattern = Pattern .compile (patternBuilder .toString ());
604
+ this .pattern = (caseSensitive ? Pattern .compile (patternBuilder .toString ()) :
605
+ Pattern .compile (patternBuilder .toString (), Pattern .CASE_INSENSITIVE ));
587
606
}
588
607
589
608
private String quote (String s , int start , int end ) {
@@ -602,10 +621,12 @@ public boolean matchStrings(String str, Map<String, String> uriTemplateVariables
602
621
if (matcher .matches ()) {
603
622
if (uriTemplateVariables != null ) {
604
623
// SPR-8455
605
- Assert .isTrue (this .variableNames .size () == matcher .groupCount (),
606
- "The number of capturing groups in the pattern segment " + this .pattern +
607
- " does not match the number of URI template variables it defines, which can occur if " +
608
- " capturing groups are used in a URI template regex. Use non-capturing groups instead." );
624
+ if (this .variableNames .size () != matcher .groupCount ()) {
625
+ throw new IllegalArgumentException ("The number of capturing groups in the pattern segment " +
626
+ this .pattern + " does not match the number of URI template variables it defines, " +
627
+ "which can occur if capturing groups are used in a URI template regex. " +
628
+ "Use non-capturing groups instead." );
629
+ }
609
630
for (int i = 1 ; i <= matcher .groupCount (); i ++) {
610
631
String name = this .variableNames .get (i - 1 );
611
632
String value = matcher .group (i );
0 commit comments