Skip to content

Commit f17a969

Browse files
committed
Add the MatchResult class to MessageMatcher
Signed-off-by: Pat McCusker <patmccusker14@gmail.com>
1 parent 29f1ea5 commit f17a969

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

messaging/src/main/java/org/springframework/security/messaging/util/matcher/MessageMatcher.java

+76
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package org.springframework.security.messaging.util.matcher;
1818

19+
import java.util.Collections;
20+
import java.util.Map;
21+
1922
import org.springframework.messaging.Message;
2023

2124
/**
@@ -50,4 +53,77 @@ public String toString() {
5053
*/
5154
boolean matches(Message<? extends T> message);
5255

56+
/**
57+
* Returns a {@link MatchResult} for this {@code MessageMatcher}. The default
58+
* implementation returns {@link Collections#emptyMap()} when
59+
* {@link MatchResult#getVariables()} is invoked.
60+
* @return the {@code MatchResult} from comparing this {@code MessageMatcher} against
61+
* the {@code Message}
62+
* @since 6.5
63+
*/
64+
default MatchResult matcher(Message<? extends T> message) {
65+
boolean match = matches(message);
66+
return new MatchResult(match, Collections.emptyMap());
67+
}
68+
69+
/**
70+
* The result of matching against a {@code Message} contains the status, true or
71+
* false, of the match and if present, any variables extracted from the match
72+
*
73+
* @since 6.5
74+
*/
75+
class MatchResult {
76+
77+
private final boolean match;
78+
79+
private final Map<String, String> variables;
80+
81+
MatchResult(boolean match, Map<String, String> variables) {
82+
this.match = match;
83+
this.variables = variables;
84+
}
85+
86+
/**
87+
* Return whether the comparison against the {@code Message} produced a successful
88+
* match
89+
*/
90+
public boolean isMatch() {
91+
return this.match;
92+
}
93+
94+
/**
95+
* Returns the extracted variable values where the key is the variable name and
96+
* the value is the variable value
97+
* @return a map containing key-value pairs representing extracted variable names
98+
* and variable values
99+
*/
100+
public Map<String, String> getVariables() {
101+
return this.variables;
102+
}
103+
104+
/**
105+
* Creates an instance of {@link MatchResult} that is a match with no variables
106+
*/
107+
public static MatchResult match() {
108+
return new MatchResult(true, Collections.emptyMap());
109+
}
110+
111+
/**
112+
* Creates an instance of {@link MatchResult} that is a match with the specified
113+
* variables
114+
*/
115+
public static MatchResult match(Map<String, String> variables) {
116+
return new MatchResult(true, variables);
117+
}
118+
119+
/**
120+
* Creates an instance of {@link MatchResult} that is not a match.
121+
* @return a {@code MatchResult} with match set to false
122+
*/
123+
public static MatchResult notMatch() {
124+
return new MatchResult(false, Collections.emptyMap());
125+
}
126+
127+
}
128+
53129
}

0 commit comments

Comments
 (0)