Search in sources :

Example 6 with SimpAnnotationMethodMessageHandler

use of org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler in project spring-framework by spring-projects.

the class MessageBrokerConfigurationTests method simpValidatorInjected.

@Test
public void simpValidatorInjected() {
    ApplicationContext context = loadConfig(SimpleBrokerConfig.class);
    SimpAnnotationMethodMessageHandler messageHandler = context.getBean(SimpAnnotationMethodMessageHandler.class);
    assertThat(messageHandler.getValidator()).isNotNull();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) SimpAnnotationMethodMessageHandler(org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler) Test(org.junit.jupiter.api.Test)

Example 7 with SimpAnnotationMethodMessageHandler

use of org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler in project spring-framework by spring-projects.

the class MessageBrokerConfigurationTests method brokerChannelUsedByAnnotatedMethod.

@Test
public void brokerChannelUsedByAnnotatedMethod() {
    ApplicationContext context = loadConfig(SimpleBrokerConfig.class);
    TestChannel channel = context.getBean("brokerChannel", TestChannel.class);
    SimpAnnotationMethodMessageHandler messageHandler = context.getBean(SimpAnnotationMethodMessageHandler.class);
    StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SEND);
    headers.setSessionId("sess1");
    headers.setSessionAttributes(new ConcurrentHashMap<>());
    headers.setDestination("/foo");
    Message<?> message = MessageBuilder.createMessage(new byte[0], headers.getMessageHeaders());
    messageHandler.handleMessage(message);
    message = channel.messages.get(0);
    headers = StompHeaderAccessor.wrap(message);
    assertThat(headers.getMessageType()).isEqualTo(SimpMessageType.MESSAGE);
    assertThat(headers.getDestination()).isEqualTo("/bar");
    assertThat(new String((byte[]) message.getPayload())).isEqualTo("bar");
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) SimpAnnotationMethodMessageHandler(org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler) StompHeaderAccessor(org.springframework.messaging.simp.stomp.StompHeaderAccessor) Test(org.junit.jupiter.api.Test)

Example 8 with SimpAnnotationMethodMessageHandler

use of org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler in project spring-framework by spring-projects.

the class TestValidator method customArgumentAndReturnValueTypes.

@Test
public void customArgumentAndReturnValueTypes() {
    loadBeanDefinitions("websocket-config-broker-custom-argument-and-return-value-types.xml");
    SimpAnnotationMethodMessageHandler handler = this.appContext.getBean(SimpAnnotationMethodMessageHandler.class);
    List<HandlerMethodArgumentResolver> customResolvers = handler.getCustomArgumentResolvers();
    assertThat(customResolvers.size()).isEqualTo(2);
    assertThat(handler.getArgumentResolvers().contains(customResolvers.get(0))).isTrue();
    assertThat(handler.getArgumentResolvers().contains(customResolvers.get(1))).isTrue();
    List<HandlerMethodReturnValueHandler> customHandlers = handler.getCustomReturnValueHandlers();
    assertThat(customHandlers.size()).isEqualTo(2);
    assertThat(handler.getReturnValueHandlers().contains(customHandlers.get(0))).isTrue();
    assertThat(handler.getReturnValueHandlers().contains(customHandlers.get(1))).isTrue();
}
Also used : HandlerMethodReturnValueHandler(org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler) SimpAnnotationMethodMessageHandler(org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler) HandlerMethodArgumentResolver(org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver) Test(org.junit.jupiter.api.Test)

Example 9 with SimpAnnotationMethodMessageHandler

use of org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler in project spring-framework by spring-projects.

the class TestValidator method annotationMethodMessageHandler.

