Rename @ReplyTo to @SendTo
This commit is contained in:
parent
41fa15a484
commit
45eab23e15
|
|
@ -35,7 +35,7 @@ import org.springframework.messaging.Message;
|
|||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface ReplyTo {
|
||||
public @interface SendTo {
|
||||
|
||||
/**
|
||||
* The destination for a message created from the return value of a method.
|
||||
|
|
@ -37,13 +37,13 @@ import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
|
|||
* @author Rossen Stoyanchev
|
||||
* @since 4.0
|
||||
*
|
||||
* @see org.springframework.messaging.handler.annotation.ReplyTo
|
||||
* @see org.springframework.messaging.handler.annotation.SendTo
|
||||
* @see org.springframework.messaging.simp.handler.UserDestinationMessageHandler
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface ReplyToUser {
|
||||
public @interface SendToUser {
|
||||
|
||||
/**
|
||||
* The destination for a message based on the return value of a method.
|
||||
|
|
@ -23,19 +23,19 @@ import org.springframework.core.annotation.AnnotationUtils;
|
|||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.messaging.core.MessagePostProcessor;
|
||||
import org.springframework.messaging.handler.annotation.ReplyTo;
|
||||
import org.springframework.messaging.handler.annotation.SendTo;
|
||||
import org.springframework.messaging.handler.method.HandlerMethodReturnValueHandler;
|
||||
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
|
||||
import org.springframework.messaging.simp.SimpMessageSendingOperations;
|
||||
import org.springframework.messaging.simp.annotation.ReplyToUser;
|
||||
import org.springframework.messaging.simp.annotation.SendToUser;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
|
||||
/**
|
||||
* A {@link HandlerMethodReturnValueHandler} for replying to destinations specified in a
|
||||
* {@link ReplyTo} or {@link ReplyToUser} method-level annotations.
|
||||
* A {@link HandlerMethodReturnValueHandler} for sending to destinations specified in a
|
||||
* {@link SendTo} or {@link SendToUser} method-level annotations.
|
||||
* <p>
|
||||
* The value returned from the method is converted, and turned to a {@link Message} and
|
||||
* sent through the provided {@link MessageChannel}. The
|
||||
|
|
@ -46,14 +46,14 @@ import org.springframework.util.ObjectUtils;
|
|||
* @author Rossen Stoyanchev
|
||||
* @since 4.0
|
||||
*/
|
||||
public class ReplyToMethodReturnValueHandler implements HandlerMethodReturnValueHandler {
|
||||
public class SendToMethodReturnValueHandler implements HandlerMethodReturnValueHandler {
|
||||
|
||||
private final SimpMessageSendingOperations messagingTemplate;
|
||||
|
||||
private final boolean annotationRequired;
|
||||
|
||||
|
||||
public ReplyToMethodReturnValueHandler(SimpMessageSendingOperations messagingTemplate, boolean annotationRequired) {
|
||||
public SendToMethodReturnValueHandler(SimpMessageSendingOperations messagingTemplate, boolean annotationRequired) {
|
||||
Assert.notNull(messagingTemplate, "messagingTemplate is required");
|
||||
this.messagingTemplate = messagingTemplate;
|
||||
this.annotationRequired = annotationRequired;
|
||||
|
|
@ -62,8 +62,8 @@ public class ReplyToMethodReturnValueHandler implements HandlerMethodReturnValue
|
|||
|
||||
@Override
|
||||
public boolean supportsReturnType(MethodParameter returnType) {
|
||||
if ((returnType.getMethodAnnotation(ReplyTo.class) != null) ||
|
||||
(returnType.getMethodAnnotation(ReplyToUser.class) != null)) {
|
||||
if ((returnType.getMethodAnnotation(SendTo.class) != null) ||
|
||||
(returnType.getMethodAnnotation(SendToUser.class) != null)) {
|
||||
return true;
|
||||
}
|
||||
return (!this.annotationRequired);
|
||||
|
|
@ -82,21 +82,21 @@ public class ReplyToMethodReturnValueHandler implements HandlerMethodReturnValue
|
|||
String sessionId = inputHeaders.getSessionId();
|
||||
MessagePostProcessor postProcessor = new SessionHeaderPostProcessor(sessionId);
|
||||
|
||||
ReplyToUser replyToUser = returnType.getMethodAnnotation(ReplyToUser.class);
|
||||
if (replyToUser != null) {
|
||||
SendToUser sendToUser = returnType.getMethodAnnotation(SendToUser.class);
|
||||
if (sendToUser != null) {
|
||||
if (inputHeaders.getUser() == null) {
|
||||
throw new MissingSessionUserException(inputMessage);
|
||||
}
|
||||
String user = inputHeaders.getUser().getName();
|
||||
for (String destination : getDestinations(replyToUser, inputHeaders.getDestination())) {
|
||||
for (String destination : getDestinations(sendToUser, inputHeaders.getDestination())) {
|
||||
this.messagingTemplate.convertAndSendToUser(user, destination, returnValue, postProcessor);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
ReplyTo replyTo = returnType.getMethodAnnotation(ReplyTo.class);
|
||||
if (replyTo != null) {
|
||||
for (String destination : getDestinations(replyTo, inputHeaders.getDestination())) {
|
||||
SendTo sendTo = returnType.getMethodAnnotation(SendTo.class);
|
||||
if (sendTo != null) {
|
||||
for (String destination : getDestinations(sendTo, inputHeaders.getDestination())) {
|
||||
this.messagingTemplate.convertAndSend(destination, returnValue, postProcessor);
|
||||
}
|
||||
return;
|
||||
|
|
@ -129,7 +129,7 @@ public class ReplyToMethodReturnValueHandler implements HandlerMethodReturnValue
|
|||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ReplyToMethodReturnValueHandler [annotationRequired=" + annotationRequired + "]";
|
||||
return "SendToMethodReturnValueHandler [annotationRequired=" + annotationRequired + "]";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -20,10 +20,10 @@ import org.springframework.core.MethodParameter;
|
|||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.core.MessagePostProcessor;
|
||||
import org.springframework.messaging.core.MessageSendingOperations;
|
||||
import org.springframework.messaging.handler.annotation.ReplyTo;
|
||||
import org.springframework.messaging.handler.annotation.SendTo;
|
||||
import org.springframework.messaging.handler.method.HandlerMethodReturnValueHandler;
|
||||
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
|
||||
import org.springframework.messaging.simp.annotation.ReplyToUser;
|
||||
import org.springframework.messaging.simp.annotation.SendToUser;
|
||||
import org.springframework.messaging.simp.annotation.SubscribeEvent;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.util.Assert;
|
||||
|
|
@ -32,7 +32,7 @@ import org.springframework.util.Assert;
|
|||
/**
|
||||
* A {@link HandlerMethodReturnValueHandler} for replying directly to a subscription. It
|
||||
* supports methods annotated with {@link SubscribeEvent} unless they're also annotated
|
||||
* with {@link ReplyTo} or {@link ReplyToUser}.
|
||||
* with {@link SendTo} or {@link SendToUser}.
|
||||
* <p>
|
||||
* The value returned from the method is converted, and turned to a {@link Message} and
|
||||
* then enriched with the sessionId, subscriptionId, and destination of the input message.
|
||||
|
|
@ -55,8 +55,8 @@ public class SubscriptionMethodReturnValueHandler implements HandlerMethodReturn
|
|||
@Override
|
||||
public boolean supportsReturnType(MethodParameter returnType) {
|
||||
return ((returnType.getMethodAnnotation(SubscribeEvent.class) != null)
|
||||
&& (returnType.getMethodAnnotation(ReplyTo.class) == null)
|
||||
&& (returnType.getMethodAnnotation(ReplyToUser.class) == null));
|
||||
&& (returnType.getMethodAnnotation(SendTo.class) == null)
|
||||
&& (returnType.getMethodAnnotation(SendToUser.class) == null));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ import org.springframework.messaging.simp.SimpMessagingTemplate;
|
|||
import org.springframework.messaging.simp.annotation.SubscribeEvent;
|
||||
import org.springframework.messaging.simp.annotation.UnsubscribeEvent;
|
||||
import org.springframework.messaging.simp.annotation.support.PrincipalMethodArgumentResolver;
|
||||
import org.springframework.messaging.simp.annotation.support.ReplyToMethodReturnValueHandler;
|
||||
import org.springframework.messaging.simp.annotation.support.SendToMethodReturnValueHandler;
|
||||
import org.springframework.messaging.simp.annotation.support.SubscriptionMethodReturnValueHandler;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.messaging.support.converter.MessageConverter;
|
||||
|
|
@ -201,14 +201,14 @@ public class AnnotationMethodMessageHandler implements MessageHandler, Applicati
|
|||
this.argumentResolvers.addResolver(new MessageBodyMethodArgumentResolver(this.messageConverter));
|
||||
|
||||
// Annotation-based return value types
|
||||
this.returnValueHandlers.addHandler(new ReplyToMethodReturnValueHandler(this.brokerTemplate, true));
|
||||
this.returnValueHandlers.addHandler(new SendToMethodReturnValueHandler(this.brokerTemplate, true));
|
||||
this.returnValueHandlers.addHandler(new SubscriptionMethodReturnValueHandler(this.webSocketResponseTemplate));
|
||||
|
||||
// custom return value types
|
||||
this.returnValueHandlers.addHandlers(this.customReturnValueHandlers);
|
||||
|
||||
// catch-all
|
||||
this.returnValueHandlers.addHandler(new ReplyToMethodReturnValueHandler(this.brokerTemplate, false));
|
||||
this.returnValueHandlers.addHandler(new SendToMethodReturnValueHandler(this.brokerTemplate, false));
|
||||
}
|
||||
|
||||
protected final void initHandlerMethods() {
|
||||
|
|
|
|||
|
|
@ -31,10 +31,10 @@ import org.springframework.core.MethodParameter;
|
|||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.messaging.handler.annotation.MessageMapping;
|
||||
import org.springframework.messaging.handler.annotation.ReplyTo;
|
||||
import org.springframework.messaging.handler.annotation.SendTo;
|
||||
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
|
||||
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||
import org.springframework.messaging.simp.annotation.ReplyToUser;
|
||||
import org.springframework.messaging.simp.annotation.SendToUser;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
import org.springframework.messaging.support.converter.MessageConverter;
|
||||
|
||||
|
|
@ -44,18 +44,18 @@ import static org.mockito.Mockito.*;
|
|||
|
||||
|
||||
/**
|
||||
* Test fixture for {@link ReplyToMethodReturnValueHandlerTests}.
|
||||
* Test fixture for {@link SendToMethodReturnValueHandlerTests}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class ReplyToMethodReturnValueHandlerTests {
|
||||
public class SendToMethodReturnValueHandlerTests {
|
||||
|
||||
private static final String payloadContent = "payload";
|
||||
|
||||
|
||||
private ReplyToMethodReturnValueHandler handler;
|
||||
private SendToMethodReturnValueHandler handler;
|
||||
|
||||
private ReplyToMethodReturnValueHandler handlerAnnotationNotRequired;
|
||||
private SendToMethodReturnValueHandler handlerAnnotationNotRequired;
|
||||
|
||||
@Mock private MessageChannel messageChannel;
|
||||
|
||||
|
|
@ -63,11 +63,11 @@ public class ReplyToMethodReturnValueHandlerTests {
|
|||
|
||||
@Mock private MessageConverter messageConverter;
|
||||
|
||||
private MethodParameter replyToReturnType;
|
||||
private MethodParameter sendToReturnType;
|
||||
|
||||
private MethodParameter replyToUserReturnType;
|
||||
private MethodParameter sendToUserReturnType;
|
||||
|
||||
private MethodParameter missingReplyToReturnType;
|
||||
private MethodParameter missingSendToReturnType;
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
|
@ -82,37 +82,37 @@ public class ReplyToMethodReturnValueHandlerTests {
|
|||
SimpMessagingTemplate messagingTemplate = new SimpMessagingTemplate(this.messageChannel);
|
||||
messagingTemplate.setConverter(this.messageConverter);
|
||||
|
||||
this.handler = new ReplyToMethodReturnValueHandler(messagingTemplate, true);
|
||||
this.handlerAnnotationNotRequired = new ReplyToMethodReturnValueHandler(messagingTemplate, false);
|
||||
this.handler = new SendToMethodReturnValueHandler(messagingTemplate, true);
|
||||
this.handlerAnnotationNotRequired = new SendToMethodReturnValueHandler(messagingTemplate, false);
|
||||
|
||||
Method method = this.getClass().getDeclaredMethod("handleAndReplyTo");
|
||||
this.replyToReturnType = new MethodParameter(method, -1);
|
||||
Method method = this.getClass().getDeclaredMethod("handleAndSendTo");
|
||||
this.sendToReturnType = new MethodParameter(method, -1);
|
||||
|
||||
method = this.getClass().getDeclaredMethod("handleAndReplyToUser");
|
||||
this.replyToUserReturnType = new MethodParameter(method, -1);
|
||||
method = this.getClass().getDeclaredMethod("handleAndSendToUser");
|
||||
this.sendToUserReturnType = new MethodParameter(method, -1);
|
||||
|
||||
method = this.getClass().getDeclaredMethod("handleWithMissingReplyTo");
|
||||
this.missingReplyToReturnType = new MethodParameter(method, -1);
|
||||
method = this.getClass().getDeclaredMethod("handleWithMissingSendTo");
|
||||
this.missingSendToReturnType = new MethodParameter(method, -1);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void supportsReturnType() throws Exception {
|
||||
assertTrue(this.handler.supportsReturnType(this.replyToReturnType));
|
||||
assertTrue(this.handler.supportsReturnType(this.replyToUserReturnType));
|
||||
assertFalse(this.handler.supportsReturnType(this.missingReplyToReturnType));
|
||||
assertTrue(this.handlerAnnotationNotRequired.supportsReturnType(this.missingReplyToReturnType));
|
||||
assertTrue(this.handler.supportsReturnType(this.sendToReturnType));
|
||||
assertTrue(this.handler.supportsReturnType(this.sendToUserReturnType));
|
||||
assertFalse(this.handler.supportsReturnType(this.missingSendToReturnType));
|
||||
assertTrue(this.handlerAnnotationNotRequired.supportsReturnType(this.missingSendToReturnType));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void replyToMethod() throws Exception {
|
||||
public void sendToMethod() throws Exception {
|
||||
|
||||
when(this.messageChannel.send(any(Message.class))).thenReturn(true);
|
||||
|
||||
String sessionId = "sess1";
|
||||
Message<?> inputMessage = createInputMessage(sessionId, "sub1", "/dest", null);
|
||||
|
||||
this.handler.handleReturnValue(payloadContent, this.replyToReturnType, inputMessage);
|
||||
this.handler.handleReturnValue(payloadContent, this.sendToReturnType, inputMessage);
|
||||
|
||||
verify(this.messageChannel, times(2)).send(this.messageCaptor.capture());
|
||||
|
||||
|
|
@ -132,7 +132,7 @@ public class ReplyToMethodReturnValueHandlerTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void replyToUserMethod() throws Exception {
|
||||
public void sendToUserMethod() throws Exception {
|
||||
|
||||
when(this.messageChannel.send(any(Message.class))).thenReturn(true);
|
||||
|
||||
|
|
@ -140,7 +140,7 @@ public class ReplyToMethodReturnValueHandlerTests {
|
|||
TestUser user = new TestUser();
|
||||
Message<?> inputMessage = createInputMessage(sessionId, "sub1", "/dest", user);
|
||||
|
||||
this.handler.handleReturnValue(payloadContent, this.replyToUserReturnType, inputMessage);
|
||||
this.handler.handleReturnValue(payloadContent, this.sendToUserReturnType, inputMessage);
|
||||
|
||||
verify(this.messageChannel, times(2)).send(this.messageCaptor.capture());
|
||||
|
||||
|
|
@ -181,19 +181,19 @@ public class ReplyToMethodReturnValueHandlerTests {
|
|||
}
|
||||
|
||||
@MessageMapping("/handle") // not needed for the tests but here for completeness
|
||||
public String handleWithMissingReplyTo() {
|
||||
public String handleWithMissingSendTo() {
|
||||
return payloadContent;
|
||||
}
|
||||
|
||||
@MessageMapping("/handle") // not needed for the tests but here for completeness
|
||||
@ReplyTo({"/dest1", "/dest2"})
|
||||
public String handleAndReplyTo() {
|
||||
@SendTo({"/dest1", "/dest2"})
|
||||
public String handleAndSendTo() {
|
||||
return payloadContent;
|
||||
}
|
||||
|
||||
@MessageMapping("/handle") // not needed for the tests but here for completeness
|
||||
@ReplyToUser({"/dest1", "/dest2"})
|
||||
public String handleAndReplyToUser() {
|
||||
@SendToUser({"/dest1", "/dest2"})
|
||||
public String handleAndSendToUser() {
|
||||
return payloadContent;
|
||||
}
|
||||
|
||||
|
|
@ -29,7 +29,7 @@ import org.springframework.core.MethodParameter;
|
|||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.messaging.handler.annotation.MessageMapping;
|
||||
import org.springframework.messaging.handler.annotation.ReplyTo;
|
||||
import org.springframework.messaging.handler.annotation.SendTo;
|
||||
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
|
||||
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||
import org.springframework.messaging.simp.annotation.SubscribeEvent;
|
||||
|
|
@ -61,7 +61,7 @@ public class SubscriptionMethodReturnValueHandlerTests {
|
|||
|
||||
private MethodParameter subscribeEventReturnType;
|
||||
|
||||
private MethodParameter subscribeEventReplyToReturnType;
|
||||
private MethodParameter subscribeEventSendToReturnType;
|
||||
|
||||
private MethodParameter messageMappingReturnType;
|
||||
|
||||
|
|
@ -83,8 +83,8 @@ public class SubscriptionMethodReturnValueHandlerTests {
|
|||
Method method = this.getClass().getDeclaredMethod("getData");
|
||||
this.subscribeEventReturnType = new MethodParameter(method, -1);
|
||||
|
||||
method = this.getClass().getDeclaredMethod("getDataAndReplyTo");
|
||||
this.subscribeEventReplyToReturnType = new MethodParameter(method, -1);
|
||||
method = this.getClass().getDeclaredMethod("getDataAndSendTo");
|
||||
this.subscribeEventSendToReturnType = new MethodParameter(method, -1);
|
||||
|
||||
method = this.getClass().getDeclaredMethod("handle");
|
||||
this.messageMappingReturnType = new MethodParameter(method, -1);
|
||||
|
|
@ -94,7 +94,7 @@ public class SubscriptionMethodReturnValueHandlerTests {
|
|||
@Test
|
||||
public void supportsReturnType() throws Exception {
|
||||
assertTrue(this.handler.supportsReturnType(this.subscribeEventReturnType));
|
||||
assertFalse(this.handler.supportsReturnType(this.subscribeEventReplyToReturnType));
|
||||
assertFalse(this.handler.supportsReturnType(this.subscribeEventSendToReturnType));
|
||||
assertFalse(this.handler.supportsReturnType(this.messageMappingReturnType));
|
||||
}
|
||||
|
||||
|
|
@ -138,8 +138,8 @@ public class SubscriptionMethodReturnValueHandlerTests {
|
|||
}
|
||||
|
||||
@SubscribeEvent("/data") // not needed for the tests but here for completeness
|
||||
@ReplyTo("/replyToDest")
|
||||
private String getDataAndReplyTo() {
|
||||
@SendTo("/sendToDest")
|
||||
private String getDataAndSendTo() {
|
||||
return payloadContent;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ import org.springframework.messaging.Message;
|
|||
import org.springframework.messaging.MessageHandler;
|
||||
import org.springframework.messaging.SubscribableChannel;
|
||||
import org.springframework.messaging.handler.annotation.MessageMapping;
|
||||
import org.springframework.messaging.handler.annotation.ReplyTo;
|
||||
import org.springframework.messaging.handler.annotation.SendTo;
|
||||
import org.springframework.messaging.handler.websocket.SubProtocolWebSocketHandler;
|
||||
import org.springframework.messaging.simp.SimpMessageType;
|
||||
import org.springframework.messaging.simp.annotation.SubscribeEvent;
|
||||
|
|
@ -284,7 +284,7 @@ public class WebSocketMessageBrokerConfigurationSupportTests {
|
|||
}
|
||||
|
||||
@MessageMapping("/foo")
|
||||
@ReplyTo("/bar")
|
||||
@SendTo("/bar")
|
||||
public String handleMessage() {
|
||||
return "bar";
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue