@SendToUser supported on the class level

Issue: SPR-12047
This commit is contained in:
Rossen Stoyanchev
2016-01-29 16:10:02 -05:00
parent bedf1a9bd0
commit 03e3ef53ab
5 changed files with 101 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,6 +30,9 @@ import org.springframework.core.annotation.AliasFor;
* destination(s) prepended with <code>"/user/{username}"</code> where the user name
* is extracted from the headers of the input message being handled.
*
* <p>The annotation may also be placed at class-level in which case all methods
* in the class where the annotation applies will inherit it.
* @author Rossen Stoyanchev
* @author Sam Brannen
* @since 4.0
@@ -37,7 +40,7 @@ import org.springframework.core.annotation.AliasFor;
* @see org.springframework.messaging.simp.user.UserDestinationMessageHandler
* @see org.springframework.messaging.simp.SimpMessageHeaderAccessor#getUser()
*/
@Target(ElementType.METHOD)
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SendToUser {

View File

@@ -134,7 +134,8 @@ public class SendToMethodReturnValueHandler implements HandlerMethodReturnValueH
public boolean supportsReturnType(MethodParameter returnType) {
if (returnType.getMethodAnnotation(SendTo.class) != null ||
AnnotationUtils.getAnnotation(returnType.getDeclaringClass(), SendTo.class) != null ||
returnType.getMethodAnnotation(SendToUser.class) != null) {
returnType.getMethodAnnotation(SendToUser.class) != null ||
AnnotationUtils.getAnnotation(returnType.getDeclaringClass(), SendToUser.class) != null) {
return true;
}
return (!this.annotationRequired);
@@ -149,7 +150,7 @@ public class SendToMethodReturnValueHandler implements HandlerMethodReturnValueH
MessageHeaders headers = message.getHeaders();
String sessionId = SimpMessageHeaderAccessor.getSessionId(headers);
PlaceholderResolver varResolver = initVarResolver(headers);
SendToUser sendToUser = returnType.getMethodAnnotation(SendToUser.class);
SendToUser sendToUser = getSendToUser(returnType);
if (sendToUser != null) {
boolean broadcast = sendToUser.broadcast();
@@ -184,6 +185,18 @@ public class SendToMethodReturnValueHandler implements HandlerMethodReturnValueH
}
}
private SendToUser getSendToUser(MethodParameter returnType) {
SendToUser annot = returnType.getMethodAnnotation(SendToUser.class);
if (annot != null && !ObjectUtils.isEmpty((annot.value()))) {
return annot;
}
SendToUser typeAnnot = AnnotationUtils.getAnnotation(returnType.getDeclaringClass(), SendToUser.class);
if (typeAnnot != null && !ObjectUtils.isEmpty((typeAnnot.value()))) {
return typeAnnot;
}
return (annot != null ? annot : typeAnnot);
}
private SendTo getSendTo(MethodParameter returnType) {
SendTo sendTo = returnType.getMethodAnnotation(SendTo.class);
if (sendTo != null && !ObjectUtils.isEmpty((sendTo.value()))) {