Search in sources :

Example 1 with CompositeMessageConverter

use of org.springframework.messaging.converter.CompositeMessageConverter in project spring-integration by spring-projects.

the class WebSocketParserTests method testCustomOutboundChannelAdapter.

@Test
public void testCustomOutboundChannelAdapter() throws URISyntaxException {
    assertSame(this.clientWebSocketContainer, TestUtils.getPropertyValue(this.customOutboundAdapter, "webSocketContainer"));
    SubProtocolHandlerRegistry subProtocolHandlerRegistry = TestUtils.getPropertyValue(this.customOutboundAdapter, "subProtocolHandlerRegistry", SubProtocolHandlerRegistry.class);
    assertSame(this.stompSubProtocolHandler, TestUtils.getPropertyValue(subProtocolHandlerRegistry, "defaultProtocolHandler"));
    Map<?, ?> protocolHandlers = TestUtils.getPropertyValue(subProtocolHandlerRegistry, "protocolHandlers", Map.class);
    assertEquals(3, protocolHandlers.size());
    // See warn log message.
    for (Object handler : protocolHandlers.values()) {
        assertSame(this.stompSubProtocolHandler, handler);
    }
    assertTrue(TestUtils.getPropertyValue(this.customOutboundAdapter, "mergeWithDefaultConverters", Boolean.class));
    CompositeMessageConverter compositeMessageConverter = TestUtils.getPropertyValue(this.customOutboundAdapter, "messageConverter", CompositeMessageConverter.class);
    List<MessageConverter> converters = compositeMessageConverter.getConverters();
    assertEquals(5, converters.size());
    assertSame(this.simpleMessageConverter, converters.get(0));
    assertSame(this.mapMessageConverter, converters.get(1));
    assertThat(converters.get(2), instanceOf(StringMessageConverter.class));
    assertTrue(TestUtils.getPropertyValue(this.customOutboundAdapter, "client", Boolean.class));
}
Also used : StringMessageConverter(org.springframework.messaging.converter.StringMessageConverter) SubProtocolHandlerRegistry(org.springframework.integration.websocket.support.SubProtocolHandlerRegistry) CompositeMessageConverter(org.springframework.messaging.converter.CompositeMessageConverter) CompositeMessageConverter(org.springframework.messaging.converter.CompositeMessageConverter) MapMessageConverter(org.springframework.integration.support.converter.MapMessageConverter) MessageConverter(org.springframework.messaging.converter.MessageConverter) SimpleMessageConverter(org.springframework.integration.support.converter.SimpleMessageConverter) StringMessageConverter(org.springframework.messaging.converter.StringMessageConverter) Test(org.junit.Test)

Example 2 with CompositeMessageConverter

use of org.springframework.messaging.converter.CompositeMessageConverter in project ArTEMiS by ls1intum.

the class WebsocketConfiguration method brokerMessageConverter.

@Override
public CompositeMessageConverter brokerMessageConverter() {
    // NOTE: We need to replace the default messageConverter for WebSocket messages
    // with a messageConverter that uses the same ObjectMapper that our REST endpoints use.
    // This gives us consistency in how specific datatypes are serialized (e.g. timestamps)
    DefaultContentTypeResolver resolver = new DefaultContentTypeResolver();
    resolver.setDefaultMimeType(MimeTypeUtils.APPLICATION_JSON);
    MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
    converter.setObjectMapper(objectMapper);
    converter.setContentTypeResolver(resolver);
    Set<MessageConverter> messageConverterSet = new HashSet<>();
    messageConverterSet.add(converter);
    return new CompositeMessageConverter(messageConverterSet);
}
Also used : MappingJackson2MessageConverter(org.springframework.messaging.converter.MappingJackson2MessageConverter) DefaultContentTypeResolver(org.springframework.messaging.converter.DefaultContentTypeResolver) CompositeMessageConverter(org.springframework.messaging.converter.CompositeMessageConverter) MessageConverter(org.springframework.messaging.converter.MessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) MappingJackson2MessageConverter(org.springframework.messaging.converter.MappingJackson2MessageConverter) CompositeMessageConverter(org.springframework.messaging.converter.CompositeMessageConverter)

Example 3 with CompositeMessageConverter

use of org.springframework.messaging.converter.CompositeMessageConverter in project spring-cloud-stream by spring-cloud.

the class MessageChannelConfigurerTests method testObjectMapperConfig.

