Skip to content

Commit 78fe55f

Browse files
brandenclarkrstoyanchev
authored andcommitted
Check hasNext on sessionIds in UserDestinationResult
Closes gh-34333 Signed-off-by: Branden Clark <[email protected]>
1 parent 65913c3 commit 78fe55f

File tree

3 files changed

+33
-12
lines changed

3 files changed

+33
-12
lines changed

Diff for: spring-messaging/src/main/java/org/springframework/messaging/simp/user/UserDestinationMessageHandler.java

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -20,7 +20,6 @@
2020
import java.util.Iterator;
2121
import java.util.List;
2222
import java.util.Map;
23-
import java.util.Set;
2423
import java.util.concurrent.ConcurrentHashMap;
2524

2625
import org.apache.commons.logging.Log;
@@ -280,12 +279,10 @@ public MessageSendingOperations<String> getMessagingTemplate() {
280279
return this.messagingTemplate;
281280
}
282281

283-
public void send(UserDestinationResult destinationResult, Message<?> message) throws MessagingException {
284-
Set<String> sessionIds = destinationResult.getSessionIds();
285-
Iterator<String> itr = (sessionIds != null ? sessionIds.iterator() : null);
286-
287-
for (String target : destinationResult.getTargetDestinations()) {
288-
String sessionId = (itr != null ? itr.next() : null);
282+
public void send(UserDestinationResult result, Message<?> message) throws MessagingException {
283+
Iterator<String> itr = result.getSessionIds().iterator();
284+
for (String target : result.getTargetDestinations()) {
285+
String sessionId = (itr.hasNext() ? itr.next() : null);
289286
getTemplateToUse(sessionId).send(target, message);
290287
}
291288
}

Diff for: spring-messaging/src/main/java/org/springframework/messaging/simp/user/UserDestinationResult.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -44,7 +44,11 @@ public class UserDestinationResult {
4444
private final Set<String> sessionIds;
4545

4646

47-
public UserDestinationResult(String sourceDestination, Set<String> targetDestinations,
47+
/**
48+
* Main constructor.
49+
*/
50+
public UserDestinationResult(
51+
String sourceDestination, Set<String> targetDestinations,
4852
String subscribeDestination, @Nullable String user) {
4953

5054
this(sourceDestination, targetDestinations, subscribeDestination, user, null);
@@ -114,7 +118,6 @@ public String getUser() {
114118
/**
115119
* Return the session id for the targetDestination.
116120
*/
117-
@Nullable
118121
public Set<String> getSessionIds() {
119122
return this.sessionIds;
120123
}

Diff for: spring-messaging/src/test/java/org/springframework/messaging/simp/user/UserDestinationMessageHandlerTests.java

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 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.user;
1818

1919
import java.nio.charset.StandardCharsets;
20+
import java.util.Set;
2021

2122
import org.junit.jupiter.api.Test;
2223
import org.mockito.ArgumentCaptor;
@@ -98,6 +99,26 @@ void handleMessage() {
9899
assertThat(accessor.getFirstNativeHeader(ORIGINAL_DESTINATION)).isEqualTo("/user/queue/foo");
99100
}
100101

102+
@Test
103+
@SuppressWarnings("rawtypes")
104+
void handleMessageWithoutSessionIds() {
105+
UserDestinationResolver resolver = mock();
106+
Message message = createWith(SimpMessageType.MESSAGE, "joe", null, "/user/joe/queue/foo");
107+
UserDestinationResult result = new UserDestinationResult("/queue/foo-user123", Set.of("/queue/foo-user123"), "/user/queue/foo", "joe");
108+
given(resolver.resolveDestination(message)).willReturn(result);
109+
110+
given(this.brokerChannel.send(Mockito.any(Message.class))).willReturn(true);
111+
UserDestinationMessageHandler handler = new UserDestinationMessageHandler(new StubMessageChannel(), this.brokerChannel, resolver);
112+
handler.handleMessage(message);
113+
114+
ArgumentCaptor<Message> captor = ArgumentCaptor.forClass(Message.class);
115+
Mockito.verify(this.brokerChannel).send(captor.capture());
116+
117+
SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.wrap(captor.getValue());
118+
assertThat(accessor.getDestination()).isEqualTo("/queue/foo-user123");
119+
assertThat(accessor.getFirstNativeHeader(ORIGINAL_DESTINATION)).isEqualTo("/user/queue/foo");
120+
}
121+
101122
@Test
102123
@SuppressWarnings("rawtypes")
103124
void handleMessageWithoutActiveSession() {

0 commit comments

Comments
 (0)