Java Code Examples for org.springframework.messaging.converter.CompositeMessageConverter

The following examples show how to use org.springframework.messaging.converter.CompositeMessageConverter. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: spring-analysis-note   Author: Vip-Augus   File: MessageBrokerConfigurationTests.java    License: MIT License 6 votes vote down vote up
@Test
public void configureMessageConvertersCustom() {
	final MessageConverter testConverter = mock(MessageConverter.class);
	AbstractMessageBrokerConfiguration config = new BaseTestMessageBrokerConfig() {
		@Override
		protected boolean configureMessageConverters(List<MessageConverter> messageConverters) {
			messageConverters.add(testConverter);
			return false;
		}
	};

	CompositeMessageConverter compositeConverter = config.brokerMessageConverter();
	assertThat(compositeConverter.getConverters().size(), Matchers.is(1));
	Iterator<MessageConverter> iterator = compositeConverter.getConverters().iterator();
	assertThat(iterator.next(), Matchers.is(testConverter));
}
 
Example #2
Source Project: spring-analysis-note   Author: Vip-Augus   File: MessageBrokerConfigurationTests.java    License: MIT License 6 votes vote down vote up
@Test
public void configureMessageConvertersCustomAndDefault() {
	final MessageConverter testConverter = mock(MessageConverter.class);

	AbstractMessageBrokerConfiguration config = new BaseTestMessageBrokerConfig() {
		@Override
		protected boolean configureMessageConverters(List<MessageConverter> messageConverters) {
			messageConverters.add(testConverter);
			return true;
		}
	};
	CompositeMessageConverter compositeConverter = config.brokerMessageConverter();

	assertThat(compositeConverter.getConverters().size(), Matchers.is(4));
	Iterator<MessageConverter> iterator = compositeConverter.getConverters().iterator();
	assertThat(iterator.next(), Matchers.is(testConverter));
	assertThat(iterator.next(), Matchers.instanceOf(StringMessageConverter.class));
	assertThat(iterator.next(), Matchers.instanceOf(ByteArrayMessageConverter.class));
	assertThat(iterator.next(), Matchers.instanceOf(MappingJackson2MessageConverter.class));
}
 
Example #3
Source Project: java-technology-stack   Author: codeEngraver   File: SimpAnnotationMethodMessageHandler.java    License: MIT License 6 votes vote down vote up
/**
 * Create an instance of SimpAnnotationMethodMessageHandler with the given
 * message channels and broker messaging template.
 * @param clientInboundChannel the channel for receiving messages from clients (e.g. WebSocket clients)
 * @param clientOutboundChannel the channel for messages to clients (e.g. WebSocket clients)
 * @param brokerTemplate a messaging template to send application messages to the broker
 */
public SimpAnnotationMethodMessageHandler(SubscribableChannel clientInboundChannel,
		MessageChannel clientOutboundChannel, SimpMessageSendingOperations brokerTemplate) {

	Assert.notNull(clientInboundChannel, "clientInboundChannel must not be null");
	Assert.notNull(clientOutboundChannel, "clientOutboundChannel must not be null");
	Assert.notNull(brokerTemplate, "brokerTemplate must not be null");

	this.clientInboundChannel = clientInboundChannel;
	this.clientMessagingTemplate = new SimpMessagingTemplate(clientOutboundChannel);
	this.brokerTemplate = brokerTemplate;

	Collection<MessageConverter> converters = new ArrayList<>();
	converters.add(new StringMessageConverter());
	converters.add(new ByteArrayMessageConverter());
	this.messageConverter = new CompositeMessageConverter(converters);
}
 
