Skip to content

Commit 16ee692

Browse files
committed
Add support for destination vars in @SendTo/SendToUser
Issue: SPR-12170
1 parent a3159df commit 16ee692

File tree

2 files changed

+162
-105
lines changed

2 files changed

+162
-105
lines changed

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

Lines changed: 33 additions & 2 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.
@@ -18,6 +18,7 @@
1818

1919
import java.lang.annotation.Annotation;
2020
import java.security.Principal;
21+
import java.util.Map;
2122

2223
import org.springframework.core.MethodParameter;
2324
import org.springframework.core.annotation.AnnotationUtils;
@@ -26,6 +27,7 @@
2627
import org.springframework.messaging.MessageHeaders;
2728
import org.springframework.messaging.handler.DestinationPatternsMessageCondition;
2829
import org.springframework.messaging.handler.annotation.SendTo;
30+
import org.springframework.messaging.handler.annotation.support.DestinationVariableMethodArgumentResolver;
2931
import org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler;
3032
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
3133
import org.springframework.messaging.simp.SimpMessageSendingOperations;
@@ -35,6 +37,8 @@
3537
import org.springframework.messaging.support.MessageHeaderInitializer;
3638
import org.springframework.util.Assert;
3739
import org.springframework.util.ObjectUtils;
40+
import org.springframework.util.PropertyPlaceholderHelper;
41+
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
3842

3943
/**
4044
* A {@link HandlerMethodReturnValueHandler} for sending to destinations specified in a
@@ -59,6 +63,8 @@ public class SendToMethodReturnValueHandler implements HandlerMethodReturnValueH
5963

6064
private String defaultUserDestinationPrefix = "/queue";
6165

66+
private PropertyPlaceholderHelper placeholderHelper = new PropertyPlaceholderHelper("{", "}", null, false);
67+
6268
private MessageHeaderInitializer headerInitializer;
6369

6470

@@ -139,8 +145,9 @@ public void handleReturnValue(Object returnValue, MethodParameter returnType, Me
139145
}
140146
MessageHeaders headers = message.getHeaders();
141147
String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
142-
148+
PlaceholderResolver varResolver = initVarResolver(headers);
143149
SendToUser sendToUser = returnType.getMethodAnnotation(SendToUser.class);
150+
144151
if (sendToUser != null) {
145152
boolean broadcast = sendToUser.broadcast();
146153
String user = getUserName(message, headers);
@@ -153,6 +160,7 @@ public void handleReturnValue(Object returnValue, MethodParameter returnType, Me
153160
}
154161
String[] destinations = getTargetDestinations(sendToUser, message, this.defaultUserDestinationPrefix);
155162
for (String destination : destinations) {
163+
destination = this.placeholderHelper.replacePlaceholders(destination, varResolver);
156164
if (broadcast) {
157165
this.messagingTemplate.convertAndSendToUser(user, destination, returnValue);
158166
}
@@ -165,11 +173,19 @@ public void handleReturnValue(Object returnValue, MethodParameter returnType, Me
165173
SendTo sendTo = returnType.getMethodAnnotation(SendTo.class);
166174
String[] destinations = getTargetDestinations(sendTo, message, this.defaultDestinationPrefix);
167175
for (String destination : destinations) {
176+
destination = this.placeholderHelper.replacePlaceholders(destination, varResolver);
168177
this.messagingTemplate.convertAndSend(destination, returnValue, createHeaders(sessionId));
169178
}
170179
}
171180
}
172181

182+
@SuppressWarnings("unchecked")
183+
private PlaceholderResolver initVarResolver(MessageHeaders headers) {
184+
String name = DestinationVariableMethodArgumentResolver.DESTINATION_TEMPLATE_VARIABLES_HEADER;
185+
Map<String, String> vars = (Map<String, String>) headers.get(name);
186+
return new DestinationVariablePlaceholderResolver(vars);
187+
}
188+
173189
protected String getUserName(Message<?> message, MessageHeaders headers) {
174190
Principal principal = SimpMessageHeaderAccessor.getUser(headers);
175191
if (principal != null) {
@@ -210,4 +226,19 @@ public String toString() {
210226
return "SendToMethodReturnValueHandler [annotationRequired=" + annotationRequired + "]";
211227
}
212228

229+
private static class DestinationVariablePlaceholderResolver implements PlaceholderResolver {
230+
231+
private final Map<String, String> vars;
232+
233+
234+
public DestinationVariablePlaceholderResolver(Map<String, String> vars) {
235+
this.vars = vars;
236+
}
237+
238+
@Override
239+
public String resolvePlaceholder(String placeholderName) {
240+
return (this.vars != null ? this.vars.get(placeholderName) : null);
241+
}
242+
}
243+
213244
}

0 commit comments

Comments
 (0)