Skip to content

Commit 5549436

Browse files
committed
SimpAnnotationMethodMessageHandler skips template variable check in case of no pattern
Issue: SPR-13704 (cherry picked from commit e8417ea)
1 parent 393e36c commit 5549436

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 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.
@@ -64,7 +64,6 @@
6464
import org.springframework.stereotype.Controller;
6565
import org.springframework.util.AntPathMatcher;
6666
import org.springframework.util.Assert;
67-
import org.springframework.util.ClassUtils;
6867
import org.springframework.util.CollectionUtils;
6968
import org.springframework.util.PathMatcher;
7069
import org.springframework.validation.Validator;
@@ -289,9 +288,8 @@ public final void stop(Runnable callback) {
289288

290289

291290
protected List<HandlerMethodArgumentResolver> initArgumentResolvers() {
292-
ConfigurableBeanFactory beanFactory =
293-
(ClassUtils.isAssignableValue(ConfigurableApplicationContext.class, getApplicationContext())) ?
294-
((ConfigurableApplicationContext) getApplicationContext()).getBeanFactory() : null;
291+
ConfigurableBeanFactory beanFactory = (getApplicationContext() instanceof ConfigurableApplicationContext ?
292+
((ConfigurableApplicationContext) getApplicationContext()).getBeanFactory() : null);
295293

296294
List<HandlerMethodArgumentResolver> resolvers = new ArrayList<HandlerMethodArgumentResolver>();
297295

@@ -315,11 +313,13 @@ protected List<? extends HandlerMethodReturnValueHandler> initReturnValueHandler
315313
List<HandlerMethodReturnValueHandler> handlers = new ArrayList<HandlerMethodReturnValueHandler>();
316314

317315
// Annotation-based return value types
318-
SendToMethodReturnValueHandler sth = new SendToMethodReturnValueHandler(this.brokerTemplate, true);
316+
SendToMethodReturnValueHandler sth =
317+
new SendToMethodReturnValueHandler(this.brokerTemplate, true);
319318
sth.setHeaderInitializer(this.headerInitializer);
320319
handlers.add(sth);
321320

322-
SubscriptionMethodReturnValueHandler sh = new SubscriptionMethodReturnValueHandler(this.clientMessagingTemplate);
321+
SubscriptionMethodReturnValueHandler sh =
322+
new SubscriptionMethodReturnValueHandler(this.clientMessagingTemplate);
323323
sh.setHeaderInitializer(this.headerInitializer);
324324
handlers.add(sh);
325325

@@ -429,13 +429,15 @@ public int compare(SimpMessageMappingInfo info1, SimpMessageMappingInfo info2) {
429429
protected void handleMatch(SimpMessageMappingInfo mapping, HandlerMethod handlerMethod,
430430
String lookupDestination, Message<?> message) {
431431

432-
String matchedPattern = mapping.getDestinationConditions().getPatterns().iterator().next();
433-
Map<String, String> vars = getPathMatcher().extractUriTemplateVariables(matchedPattern, lookupDestination);
434-
435-
if (!CollectionUtils.isEmpty(vars)) {
436-
MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
437-
Assert.state(accessor != null && accessor.isMutable());
438-
accessor.setHeader(DestinationVariableMethodArgumentResolver.DESTINATION_TEMPLATE_VARIABLES_HEADER, vars);
432+
Set<String> patterns = mapping.getDestinationConditions().getPatterns();
433+
if (!CollectionUtils.isEmpty(patterns)) {
434+
String pattern = patterns.iterator().next();
435+
Map<String, String> vars = getPathMatcher().extractUriTemplateVariables(pattern, lookupDestination);
436+
if (!CollectionUtils.isEmpty(vars)) {
437+
MessageHeaderAccessor mha = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
438+
Assert.state(mha != null && mha.isMutable());
439+
mha.setHeader(DestinationVariableMethodArgumentResolver.DESTINATION_TEMPLATE_VARIABLES_HEADER, vars);
440+
}
439441
}
440442

441443
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)