|
18 | 18 |
|
19 | 19 | import java.util.ArrayList;
|
20 | 20 | import java.util.List;
|
| 21 | +import java.util.Map; |
21 | 22 | import java.util.function.Supplier;
|
22 | 23 |
|
23 | 24 | import org.apache.commons.logging.Log;
|
|
37 | 38 | import org.springframework.util.AntPathMatcher;
|
38 | 39 | import org.springframework.util.Assert;
|
39 | 40 | import org.springframework.util.PathMatcher;
|
| 41 | +import org.springframework.util.function.SingletonSupplier; |
40 | 42 |
|
41 | 43 | public final class MessageMatcherDelegatingAuthorizationManager implements AuthorizationManager<Message<?>> {
|
42 | 44 |
|
@@ -87,6 +89,10 @@ private MessageAuthorizationContext<?> authorizationContext(MessageMatcher<?> ma
|
87 | 89 | SimpDestinationMessageMatcher simp = (SimpDestinationMessageMatcher) matcher;
|
88 | 90 | return new MessageAuthorizationContext<>(message, simp.extractPathVariables(message));
|
89 | 91 | }
|
| 92 | + if (matcher instanceof Builder.LazySimpDestinationMessageMatcher) { |
| 93 | + Builder.LazySimpDestinationMessageMatcher path = (Builder.LazySimpDestinationMessageMatcher) matcher; |
| 94 | + return new MessageAuthorizationContext<>(message, path.extractPathVariables(message)); |
| 95 | + } |
90 | 96 | return new MessageAuthorizationContext<>(message);
|
91 | 97 | }
|
92 | 98 |
|
@@ -192,8 +198,7 @@ public Builder.Constraint simpSubscribeDestMatchers(String... patterns) {
|
192 | 198 | private Builder.Constraint simpDestMatchers(SimpMessageType type, String... patterns) {
|
193 | 199 | List<MessageMatcher<?>> matchers = new ArrayList<>(patterns.length);
|
194 | 200 | for (String pattern : patterns) {
|
195 |
| - Supplier<MessageMatcher<Object>> supplier = new Builder.PathMatcherMessageMatcherBuilder(pattern, type); |
196 |
| - MessageMatcher<?> matcher = new Builder.SupplierMessageMatcher(supplier); |
| 201 | + MessageMatcher<Object> matcher = new LazySimpDestinationMessageMatcher(pattern, type); |
197 | 202 | matchers.add(matcher);
|
198 | 203 | }
|
199 | 204 | return new Builder.Constraint(matchers);
|
@@ -375,58 +380,33 @@ public Builder access(AuthorizationManager<MessageAuthorizationContext<?>> autho
|
375 | 380 |
|
376 | 381 | }
|
377 | 382 |
|
378 |
| - private static final class SupplierMessageMatcher implements MessageMatcher<Object> { |
379 |
| - |
380 |
| - private final Supplier<MessageMatcher<Object>> supplier; |
| 383 | + private final class LazySimpDestinationMessageMatcher implements MessageMatcher<Object> { |
381 | 384 |
|
382 |
| - private volatile MessageMatcher<Object> delegate; |
| 385 | + private final Supplier<SimpDestinationMessageMatcher> delegate; |
383 | 386 |
|
384 |
| - SupplierMessageMatcher(Supplier<MessageMatcher<Object>> supplier) { |
385 |
| - this.supplier = supplier; |
| 387 | + private LazySimpDestinationMessageMatcher(String pattern, SimpMessageType type) { |
| 388 | + this.delegate = SingletonSupplier.of(() -> { |
| 389 | + PathMatcher pathMatcher = Builder.this.pathMatcher.get(); |
| 390 | + if (type == null) { |
| 391 | + return new SimpDestinationMessageMatcher(pattern, pathMatcher); |
| 392 | + } |
| 393 | + if (SimpMessageType.MESSAGE == type) { |
| 394 | + return SimpDestinationMessageMatcher.createMessageMatcher(pattern, pathMatcher); |
| 395 | + } |
| 396 | + if (SimpMessageType.SUBSCRIBE == type) { |
| 397 | + return SimpDestinationMessageMatcher.createSubscribeMatcher(pattern, pathMatcher); |
| 398 | + } |
| 399 | + throw new IllegalStateException(type + " is not supported since it does not have a destination"); |
| 400 | + }); |
386 | 401 | }
|
387 | 402 |
|
388 | 403 | @Override
|
389 | 404 | public boolean matches(Message<?> message) {
|
390 |
| - if (this.delegate == null) { |
391 |
| - synchronized (this.supplier) { |
392 |
| - if (this.delegate == null) { |
393 |
| - this.delegate = this.supplier.get(); |
394 |
| - } |
395 |
| - } |
396 |
| - } |
397 |
| - return this.delegate.matches(message); |
398 |
| - } |
399 |
| - |
400 |
| - } |
401 |
| - |
402 |
| - private final class PathMatcherMessageMatcherBuilder implements Supplier<MessageMatcher<Object>> { |
403 |
| - |
404 |
| - private final String pattern; |
405 |
| - |
406 |
| - private final SimpMessageType type; |
407 |
| - |
408 |
| - private PathMatcherMessageMatcherBuilder(String pattern, SimpMessageType type) { |
409 |
| - this.pattern = pattern; |
410 |
| - this.type = type; |
411 |
| - } |
412 |
| - |
413 |
| - private PathMatcher resolvePathMatcher() { |
414 |
| - return Builder.this.pathMatcher.get(); |
| 405 | + return this.delegate.get().matches(message); |
415 | 406 | }
|
416 | 407 |
|
417 |
| - @Override |
418 |
| - public MessageMatcher<Object> get() { |
419 |
| - PathMatcher pathMatcher = resolvePathMatcher(); |
420 |
| - if (this.type == null) { |
421 |
| - return new SimpDestinationMessageMatcher(this.pattern, pathMatcher); |
422 |
| - } |
423 |
| - if (SimpMessageType.MESSAGE == this.type) { |
424 |
| - return SimpDestinationMessageMatcher.createMessageMatcher(this.pattern, pathMatcher); |
425 |
| - } |
426 |
| - if (SimpMessageType.SUBSCRIBE == this.type) { |
427 |
| - return SimpDestinationMessageMatcher.createSubscribeMatcher(this.pattern, pathMatcher); |
428 |
| - } |
429 |
| - throw new IllegalStateException(this.type + " is not supported since it does not have a destination"); |
| 408 | + Map<String, String> extractPathVariables(Message<?> message) { |
| 409 | + return this.delegate.get().extractPathVariables(message); |
430 | 410 | }
|
431 | 411 |
|
432 | 412 | }
|
|
0 commit comments