1
1
/*
2
- * Copyright 2002-2014 the original author or authors.
2
+ * Copyright 2002-2015 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
18
18
19
19
import java .lang .annotation .Annotation ;
20
20
import java .security .Principal ;
21
+ import java .util .Map ;
21
22
22
23
import org .springframework .core .MethodParameter ;
23
24
import org .springframework .core .annotation .AnnotationUtils ;
26
27
import org .springframework .messaging .MessageHeaders ;
27
28
import org .springframework .messaging .handler .DestinationPatternsMessageCondition ;
28
29
import org .springframework .messaging .handler .annotation .SendTo ;
30
+ import org .springframework .messaging .handler .annotation .support .DestinationVariableMethodArgumentResolver ;
29
31
import org .springframework .messaging .handler .invocation .HandlerMethodReturnValueHandler ;
30
32
import org .springframework .messaging .simp .SimpMessageHeaderAccessor ;
31
33
import org .springframework .messaging .simp .SimpMessageSendingOperations ;
35
37
import org .springframework .messaging .support .MessageHeaderInitializer ;
36
38
import org .springframework .util .Assert ;
37
39
import org .springframework .util .ObjectUtils ;
40
+ import org .springframework .util .PropertyPlaceholderHelper ;
41
+ import org .springframework .util .PropertyPlaceholderHelper .PlaceholderResolver ;
38
42
39
43
/**
40
44
* A {@link HandlerMethodReturnValueHandler} for sending to destinations specified in a
@@ -59,6 +63,8 @@ public class SendToMethodReturnValueHandler implements HandlerMethodReturnValueH
59
63
60
64
private String defaultUserDestinationPrefix = "/queue" ;
61
65
66
+ private PropertyPlaceholderHelper placeholderHelper = new PropertyPlaceholderHelper ("{" , "}" , null , false );
67
+
62
68
private MessageHeaderInitializer headerInitializer ;
63
69
64
70
@@ -139,8 +145,9 @@ public void handleReturnValue(Object returnValue, MethodParameter returnType, Me
139
145
}
140
146
MessageHeaders headers = message .getHeaders ();
141
147
String sessionId = SimpMessageHeaderAccessor .getSessionId (headers );
142
-
148
+ PlaceholderResolver varResolver = initVarResolver ( headers );
143
149
SendToUser sendToUser = returnType .getMethodAnnotation (SendToUser .class );
150
+
144
151
if (sendToUser != null ) {
145
152
boolean broadcast = sendToUser .broadcast ();
146
153
String user = getUserName (message , headers );
@@ -153,6 +160,7 @@ public void handleReturnValue(Object returnValue, MethodParameter returnType, Me
153
160
}
154
161
String [] destinations = getTargetDestinations (sendToUser , message , this .defaultUserDestinationPrefix );
155
162
for (String destination : destinations ) {
163
+ destination = this .placeholderHelper .replacePlaceholders (destination , varResolver );
156
164
if (broadcast ) {
157
165
this .messagingTemplate .convertAndSendToUser (user , destination , returnValue );
158
166
}
@@ -165,11 +173,19 @@ public void handleReturnValue(Object returnValue, MethodParameter returnType, Me
165
173
SendTo sendTo = returnType .getMethodAnnotation (SendTo .class );
166
174
String [] destinations = getTargetDestinations (sendTo , message , this .defaultDestinationPrefix );
167
175
for (String destination : destinations ) {
176
+ destination = this .placeholderHelper .replacePlaceholders (destination , varResolver );
168
177
this .messagingTemplate .convertAndSend (destination , returnValue , createHeaders (sessionId ));
169
178
}
170
179
}
171
180
}
172
181
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
+
173
189
protected String getUserName (Message <?> message , MessageHeaders headers ) {
174
190
Principal principal = SimpMessageHeaderAccessor .getUser (headers );
175
191
if (principal != null ) {
@@ -210,4 +226,19 @@ public String toString() {
210
226
return "SendToMethodReturnValueHandler [annotationRequired=" + annotationRequired + "]" ;
211
227
}
212
228
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
+
213
244
}
0 commit comments