Skip to content

Commit 67881a5

Browse files
committed
Polish PathPattern parser (including package change to web.util.pattern)
Issue: SPR-15531
1 parent eaac348 commit 67881a5

35 files changed

+958
-1006
lines changed

spring-web/src/main/java/org/springframework/web/cors/reactive/UrlBasedCorsConfigurationSource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,7 +25,7 @@
2525
import org.springframework.web.cors.CorsConfiguration;
2626
import org.springframework.web.server.ServerWebExchange;
2727
import org.springframework.web.server.support.HttpRequestPathHelper;
28-
import org.springframework.web.util.ParsingPathMatcher;
28+
import org.springframework.web.util.pattern.ParsingPathMatcher;
2929

3030
/**
3131
* Provide a per reactive request {@link CorsConfiguration} instance based on a

spring-web/src/main/java/org/springframework/web/util/patterns/CaptureTheRestPathElement.java renamed to spring-web/src/main/java/org/springframework/web/util/pattern/CaptureTheRestPathElement.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,21 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.web.util.patterns;
17+
package org.springframework.web.util.pattern;
1818

19-
import org.springframework.web.util.patterns.PathPattern.MatchingContext;
19+
import org.springframework.web.util.pattern.PathPattern.MatchingContext;
2020

2121
/**
2222
* A path element representing capturing the rest of a path. In the pattern
2323
* '/foo/{*foobar}' the /{*foobar} is represented as a {@link CaptureTheRestPathElement}.
2424
*
2525
* @author Andy Clement
26+
* @since 5.0
2627
*/
2728
class CaptureTheRestPathElement extends PathElement {
2829

29-
private String variableName;
30+
private final String variableName;
31+
3032

3133
/**
3234
* @param pos position of the path element within the path pattern text
@@ -35,9 +37,10 @@ class CaptureTheRestPathElement extends PathElement {
3537
*/
3638
CaptureTheRestPathElement(int pos, char[] captureDescriptor, char separator) {
3739
super(pos, separator);
38-
variableName = new String(captureDescriptor, 2, captureDescriptor.length - 3);
40+
this.variableName = new String(captureDescriptor, 2, captureDescriptor.length - 3);
3941
}
4042

43+
4144
@Override
4245
public boolean matches(int candidateIndex, MatchingContext matchingContext) {
4346
// No need to handle 'match start' checking as this captures everything
@@ -59,10 +62,6 @@ public boolean matches(int candidateIndex, MatchingContext matchingContext) {
5962
return true;
6063
}
6164

62-
public String toString() {
63-
return "CaptureTheRest(/{*" + variableName + "})";
64-
}
65-
6665
@Override
6766
public int getNormalizedLength() {
6867
return 1;
@@ -77,4 +76,10 @@ public int getWildcardCount() {
7776
public int getCaptureCount() {
7877
return 1;
7978
}
80-
}
79+
80+
81+
public String toString() {
82+
return "CaptureTheRest(/{*" + this.variableName + "})";
83+
}
84+
85+
}

spring-web/src/main/java/org/springframework/web/util/patterns/CaptureVariablePathElement.java renamed to spring-web/src/main/java/org/springframework/web/util/pattern/CaptureVariablePathElement.java

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,25 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.web.util.patterns;
17+
package org.springframework.web.util.pattern;
1818

1919
import java.util.regex.Matcher;
20-
21-
import org.springframework.web.util.patterns.PathPattern.MatchingContext;
20+
import java.util.regex.Pattern;
2221

2322
/**
2423
* A path element representing capturing a piece of the path as a variable. In the pattern
2524
* '/foo/{bar}/goo' the {bar} is represented as a {@link CaptureVariablePathElement}. There
2625
* must be at least one character to bind to the variable.
2726
*
2827
* @author Andy Clement
28+
* @since 5.0
2929
*/
3030
class CaptureVariablePathElement extends PathElement {
3131

32-
private String variableName;
32+
private final String variableName;
33+
34+
private Pattern constraintPattern;
3335

34-
private java.util.regex.Pattern constraintPattern;
3536

3637
/**
3738
* @param pos the position in the pattern of this capture element
@@ -48,43 +49,47 @@ class CaptureVariablePathElement extends PathElement {
4849
}
4950
if (colon == -1) {
5051
// no constraint
51-
variableName = new String(captureDescriptor, 1, captureDescriptor.length - 2);
52+
this.variableName = new String(captureDescriptor, 1, captureDescriptor.length - 2);
5253
}
5354
else {
54-
variableName = new String(captureDescriptor, 1, colon - 1);
55+
this.variableName = new String(captureDescriptor, 1, colon - 1);
5556
if (caseSensitive) {
56-
constraintPattern = java.util.regex.Pattern
57-
.compile(new String(captureDescriptor, colon + 1, captureDescriptor.length - colon - 2));
57+
this.constraintPattern = Pattern.compile(
58+
new String(captureDescriptor, colon + 1, captureDescriptor.length - colon - 2));
5859
}
5960
else {
60-
constraintPattern = java.util.regex.Pattern.compile(
61+
this.constraintPattern = Pattern.compile(
6162
new String(captureDescriptor, colon + 1, captureDescriptor.length - colon - 2),
62-
java.util.regex.Pattern.CASE_INSENSITIVE);
63+
Pattern.CASE_INSENSITIVE);
6364
}
6465
}
6566
}
6667

68+
6769
@Override
68-
public boolean matches(int candidateIndex, MatchingContext matchingContext) {
70+
public boolean matches(int candidateIndex, PathPattern.MatchingContext matchingContext) {
6971
int nextPos = matchingContext.scanAhead(candidateIndex);
7072
// There must be at least one character to capture:
7173
if (nextPos == candidateIndex) {
7274
return false;
7375
}
76+
7477
CharSequence candidateCapture = null;
75-
if (constraintPattern != null) {
78+
if (this.constraintPattern != null) {
7679
// TODO possible optimization - only regex match if rest of pattern matches? Benefit likely to vary pattern to pattern
7780
candidateCapture = new SubSequence(matchingContext.candidate, candidateIndex, nextPos);
78-
Matcher m = constraintPattern.matcher(candidateCapture);
79-
if (m.groupCount() != 0) {
80-
throw new IllegalArgumentException("No capture groups allowed in the constraint regex: " + constraintPattern.pattern());
81+
Matcher matcher = constraintPattern.matcher(candidateCapture);
82+
if (matcher.groupCount() != 0) {
83+
throw new IllegalArgumentException(
84+
"No capture groups allowed in the constraint regex: " + this.constraintPattern.pattern());
8185
}
82-
if (!m.matches()) {
86+
if (!matcher.matches()) {
8387
return false;
8488
}
8589
}
90+
8691
boolean match = false;
87-
if (next == null) {
92+
if (this.next == null) {
8893
if (matchingContext.determineRemainingPath && nextPos > candidateIndex) {
8994
matchingContext.remainingPathIndex = nextPos;
9095
match = true;
@@ -101,14 +106,16 @@ public boolean matches(int candidateIndex, MatchingContext matchingContext) {
101106
}
102107
else {
103108
if (matchingContext.isMatchStartMatching && nextPos == matchingContext.candidateLength) {
104-
match = true; // no more data but matches up to this point
109+
match = true; // no more data but matches up to this point
105110
}
106111
else {
107-
match = next.matches(nextPos, matchingContext);
112+
match = this.next.matches(nextPos, matchingContext);
108113
}
109114
}
115+
110116
if (match && matchingContext.extractingVariables) {
111-
matchingContext.set(variableName, new String(matchingContext.candidate, candidateIndex, nextPos - candidateIndex));
117+
matchingContext.set(this.variableName,
118+
new String(matchingContext.candidate, candidateIndex, nextPos - candidateIndex));
112119
}
113120
return match;
114121
}
@@ -117,10 +124,6 @@ public String getVariableName() {
117124
return this.variableName;
118125
}
119126

120-
public String toString() {
121-
return "CaptureVariable({" + variableName + (constraintPattern == null ? "" : ":" + constraintPattern.pattern()) + "})";
122-
}
123-
124127
@Override
125128
public int getNormalizedLength() {
126129
return 1;
@@ -140,4 +143,11 @@ public int getCaptureCount() {
140143
public int getScore() {
141144
return CAPTURE_VARIABLE_WEIGHT;
142145
}
143-
}
146+
147+
148+
public String toString() {
149+
return "CaptureVariable({" + this.variableName +
150+
(this.constraintPattern != null ? ":" + this.constraintPattern.pattern() : "") + "})";
151+
}
152+
153+
}

0 commit comments

Comments
 (0)