Search in sources :

Example 1 with CompositeMessageCondition

use of org.springframework.messaging.handler.CompositeMessageCondition in project spring-framework by spring-projects.

the class MessageMappingMessageHandler method getCondition.

/**
 * Determine the mapping condition for the given annotated element.
 * @param element the element to check
 * @return the condition, or {@code null}
 */
@Nullable
protected CompositeMessageCondition getCondition(AnnotatedElement element) {
    MessageMapping ann = AnnotatedElementUtils.findMergedAnnotation(element, MessageMapping.class);
    if (ann == null || ann.value().length == 0) {
        return null;
    }
    String[] patterns = processDestinations(ann.value());
    return new CompositeMessageCondition(new DestinationPatternsMessageCondition(patterns, obtainRouteMatcher()));
}
Also used : CompositeMessageCondition(org.springframework.messaging.handler.CompositeMessageCondition) DestinationPatternsMessageCondition(org.springframework.messaging.handler.DestinationPatternsMessageCondition) MessageMapping(org.springframework.messaging.handler.annotation.MessageMapping) Nullable(org.springframework.lang.Nullable)

Example 2 with CompositeMessageCondition

use of org.springframework.messaging.handler.CompositeMessageCondition in project spring-framework by spring-projects.

the class RSocketMessageHandler method getCondition.

@Override
@Nullable
protected CompositeMessageCondition getCondition(AnnotatedElement element) {
    MessageMapping ann1 = AnnotatedElementUtils.findMergedAnnotation(element, MessageMapping.class);
    if (ann1 != null && ann1.value().length > 0) {
        return new CompositeMessageCondition(RSocketFrameTypeMessageCondition.EMPTY_CONDITION, new DestinationPatternsMessageCondition(processDestinations(ann1.value()), obtainRouteMatcher()));
    }
    ConnectMapping ann2 = AnnotatedElementUtils.findMergedAnnotation(element, ConnectMapping.class);
    if (ann2 != null) {
        String[] patterns = processDestinations(ann2.value());
        return new CompositeMessageCondition(RSocketFrameTypeMessageCondition.CONNECT_CONDITION, new DestinationPatternsMessageCondition(patterns, obtainRouteMatcher()));
    }
    return null;
}
Also used : CompositeMessageCondition(org.springframework.messaging.handler.CompositeMessageCondition) DestinationPatternsMessageCondition(org.springframework.messaging.handler.DestinationPatternsMessageCondition) ConnectMapping(org.springframework.messaging.rsocket.annotation.ConnectMapping) MessageMapping(org.springframework.messaging.handler.annotation.MessageMapping) Nullable(org.springframework.lang.Nullable)

Example 3 with CompositeMessageCondition

use of org.springframework.messaging.handler.CompositeMessageCondition in project spring-framework by spring-projects.

the class RSocketMessageHandler method extendMapping.

@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));
}
Also used : DestinationPatternsMessageCondition(org.springframework.messaging.handler.DestinationPatternsMessageCondition) MessageCondition(org.springframework.messaging.handler.MessageCondition) CompositeMessageCondition(org.springframework.messaging.handler.CompositeMessageCondition) CompositeMessageCondition(org.springframework.messaging.handler.CompositeMessageCondition) DestinationPatternsMessageCondition(org.springframework.messaging.handler.DestinationPatternsMessageCondition) PayloadMethodArgumentResolver(org.springframework.messaging.handler.annotation.reactive.PayloadMethodArgumentResolver) MethodParameter(org.springframework.core.MethodParameter)

Example 4 with CompositeMessageCondition

use of org.springframework.messaging.handler.CompositeMessageCondition in project spring-framework by spring-projects.

the class RSocketMessageHandlerTests method testMapping.

private static void testMapping(Object controller, String... expectedPatterns) {
    RSocketMessageHandler handler = new RSocketMessageHandler();
    handler.setDecoders(Collections.singletonList(StringDecoder.allMimeTypes()));
    handler.setEncoders(Collections.singletonList(CharSequenceEncoder.allMimeTypes()));
    handler.setHandlers(Collections.singletonList(controller));
    handler.afterPropertiesSet();
    Map<CompositeMessageCondition, HandlerMethod> map = handler.getHandlerMethods();
    assertThat(map).hasSize(1);
    CompositeMessageCondition condition = map.entrySet().iterator().next().getKey();
    RSocketFrameTypeMessageCondition c1 = condition.getCondition(RSocketFrameTypeMessageCondition.class);
    assertThat(c1.getFrameTypes()).contains(FrameType.SETUP, FrameType.METADATA_PUSH);
    DestinationPatternsMessageCondition c2 = condition.getCondition(DestinationPatternsMessageCondition.class);
    if (ObjectUtils.isEmpty(expectedPatterns)) {
        assertThat(c2.getPatterns()).isEmpty();
    } else {
        assertThat(c2.getPatterns()).contains(expectedPatterns);
    }
}
Also used : CompositeMessageCondition(org.springframework.messaging.handler.CompositeMessageCondition) DestinationPatternsMessageCondition(org.springframework.messaging.handler.DestinationPatternsMessageCondition) HandlerMethod(org.springframework.messaging.handler.HandlerMethod)

