1
1
/*
2
- * Copyright 2002-2017 the original author or authors.
2
+ * Copyright 2002-2018 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 .Collections ;
21
22
import java .util .Map ;
22
23
23
24
import org .springframework .core .MethodParameter ;
@@ -153,11 +154,10 @@ public void handleReturnValue(@Nullable Object returnValue, MethodParameter retu
153
154
154
155
MessageHeaders headers = message .getHeaders ();
155
156
String sessionId = SimpMessageHeaderAccessor .getSessionId (headers );
156
- PlaceholderResolver varResolver = initVarResolver (headers );
157
- Object annotation = findAnnotation (returnType );
157
+ DestinationHelper destinationHelper = getDestinationHelper (headers , returnType );
158
158
159
- if ( annotation instanceof SendToUser ) {
160
- SendToUser sendToUser = ( SendToUser ) annotation ;
159
+ SendToUser sendToUser = destinationHelper . getSendToUser ();
160
+ if ( sendToUser != null ) {
161
161
boolean broadcast = sendToUser .broadcast ();
162
162
String user = getUserName (message , headers );
163
163
if (user == null ) {
@@ -169,7 +169,7 @@ public void handleReturnValue(@Nullable Object returnValue, MethodParameter retu
169
169
}
170
170
String [] destinations = getTargetDestinations (sendToUser , message , this .defaultUserDestinationPrefix );
171
171
for (String destination : destinations ) {
172
- destination = this . placeholderHelper . replacePlaceholders (destination , varResolver );
172
+ destination = destinationHelper . expandTemplateVars (destination );
173
173
if (broadcast ) {
174
174
this .messagingTemplate .convertAndSendToUser (
175
175
user , destination , returnValue , createHeaders (null , returnType ));
@@ -180,51 +180,33 @@ public void handleReturnValue(@Nullable Object returnValue, MethodParameter retu
180
180
}
181
181
}
182
182
}
183
- else {
184
- SendTo sendTo = (SendTo ) annotation ; // possibly null
183
+
184
+ SendTo sendTo = destinationHelper .getSendTo ();
185
+ if (sendTo != null || sendToUser == null ) {
185
186
String [] destinations = getTargetDestinations (sendTo , message , this .defaultDestinationPrefix );
186
187
for (String destination : destinations ) {
187
- destination = this . placeholderHelper . replacePlaceholders (destination , varResolver );
188
+ destination = destinationHelper . expandTemplateVars (destination );
188
189
this .messagingTemplate .convertAndSend (destination , returnValue , createHeaders (sessionId , returnType ));
189
190
}
190
191
}
191
192
}
192
193
193
- @ Nullable
194
- private Object findAnnotation (MethodParameter returnType ) {
195
- Annotation [] anns = new Annotation [4 ];
196
- anns [0 ] = AnnotatedElementUtils .findMergedAnnotation (returnType .getExecutable (), SendToUser .class );
197
- anns [1 ] = AnnotatedElementUtils .findMergedAnnotation (returnType .getExecutable (), SendTo .class );
198
- anns [2 ] = AnnotatedElementUtils .findMergedAnnotation (returnType .getDeclaringClass (), SendToUser .class );
199
- anns [3 ] = AnnotatedElementUtils .findMergedAnnotation (returnType .getDeclaringClass (), SendTo .class );
200
-
201
- if (anns [0 ] != null && !ObjectUtils .isEmpty (((SendToUser ) anns [0 ]).value ())) {
202
- return anns [0 ];
203
- }
204
- if (anns [1 ] != null && !ObjectUtils .isEmpty (((SendTo ) anns [1 ]).value ())) {
205
- return anns [1 ];
206
- }
207
- if (anns [2 ] != null && !ObjectUtils .isEmpty (((SendToUser ) anns [2 ]).value ())) {
208
- return anns [2 ];
209
- }
210
- if (anns [3 ] != null && !ObjectUtils .isEmpty (((SendTo ) anns [3 ]).value ())) {
211
- return anns [3 ];
212
- }
194
+ private DestinationHelper getDestinationHelper (MessageHeaders headers , MethodParameter returnType ) {
213
195
214
- for ( int i = 0 ; i < 4 ; i ++) {
215
- if ( anns [ i ] != null ) {
216
- return anns [ i ];
217
- }
196
+ SendToUser m1 = AnnotatedElementUtils . findMergedAnnotation ( returnType . getExecutable (), SendToUser . class );
197
+ SendTo m2 = AnnotatedElementUtils . findMergedAnnotation ( returnType . getExecutable (), SendTo . class );
198
+ if (( m1 != null && ! ObjectUtils . isEmpty ( m1 . value ())) || ( m2 != null && ! ObjectUtils . isEmpty ( m2 . value ()))) {
199
+ return new DestinationHelper ( headers , m1 , m2 );
218
200
}
219
201
220
- return null ;
221
- }
202
+ SendToUser c1 = AnnotatedElementUtils .findMergedAnnotation (returnType .getDeclaringClass (), SendToUser .class );
203
+ SendTo c2 = AnnotatedElementUtils .findMergedAnnotation (returnType .getDeclaringClass (), SendTo .class );
204
+ if ((c1 != null && !ObjectUtils .isEmpty (c1 .value ())) || (c2 != null && !ObjectUtils .isEmpty (c2 .value ()))) {
205
+ return new DestinationHelper (headers , c1 , c2 );
206
+ }
222
207
223
- @ SuppressWarnings ("unchecked" )
224
- private PlaceholderResolver initVarResolver (MessageHeaders headers ) {
225
- String name = DestinationVariableMethodArgumentResolver .DESTINATION_TEMPLATE_VARIABLES_HEADER ;
226
- Map <String , String > vars = (Map <String , String >) headers .get (name );
227
- return new DestinationVariablePlaceholderResolver (vars );
208
+ return m1 != null || m2 != null ?
209
+ new DestinationHelper (headers , m1 , m2 ) : new DestinationHelper (headers , c1 , c2 );
228
210
}
229
211
230
212
@ Nullable
@@ -275,20 +257,43 @@ public String toString() {
275
257
}
276
258
277
259
278
- private static class DestinationVariablePlaceholderResolver implements PlaceholderResolver {
260
+ private class DestinationHelper {
261
+
262
+ private final PlaceholderResolver placeholderResolver ;
279
263
280
264
@ Nullable
281
- private final Map < String , String > vars ;
265
+ private final SendTo sendTo ;
282
266
283
- public DestinationVariablePlaceholderResolver (@ Nullable Map <String , String > vars ) {
284
- this .vars = vars ;
267
+ @ Nullable
268
+ private final SendToUser sendToUser ;
269
+
270
+
271
+ public DestinationHelper (MessageHeaders headers , @ Nullable SendToUser sendToUser , @ Nullable SendTo sendTo ) {
272
+ Map <String , String > variables = getTemplateVariables (headers );
273
+ this .placeholderResolver = variables ::get ;
274
+ this .sendTo = sendTo ;
275
+ this .sendToUser = sendToUser ;
276
+ }
277
+
278
+ @ SuppressWarnings ("unchecked" )
279
+ private Map <String , String > getTemplateVariables (MessageHeaders headers ) {
280
+ String name = DestinationVariableMethodArgumentResolver .DESTINATION_TEMPLATE_VARIABLES_HEADER ;
281
+ return (Map <String , String >) headers .getOrDefault (name , Collections .emptyMap ());
285
282
}
286
283
287
- @ Override
288
284
@ Nullable
289
- public String resolvePlaceholder ( String placeholderName ) {
290
- return ( this .vars != null ? this . vars . get ( placeholderName ) : null ) ;
285
+ public SendTo getSendTo ( ) {
286
+ return this .sendTo ;
291
287
}
292
- }
293
288
294
- }
289
+ @ Nullable
290
+ public SendToUser getSendToUser () {
291
+ return this .sendToUser ;
292
+ }
293
+
294
+
295
+ public String expandTemplateVars (String destination ) {
296
+ return placeholderHelper .replacePlaceholders (destination , this .placeholderResolver );
297
+ }
298
+ }
299
+ }
0 commit comments