@Test
public void annotationMethodMessageHandler() {
    loadBeanDefinitions("websocket-config-broker-simple.xml");
    SimpAnnotationMethodMessageHandler annotationMethodMessageHandler = this.appContext.getBean(SimpAnnotationMethodMessageHandler.class);
    assertThat(annotationMethodMessageHandler).isNotNull();
    MessageConverter messageConverter = annotationMethodMessageHandler.getMessageConverter();
    assertThat(messageConverter).isNotNull();
    assertThat(messageConverter instanceof CompositeMessageConverter).isTrue();
    String name = MessageBrokerBeanDefinitionParser.MESSAGE_CONVERTER_BEAN_NAME;
    CompositeMessageConverter compositeMessageConverter = this.appContext.getBean(name, CompositeMessageConverter.class);
    assertThat(compositeMessageConverter).isNotNull();
    name = MessageBrokerBeanDefinitionParser.MESSAGING_TEMPLATE_BEAN_NAME;
    SimpMessagingTemplate simpMessagingTemplate = this.appContext.getBean(name, SimpMessagingTemplate.class);
    assertThat(simpMessagingTemplate).isNotNull();
    assertThat(simpMessagingTemplate.getUserDestinationPrefix()).isEqualTo("/personal/");
    List<MessageConverter> converters = compositeMessageConverter.getConverters();
    assertThat(converters).hasSize(3);
    assertThat(converters.get(0)).isInstanceOf(StringMessageConverter.class);
    assertThat(converters.get(1)).isInstanceOf(ByteArrayMessageConverter.class);
    assertThat(converters.get(2)).isInstanceOf(MappingJackson2MessageConverter.class);
    ContentTypeResolver resolver = ((MappingJackson2MessageConverter) converters.get(2)).getContentTypeResolver();
    assertThat(((DefaultContentTypeResolver) resolver).getDefaultMimeType()).isEqualTo(MimeTypeUtils.APPLICATION_JSON);
    DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(annotationMethodMessageHandler);
    Object pathMatcher = handlerAccessor.getPropertyValue("pathMatcher");
    String pathSeparator = (String) new DirectFieldAccessor(pathMatcher).getPropertyValue("pathSeparator");
    assertThat(pathSeparator).isEqualTo(".");
}
Also used : SimpMessagingTemplate(org.springframework.messaging.simp.SimpMessagingTemplate) MappingJackson2MessageConverter(org.springframework.messaging.converter.MappingJackson2MessageConverter) DefaultContentTypeResolver(org.springframework.messaging.converter.DefaultContentTypeResolver) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) ContentTypeResolver(org.springframework.messaging.converter.ContentTypeResolver) DefaultContentTypeResolver(org.springframework.messaging.converter.DefaultContentTypeResolver) SimpAnnotationMethodMessageHandler(org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler) ByteArrayMessageConverter(org.springframework.messaging.converter.ByteArrayMessageConverter) CompositeMessageConverter(org.springframework.messaging.converter.CompositeMessageConverter) MessageConverter(org.springframework.messaging.converter.MessageConverter) MappingJackson2MessageConverter(org.springframework.messaging.converter.MappingJackson2MessageConverter) StringMessageConverter(org.springframework.messaging.converter.StringMessageConverter) CompositeMessageConverter(org.springframework.messaging.converter.CompositeMessageConverter) Test(org.junit.jupiter.api.Test)

Aggregations

SimpAnnotationMethodMessageHandler (org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler)9 Test (org.junit.jupiter.api.Test)7 ApplicationContext (org.springframework.context.ApplicationContext)4 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)4 StaticApplicationContext (org.springframework.context.support.StaticApplicationContext)4 HandlerMethodArgumentResolver (org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver)4 HandlerMethodReturnValueHandler (org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler)4 ArrayList (java.util.ArrayList)2 Bean (org.springframework.context.annotation.Bean)2 StompHeaderAccessor (org.springframework.messaging.simp.stomp.StompHeaderAccessor)2 PathMatcher (org.springframework.util.PathMatcher)2 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 MessageHandler (org.springframework.messaging.MessageHandler)1 ByteArrayMessageConverter (org.springframework.messaging.converter.ByteArrayMessageConverter)1 CompositeMessageConverter (org.springframework.messaging.converter.CompositeMessageConverter)1 ContentTypeResolver (org.springframework.messaging.converter.ContentTypeResolver)1 DefaultContentTypeResolver (org.springframework.messaging.converter.DefaultContentTypeResolver)1 MappingJackson2MessageConverter (org.springframework.messaging.converter.MappingJackson2MessageConverter)1 MessageConverter (org.springframework.messaging.converter.MessageConverter)1 StringMessageConverter (org.springframework.messaging.converter.StringMessageConverter)1