@Test
public void testObjectMapperConfig() throws Exception {
    CompositeMessageConverter converters = (CompositeMessageConverter) messageConverterFactory.getMessageConverterForType(MimeTypeUtils.APPLICATION_JSON);
    for (MessageConverter converter : converters.getConverters()) {
        DirectFieldAccessor converterAccessor = new DirectFieldAccessor(converter);
        ObjectMapper objectMapper = (ObjectMapper) converterAccessor.getPropertyValue("objectMapper");
        // assert that the ObjectMapper used by the converters is compliant with the
        // Boot configuration
        assertThat(!objectMapper.getSerializationConfig().isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)).withFailMessage("SerializationFeature 'WRITE_DATES_AS_TIMESTAMPS' should be disabled");
    // assert that the globally set bean is used by the converters
    }
}
Also used : DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) CompositeMessageConverter(org.springframework.messaging.converter.CompositeMessageConverter) CompositeMessageConverter(org.springframework.messaging.converter.CompositeMessageConverter) MessageConverter(org.springframework.messaging.converter.MessageConverter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with CompositeMessageConverter

use of org.springframework.messaging.converter.CompositeMessageConverter in project spring-cloud-stream by spring-cloud.

the class MessageChannelToInputFluxParameterAdapterTests method testWrapperFluxSupportsMultipleSubscriptions.

@Test
public void testWrapperFluxSupportsMultipleSubscriptions() throws Exception {
    List<String> results = Collections.synchronizedList(new ArrayList<>());
    CountDownLatch latch = new CountDownLatch(4);
    final MessageChannelToInputFluxParameterAdapter messageChannelToInputFluxParameterAdapter = new MessageChannelToInputFluxParameterAdapter(new CompositeMessageConverter(Collections.singleton(new MappingJackson2MessageConverter())));
    final Method processMethod = ReflectionUtils.findMethod(MessageChannelToInputFluxParameterAdapterTests.class, "process", Flux.class);
    final DirectChannel adaptedChannel = new DirectChannel();
    @SuppressWarnings("unchecked") final Flux<Message<?>> adapterFlux = (Flux<Message<?>>) messageChannelToInputFluxParameterAdapter.adapt(adaptedChannel, new MethodParameter(processMethod, 0));
    String uuid1 = UUID.randomUUID().toString();
    String uuid2 = UUID.randomUUID().toString();
    adapterFlux.map(m -> m.getPayload() + uuid1).subscribe(s -> {
        results.add(s);
        latch.countDown();
    });
    adapterFlux.map(m -> m.getPayload() + uuid2).subscribe(s -> {
        results.add(s);
        latch.countDown();
    });
    adaptedChannel.send(MessageBuilder.withPayload("A").build());
    adaptedChannel.send(MessageBuilder.withPayload("B").build());
    assertThat(latch.await(5000, TimeUnit.MILLISECONDS)).isTrue();
    assertThat(results).containsExactlyInAnyOrder("A" + uuid1, "B" + uuid1, "A" + uuid2, "B" + uuid2);
}
Also used : Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) UUID(java.util.UUID) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) CompositeMessageConverter(org.springframework.messaging.converter.CompositeMessageConverter) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) List(java.util.List) MappingJackson2MessageConverter(org.springframework.messaging.converter.MappingJackson2MessageConverter) ReflectionUtils(org.springframework.util.ReflectionUtils) MethodParameter(org.springframework.core.MethodParameter) Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) Method(java.lang.reflect.Method) Collections(java.util.Collections) MessageBuilder(org.springframework.messaging.support.MessageBuilder) Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) Flux(reactor.core.publisher.Flux) Method(java.lang.reflect.Method) CountDownLatch(java.util.concurrent.CountDownLatch) CompositeMessageConverter(org.springframework.messaging.converter.CompositeMessageConverter) MappingJackson2MessageConverter(org.springframework.messaging.converter.MappingJackson2MessageConverter) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 5 with CompositeMessageConverter

use of org.springframework.messaging.converter.CompositeMessageConverter in project spring-framework by spring-projects.

the class TestValidator method messageConvertersDefaultsOff.

@Test
public void messageConvertersDefaultsOff() {
    loadBeanDefinitions("websocket-config-broker-converters-defaults-off.xml");
    CompositeMessageConverter compositeConverter = this.appContext.getBean(CompositeMessageConverter.class);
    assertThat(compositeConverter).isNotNull();
    assertThat(compositeConverter.getConverters().size()).isEqualTo(1);
    assertThat(compositeConverter.getConverters().iterator().next().getClass()).isEqualTo(StringMessageConverter.class);
}
Also used : CompositeMessageConverter(org.springframework.messaging.converter.CompositeMessageConverter) Test(org.junit.jupiter.api.Test)

Aggregations

CompositeMessageConverter (org.springframework.messaging.converter.CompositeMessageConverter)15 MessageConverter (org.springframework.messaging.converter.MessageConverter)10 StringMessageConverter (org.springframework.messaging.converter.StringMessageConverter)9 MappingJackson2MessageConverter (org.springframework.messaging.converter.MappingJackson2MessageConverter)8 Test (org.junit.jupiter.api.Test)7 ByteArrayMessageConverter (org.springframework.messaging.converter.ByteArrayMessageConverter)6 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 List (java.util.List)3 DefaultContentTypeResolver (org.springframework.messaging.converter.DefaultContentTypeResolver)3 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)2 MapMessageConverter (org.springframework.integration.support.converter.MapMessageConverter)2 SimpleMessageConverter (org.springframework.integration.support.converter.SimpleMessageConverter)2 SubProtocolHandlerRegistry (org.springframework.integration.websocket.support.SubProtocolHandlerRegistry)2 ContentTypeResolver (org.springframework.messaging.converter.ContentTypeResolver)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Method (java.lang.reflect.Method)1 URI (java.net.URI)1 Collections (java.util.Collections)1 UUID (java.util.UUID)1