Example #4
Source Project: java-technology-stack   Author: codeEngraver   File: MessageBrokerConfigurationTests.java    License: MIT License 6 votes vote down vote up
@Test
public void configureMessageConvertersCustom() {
	final MessageConverter testConverter = mock(MessageConverter.class);
	AbstractMessageBrokerConfiguration config = new BaseTestMessageBrokerConfig() {
		@Override
		protected boolean configureMessageConverters(List<MessageConverter> messageConverters) {
			messageConverters.add(testConverter);
			return false;
		}
	};

	CompositeMessageConverter compositeConverter = config.brokerMessageConverter();
	assertThat(compositeConverter.getConverters().size(), Matchers.is(1));
	Iterator<MessageConverter> iterator = compositeConverter.getConverters().iterator();
	assertThat(iterator.next(), Matchers.is(testConverter));
}
 
Example #5
Source Project: java-technology-stack   Author: codeEngraver   File: MessageBrokerConfigurationTests.java    License: MIT License 6 votes vote down vote up
@Test
public void configureMessageConvertersCustomAndDefault() {
	final MessageConverter testConverter = mock(MessageConverter.class);

	AbstractMessageBrokerConfiguration config = new BaseTestMessageBrokerConfig() {
		@Override
		protected boolean configureMessageConverters(List<MessageConverter> messageConverters) {
			messageConverters.add(testConverter);
			return true;
		}
	};
	CompositeMessageConverter compositeConverter = config.brokerMessageConverter();

	assertThat(compositeConverter.getConverters().size(), Matchers.is(4));
	Iterator<MessageConverter> iterator = compositeConverter.getConverters().iterator();
	assertThat(iterator.next(), Matchers.is(testConverter));
	assertThat(iterator.next(), Matchers.instanceOf(StringMessageConverter.class));
	assertThat(iterator.next(), Matchers.instanceOf(ByteArrayMessageConverter.class));
	assertThat(iterator.next(), Matchers.instanceOf(MappingJackson2MessageConverter.class));
}
 
Example #6
Source Project: rocketmq-spring   Author: apache   File: RocketMQMessageConverter.java    License: Apache License 2.0 6 votes vote down vote up
public RocketMQMessageConverter() {
    List<MessageConverter> messageConverters = new ArrayList<>();
    ByteArrayMessageConverter byteArrayMessageConverter = new ByteArrayMessageConverter();
    byteArrayMessageConverter.setContentTypeResolver(null);
    messageConverters.add(byteArrayMessageConverter);
    messageConverters.add(new StringMessageConverter());
    if (JACKSON_PRESENT) {
        messageConverters.add(new MappingJackson2MessageConverter());
    }
    if (FASTJSON_PRESENT) {
        try {
            messageConverters.add(
                (MessageConverter)ClassUtils.forName(
                    "com.alibaba.fastjson.support.spring.messaging.MappingFastJsonMessageConverter",
                    ClassUtils.getDefaultClassLoader()).newInstance());
        } catch (ClassNotFoundException | IllegalAccessException | InstantiationException ignored) {
            //ignore this exception
        }
    }
    messageConverter = new CompositeMessageConverter(messageConverters);
}
 
Example #7
Source Project: spring4-understanding   Author: langtianya   File: SimpAnnotationMethodMessageHandler.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Create an instance of SimpAnnotationMethodMessageHandler with the given
 * message channels and broker messaging template.
 * @param clientInboundChannel the channel for receiving messages from clients (e.g. WebSocket clients)
 * @param clientOutboundChannel the channel for messages to clients (e.g. WebSocket clients)
 * @param brokerTemplate a messaging template to send application messages to the broker
 */
public SimpAnnotationMethodMessageHandler(SubscribableChannel clientInboundChannel,
		MessageChannel clientOutboundChannel, SimpMessageSendingOperations brokerTemplate) {

	Assert.notNull(clientInboundChannel, "clientInboundChannel must not be null");
	Assert.notNull(clientOutboundChannel, "clientOutboundChannel must not be null");
	Assert.notNull(brokerTemplate, "brokerTemplate must not be null");

	this.clientInboundChannel = clientInboundChannel;
	this.clientMessagingTemplate = new SimpMessagingTemplate(clientOutboundChannel);
	this.brokerTemplate = brokerTemplate;

	Collection<MessageConverter> converters = new ArrayList<MessageConverter>();
	converters.add(new StringMessageConverter());
	converters.add(new ByteArrayMessageConverter());
	this.messageConverter = new CompositeMessageConverter(converters);
}
 
