Use method signature to refine RSocket @MessageMapping
Before this change an @MessageMapping could be matched to any RSocket interaction type, which is arguably too flexible, makes it difficult to reason what would happen in case of a significant mismatch of cardinality, e.g. request for Fire-And-Forget (1-to-0) mapped to a method that returns Flux, and could result in payloads being ignored, or not seen unintentionally. This commit checks @ConnectMapping method on startup and rejects them if they return any values (sync or async). It also refines each @MessageMapping to match only the RSocket interaction type it fits based on the input and output cardinality of the handler method. Subsequently if a request is not matched, we'll do a second search to identify partial matches (by route only) and raise a helpful error that explains which interaction type is actually supported. The reference docs has been updated to explain the options. Closes gh-23999
This commit is contained in:
@@ -64,18 +64,34 @@ import org.springframework.messaging.Message;
|
||||
* authenticated user.</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>How the return value is handled depends on the processing scenario. For
|
||||
* STOMP over WebSocket, it is turned into a message and sent to a default response
|
||||
* destination or to a custom destination specified with an {@link SendTo @SendTo}
|
||||
* or {@link org.springframework.messaging.simp.annotation.SendToUser @SendToUser}
|
||||
* annotation. For RSocket, the response is used to reply to the stream request.
|
||||
* <p>Return value handling depends on the processing scenario:
|
||||
* <ul>
|
||||
* <li>STOMP over WebSocket -- the value is turned into a message and sent to a
|
||||
* default response destination or to a custom destination specified with an
|
||||
* {@link SendTo @SendTo} or
|
||||
* {@link org.springframework.messaging.simp.annotation.SendToUser @SendToUser}
|
||||
* annotation.
|
||||
* <li>RSocket -- the response is used to reply to the stream request.
|
||||
* </ul>
|
||||
*
|
||||
* <p>Specializations of this annotation including
|
||||
* {@link org.springframework.messaging.simp.annotation.SubscribeMapping @SubscribeMapping} or
|
||||
* <p>Specializations of this annotation include
|
||||
* {@link org.springframework.messaging.simp.annotation.SubscribeMapping @SubscribeMapping}
|
||||
* (e.g. STOMP subscriptions) and
|
||||
* {@link org.springframework.messaging.rsocket.annotation.ConnectMapping @ConnectMapping}
|
||||
* further narrow the mapping by message type. Both can be combined with a
|
||||
* type-level {@code @MessageMapping} for declaring a common pattern prefix
|
||||
* (or prefixes).
|
||||
* (e.g. RSocket connections). Both narrow the primary mapping further and also match
|
||||
* against the message type. Both can be combined with a type-level
|
||||
* {@code @MessageMapping} that declares a common pattern prefix (or prefixes).
|
||||
*
|
||||
* <p>For further details on the use of this annotation in different contexts,
|
||||
* see the following sections of the Spring Framework reference:
|
||||
* <ul>
|
||||
* <li>STOMP over WebSocket
|
||||
* <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#websocket-stomp-handle-annotations">
|
||||
* "Annotated Controllers"</a>.
|
||||
* <li>RSocket
|
||||
* <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#rsocket-annot-responders">
|
||||
* "Annotated Responders"</a>.
|
||||
* </ul>
|
||||
*
|
||||
* <p><b>NOTE:</b> When using controller interfaces (e.g. for AOP proxying),
|
||||
* make sure to consistently put <i>all</i> your mapping annotations - such as
|
||||
|
||||
@@ -232,6 +232,15 @@ public abstract class AbstractMethodMessageHandler<T>
|
||||
return CollectionUtils.unmodifiableMultiValueMap(this.destinationLookup);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the argument resolvers initialized during {@link #afterPropertiesSet()}.
|
||||
* Primarily for internal use in sub-classes.
|
||||
* @since 5.2.2
|
||||
*/
|
||||
protected HandlerMethodArgumentResolverComposite getArgumentResolvers() {
|
||||
return this.invocableHelper.getArgumentResolvers();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
@@ -377,6 +386,7 @@ public abstract class AbstractMethodMessageHandler<T>
|
||||
oldHandlerMethod.getBean() + "' bean method\n" + oldHandlerMethod + " mapped.");
|
||||
}
|
||||
|
||||
mapping = extendMapping(mapping, newHandlerMethod);
|
||||
this.handlerMethods.put(mapping, newHandlerMethod);
|
||||
|
||||
for (String pattern : getDirectLookupMappings(mapping)) {
|
||||
@@ -402,6 +412,21 @@ public abstract class AbstractMethodMessageHandler<T>
|
||||
return handlerMethod;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is invoked just before mappings are added. It allows
|
||||
* sub-classes to update the mapping with the {@link HandlerMethod} in mind.
|
||||
* This can be useful when the method signature is used to refine the
|
||||
* mapping, e.g. based on the cardinality of input and output.
|
||||
* <p>By default this method returns the mapping that is passed in.
|
||||
* @param mapping the mapping to be added
|
||||
* @param handlerMethod the target handler for the mapping
|
||||
* @return a new mapping or the same
|
||||
* @since 5.2.2
|
||||
*/
|
||||
protected T extendMapping(T mapping, HandlerMethod handlerMethod) {
|
||||
return mapping;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return String-based destinations for the given mapping, if any, that can
|
||||
* be used to find matches with a direct lookup (i.e. non-patterns).
|
||||
@@ -414,7 +439,13 @@ public abstract class AbstractMethodMessageHandler<T>
|
||||
|
||||
@Override
|
||||
public Mono<Void> handleMessage(Message<?> message) throws MessagingException {
|
||||
Match<T> match = getHandlerMethod(message);
|
||||
Match<T> match = null;
|
||||
try {
|
||||
match = getHandlerMethod(message);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
return Mono.error(ex);
|
||||
}
|
||||
if (match == null) {
|
||||
// handleNoMatch would have been invoked already
|
||||
return Mono.empty();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -38,7 +38,7 @@ import org.springframework.messaging.Message;
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 5.2
|
||||
*/
|
||||
class HandlerMethodArgumentResolverComposite implements HandlerMethodArgumentResolver {
|
||||
public class HandlerMethodArgumentResolverComposite implements HandlerMethodArgumentResolver {
|
||||
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
@@ -125,7 +125,7 @@ class HandlerMethodArgumentResolverComposite implements HandlerMethodArgumentRes
|
||||
* the given method parameter.
|
||||
*/
|
||||
@Nullable
|
||||
private HandlerMethodArgumentResolver getArgumentResolver(MethodParameter parameter) {
|
||||
public HandlerMethodArgumentResolver getArgumentResolver(MethodParameter parameter) {
|
||||
HandlerMethodArgumentResolver result = this.argumentResolverCache.get(parameter);
|
||||
if (result == null) {
|
||||
for (HandlerMethodArgumentResolver methodArgumentResolver : this.argumentResolvers) {
|
||||
|
||||
@@ -80,6 +80,14 @@ class InvocableHelper {
|
||||
this.argumentResolvers.addResolvers(resolvers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the configured resolvers.
|
||||
* @since 5.2.2
|
||||
*/
|
||||
public HandlerMethodArgumentResolverComposite getArgumentResolvers() {
|
||||
return this.argumentResolvers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the return value handlers to use for message handling and exception
|
||||
* handling methods.
|
||||
|
||||
@@ -56,6 +56,13 @@ public class RSocketFrameTypeMessageCondition extends AbstractMessageCondition<R
|
||||
}
|
||||
|
||||
|
||||
static final RSocketFrameTypeMessageCondition CONNECT_CONDITION =
|
||||
new RSocketFrameTypeMessageCondition(FrameType.SETUP, FrameType.METADATA_PUSH);
|
||||
|
||||
static final RSocketFrameTypeMessageCondition EMPTY_CONDITION = new RSocketFrameTypeMessageCondition();
|
||||
|
||||
|
||||
|
||||
private final Set<FrameType> frameTypes;
|
||||
|
||||
|
||||
@@ -68,6 +75,10 @@ public class RSocketFrameTypeMessageCondition extends AbstractMessageCondition<R
|
||||
this.frameTypes = Collections.unmodifiableSet(new LinkedHashSet<>(frameTypes));
|
||||
}
|
||||
|
||||
private RSocketFrameTypeMessageCondition() {
|
||||
this.frameTypes = Collections.emptySet();
|
||||
}
|
||||
|
||||
|
||||
public Set<FrameType> getFrameTypes() {
|
||||
return this.frameTypes;
|
||||
@@ -124,18 +135,71 @@ public class RSocketFrameTypeMessageCondition extends AbstractMessageCondition<R
|
||||
}
|
||||
|
||||
|
||||
/** Condition to match the initial SETUP frame and subsequent metadata pushes. */
|
||||
public static final RSocketFrameTypeMessageCondition CONNECT_CONDITION =
|
||||
new RSocketFrameTypeMessageCondition(
|
||||
FrameType.SETUP,
|
||||
FrameType.METADATA_PUSH);
|
||||
/**
|
||||
* Return a condition for matching the RSocket request interaction type with
|
||||
* that is selected based on the delcared request and response cardinality
|
||||
* of some handler method.
|
||||
* <p>The table below shows the selections made:
|
||||
* <table>
|
||||
* <tr>
|
||||
* <th>Request Cardinality</th>
|
||||
* <th>Response Cardinality</th>
|
||||
* <th>Interaction Types</th>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>0,1</td>
|
||||
* <td>0</td>
|
||||
* <td>Fire-And-Forget, Request-Response</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>0,1</td>
|
||||
* <td>1</td>
|
||||
* <td>Request-Response</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>0,1</td>
|
||||
* <td>2</td>
|
||||
* <td>Request-Stream</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>2</td>
|
||||
* <td>Any</td>
|
||||
* <td>Request-Channel</td>
|
||||
* </tr>
|
||||
* </table>
|
||||
* @param cardinalityIn -- the request cardinality: 1 for a single payload,
|
||||
* 2 for many payloads, and 0 if input is not handled.
|
||||
* @param cardinalityOut -- the response cardinality: 0 for no output
|
||||
* payloads, 1 for a single payload, and 2 for many payloads.
|
||||
* @return a condition to use for matching the interaction type
|
||||
* @since 5.2.2
|
||||
*/
|
||||
public static RSocketFrameTypeMessageCondition getCondition(int cardinalityIn, int cardinalityOut) {
|
||||
switch (cardinalityIn) {
|
||||
case 0:
|
||||
case 1:
|
||||
switch (cardinalityOut) {
|
||||
case 0: return FF_RR_CONDITION;
|
||||
case 1: return RR_CONDITION;
|
||||
case 2: return RS_CONDITION;
|
||||
default: throw new IllegalStateException("Invalid cardinality: " + cardinalityOut);
|
||||
}
|
||||
case 2:
|
||||
return RC_CONDITION;
|
||||
default:
|
||||
throw new IllegalStateException("Invalid cardinality: " + cardinalityIn);
|
||||
}
|
||||
}
|
||||
|
||||
/** Condition to match one of the 4 stream request types. */
|
||||
public static final RSocketFrameTypeMessageCondition REQUEST_CONDITION =
|
||||
new RSocketFrameTypeMessageCondition(
|
||||
FrameType.REQUEST_FNF,
|
||||
FrameType.REQUEST_RESPONSE,
|
||||
FrameType.REQUEST_STREAM,
|
||||
FrameType.REQUEST_CHANNEL);
|
||||
|
||||
private static final RSocketFrameTypeMessageCondition FF_CONDITION = from(FrameType.REQUEST_FNF);
|
||||
private static final RSocketFrameTypeMessageCondition RR_CONDITION = from(FrameType.REQUEST_RESPONSE);
|
||||
private static final RSocketFrameTypeMessageCondition RS_CONDITION = from(FrameType.REQUEST_STREAM);
|
||||
private static final RSocketFrameTypeMessageCondition RC_CONDITION = from(FrameType.REQUEST_CHANNEL);
|
||||
private static final RSocketFrameTypeMessageCondition FF_RR_CONDITION = FF_CONDITION.combine(RR_CONDITION);
|
||||
|
||||
private static RSocketFrameTypeMessageCondition from(FrameType... frameTypes) {
|
||||
return new RSocketFrameTypeMessageCondition(frameTypes);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,6 +19,9 @@ package org.springframework.messaging.rsocket.annotation.support;
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import io.rsocket.ConnectionSetupPayload;
|
||||
import io.rsocket.RSocket;
|
||||
@@ -28,6 +31,8 @@ import io.rsocket.metadata.WellKnownMimeType;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.ReactiveAdapter;
|
||||
import org.springframework.core.ReactiveAdapterRegistry;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.core.codec.Decoder;
|
||||
@@ -37,8 +42,11 @@ import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageDeliveryException;
|
||||
import org.springframework.messaging.handler.CompositeMessageCondition;
|
||||
import org.springframework.messaging.handler.DestinationPatternsMessageCondition;
|
||||
import org.springframework.messaging.handler.HandlerMethod;
|
||||
import org.springframework.messaging.handler.MessageCondition;
|
||||
import org.springframework.messaging.handler.annotation.MessageMapping;
|
||||
import org.springframework.messaging.handler.annotation.reactive.MessageMappingMessageHandler;
|
||||
import org.springframework.messaging.handler.annotation.reactive.PayloadMethodArgumentResolver;
|
||||
import org.springframework.messaging.handler.invocation.reactive.HandlerMethodReturnValueHandler;
|
||||
import org.springframework.messaging.rsocket.ClientRSocketFactoryConfigurer;
|
||||
import org.springframework.messaging.rsocket.MetadataExtractor;
|
||||
@@ -55,12 +63,27 @@ import org.springframework.util.StringUtils;
|
||||
* Extension of {@link MessageMappingMessageHandler} for handling RSocket
|
||||
* requests with {@link ConnectMapping @ConnectMapping} and
|
||||
* {@link MessageMapping @MessageMapping} methods.
|
||||
* <p>Use {@link #responder()} to obtain a {@link SocketAcceptor} adapter to
|
||||
* plug in as responder into an {@link io.rsocket.RSocketFactory}.
|
||||
* <p>Use {@link #clientResponder(RSocketStrategies, Object...)} to obtain a
|
||||
* client responder configurer
|
||||
*
|
||||
* <p>For server scenarios this class can be declared as a bean in Spring
|
||||
* configuration and that would detect {@code @MessageMapping} methods in
|
||||
* {@code @Controller} beans. What beans are checked can be changed through a
|
||||
* {@link #setHandlerPredicate(Predicate) handlerPredicate}. Given an instance
|
||||
* of this class, you can then use {@link #responder()} to obtain a
|
||||
* {@link SocketAcceptor} adapter to register with the
|
||||
* {@link io.rsocket.RSocketFactory}.
|
||||
*
|
||||
* <p>For client scenarios, possibly in the same process as a server, consider
|
||||
* consider using the static factory method
|
||||
* {@link #clientResponder(RSocketStrategies, Object...)} to obtain a client
|
||||
* responder to be registered with an
|
||||
* {@link org.springframework.messaging.rsocket.RSocketRequester.Builder#rsocketFactory
|
||||
* RSocketRequester}.
|
||||
* RSocketRequester.Builder}.
|
||||
*
|
||||
* <p>For {@code @MessageMapping} methods, this class automatically determines
|
||||
* the RSocket interaction type based on the input and output cardinality of the
|
||||
* method. See the
|
||||
* <a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#rsocket-annot-responders">
|
||||
* "Annotated Responders"</a> section of the Spring Framework reference for more details.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 5.2
|
||||
@@ -263,6 +286,17 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler {
|
||||
getArgumentResolverConfigurer().addCustomResolver(new RSocketRequesterMethodArgumentResolver());
|
||||
|
||||
super.afterPropertiesSet();
|
||||
|
||||
getHandlerMethods().forEach((composite, handler) -> {
|
||||
if (composite.getMessageConditions().contains(RSocketFrameTypeMessageCondition.CONNECT_CONDITION)) {
|
||||
MethodParameter returnType = handler.getReturnType();
|
||||
if (getCardinality(returnType) > 0) {
|
||||
throw new IllegalStateException(
|
||||
"Invalid @ConnectMapping method. " +
|
||||
"Return type must be void or a void async type: " + handler);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -279,10 +313,9 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler {
|
||||
protected CompositeMessageCondition getCondition(AnnotatedElement element) {
|
||||
MessageMapping ann1 = AnnotatedElementUtils.findMergedAnnotation(element, MessageMapping.class);
|
||||
if (ann1 != null && ann1.value().length > 0) {
|
||||
String[] patterns = processDestinations(ann1.value());
|
||||
return new CompositeMessageCondition(
|
||||
RSocketFrameTypeMessageCondition.REQUEST_CONDITION,
|
||||
new DestinationPatternsMessageCondition(patterns, obtainRouteMatcher()));
|
||||
RSocketFrameTypeMessageCondition.EMPTY_CONDITION,
|
||||
new DestinationPatternsMessageCondition(processDestinations(ann1.value()), obtainRouteMatcher()));
|
||||
}
|
||||
ConnectMapping ann2 = AnnotatedElementUtils.findMergedAnnotation(element, ConnectMapping.class);
|
||||
if (ann2 != null) {
|
||||
@@ -294,6 +327,45 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CompositeMessageCondition extendMapping(CompositeMessageCondition composite, HandlerMethod handler) {
|
||||
|
||||
List<MessageCondition<?>> conditions = composite.getMessageConditions();
|
||||
Assert.isTrue(conditions.size() == 2 &&
|
||||
conditions.get(0) instanceof RSocketFrameTypeMessageCondition &&
|
||||
conditions.get(1) instanceof DestinationPatternsMessageCondition,
|
||||
"Unexpected message condition types");
|
||||
|
||||
if (conditions.get(0) != RSocketFrameTypeMessageCondition.EMPTY_CONDITION) {
|
||||
return composite;
|
||||
}
|
||||
|
||||
int responseCardinality = getCardinality(handler.getReturnType());
|
||||
int requestCardinality = 0;
|
||||
for (MethodParameter parameter : handler.getMethodParameters()) {
|
||||
if (getArgumentResolvers().getArgumentResolver(parameter) instanceof PayloadMethodArgumentResolver) {
|
||||
requestCardinality = getCardinality(parameter);
|
||||
}
|
||||
}
|
||||
|
||||
return new CompositeMessageCondition(
|
||||
RSocketFrameTypeMessageCondition.getCondition(requestCardinality, responseCardinality),
|
||||
conditions.get(1));
|
||||
}
|
||||
|
||||
private int getCardinality(MethodParameter parameter) {
|
||||
Class<?> clazz = parameter.getParameterType();
|
||||
ReactiveAdapter adapter = getReactiveAdapterRegistry().getAdapter(clazz);
|
||||
if (adapter == null) {
|
||||
return clazz.equals(void.class) ? 0 : 1;
|
||||
}
|
||||
else if (parameter.nested().getNestedParameterType().equals(Void.class)) {
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
return adapter.isMultiValue() ? 2 : 1;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleNoMatch(@Nullable RouteMatcher.Route destination, Message<?> message) {
|
||||
@@ -306,7 +378,18 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler {
|
||||
logger.warn("No handler for fireAndForget to '" + destination + "'");
|
||||
return;
|
||||
}
|
||||
throw new MessageDeliveryException("No handler for destination '" + destination + "'");
|
||||
|
||||
Set<FrameType> frameTypes = getHandlerMethods().keySet().stream()
|
||||
.map(CompositeMessageCondition::getMessageConditions)
|
||||
.filter(conditions -> conditions.get(1).getMatchingCondition(message) != null)
|
||||
.map(conditions -> (RSocketFrameTypeMessageCondition) conditions.get(0))
|
||||
.flatMap(condition -> condition.getFrameTypes().stream())
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
throw new MessageDeliveryException(frameTypes.isEmpty() ?
|
||||
"No handler for destination '" + destination + "'" :
|
||||
"Destination '" + destination + "' does not support " + frameType + ". " +
|
||||
"Supported interaction(s): " + frameTypes);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user