org.springframework.messaging.converter.ByteArrayMessageConverter Java Examples

The following examples show how to use org.springframework.messaging.converter.ByteArrayMessageConverter. 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 File: CompositeMessageConverterFactory.java    From spring-cloud-stream with Apache License 2.0 6 votes vote down vote up
private void initDefaultConverters() {
	ApplicationJsonMessageMarshallingConverter applicationJsonConverter = new ApplicationJsonMessageMarshallingConverter(
			this.objectMapper);
	applicationJsonConverter.setStrictContentTypeMatch(true);
	this.converters.add(applicationJsonConverter);
	this.converters.add(new ByteArrayMessageConverter() {
		@Override
		protected boolean supports(Class<?> clazz) {
			if (!super.supports(clazz)) {
				return (Object.class == clazz);
			}
			return true;
		}
	});
	this.converters.add(new ObjectStringMessageConverter());
}
 
Example #2
Source File: MessageBrokerConfigurationTests.java    From spring-analysis-note with 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 File: SimpAnnotationMethodMessageHandler.java    From java-technology-stack with 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 File: MessageBrokerConfigurationTests.java    From spring4-understanding with 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 #5
Source File: MessageBrokerConfigurationTests.java    From java-technology-stack with 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 File: RocketMQMessageConverter.java    From rocketmq-spring with 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 File: SimpAnnotationMethodMessageHandler.java    From spring4-understanding with 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 File: MessageBrokerBeanDefinitionParser.java    From java-technology-stack with 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 #9
Source File: SimpleFunctionRegistryTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@BeforeEach
public void before() {
	List<MessageConverter> messageConverters = new ArrayList<>();
	JsonMapper jsonMapper = new GsonMapper(new Gson());
	messageConverters.add(NegotiatingMessageConverterWrapper.wrap(new JsonMessageConverter(jsonMapper)));
	messageConverters.add(NegotiatingMessageConverterWrapper.wrap(new ByteArrayMessageConverter()));
	messageConverters.add(NegotiatingMessageConverterWrapper.wrap(new StringMessageConverter()));
	this.messageConverter = new CompositeMessageConverter(messageConverters);

	this.conversionService = new DefaultConversionService();
}
 
Example #10
Source File: WebSocketConfig.java    From WebIDE-Backend with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean configureMessageConverters(List<MessageConverter> messageConverters) {
    messageConverters.add(new StringMessageConverter());
    messageConverters.add(new ByteArrayMessageConverter());

    GsonMessageConverter gsonMessageConverter = new GsonMessageConverter();
    gsonMessageConverter.setGson(Converters.registerDateTime(new GsonBuilder()).create());
    messageConverters.add(gsonMessageConverter);

    return false;
}
 
Example #11
Source File: MessageBrokerBeanDefinitionParser.java    From spring4-understanding with 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));
}
 
Example #12
Source File: DefaultMessageHandlerMethodFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void customMessageConverterFailure() throws Exception {
	DefaultMessageHandlerMethodFactory instance = createInstance();
	MessageConverter messageConverter = new ByteArrayMessageConverter();
	instance.setMessageConverter(messageConverter);
	instance.afterPropertiesSet();

	InvocableHandlerMethod invocableHandlerMethod =
			createInvocableHandlerMethod(instance, "simpleString", String.class);

	thrown.expect(MessageConversionException.class);
	invocableHandlerMethod.invoke(MessageBuilder.withPayload(123).build());
}
 
Example #13
Source File: MessageBrokerConfigurationTests.java    From spring4-understanding with 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 #14
Source File: AbstractMessageBrokerConfiguration.java    From spring4-understanding with 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 #15
Source File: SimpAnnotationMethodMessageHandler.java    From spring-analysis-note with 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 #16
Source File: DefaultMessageHandlerMethodFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void customMessageConverterFailure() throws Exception {
	DefaultMessageHandlerMethodFactory instance = createInstance();
	MessageConverter messageConverter = new ByteArrayMessageConverter();
	instance.setMessageConverter(messageConverter);
	instance.afterPropertiesSet();

	InvocableHandlerMethod invocableHandlerMethod =
			createInvocableHandlerMethod(instance, "simpleString", String.class);

	thrown.expect(MessageConversionException.class);
	invocableHandlerMethod.invoke(MessageBuilder.withPayload(123).build());
}
 
Example #17
Source File: MessageBrokerConfigurationTests.java    From java-technology-stack with 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 #18
Source File: AbstractMessageBrokerConfiguration.java    From java-technology-stack with 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 #19
Source File: MessageBrokerBeanDefinitionParser.java    From spring-analysis-note with 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 #20
Source File: DefaultMessageHandlerMethodFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void customMessageConverterFailure() throws Exception {
	DefaultMessageHandlerMethodFactory instance = createInstance();
	MessageConverter messageConverter = new ByteArrayMessageConverter();
	instance.setMessageConverter(messageConverter);
	instance.afterPropertiesSet();

	InvocableHandlerMethod invocableHandlerMethod =
			createInvocableHandlerMethod(instance, "simpleString", String.class);

	assertThatExceptionOfType(MessageConversionException.class).isThrownBy(() ->
			invocableHandlerMethod.invoke(MessageBuilder.withPayload(123).build()));
}
 
Example #21
Source File: MessageBrokerConfigurationTests.java    From spring-analysis-note with 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 #22
Source File: AbstractMessageBrokerConfiguration.java    From spring-analysis-note with 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);
}