Skip to content

Commit e8417ea

Browse files
committed
SimpAnnotationMethodMessageHandler skips template variable check in case of no pattern
Issue: SPR-13704
1 parent 9d9433a commit e8417ea

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandler.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@
8686
public class SimpAnnotationMethodMessageHandler extends AbstractMethodMessageHandler<SimpMessageMappingInfo>
8787
implements EmbeddedValueResolverAware, SmartLifecycle {
8888

89-
private static final boolean completableFuturePresent = ClassUtils.isPresent("java.util.concurrent.CompletableFuture",
90-
SimpAnnotationMethodMessageHandler.class.getClassLoader());
89+
private static final boolean completableFuturePresent = ClassUtils.isPresent(
90+
"java.util.concurrent.CompletableFuture", SimpAnnotationMethodMessageHandler.class.getClassLoader());
9191

9292

9393
private final SubscribableChannel clientInboundChannel;
@@ -304,9 +304,8 @@ public final void stop(Runnable callback) {
304304

305305

306306
protected List<HandlerMethodArgumentResolver> initArgumentResolvers() {
307-
ConfigurableBeanFactory beanFactory =
308-
(ClassUtils.isAssignableValue(ConfigurableApplicationContext.class, getApplicationContext())) ?
309-
((ConfigurableApplicationContext) getApplicationContext()).getBeanFactory() : null;
307+
ConfigurableBeanFactory beanFactory = (getApplicationContext() instanceof ConfigurableApplicationContext ?
308+
((ConfigurableApplicationContext) getApplicationContext()).getBeanFactory() : null);
310309

311310
List<HandlerMethodArgumentResolver> resolvers = new ArrayList<HandlerMethodArgumentResolver>();
312311

@@ -327,7 +326,6 @@ protected List<HandlerMethodArgumentResolver> initArgumentResolvers() {
327326

328327
@Override
329328
protected List<? extends HandlerMethodReturnValueHandler> initReturnValueHandlers() {
330-
331329
List<HandlerMethodReturnValueHandler> handlers = new ArrayList<HandlerMethodReturnValueHandler>();
332330

333331
// Single-purpose return value types
@@ -337,11 +335,13 @@ protected List<? extends HandlerMethodReturnValueHandler> initReturnValueHandler
337335
}
338336

339337
// Annotation-based return value types
340-
SendToMethodReturnValueHandler sth = new SendToMethodReturnValueHandler(this.brokerTemplate, true);
338+
SendToMethodReturnValueHandler sth =
339+
new SendToMethodReturnValueHandler(this.brokerTemplate, true);
341340
sth.setHeaderInitializer(this.headerInitializer);
342341
handlers.add(sth);
343342

344-
SubscriptionMethodReturnValueHandler sh = new SubscriptionMethodReturnValueHandler(this.clientMessagingTemplate);
343+
SubscriptionMethodReturnValueHandler sh =
344+
new SubscriptionMethodReturnValueHandler(this.clientMessagingTemplate);
345345
sh.setHeaderInitializer(this.headerInitializer);
346346
handlers.add(sh);
347347

@@ -468,13 +468,15 @@ public int compare(SimpMessageMappingInfo info1, SimpMessageMappingInfo info2) {
468468
protected void handleMatch(SimpMessageMappingInfo mapping, HandlerMethod handlerMethod,
469469
String lookupDestination, Message<?> message) {
470470

471-
String matchedPattern = mapping.getDestinationConditions().getPatterns().iterator().next();
472-
Map<String, String> vars = getPathMatcher().extractUriTemplateVariables(matchedPattern, lookupDestination);
473-
474-
if (!CollectionUtils.isEmpty(vars)) {
475-
MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
476-
Assert.state(accessor != null && accessor.isMutable());
477-
accessor.setHeader(DestinationVariableMethodArgumentResolver.DESTINATION_TEMPLATE_VARIABLES_HEADER, vars);
471+
Set<String> patterns = mapping.getDestinationConditions().getPatterns();
472+
if (!CollectionUtils.isEmpty(patterns)) {
473+
String pattern = patterns.iterator().next();
474+
Map<String, String> vars = getPathMatcher().extractUriTemplateVariables(pattern, lookupDestination);
475+
if (!CollectionUtils.isEmpty(vars)) {
476+
MessageHeaderAccessor mha = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
477+
Assert.state(mha != null && mha.isMutable());
478+
mha.setHeader(DestinationVariableMethodArgumentResolver.DESTINATION_TEMPLATE_VARIABLES_HEADER, vars);
479+
}
478480
}
479481

480482
try {

spring-messaging/src/main/java/org/springframework/messaging/support/MessageHeaderAccessor.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,8 @@ public void setHeader(String name, Object value) {
319319

320320
protected void verifyType(String headerName, Object headerValue) {
321321
if (headerName != null && headerValue != null) {
322-
if (MessageHeaders.ERROR_CHANNEL.equals(headerName) || MessageHeaders.REPLY_CHANNEL.endsWith(headerName)) {
322+
if (MessageHeaders.ERROR_CHANNEL.equals(headerName) ||
323+
MessageHeaders.REPLY_CHANNEL.endsWith(headerName)) {
323324
if (!(headerValue instanceof MessageChannel || headerValue instanceof String)) {
324325
throw new IllegalArgumentException(
325326
"'" + headerName + "' header value must be a MessageChannel or String");
@@ -572,11 +573,13 @@ public static <T extends MessageHeaderAccessor> T getAccessor(Message<?> message
572573
* A variation of {@link #getAccessor(org.springframework.messaging.Message, Class)}
573574
* with a {@code MessageHeaders} instance instead of a {@code Message}.
574575
* <p>This is for cases when a full message may not have been created yet.
575-
* @return an accessor instance of the specified typem or {@code null} if none
576+
* @return an accessor instance of the specified type, or {@code null} if none
576577
* @since 4.1
577578
*/
578579
@SuppressWarnings("unchecked")
579-
public static <T extends MessageHeaderAccessor> T getAccessor(MessageHeaders messageHeaders, Class<T> requiredType) {
580+
public static <T extends MessageHeaderAccessor> T getAccessor(
581+
MessageHeaders messageHeaders, Class<T> requiredType) {
582+
580583
if (messageHeaders instanceof MutableMessageHeaders) {
581584
MutableMessageHeaders mutableHeaders = (MutableMessageHeaders) messageHeaders;
582585
MessageHeaderAccessor headerAccessor = mutableHeaders.getMessageHeaderAccessor();
@@ -593,7 +596,7 @@ public static <T extends MessageHeaderAccessor> T getAccessor(MessageHeaders mes
593596
* wrapping the message with a {@code MessageHeaderAccessor} instance.
594597
* <p>This is for cases where a header needs to be updated in generic code
595598
* while preserving the accessor type for downstream processing.
596-
* @return an accessor of the required type, never {@code null}.
599+
* @return an accessor of the required type (never {@code null})
597600
* @since 4.1
598601
*/
599602
public static MessageHeaderAccessor getMutableAccessor(Message<?> message) {
@@ -646,7 +649,6 @@ public void setIdAndTimestamp() {
646649
if (getId() == null) {
647650
IdGenerator idGenerator = (MessageHeaderAccessor.this.idGenerator != null ?
648651
MessageHeaderAccessor.this.idGenerator : MessageHeaders.getIdGenerator());
649-
650652
UUID id = idGenerator.generateId();
651653
if (id != null && id != MessageHeaders.ID_VALUE_NONE) {
652654
getRawHeaders().put(ID, id);

0 commit comments

Comments
 (0)