|
16 | 16 |
|
17 | 17 | package org.springframework.security.messaging.util.matcher;
|
18 | 18 |
|
| 19 | +import java.util.Collections; |
| 20 | +import java.util.Map; |
| 21 | + |
19 | 22 | import org.springframework.messaging.Message;
|
20 | 23 |
|
21 | 24 | /**
|
@@ -50,4 +53,77 @@ public String toString() {
|
50 | 53 | */
|
51 | 54 | boolean matches(Message<? extends T> message);
|
52 | 55 |
|
| 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 | + |
53 | 129 | }
|
0 commit comments