Consolidate SendTo vs SendToUser detection

SendTo and SendToUser are treated as mutually exclusive. The addition of
type-level support in 4.3. RC1 however did not consider the possibility
to mix and match of the two betwee type and method level.

This commit consolidates the detection of SendTo and SendToUser
annotations and considers them all together.

Issue: SPR-14238
This commit is contained in:
Rossen Stoyanchev
2016-05-03 12:30:46 -04:00
parent 05b29a4a17
commit 46e41a9d94
2 changed files with 102 additions and 25 deletions

View File

@@ -151,9 +151,10 @@ public class SendToMethodReturnValueHandler implements HandlerMethodReturnValueH
MessageHeaders headers = message.getHeaders();
String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
PlaceholderResolver varResolver = initVarResolver(headers);
SendToUser sendToUser = getSendToUser(returnType);
Object annotation = findAnnotation(returnType);
if (sendToUser != null) {
if (annotation != null && annotation instanceof SendToUser) {
SendToUser sendToUser = (SendToUser) annotation;
boolean broadcast = sendToUser.broadcast();
String user = getUserName(message, headers);
if (user == null) {
@@ -177,7 +178,7 @@ public class SendToMethodReturnValueHandler implements HandlerMethodReturnValueH
}
}
else {
SendTo sendTo = getSendTo(returnType);
SendTo sendTo = (SendTo) annotation;
String[] destinations = getTargetDestinations(sendTo, message, this.defaultDestinationPrefix);
for (String destination : destinations) {
destination = this.placeholderHelper.replacePlaceholders(destination, varResolver);
@@ -186,6 +187,35 @@ public class SendToMethodReturnValueHandler implements HandlerMethodReturnValueH
}
}
private Object findAnnotation(MethodParameter returnType) {
Annotation[] annot = new Annotation[4];
annot[0] = AnnotatedElementUtils.findMergedAnnotation(returnType.getMethod(), SendToUser.class);
annot[1] = AnnotatedElementUtils.findMergedAnnotation(returnType.getMethod(), SendTo.class);
annot[2] = AnnotatedElementUtils.findMergedAnnotation(returnType.getDeclaringClass(), SendToUser.class);
annot[3] = AnnotatedElementUtils.findMergedAnnotation(returnType.getDeclaringClass(), SendTo.class);
if (annot[0] != null && !ObjectUtils.isEmpty(((SendToUser) annot[0]).value())) {
return annot[0];
}
if (annot[1] != null && !ObjectUtils.isEmpty(((SendTo) annot[1]).value())) {
return annot[1];
}
if (annot[2] != null && !ObjectUtils.isEmpty(((SendToUser) annot[2]).value())) {
return annot[2];
}
if (annot[3] != null && !ObjectUtils.isEmpty(((SendTo) annot[3]).value())) {
return annot[3];
}
for (int i=0; i < 4; i++) {
if (annot[i] != null) {
return annot[i];
}
}
return null;
}
private SendToUser getSendToUser(MethodParameter returnType) {
SendToUser annot = AnnotatedElementUtils.findMergedAnnotation(returnType.getMethod(), SendToUser.class);
if (annot != null && !ObjectUtils.isEmpty(annot.value())) {