Rename @ReplyTo to @SendTo

This commit is contained in:
Rossen Stoyanchev 2013-09-08 21:22:53 -04:00
parent 41fa15a484
commit 45eab23e15
8 changed files with 65 additions and 65 deletions

View File

@ -35,7 +35,7 @@ import org.springframework.messaging.Message;
@Target(ElementType.METHOD) @Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Documented @Documented
public @interface ReplyTo { public @interface SendTo {
/** /**
* The destination for a message created from the return value of a method. * The destination for a message created from the return value of a method.

View File

@ -37,13 +37,13 @@ import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 4.0 * @since 4.0
* *
* @see org.springframework.messaging.handler.annotation.ReplyTo * @see org.springframework.messaging.handler.annotation.SendTo
* @see org.springframework.messaging.simp.handler.UserDestinationMessageHandler * @see org.springframework.messaging.simp.handler.UserDestinationMessageHandler
*/ */
@Target(ElementType.METHOD) @Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Documented @Documented
public @interface ReplyToUser { public @interface SendToUser {
/** /**
* The destination for a message based on the return value of a method. * The destination for a message based on the return value of a method.

View File

@ -23,19 +23,19 @@ import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.messaging.Message; import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel; import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.core.MessagePostProcessor; 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.handler.method.HandlerMethodReturnValueHandler;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor; import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessageSendingOperations; 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.messaging.support.MessageBuilder;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
/** /**
* A {@link HandlerMethodReturnValueHandler} for replying to destinations specified in a * A {@link HandlerMethodReturnValueHandler} for sending to destinations specified in a
* {@link ReplyTo} or {@link ReplyToUser} method-level annotations. * {@link SendTo} or {@link SendToUser} method-level annotations.
* <p> * <p>
* The value returned from the method is converted, and turned to a {@link Message} and * The value returned from the method is converted, and turned to a {@link Message} and
* sent through the provided {@link MessageChannel}. The * sent through the provided {@link MessageChannel}. The
@ -46,14 +46,14 @@ import org.springframework.util.ObjectUtils;
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 4.0 * @since 4.0
*/ */
public class ReplyToMethodReturnValueHandler implements HandlerMethodReturnValueHandler { public class SendToMethodReturnValueHandler implements HandlerMethodReturnValueHandler {
private final SimpMessageSendingOperations messagingTemplate; private final SimpMessageSendingOperations messagingTemplate;
private final boolean annotationRequired; private final boolean annotationRequired;
public ReplyToMethodReturnValueHandler(SimpMessageSendingOperations messagingTemplate, boolean annotationRequired) { public SendToMethodReturnValueHandler(SimpMessageSendingOperations messagingTemplate, boolean annotationRequired) {
Assert.notNull(messagingTemplate, "messagingTemplate is required"); Assert.notNull(messagingTemplate, "messagingTemplate is required");
this.messagingTemplate = messagingTemplate; this.messagingTemplate = messagingTemplate;
this.annotationRequired = annotationRequired; this.annotationRequired = annotationRequired;
@ -62,8 +62,8 @@ public class ReplyToMethodReturnValueHandler implements HandlerMethodReturnValue
@Override @Override
public boolean supportsReturnType(MethodParameter returnType) { public boolean supportsReturnType(MethodParameter returnType) {
if ((returnType.getMethodAnnotation(ReplyTo.class) != null) || if ((returnType.getMethodAnnotation(SendTo.class) != null) ||
(returnType.getMethodAnnotation(ReplyToUser.class) != null)) { (returnType.getMethodAnnotation(SendToUser.class) != null)) {
return true; return true;
} }
return (!this.annotationRequired); return (!this.annotationRequired);
@ -82,21 +82,21 @@ public class ReplyToMethodReturnValueHandler implements HandlerMethodReturnValue
String sessionId = inputHeaders.getSessionId(); String sessionId = inputHeaders.getSessionId();
MessagePostProcessor postProcessor = new SessionHeaderPostProcessor(sessionId); MessagePostProcessor postProcessor = new SessionHeaderPostProcessor(sessionId);
ReplyToUser replyToUser = returnType.getMethodAnnotation(ReplyToUser.class); SendToUser sendToUser = returnType.getMethodAnnotation(SendToUser.class);
if (replyToUser != null) { if (sendToUser != null) {
if (inputHeaders.getUser() == null) { if (inputHeaders.getUser() == null) {
throw new MissingSessionUserException(inputMessage); throw new MissingSessionUserException(inputMessage);
} }
String user = inputHeaders.getUser().getName(); 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); this.messagingTemplate.convertAndSendToUser(user, destination, returnValue, postProcessor);
} }
return; return;
} }
ReplyTo replyTo = returnType.getMethodAnnotation(ReplyTo.class); SendTo sendTo = returnType.getMethodAnnotation(SendTo.class);
if (replyTo != null) { if (sendTo != null) {
for (String destination : getDestinations(replyTo, inputHeaders.getDestination())) { for (String destination : getDestinations(sendTo, inputHeaders.getDestination())) {
this.messagingTemplate.convertAndSend(destination, returnValue, postProcessor); this.messagingTemplate.convertAndSend(destination, returnValue, postProcessor);
} }
return; return;
@ -129,7 +129,7 @@ public class ReplyToMethodReturnValueHandler implements HandlerMethodReturnValue
@Override @Override
public String toString() { public String toString() {
return "ReplyToMethodReturnValueHandler [annotationRequired=" + annotationRequired + "]"; return "SendToMethodReturnValueHandler [annotationRequired=" + annotationRequired + "]";
} }
} }

View File

@ -20,10 +20,10 @@ import org.springframework.core.MethodParameter;
import org.springframework.messaging.Message; import org.springframework.messaging.Message;
import org.springframework.messaging.core.MessagePostProcessor; import org.springframework.messaging.core.MessagePostProcessor;
import org.springframework.messaging.core.MessageSendingOperations; 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.handler.method.HandlerMethodReturnValueHandler;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor; 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.simp.annotation.SubscribeEvent;
import org.springframework.messaging.support.MessageBuilder; import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -32,7 +32,7 @@ import org.springframework.util.Assert;
/** /**
* A {@link HandlerMethodReturnValueHandler} for replying directly to a subscription. It * A {@link HandlerMethodReturnValueHandler} for replying directly to a subscription. It
* supports methods annotated with {@link SubscribeEvent} unless they're also annotated * supports methods annotated with {@link SubscribeEvent} unless they're also annotated
* with {@link ReplyTo} or {@link ReplyToUser}. * with {@link SendTo} or {@link SendToUser}.
* <p> * <p>
* The value returned from the method is converted, and turned to a {@link Message} and * 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. * then enriched with the sessionId, subscriptionId, and destination of the input message.
@ -55,8 +55,8 @@ public class SubscriptionMethodReturnValueHandler implements HandlerMethodReturn
@Override @Override
public boolean supportsReturnType(MethodParameter returnType) { public boolean supportsReturnType(MethodParameter returnType) {
return ((returnType.getMethodAnnotation(SubscribeEvent.class) != null) return ((returnType.getMethodAnnotation(SubscribeEvent.class) != null)
&& (returnType.getMethodAnnotation(ReplyTo.class) == null) && (returnType.getMethodAnnotation(SendTo.class) == null)
&& (returnType.getMethodAnnotation(ReplyToUser.class) == null)); && (returnType.getMethodAnnotation(SendToUser.class) == null));
} }
@Override @Override

View File

@ -58,7 +58,7 @@ import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SubscribeEvent; import org.springframework.messaging.simp.annotation.SubscribeEvent;
import org.springframework.messaging.simp.annotation.UnsubscribeEvent; import org.springframework.messaging.simp.annotation.UnsubscribeEvent;
import org.springframework.messaging.simp.annotation.support.PrincipalMethodArgumentResolver; 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.simp.annotation.support.SubscriptionMethodReturnValueHandler;
import org.springframework.messaging.support.MessageBuilder; import org.springframework.messaging.support.MessageBuilder;
import org.springframework.messaging.support.converter.MessageConverter; import org.springframework.messaging.support.converter.MessageConverter;
@ -201,14 +201,14 @@ public class AnnotationMethodMessageHandler implements MessageHandler, Applicati
this.argumentResolvers.addResolver(new MessageBodyMethodArgumentResolver(this.messageConverter)); this.argumentResolvers.addResolver(new MessageBodyMethodArgumentResolver(this.messageConverter));
// Annotation-based return value types // 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)); this.returnValueHandlers.addHandler(new SubscriptionMethodReturnValueHandler(this.webSocketResponseTemplate));
// custom return value types // custom return value types
this.returnValueHandlers.addHandlers(this.customReturnValueHandlers); this.returnValueHandlers.addHandlers(this.customReturnValueHandlers);
// catch-all // catch-all
this.returnValueHandlers.addHandler(new ReplyToMethodReturnValueHandler(this.brokerTemplate, false)); this.returnValueHandlers.addHandler(new SendToMethodReturnValueHandler(this.brokerTemplate, false));
} }
protected final void initHandlerMethods() { protected final void initHandlerMethods() {

View File

@ -31,10 +31,10 @@ import org.springframework.core.MethodParameter;
import org.springframework.messaging.Message; import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel; import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.handler.annotation.MessageMapping; 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.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessagingTemplate; 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.MessageBuilder;
import org.springframework.messaging.support.converter.MessageConverter; 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 * @author Rossen Stoyanchev
*/ */
public class ReplyToMethodReturnValueHandlerTests { public class SendToMethodReturnValueHandlerTests {
private static final String payloadContent = "payload"; private static final String payloadContent = "payload";
private ReplyToMethodReturnValueHandler handler; private SendToMethodReturnValueHandler handler;
private ReplyToMethodReturnValueHandler handlerAnnotationNotRequired; private SendToMethodReturnValueHandler handlerAnnotationNotRequired;
@Mock private MessageChannel messageChannel; @Mock private MessageChannel messageChannel;
@ -63,11 +63,11 @@ public class ReplyToMethodReturnValueHandlerTests {
@Mock private MessageConverter messageConverter; @Mock private MessageConverter messageConverter;
private MethodParameter replyToReturnType; private MethodParameter sendToReturnType;
private MethodParameter replyToUserReturnType; private MethodParameter sendToUserReturnType;
private MethodParameter missingReplyToReturnType; private MethodParameter missingSendToReturnType;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -82,37 +82,37 @@ public class ReplyToMethodReturnValueHandlerTests {
SimpMessagingTemplate messagingTemplate = new SimpMessagingTemplate(this.messageChannel); SimpMessagingTemplate messagingTemplate = new SimpMessagingTemplate(this.messageChannel);
messagingTemplate.setConverter(this.messageConverter); messagingTemplate.setConverter(this.messageConverter);
this.handler = new ReplyToMethodReturnValueHandler(messagingTemplate, true); this.handler = new SendToMethodReturnValueHandler(messagingTemplate, true);
this.handlerAnnotationNotRequired = new ReplyToMethodReturnValueHandler(messagingTemplate, false); this.handlerAnnotationNotRequired = new SendToMethodReturnValueHandler(messagingTemplate, false);
Method method = this.getClass().getDeclaredMethod("handleAndReplyTo"); Method method = this.getClass().getDeclaredMethod("handleAndSendTo");
this.replyToReturnType = new MethodParameter(method, -1); this.sendToReturnType = new MethodParameter(method, -1);
method = this.getClass().getDeclaredMethod("handleAndReplyToUser"); method = this.getClass().getDeclaredMethod("handleAndSendToUser");
this.replyToUserReturnType = new MethodParameter(method, -1); this.sendToUserReturnType = new MethodParameter(method, -1);
method = this.getClass().getDeclaredMethod("handleWithMissingReplyTo"); method = this.getClass().getDeclaredMethod("handleWithMissingSendTo");
this.missingReplyToReturnType = new MethodParameter(method, -1); this.missingSendToReturnType = new MethodParameter(method, -1);
} }
@Test @Test
public void supportsReturnType() throws Exception { public void supportsReturnType() throws Exception {
assertTrue(this.handler.supportsReturnType(this.replyToReturnType)); assertTrue(this.handler.supportsReturnType(this.sendToReturnType));
assertTrue(this.handler.supportsReturnType(this.replyToUserReturnType)); assertTrue(this.handler.supportsReturnType(this.sendToUserReturnType));
assertFalse(this.handler.supportsReturnType(this.missingReplyToReturnType)); assertFalse(this.handler.supportsReturnType(this.missingSendToReturnType));
assertTrue(this.handlerAnnotationNotRequired.supportsReturnType(this.missingReplyToReturnType)); assertTrue(this.handlerAnnotationNotRequired.supportsReturnType(this.missingSendToReturnType));
} }
@Test @Test
public void replyToMethod() throws Exception { public void sendToMethod() throws Exception {
when(this.messageChannel.send(any(Message.class))).thenReturn(true); when(this.messageChannel.send(any(Message.class))).thenReturn(true);
String sessionId = "sess1"; String sessionId = "sess1";
Message<?> inputMessage = createInputMessage(sessionId, "sub1", "/dest", null); 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()); verify(this.messageChannel, times(2)).send(this.messageCaptor.capture());
@ -132,7 +132,7 @@ public class ReplyToMethodReturnValueHandlerTests {
} }
@Test @Test
public void replyToUserMethod() throws Exception { public void sendToUserMethod() throws Exception {
when(this.messageChannel.send(any(Message.class))).thenReturn(true); when(this.messageChannel.send(any(Message.class))).thenReturn(true);
@ -140,7 +140,7 @@ public class ReplyToMethodReturnValueHandlerTests {
TestUser user = new TestUser(); TestUser user = new TestUser();
Message<?> inputMessage = createInputMessage(sessionId, "sub1", "/dest", user); 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()); 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 @MessageMapping("/handle") // not needed for the tests but here for completeness
public String handleWithMissingReplyTo() { public String handleWithMissingSendTo() {
return payloadContent; return payloadContent;
} }
@MessageMapping("/handle") // not needed for the tests but here for completeness @MessageMapping("/handle") // not needed for the tests but here for completeness
@ReplyTo({"/dest1", "/dest2"}) @SendTo({"/dest1", "/dest2"})
public String handleAndReplyTo() { public String handleAndSendTo() {
return payloadContent; return payloadContent;
} }
@MessageMapping("/handle") // not needed for the tests but here for completeness @MessageMapping("/handle") // not needed for the tests but here for completeness
@ReplyToUser({"/dest1", "/dest2"}) @SendToUser({"/dest1", "/dest2"})
public String handleAndReplyToUser() { public String handleAndSendToUser() {
return payloadContent; return payloadContent;
} }

View File

@ -29,7 +29,7 @@ import org.springframework.core.MethodParameter;
import org.springframework.messaging.Message; import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel; import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.handler.annotation.MessageMapping; 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.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SubscribeEvent; import org.springframework.messaging.simp.annotation.SubscribeEvent;
@ -61,7 +61,7 @@ public class SubscriptionMethodReturnValueHandlerTests {
private MethodParameter subscribeEventReturnType; private MethodParameter subscribeEventReturnType;
private MethodParameter subscribeEventReplyToReturnType; private MethodParameter subscribeEventSendToReturnType;
private MethodParameter messageMappingReturnType; private MethodParameter messageMappingReturnType;
@ -83,8 +83,8 @@ public class SubscriptionMethodReturnValueHandlerTests {
Method method = this.getClass().getDeclaredMethod("getData"); Method method = this.getClass().getDeclaredMethod("getData");
this.subscribeEventReturnType = new MethodParameter(method, -1); this.subscribeEventReturnType = new MethodParameter(method, -1);
method = this.getClass().getDeclaredMethod("getDataAndReplyTo"); method = this.getClass().getDeclaredMethod("getDataAndSendTo");
this.subscribeEventReplyToReturnType = new MethodParameter(method, -1); this.subscribeEventSendToReturnType = new MethodParameter(method, -1);
method = this.getClass().getDeclaredMethod("handle"); method = this.getClass().getDeclaredMethod("handle");
this.messageMappingReturnType = new MethodParameter(method, -1); this.messageMappingReturnType = new MethodParameter(method, -1);
@ -94,7 +94,7 @@ public class SubscriptionMethodReturnValueHandlerTests {
@Test @Test
public void supportsReturnType() throws Exception { public void supportsReturnType() throws Exception {
assertTrue(this.handler.supportsReturnType(this.subscribeEventReturnType)); assertTrue(this.handler.supportsReturnType(this.subscribeEventReturnType));
assertFalse(this.handler.supportsReturnType(this.subscribeEventReplyToReturnType)); assertFalse(this.handler.supportsReturnType(this.subscribeEventSendToReturnType));
assertFalse(this.handler.supportsReturnType(this.messageMappingReturnType)); assertFalse(this.handler.supportsReturnType(this.messageMappingReturnType));
} }
@ -138,8 +138,8 @@ public class SubscriptionMethodReturnValueHandlerTests {
} }
@SubscribeEvent("/data") // not needed for the tests but here for completeness @SubscribeEvent("/data") // not needed for the tests but here for completeness
@ReplyTo("/replyToDest") @SendTo("/sendToDest")
private String getDataAndReplyTo() { private String getDataAndSendTo() {
return payloadContent; return payloadContent;
} }

View File

@ -30,7 +30,7 @@ import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler; import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.SubscribableChannel; import org.springframework.messaging.SubscribableChannel;
import org.springframework.messaging.handler.annotation.MessageMapping; 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.handler.websocket.SubProtocolWebSocketHandler;
import org.springframework.messaging.simp.SimpMessageType; import org.springframework.messaging.simp.SimpMessageType;
import org.springframework.messaging.simp.annotation.SubscribeEvent; import org.springframework.messaging.simp.annotation.SubscribeEvent;
@ -284,7 +284,7 @@ public class WebSocketMessageBrokerConfigurationSupportTests {
} }
@MessageMapping("/foo") @MessageMapping("/foo")
@ReplyTo("/bar") @SendTo("/bar")
public String handleMessage() { public String handleMessage() {
return "bar"; return "bar";
} }