Example #8
Source Project: spring4-understanding   Author: langtianya   File: MessageBrokerConfigurationTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void configureMessageConvertersCustom() {
	final MessageConverter testConverter = mock(MessageConverter.class);
	AbstractMessageBrokerConfiguration config = new BaseTestMessageBrokerConfig() {
		@Override
		protected boolean configureMessageConverters(List<MessageConverter> messageConverters) {
			messageConverters.add(testConverter);
			return false;
		}
	};

	CompositeMessageConverter compositeConverter = config.brokerMessageConverter();
	assertThat(compositeConverter.getConverters().size(), Matchers.is(1));
	Iterator<MessageConverter> iterator = compositeConverter.getConverters().iterator();
	assertThat(iterator.next(), Matchers.is(testConverter));
}
 
Example #9
Source Project: spring4-understanding   Author: langtianya   File: MessageBrokerConfigurationTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void configureMessageConvertersCustomAndDefault() {
	final MessageConverter testConverter = mock(MessageConverter.class);

	AbstractMessageBrokerConfiguration config = new BaseTestMessageBrokerConfig() {
		@Override
		protected boolean configureMessageConverters(List<MessageConverter> messageConverters) {
			messageConverters.add(testConverter);
			return true;
		}
	};
	CompositeMessageConverter compositeConverter = config.brokerMessageConverter();

	assertThat(compositeConverter.getConverters().size(), Matchers.is(4));
	Iterator<MessageConverter> iterator = compositeConverter.getConverters().iterator();
	assertThat(iterator.next(), Matchers.is(testConverter));
	assertThat(iterator.next(), Matchers.instanceOf(StringMessageConverter.class));
	assertThat(iterator.next(), Matchers.instanceOf(ByteArrayMessageConverter.class));
	assertThat(iterator.next(), Matchers.instanceOf(MappingJackson2MessageConverter.class));
}
 
Example #10
Source Project: spring-cloud-aws   Author: spring-cloud   File: QueueMessageHandler.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected List<? extends HandlerMethodArgumentResolver> initArgumentResolvers() {
	List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>(
			getCustomArgumentResolvers());

	resolvers.add(new HeaderMethodArgumentResolver(null, null));
	resolvers.add(new SqsHeadersMethodArgumentResolver());

	resolvers.add(new NotificationSubjectArgumentResolver());
	resolvers.add(new AcknowledgmentHandlerMethodArgumentResolver(ACKNOWLEDGMENT));
	resolvers.add(new VisibilityHandlerMethodArgumentResolver(VISIBILITY));

	CompositeMessageConverter compositeMessageConverter = createPayloadArgumentCompositeConverter();
	resolvers.add(new NotificationMessageArgumentResolver(compositeMessageConverter));
	resolvers.add(new MessageMethodArgumentResolver(
			this.messageConverters.isEmpty() ? new StringMessageConverter()
					: new CompositeMessageConverter(this.messageConverters)));
	resolvers.add(new PayloadArgumentResolver(compositeMessageConverter,
			new NoOpValidator()));

	return resolvers;
}
 
Example #11
Source Project: spring-cloud-aws   Author: spring-cloud   File: AbstractMessageChannelMessagingSendingTemplate.java    License: Apache License 2.0 6 votes vote down vote up
protected void initMessageConverter(MessageConverter messageConverter) {

		StringMessageConverter stringMessageConverter = new StringMessageConverter();
		stringMessageConverter.setSerializedPayloadClass(String.class);

		List<MessageConverter> messageConverters = new ArrayList<>();
		messageConverters.add(stringMessageConverter);

		if (messageConverter != null) {
			messageConverters.add(messageConverter);
		}
		else {
			MappingJackson2MessageConverter mappingJackson2MessageConverter = new MappingJackson2MessageConverter();
			mappingJackson2MessageConverter.setSerializedPayloadClass(String.class);
			messageConverters.add(mappingJackson2MessageConverter);
		}

		setMessageConverter(new CompositeMessageConverter(messageConverters));
	}
 
