org.springframework.messaging.converter.AbstractMessageConverter Java Examples

The following examples show how to use org.springframework.messaging.converter.AbstractMessageConverter. 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: FunctionInvokerBackgroundTests.java    From spring-cloud-function with Apache License 2.0 6 votes vote down vote up
@Bean
public MessageConverter messageToIncomingRequestConverter(JsonMapper mapper) {
	return new AbstractMessageConverter() {

		@Override
		protected boolean supports(Class<?> aClass) {
			return aClass == IncomingRequest.class;
		}

		@Override
		protected Object convertFromInternal(Message<?> message, Class<?> targetClass, Object conversionHint) {
			PubSubMessage pubSubMessage = mapper.fromJson(message.getPayload(), PubSubMessage.class);
			return mapper.fromJson(pubSubMessage.getData(), IncomingRequest.class);
		}
	};
}
 
Example #2
Source File: NegotiatingMessageConverterWrapperTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimpleDeserializationDelegation() {
	Message<String> someMessage = MessageBuilder.withPayload("some payload")
		.setHeader(MessageHeaders.CONTENT_TYPE, "text/plain").build();
	AbstractMessageConverter delegate = mock(AbstractMessageConverter.class);

	Object result = NegotiatingMessageConverterWrapper.wrap(delegate).fromMessage(someMessage, String.class);

	verify(delegate).fromMessage(someMessage, String.class);
	assertThat(result).isEqualTo(delegate.fromMessage(someMessage, String.class));
}
 
Example #3
Source File: NegotiatingMessageConverterWrapperTests.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Test
public void testSmartDeserializationDelegation() {
	Message<String> someMessage = MessageBuilder.withPayload("some payload")
		.setHeader(MessageHeaders.CONTENT_TYPE, "text/plain").build();
	MethodParameter someHint = mock(MethodParameter.class);
	AbstractMessageConverter delegate = mock(AbstractMessageConverter.class);

	Object result = NegotiatingMessageConverterWrapper.wrap(delegate)
		.fromMessage(someMessage, String.class, someHint);

	verify(delegate).fromMessage(someMessage, String.class, someHint);
	assertThat(result).isEqualTo(delegate.fromMessage(someMessage, String.class, someHint));
}
 
Example #4
Source File: CompositeMessageConverterFactory.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
/**
 * @param customConverters a list of {@link AbstractMessageConverter}
 * @param objectMapper object mapper for for serialization / deserialization
 */
public CompositeMessageConverterFactory(
		List<? extends MessageConverter> customConverters,
		ObjectMapper objectMapper) {
	this.objectMapper = objectMapper;
	if (!CollectionUtils.isEmpty(customConverters)) {
		this.converters = new ArrayList<>(customConverters);
	}
	else {
		this.converters = new ArrayList<>();
	}
	initDefaultConverters();

	Field headersField = ReflectionUtils.findField(MessageHeaders.class, "headers");
	headersField.setAccessible(true);
	DefaultContentTypeResolver resolver = new DefaultContentTypeResolver() {
		@Override
		@SuppressWarnings("unchecked")
		public MimeType resolve(@Nullable MessageHeaders headers) {
			Object contentType = headers.get(MessageHeaders.CONTENT_TYPE);
			if (contentType instanceof byte[]) {
				contentType = new String((byte[]) contentType, StandardCharsets.UTF_8);
				contentType = ((String) contentType).replace("\"", "");
				Map<String, Object> headersMap = (Map<String, Object>) ReflectionUtils.getField(headersField, headers);
				headersMap.put(MessageHeaders.CONTENT_TYPE, contentType);
			}
			return super.resolve(headers);
		}
	};
	resolver.setDefaultMimeType(BindingProperties.DEFAULT_CONTENT_TYPE);
	this.converters.stream().filter(mc -> mc instanceof AbstractMessageConverter)
			.forEach(mc -> ((AbstractMessageConverter) mc)
					.setContentTypeResolver(resolver));
}
 
Example #5
Source File: CompositeMessageConverterFactory.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
/**
 * Creation method.
 * @param mimeType the target MIME type
 * @return a converter for the target MIME type
 */
public MessageConverter getMessageConverterForType(MimeType mimeType) {
	List<MessageConverter> converters = new ArrayList<>();
	for (MessageConverter converter : this.converters) {
		if (converter instanceof AbstractMessageConverter) {
			for (MimeType type : ((AbstractMessageConverter) converter)
					.getSupportedMimeTypes()) {
				if (type.includes(mimeType)) {
					converters.add(converter);
				}
			}
		}
		else {
			if (this.log.isDebugEnabled()) {
				this.log.debug("Ommitted " + converter + " of type "
						+ converter.getClass().toString() + " for '"
						+ mimeType.toString()
						+ "' as it is not an AbstractMessageConverter");
			}
		}
	}
	if (CollectionUtils.isEmpty(converters)) {
		throw new ConversionException(
				"No message converter is registered for " + mimeType.toString());
	}
	if (converters.size() > 1) {
		return new CompositeMessageConverter(converters);
	}
	else {
		return converters.get(0);
	}
}
 
Example #6
Source File: MessageConverterConfigurerTests.java    From spring-cloud-stream with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore
public void testConfigureOutputChannelCannotConvert() {
	BindingServiceProperties props = new BindingServiceProperties();
	BindingProperties bindingProps = new BindingProperties();
	bindingProps.setContentType("foo/bar");
	props.setBindings(Collections.singletonMap("foo", bindingProps));
	MessageConverter converter = new AbstractMessageConverter(
			new MimeType("foo", "bar")) {

		@Override
		protected boolean supports(Class<?> clazz) {
			return true;
		}

		@Override
		protected Object convertToInternal(Object payload, MessageHeaders headers,
				Object conversionHint) {
			return null;
		}

	};
	CompositeMessageConverterFactory converterFactory = new CompositeMessageConverterFactory(
			Collections.<MessageConverter>singletonList(converter), null);
	MessageConverterConfigurer configurer = new MessageConverterConfigurer(props,
			converterFactory.getMessageConverterForAllRegistered());
	QueueChannel out = new QueueChannel();
	configurer.configureOutputChannel(out, "foo");
	try {
		out.send(new GenericMessage<Foo>(new Foo(),
				Collections.<String, Object>singletonMap(MessageHeaders.CONTENT_TYPE,
						"bad/ct")));
		fail("Expected MessageConversionException: " + out.receive(0));
	}
	catch (MessageConversionException e) {
		assertThat(e.getMessage())
				.endsWith("to the configured output type: 'foo/bar'");
	}
}
 
Example #7
Source File: BusJacksonAutoConfiguration.java    From spring-cloud-bus with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean(name = "busJsonConverter")
@StreamMessageConverter
public AbstractMessageConverter busJsonConverter(
		@Autowired(required = false) ObjectMapper objectMapper) {
	return new BusJacksonMessageConverter(objectMapper);
}
 
Example #8
Source File: NegotiatingMessageConverterWrapper.java    From spring-cloud-function with Apache License 2.0 4 votes vote down vote up
private NegotiatingMessageConverterWrapper(AbstractMessageConverter delegate) {
	this.delegate = delegate;
}
 
Example #9
Source File: NegotiatingMessageConverterWrapper.java    From spring-cloud-function with Apache License 2.0 4 votes vote down vote up
public static NegotiatingMessageConverterWrapper wrap(AbstractMessageConverter delegate) {
	return new NegotiatingMessageConverterWrapper(delegate);
}