Skip to content

Commit 59f3970

Browse files
sdeleuzerstoyanchev
authored andcommitted
Fix default target destination when using "." as path separator
Issue: SPR-11660
1 parent e8d8c33 commit 59f3970

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.springframework.messaging.support.MessageHeaderInitializer;
3333
import org.springframework.util.Assert;
3434
import org.springframework.util.ObjectUtils;
35+
import org.springframework.util.StringUtils;
3536

3637
import java.lang.annotation.Annotation;
3738
import java.security.Principal;
@@ -187,7 +188,11 @@ protected String[] getTargetDestinations(Annotation annotation, Message<?> messa
187188
}
188189
}
189190
String name = DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER;
190-
return new String[] { defaultPrefix + message.getHeaders().get(name) };
191+
String destination = (String)message.getHeaders().get(name);
192+
if (StringUtils.hasLength(destination) && !destination.startsWith("/")) {
193+
destination = "/" + destination;
194+
}
195+
return new String[] { defaultPrefix + destination };
191196
}
192197

193198
private MessageHeaders createHeaders(String sessionId) {

spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SendToMethodReturnValueHandlerTests.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,21 @@ public void sendToDefaultDestination() throws Exception {
185185
assertNull("Subscription id should not be copied", headers.getSubscriptionId());
186186
}
187187

188+
@Test
189+
public void sendToDefaultDestinationWithoutLeadingSlash() throws Exception {
190+
191+
when(this.messageChannel.send(any(Message.class))).thenReturn(true);
192+
193+
Message<?> inputMessage = createInputMessage("sess1", "sub1", "/app", "dest", null);
194+
this.handler.handleReturnValue(PAYLOAD, this.sendToDefaultDestReturnType, inputMessage);
195+
196+
verify(this.messageChannel, times(1)).send(this.messageCaptor.capture());
197+
198+
Message<?> message = this.messageCaptor.getAllValues().get(0);
199+
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
200+
assertEquals("/topic/dest", headers.getDestination());
201+
}
202+
188203
@Test
189204
public void testHeadersToSend() throws Exception {
190205

@@ -297,6 +312,22 @@ public void sendToUserDefaultDestination() throws Exception {
297312
assertEquals("/user/" + user.getName() + "/queue/dest", headers.getDestination());
298313
}
299314

315+
@Test
316+
public void sendToUserDefaultDestinationWithoutLeadingSlash() throws Exception {
317+
318+
when(this.messageChannel.send(any(Message.class))).thenReturn(true);
319+
320+
TestUser user = new TestUser();
321+
Message<?> inputMessage = createInputMessage("sess1", "sub1", "/app", "dest", user);
322+
this.handler.handleReturnValue(PAYLOAD, this.sendToUserDefaultDestReturnType, inputMessage);
323+
324+
verify(this.messageChannel, times(1)).send(this.messageCaptor.capture());
325+
326+
Message<?> message = this.messageCaptor.getAllValues().get(0);
327+
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(message);
328+
assertEquals("/user/" + user.getName() + "/queue/dest", headers.getDestination());
329+
}
330+
300331
@Test
301332
public void sendToUserDefaultDestinationSingleSession() throws Exception {
302333

0 commit comments

Comments
 (0)