Example #12
Source Project: spring-cloud-aws   Author: spring-cloud   File: QueueMessagingTemplateTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
void instantiation_withConverter_shouldAddItToTheCompositeConverter() {
	// Arrange
	SimpleMessageConverter simpleMessageConverter = new SimpleMessageConverter();

	// Act
	QueueMessagingTemplate queueMessagingTemplate = new QueueMessagingTemplate(
			createAmazonSqs(), (ResourceIdResolver) null, simpleMessageConverter);

	// Assert
	assertThat(
			((CompositeMessageConverter) queueMessagingTemplate.getMessageConverter())
					.getConverters()).hasSize(2);
	assertThat(
			((CompositeMessageConverter) queueMessagingTemplate.getMessageConverter())
					.getConverters().get(1)).isEqualTo(simpleMessageConverter);
}
 
Example #13
Source Project: rqueue   Author: sonus21   File: RqueueMessageSenderImpl.java    License: Apache License 2.0 5 votes vote down vote up
private RqueueMessageSenderImpl(
    RqueueMessageTemplate messageTemplate,
    List<MessageConverter> messageConverters,
    boolean addDefault) {
  notNull(messageTemplate, "messageTemplate cannot be null");
  notEmpty(messageConverters, "messageConverters cannot be empty");
  this.messageTemplate = messageTemplate;
  this.messageConverter =
      new CompositeMessageConverter(getMessageConverters(addDefault, messageConverters));
}
 
Example #14
Source Project: spring-analysis-note   Author: Vip-Augus   File: SimpAnnotationMethodMessageHandler.java    License: MIT License 5 votes vote down vote up
/**
 * Create an instance of SimpAnnotationMethodMessageHandler with the given
 * message channels and broker messaging template.
 * @param clientInboundChannel the channel for receiving messages from clients (e.g. WebSocket clients)
 * @param clientOutboundChannel the channel for messages to clients (e.g. WebSocket clients)
 * @param brokerTemplate a messaging template to send application messages to the broker
 */
public SimpAnnotationMethodMessageHandler(SubscribableChannel clientInboundChannel,
		MessageChannel clientOutboundChannel, SimpMessageSendingOperations brokerTemplate) {

	Assert.notNull(clientInboundChannel, "clientInboundChannel must not be null");
	Assert.notNull(clientOutboundChannel, "clientOutboundChannel must not be null");
	Assert.notNull(brokerTemplate, "brokerTemplate must not be null");

	this.clientInboundChannel = clientInboundChannel;
	this.clientMessagingTemplate = new SimpMessagingTemplate(clientOutboundChannel);
	this.brokerTemplate = brokerTemplate;

	Collection<MessageConverter> converters = new ArrayList<>();
	converters.add(new StringMessageConverter());
	converters.add(new ByteArrayMessageConverter());
	this.messageConverter = new CompositeMessageConverter(converters);
}
 
Example #15
Source Project: spring-analysis-note   Author: Vip-Augus   File: AbstractMessageBrokerConfiguration.java    License: MIT License 5 votes vote down vote up
@Bean
public CompositeMessageConverter brokerMessageConverter() {
	List<MessageConverter> converters = new ArrayList<>();
	boolean registerDefaults = configureMessageConverters(converters);
	if (registerDefaults) {
		converters.add(new StringMessageConverter());
		converters.add(new ByteArrayMessageConverter());
		if (jackson2Present) {
			converters.add(createJacksonConverter());
		}
	}
	return new CompositeMessageConverter(converters);
}
 
