use of org.springframework.messaging.converter.MessageConversionException in project spring-cloud-stream by spring-cloud.
the class MessageConverterConfigurerTests method testConfigureOutputChannelCannotConvert.
@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);
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'");
}
}
use of org.springframework.messaging.converter.MessageConversionException in project spring-cloud-stream by spring-cloud.
the class AbstractAvroMessageConverter method convertFromInternal.
@Override
protected Object convertFromInternal(Message<?> message, Class<?> targetClass, Object conversionHint) {
Object result = null;
try {
byte[] payload = (byte[]) message.getPayload();
MimeType mimeType = getContentTypeResolver().resolve(message.getHeaders());
if (mimeType == null) {
if (conversionHint instanceof MimeType) {
mimeType = (MimeType) conversionHint;
} else {
return null;
}
}
Schema writerSchema = resolveWriterSchemaForDeserialization(mimeType);
Schema readerSchema = resolveReaderSchemaForDeserialization(targetClass);
@SuppressWarnings("unchecked") DatumReader<Object> reader = getDatumReader((Class<Object>) targetClass, readerSchema, writerSchema);
Decoder decoder = DecoderFactory.get().binaryDecoder(payload, null);
result = reader.read(null, decoder);
} catch (IOException e) {
throw new MessageConversionException(message, "Failed to read payload", e);
}
return result;
}
use of org.springframework.messaging.converter.MessageConversionException in project spring-framework by spring-projects.
the class MessageReceivingTemplateTests method receiveAndConvertNoConverter.
@Test
public void receiveAndConvertNoConverter() {
Message<?> expected = new GenericMessage<>("payload");
this.template.setDefaultDestination("home");
this.template.setReceiveMessage(expected);
this.template.setMessageConverter(new GenericMessageConverter());
try {
this.template.receiveAndConvert(Writer.class);
} catch (MessageConversionException ex) {
assertThat(ex.getMessage().contains("payload")).as("Invalid exception message '" + ex.getMessage() + "'").isTrue();
assertThat(ex.getFailedMessage()).isSameAs(expected);
}
}
use of org.springframework.messaging.converter.MessageConversionException in project spring-framework by spring-projects.
the class DefaultStompSession method invokeHandler.
private void invokeHandler(StompFrameHandler handler, Message<byte[]> message, StompHeaders headers) {
if (message.getPayload().length == 0) {
handler.handleFrame(headers, null);
return;
}
Type payloadType = handler.getPayloadType(headers);
Class<?> resolvedType = ResolvableType.forType(payloadType).resolve();
if (resolvedType == null) {
throw new MessageConversionException("Unresolvable payload type [" + payloadType + "] from handler type [" + handler.getClass() + "]");
}
Object object = getMessageConverter().fromMessage(message, resolvedType);
if (object == null) {
throw new MessageConversionException("No suitable converter for payload type [" + payloadType + "] from handler type [" + handler.getClass() + "]");
}
handler.handleFrame(headers, object);
}
use of org.springframework.messaging.converter.MessageConversionException in project spring-cloud-stream by spring-cloud.
the class AbstractAvroMessageConverter method convertToInternal.
@Override
protected Object convertToInternal(Object payload, MessageHeaders headers, Object conversionHint) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
MimeType hintedContentType = null;
if (conversionHint instanceof MimeType) {
hintedContentType = (MimeType) conversionHint;
}
Schema schema = resolveSchemaForWriting(payload, headers, hintedContentType);
@SuppressWarnings("unchecked") DatumWriter<Object> writer = getDatumWriter((Class<Object>) payload.getClass(), schema);
Encoder encoder = EncoderFactory.get().binaryEncoder(baos, null);
writer.write(payload, encoder);
encoder.flush();
} catch (IOException e) {
throw new MessageConversionException("Failed to write payload", e);
}
return baos.toByteArray();
}
Aggregations