Search in sources :

Example 6 with MessageConversionException

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'");
    }
}
Also used : MessageConversionException(org.springframework.messaging.converter.MessageConversionException) QueueChannel(org.springframework.integration.channel.QueueChannel) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) AbstractMessageConverter(org.springframework.messaging.converter.AbstractMessageConverter) MessageConverter(org.springframework.messaging.converter.MessageConverter) AbstractMessageConverter(org.springframework.messaging.converter.AbstractMessageConverter) MimeType(org.springframework.util.MimeType) BindingServiceProperties(org.springframework.cloud.stream.config.BindingServiceProperties) MessageHeaders(org.springframework.messaging.MessageHeaders) CompositeMessageConverterFactory(org.springframework.cloud.stream.converter.CompositeMessageConverterFactory) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 7 with MessageConversionException

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;
}
Also used : MessageConversionException(org.springframework.messaging.converter.MessageConversionException) Schema(org.apache.avro.Schema) IOException(java.io.IOException) Decoder(org.apache.avro.io.Decoder) MimeType(org.springframework.util.MimeType)

Example 8 with MessageConversionException

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);
    }
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) GenericMessageConverter(org.springframework.messaging.converter.GenericMessageConverter) MessageConversionException(org.springframework.messaging.converter.MessageConversionException) Test(org.junit.jupiter.api.Test)

Example 9 with MessageConversionException

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);
}
Also used : ResolvableType(org.springframework.core.ResolvableType) Type(java.lang.reflect.Type) MessageConversionException(org.springframework.messaging.converter.MessageConversionException)

Example 10 with MessageConversionException

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();
}
Also used : MessageConversionException(org.springframework.messaging.converter.MessageConversionException) Encoder(org.apache.avro.io.Encoder) Schema(org.apache.avro.Schema) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) MimeType(org.springframework.util.MimeType)

Aggregations

MessageConversionException (org.springframework.messaging.converter.MessageConversionException)16 IOException (java.io.IOException)3 Test (org.junit.Test)3 MessageHandlingException (org.springframework.messaging.MessageHandlingException)3 MessageConverter (org.springframework.messaging.converter.MessageConverter)3 SmartMessageConverter (org.springframework.messaging.converter.SmartMessageConverter)3 MimeType (org.springframework.util.MimeType)3 Schema (org.apache.avro.Schema)2 SpringApplicationBuilder (org.springframework.boot.builder.SpringApplicationBuilder)2 InputDestination (org.springframework.cloud.stream.binder.test.InputDestination)2 TestChannelBinder (org.springframework.cloud.stream.binder.test.TestChannelBinder)2 ApplicationContext (org.springframework.context.ApplicationContext)2 MessageHeaders (org.springframework.messaging.MessageHeaders)2 JavaType (com.fasterxml.jackson.databind.JavaType)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Type (java.lang.reflect.Type)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1