Example #16
Source Project: spring-analysis-note   Author: Vip-Augus   File: MessageBrokerConfigurationTests.java    License: MIT License 5 votes vote down vote up
@Test
public void configureMessageConvertersDefault() {
	AbstractMessageBrokerConfiguration config = new BaseTestMessageBrokerConfig();
	CompositeMessageConverter compositeConverter = config.brokerMessageConverter();

	List<MessageConverter> converters = compositeConverter.getConverters();
	assertThat(converters.size(), Matchers.is(3));
	assertThat(converters.get(0), Matchers.instanceOf(StringMessageConverter.class));
	assertThat(converters.get(1), Matchers.instanceOf(ByteArrayMessageConverter.class));
	assertThat(converters.get(2), Matchers.instanceOf(MappingJackson2MessageConverter.class));

	ContentTypeResolver resolver = ((MappingJackson2MessageConverter) converters.get(2)).getContentTypeResolver();
	assertEquals(MimeTypeUtils.APPLICATION_JSON, ((DefaultContentTypeResolver) resolver).getDefaultMimeType());
}
 
Example #17
Source Project: spring-analysis-note   Author: Vip-Augus   File: MessageSendingTemplateTests.java    License: MIT License 5 votes vote down vote up
@Test(expected = MessageConversionException.class)
public void convertAndSendNoMatchingConverter() {

	MessageConverter converter = new CompositeMessageConverter(
			Arrays.<MessageConverter>asList(new MappingJackson2MessageConverter()));
	this.template.setMessageConverter(converter);

	this.headers.put(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_XML);
	this.template.convertAndSend("home", "payload", new MessageHeaders(this.headers));
}
 
Example #18
Source Project: spring-analysis-note   Author: Vip-Augus   File: MessageBrokerBeanDefinitionParser.java    License: MIT License 5 votes vote down vote up
private RuntimeBeanReference registerMessageConverter(
		Element element, ParserContext context, @Nullable Object source) {

	Element convertersElement = DomUtils.getChildElementByTagName(element, "message-converters");
	ManagedList<Object> converters = new ManagedList<>();
	if (convertersElement != null) {
		converters.setSource(source);
		for (Element beanElement : DomUtils.getChildElementsByTagName(convertersElement, "bean", "ref")) {
			Object object = context.getDelegate().parsePropertySubElement(beanElement, null);
			converters.add(object);
		}
	}
	if (convertersElement == null || Boolean.valueOf(convertersElement.getAttribute("register-defaults"))) {
		converters.setSource(source);
		converters.add(new RootBeanDefinition(StringMessageConverter.class));
		converters.add(new RootBeanDefinition(ByteArrayMessageConverter.class));
		if (jackson2Present) {
			RootBeanDefinition jacksonConverterDef = new RootBeanDefinition(MappingJackson2MessageConverter.class);
			RootBeanDefinition resolverDef = new RootBeanDefinition(DefaultContentTypeResolver.class);
			resolverDef.getPropertyValues().add("defaultMimeType", MimeTypeUtils.APPLICATION_JSON);
			jacksonConverterDef.getPropertyValues().add("contentTypeResolver", resolverDef);
			// Use Jackson factory in order to have JSR-310 and Joda-Time modules registered automatically
			GenericBeanDefinition jacksonFactoryDef = new GenericBeanDefinition();
			jacksonFactoryDef.setBeanClass(Jackson2ObjectMapperFactoryBean.class);
			jacksonFactoryDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
			jacksonFactoryDef.setSource(source);
			jacksonConverterDef.getPropertyValues().add("objectMapper", jacksonFactoryDef);
			converters.add(jacksonConverterDef);
		}
	}
	ConstructorArgumentValues cargs = new ConstructorArgumentValues();
	cargs.addIndexedArgumentValue(0, converters);
	RootBeanDefinition messageConverterDef = new RootBeanDefinition(CompositeMessageConverter.class, cargs, null);
	String name = MESSAGE_CONVERTER_BEAN_NAME;
	registerBeanDefByName(name, messageConverterDef, context, source);
	return new RuntimeBeanReference(name);
}
 