Example 5 with CompositeMessageCondition

use of org.springframework.messaging.handler.CompositeMessageCondition in project spring-framework by spring-projects.

the class RSocketMessageHandler method handleNoMatch.

@Override
protected void handleNoMatch(@Nullable RouteMatcher.Route destination, Message<?> message) {
    FrameType frameType = RSocketFrameTypeMessageCondition.getFrameType(message);
    if (frameType == FrameType.SETUP || frameType == FrameType.METADATA_PUSH) {
        // optional handling
        return;
    }
    if (frameType == FrameType.REQUEST_FNF) {
        // Can't propagate error to client, so just log
        logger.warn("No handler for fireAndForget to '" + destination + "'");
        return;
    }
    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);
}
Also used : AnnotatedElementUtils(org.springframework.core.annotation.AnnotatedElementUtils) Decoder(org.springframework.core.codec.Decoder) DestinationPatternsMessageCondition(org.springframework.messaging.handler.DestinationPatternsMessageCondition) MetadataExtractor(org.springframework.messaging.rsocket.MetadataExtractor) RouteMatcher(org.springframework.util.RouteMatcher) HandlerMethodReturnValueHandler(org.springframework.messaging.handler.invocation.reactive.HandlerMethodReturnValueHandler) ConnectMapping(org.springframework.messaging.rsocket.annotation.ConnectMapping) ArrayList(java.util.ArrayList) MimeType(org.springframework.util.MimeType) WellKnownMimeType(io.rsocket.metadata.WellKnownMimeType) RSocket(io.rsocket.RSocket) MethodParameter(org.springframework.core.MethodParameter) Nullable(org.springframework.lang.Nullable) Message(org.springframework.messaging.Message) RSocketStrategies(org.springframework.messaging.rsocket.RSocketStrategies) Encoder(org.springframework.core.codec.Encoder) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) RSocketRequester(org.springframework.messaging.rsocket.RSocketRequester) FrameType(io.rsocket.frame.FrameType) ReactiveAdapter(org.springframework.core.ReactiveAdapter) MessageCondition(org.springframework.messaging.handler.MessageCondition) Predicate(java.util.function.Predicate) Set(java.util.Set) Mono(reactor.core.publisher.Mono) MimeTypeUtils(org.springframework.util.MimeTypeUtils) SocketAcceptor(io.rsocket.SocketAcceptor) Collectors(java.util.stream.Collectors) MessageMapping(org.springframework.messaging.handler.annotation.MessageMapping) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) List(java.util.List) CompositeMessageCondition(org.springframework.messaging.handler.CompositeMessageCondition) MessageMappingMessageHandler(org.springframework.messaging.handler.annotation.reactive.MessageMappingMessageHandler) HandlerMethod(org.springframework.messaging.handler.HandlerMethod) ConnectionSetupPayload(io.rsocket.ConnectionSetupPayload) PayloadMethodArgumentResolver(org.springframework.messaging.handler.annotation.reactive.PayloadMethodArgumentResolver) AnnotatedElement(java.lang.reflect.AnnotatedElement) BeanUtils(org.springframework.beans.BeanUtils) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) CompositeMessageCondition(org.springframework.messaging.handler.CompositeMessageCondition) FrameType(io.rsocket.frame.FrameType) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException)

Aggregations

CompositeMessageCondition (org.springframework.messaging.handler.CompositeMessageCondition)5 DestinationPatternsMessageCondition (org.springframework.messaging.handler.DestinationPatternsMessageCondition)5 Nullable (org.springframework.lang.Nullable)3 MessageMapping (org.springframework.messaging.handler.annotation.MessageMapping)3 MethodParameter (org.springframework.core.MethodParameter)2 HandlerMethod (org.springframework.messaging.handler.HandlerMethod)2 MessageCondition (org.springframework.messaging.handler.MessageCondition)2 PayloadMethodArgumentResolver (org.springframework.messaging.handler.annotation.reactive.PayloadMethodArgumentResolver)2 ConnectMapping (org.springframework.messaging.rsocket.annotation.ConnectMapping)2 ConnectionSetupPayload (io.rsocket.ConnectionSetupPayload)1 RSocket (io.rsocket.RSocket)1 SocketAcceptor (io.rsocket.SocketAcceptor)1 FrameType (io.rsocket.frame.FrameType)1 WellKnownMimeType (io.rsocket.metadata.WellKnownMimeType)1 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Set (java.util.Set)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1