Skip to content

Commit ae06c3a

Browse files
committed
Use DestinationUserNameProvider with @sendto
Issue: SPR-11327
1 parent e4ad2b3 commit ae06c3a

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -17,6 +17,7 @@
1717
package org.springframework.messaging.simp.annotation.support;
1818

1919
import java.lang.annotation.Annotation;
20+
import java.security.Principal;
2021

2122
import org.springframework.core.MethodParameter;
2223
import org.springframework.core.annotation.AnnotationUtils;
@@ -28,6 +29,7 @@
2829
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
2930
import org.springframework.messaging.simp.SimpMessageSendingOperations;
3031
import org.springframework.messaging.simp.annotation.SendToUser;
32+
import org.springframework.messaging.simp.user.DestinationUserNameProvider;
3133
import org.springframework.messaging.support.MessageBuilder;
3234
import org.springframework.util.Assert;
3335
import org.springframework.util.ObjectUtils;
@@ -124,13 +126,17 @@ public void handleReturnValue(Object returnValue, MethodParameter returnType, Me
124126

125127
SendToUser sendToUser = returnType.getMethodAnnotation(SendToUser.class);
126128
if (sendToUser != null) {
127-
if (inputHeaders.getUser() == null) {
129+
Principal principal = inputHeaders.getUser();
130+
if (principal == null) {
128131
throw new MissingSessionUserException(inputMessage);
129132
}
130-
String user = inputHeaders.getUser().getName();
133+
String userName = principal.getName();
134+
if (principal instanceof DestinationUserNameProvider) {
135+
userName = ((DestinationUserNameProvider) principal).getDestinationUserName();
136+
}
131137
String[] destinations = getTargetDestinations(sendToUser, inputHeaders, this.defaultUserDestinationPrefix);
132138
for (String destination : destinations) {
133-
this.messagingTemplate.convertAndSendToUser(user, destination, returnValue, postProcessor);
139+
this.messagingTemplate.convertAndSendToUser(userName, destination, returnValue, postProcessor);
134140
}
135141
return;
136142
}

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

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -33,7 +33,9 @@
3333
import org.springframework.messaging.handler.annotation.SendTo;
3434
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
3535
import org.springframework.messaging.simp.SimpMessagingTemplate;
36+
import org.springframework.messaging.simp.TestPrincipal;
3637
import org.springframework.messaging.simp.annotation.SendToUser;
38+
import org.springframework.messaging.simp.user.DestinationUserNameProvider;
3739
import org.springframework.messaging.support.MessageBuilder;
3840
import org.springframework.messaging.converter.MessageConverter;
3941

@@ -126,7 +128,7 @@ public void sendToNoAnnotations() throws Exception {
126128
}
127129

128130
@Test
129-
public void sendToMethod() throws Exception {
131+
public void sendTo() throws Exception {
130132

131133
when(this.messageChannel.send(any(Message.class))).thenReturn(true);
132134

@@ -150,7 +152,7 @@ public void sendToMethod() throws Exception {
150152
}
151153

152154
@Test
153-
public void sendToDefaultDestinationMethod() throws Exception {
155+
public void sendToDefaultDestination() throws Exception {
154156

155157
when(this.messageChannel.send(any(Message.class))).thenReturn(true);
156158

@@ -168,7 +170,7 @@ public void sendToDefaultDestinationMethod() throws Exception {
168170
}
169171

170172
@Test
171-
public void sendToUserMethod() throws Exception {
173+
public void sendToUser() throws Exception {
172174

173175
when(this.messageChannel.send(any(Message.class))).thenReturn(true);
174176

@@ -193,7 +195,26 @@ public void sendToUserMethod() throws Exception {
193195
}
194196

195197
@Test
196-
public void sendToUserDefaultDestinationMethod() throws Exception {
198+
public void sendToUserWithUserNameProvider() throws Exception {
199+
200+
when(this.messageChannel.send(any(Message.class))).thenReturn(true);
201+
202+
String sessionId = "sess1";
203+
TestUser user = new UniqueUser();
204+
Message<?> inputMessage = createInputMessage(sessionId, "sub1", null, user);
205+
this.handler.handleReturnValue(payloadContent, this.sendToUserReturnType, inputMessage);
206+
207+
verify(this.messageChannel, times(2)).send(this.messageCaptor.capture());
208+
209+
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.wrap(this.messageCaptor.getAllValues().get(0));
210+
assertEquals("/user/Me myself and I/dest1", headers.getDestination());
211+
212+
headers = SimpMessageHeaderAccessor.wrap(this.messageCaptor.getAllValues().get(1));
213+
assertEquals("/user/Me myself and I/dest2", headers.getDestination());
214+
}
215+
216+
@Test
217+
public void sendToUserDefaultDestination() throws Exception {
197218

198219
when(this.messageChannel.send(any(Message.class))).thenReturn(true);
199220

@@ -236,6 +257,14 @@ public boolean implies(Subject subject) {
236257
}
237258
}
238259

260+
private static class UniqueUser extends TestUser implements DestinationUserNameProvider {
261+
262+
@Override
263+
public String getDestinationUserName() {
264+
return "Me myself and I";
265+
}
266+
}
267+
239268
public String handleNoAnnotations() {
240269
return payloadContent;
241270
}

spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,13 @@ private void afterStompSessionConnected(StompHeaderAccessor headers, WebSocketSe
241241
if (principal != null) {
242242
headers.setNativeHeader(CONNECTED_USER_HEADER, principal.getName());
243243
if (this.userSessionRegistry != null) {
244-
String userName = getNameForUserSessionRegistry(principal);
244+
String userName = resolveNameForUserSessionRegistry(principal);
245245
this.userSessionRegistry.registerSessionId(userName, session.getId());
246246
}
247247
}
248248
}
249249

250-
private String getNameForUserSessionRegistry(Principal principal) {
250+
private String resolveNameForUserSessionRegistry(Principal principal) {
251251
String userName = principal.getName();
252252
if (principal instanceof DestinationUserNameProvider) {
253253
userName = ((DestinationUserNameProvider) principal).getDestinationUserName();
@@ -270,7 +270,7 @@ public void afterSessionEnded(WebSocketSession session, CloseStatus closeStatus,
270270

271271
Principal principal = session.getPrincipal();
272272
if ((this.userSessionRegistry != null) && (principal != null)) {
273-
String userName = getNameForUserSessionRegistry(principal);
273+
String userName = resolveNameForUserSessionRegistry(principal);
274274
this.userSessionRegistry.unregisterSessionId(userName, session.getId());
275275
}
276276

0 commit comments

Comments
 (0)