Example #19
Source Project: spring-analysis-note   Author: Vip-Augus   File: MessageBrokerBeanDefinitionParserTests.java    License: MIT License 5 votes vote down vote up
@Test
public void messageConverters() {
	loadBeanDefinitions("websocket-config-broker-converters.xml");

	CompositeMessageConverter compositeConverter = this.appContext.getBean(CompositeMessageConverter.class);
	assertNotNull(compositeConverter);

	assertEquals(4, compositeConverter.getConverters().size());
	assertEquals(StringMessageConverter.class, compositeConverter.getConverters().iterator().next().getClass());
}
 
Example #20
Source Project: spring-analysis-note   Author: Vip-Augus   File: MessageBrokerBeanDefinitionParserTests.java    License: MIT License 5 votes vote down vote up
@Test
public void messageConvertersDefaultsOff() {
	loadBeanDefinitions("websocket-config-broker-converters-defaults-off.xml");

	CompositeMessageConverter compositeConverter = this.appContext.getBean(CompositeMessageConverter.class);
	assertNotNull(compositeConverter);

	assertEquals(1, compositeConverter.getConverters().size());
	assertEquals(StringMessageConverter.class, compositeConverter.getConverters().iterator().next().getClass());
}
 
Example #21
Source Project: java-technology-stack   Author: codeEngraver   File: AbstractMessageBrokerConfiguration.java    License: MIT License 5 votes vote down vote up
@Bean
public CompositeMessageConverter brokerMessageConverter() {
	List<MessageConverter> converters = new ArrayList<>();
	boolean registerDefaults = configureMessageConverters(converters);
	if (registerDefaults) {
		converters.add(new StringMessageConverter());
		converters.add(new ByteArrayMessageConverter());
		if (jackson2Present) {
			converters.add(createJacksonConverter());
		}
	}
	return new CompositeMessageConverter(converters);
}
 
Example #22
Source Project: java-technology-stack   Author: codeEngraver   File: MessageBrokerConfigurationTests.java    License: MIT License 5 votes vote down vote up
@Test
public void configureMessageConvertersDefault() {
	AbstractMessageBrokerConfiguration config = new BaseTestMessageBrokerConfig();
	CompositeMessageConverter compositeConverter = config.brokerMessageConverter();

	List<MessageConverter> converters = compositeConverter.getConverters();
	assertThat(converters.size(), Matchers.is(3));
	assertThat(converters.get(0), Matchers.instanceOf(StringMessageConverter.class));
	assertThat(converters.get(1), Matchers.instanceOf(ByteArrayMessageConverter.class));
	assertThat(converters.get(2), Matchers.instanceOf(MappingJackson2MessageConverter.class));

	ContentTypeResolver resolver = ((MappingJackson2MessageConverter) converters.get(2)).getContentTypeResolver();
	assertEquals(MimeTypeUtils.APPLICATION_JSON, ((DefaultContentTypeResolver) resolver).getDefaultMimeType());
}
 
Example #23
Source Project: java-technology-stack   Author: codeEngraver   File: MessageSendingTemplateTests.java    License: MIT License 5 votes vote down vote up
@Test(expected = MessageConversionException.class)
public void convertAndSendNoMatchingConverter() {

	MessageConverter converter = new CompositeMessageConverter(
			Arrays.<MessageConverter>asList(new MappingJackson2MessageConverter()));
	this.template.setMessageConverter(converter);

	this.headers.put(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_XML);
	this.template.convertAndSend("home", "payload", new MessageHeaders(this.headers));
}
 
Example #24
Source Project: java-technology-stack   Author: codeEngraver   File: MessageBrokerBeanDefinitionParser.java    License: MIT License 5 votes vote down vote up
private RuntimeBeanReference registerMessageConverter(
		Element element, ParserContext context, @Nullable Object source) {

	Element convertersElement = DomUtils.getChildElementByTagName(element, "message-converters");
	ManagedList<Object> converters = new ManagedList<>();
	if (convertersElement != null) {
		converters.setSource(source);
		for (Element beanElement : DomUtils.getChildElementsByTagName(convertersElement, "bean", "ref")) {
			Object object = context.getDelegate().parsePropertySubElement(beanElement, null);
			converters.add(object);
		}
	}
	if (convertersElement == null || Boolean.valueOf(convertersElement.getAttribute("register-defaults"))) {
		converters.setSource(source);
		converters.add(new RootBeanDefinition(StringMessageConverter.class));
		converters.add(new RootBeanDefinition(ByteArrayMessageConverter.class));
		if (jackson2Present) {
			RootBeanDefinition jacksonConverterDef = new RootBeanDefinition(MappingJackson2MessageConverter.class);
			RootBeanDefinition resolverDef = new RootBeanDefinition(DefaultContentTypeResolver.class);
			resolverDef.getPropertyValues().add("defaultMimeType", MimeTypeUtils.APPLICATION_JSON);
			jacksonConverterDef.getPropertyValues().add("contentTypeResolver", resolverDef);
			// Use Jackson factory in order to have JSR-310 and Joda-Time modules registered automatically
			GenericBeanDefinition jacksonFactoryDef = new GenericBeanDefinition();
			jacksonFactoryDef.setBeanClass(Jackson2ObjectMapperFactoryBean.class);
			jacksonFactoryDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
			jacksonFactoryDef.setSource(source);
			jacksonConverterDef.getPropertyValues().add("objectMapper", jacksonFactoryDef);
			converters.add(jacksonConverterDef);
		}
	}
	ConstructorArgumentValues cargs = new ConstructorArgumentValues();
	cargs.addIndexedArgumentValue(0, converters);
	RootBeanDefinition messageConverterDef = new RootBeanDefinition(CompositeMessageConverter.class, cargs, null);
	String name = MESSAGE_CONVERTER_BEAN_NAME;
	registerBeanDefByName(name, messageConverterDef, context, source);
	return new RuntimeBeanReference(name);
}
 
Example #25
Source Project: java-technology-stack   Author: codeEngraver   File: MessageBrokerBeanDefinitionParserTests.java    License: MIT License 5 votes vote down vote up
@Test
public void messageConverters() {
	loadBeanDefinitions("websocket-config-broker-converters.xml");

	CompositeMessageConverter compositeConverter = this.appContext.getBean(CompositeMessageConverter.class);
	assertNotNull(compositeConverter);

	assertEquals(4, compositeConverter.getConverters().size());
	assertEquals(StringMessageConverter.class, compositeConverter.getConverters().iterator().next().getClass());
}
 
Example #26
Source Project: java-technology-stack   Author: codeEngraver   File: MessageBrokerBeanDefinitionParserTests.java    License: MIT License 5 votes vote down vote up
@Test
public void messageConvertersDefaultsOff() {
	loadBeanDefinitions("websocket-config-broker-converters-defaults-off.xml");

	CompositeMessageConverter compositeConverter = this.appContext.getBean(CompositeMessageConverter.class);
	assertNotNull(compositeConverter);

	assertEquals(1, compositeConverter.getConverters().size());
	assertEquals(StringMessageConverter.class, compositeConverter.getConverters().iterator().next().getClass());
}
 
Example #27
Source Project: spring4-understanding   Author: langtianya   File: AbstractMessageBrokerConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public CompositeMessageConverter brokerMessageConverter() {
	List<MessageConverter> converters = new ArrayList<MessageConverter>();
	boolean registerDefaults = configureMessageConverters(converters);
	if (registerDefaults) {
		converters.add(new StringMessageConverter());
		converters.add(new ByteArrayMessageConverter());
		if (jackson2Present) {
			converters.add(createJacksonConverter());
		}
	}
	return new CompositeMessageConverter(converters);
}
 
Example #28
Source Project: spring4-understanding   Author: langtianya   File: MessageBrokerConfigurationTests.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void configureMessageConvertersDefault() {
	AbstractMessageBrokerConfiguration config = new BaseTestMessageBrokerConfig();
	CompositeMessageConverter compositeConverter = config.brokerMessageConverter();

	List<MessageConverter> converters = compositeConverter.getConverters();
	assertThat(converters.size(), Matchers.is(3));
	assertThat(converters.get(0), Matchers.instanceOf(StringMessageConverter.class));
	assertThat(converters.get(1), Matchers.instanceOf(ByteArrayMessageConverter.class));
	assertThat(converters.get(2), Matchers.instanceOf(MappingJackson2MessageConverter.class));

	ContentTypeResolver resolver = ((MappingJackson2MessageConverter) converters.get(2)).getContentTypeResolver();
	assertEquals(MimeTypeUtils.APPLICATION_JSON, ((DefaultContentTypeResolver) resolver).getDefaultMimeType());
}
 
Example #29
Source Project: spring4-understanding   Author: langtianya   File: MessageSendingTemplateTests.java    License: Apache License 2.0 5 votes vote down vote up
@Test(expected = MessageConversionException.class)
public void convertAndSendNoMatchingConverter() {

	MessageConverter converter = new CompositeMessageConverter(
			Arrays.<MessageConverter>asList(new MappingJackson2MessageConverter()));
	this.template.setMessageConverter(converter);

	this.headers.put(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_XML);
	this.template.convertAndSend("home", "payload", new MessageHeaders(this.headers));
}
 
Example #30
Source Project: spring4-understanding   Author: langtianya   File: MessageBrokerBeanDefinitionParser.java    License: Apache License 2.0 5 votes vote down vote up
private RuntimeBeanReference registerMessageConverter(Element element, ParserContext context, Object source) {
	Element convertersElement = DomUtils.getChildElementByTagName(element, "message-converters");
	ManagedList<? super Object> converters = new ManagedList<Object>();
	if (convertersElement != null) {
		converters.setSource(source);
		for (Element beanElement : DomUtils.getChildElementsByTagName(convertersElement, "bean", "ref")) {
			Object object = context.getDelegate().parsePropertySubElement(beanElement, null);
			converters.add(object);
		}
	}
	if (convertersElement == null || Boolean.valueOf(convertersElement.getAttribute("register-defaults"))) {
		converters.setSource(source);
		converters.add(new RootBeanDefinition(StringMessageConverter.class));
		converters.add(new RootBeanDefinition(ByteArrayMessageConverter.class));
		if (jackson2Present) {
			RootBeanDefinition jacksonConverterDef = new RootBeanDefinition(MappingJackson2MessageConverter.class);
			RootBeanDefinition resolverDef = new RootBeanDefinition(DefaultContentTypeResolver.class);
			resolverDef.getPropertyValues().add("defaultMimeType", MimeTypeUtils.APPLICATION_JSON);
			jacksonConverterDef.getPropertyValues().add("contentTypeResolver", resolverDef);
			// Use Jackson factory in order to have JSR-310 and Joda-Time modules registered automatically
			GenericBeanDefinition jacksonFactoryDef = new GenericBeanDefinition();
			jacksonFactoryDef.setBeanClass(Jackson2ObjectMapperFactoryBean.class);
			jacksonFactoryDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
			jacksonFactoryDef.setSource(source);
			jacksonConverterDef.getPropertyValues().add("objectMapper", jacksonFactoryDef);
			converters.add(jacksonConverterDef);
		}
	}
	ConstructorArgumentValues cavs = new ConstructorArgumentValues();
	cavs.addIndexedArgumentValue(0, converters);
	RootBeanDefinition messageConverterDef = new RootBeanDefinition(CompositeMessageConverter.class, cavs, null);
	return new RuntimeBeanReference(registerBeanDef(messageConverterDef, context, source));
}