Add null check after message conversion

AbstractMessageSendingTemplate now checks if MessageConverter.toMessage
returns null and raises an exception.

Issue: SPR-11370
This commit is contained in:
Rossen Stoyanchev 2014-01-31 21:34:18 -05:00
parent f0a53ae54e
commit da369aa826
5 changed files with 51 additions and 5 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -106,4 +106,9 @@ public class CompositeMessageConverter implements MessageConverter {
return null; return null;
} }
@Override
public String toString() {
return "CompositeMessageConverter[contentTypeResolver=" + this.contentTypeResolver +
", converters=" + this.converters + "]";
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -56,4 +56,8 @@ public class DefaultContentTypeResolver implements ContentTypeResolver {
return (mimeType != null) ? mimeType : this.defaultMimeType; return (mimeType != null) ? mimeType : this.defaultMimeType;
} }
@Override
public String toString() {
return "DefaultContentTypeResolver[" + "defaultMimeType=" + this.defaultMimeType + "]";
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,6 +23,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.messaging.Message; import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders; import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.MessagingException; import org.springframework.messaging.MessagingException;
import org.springframework.messaging.converter.MessageConversionException;
import org.springframework.messaging.converter.MessageConverter; import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.converter.SimpleMessageConverter; import org.springframework.messaging.converter.SimpleMessageConverter;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -130,6 +131,14 @@ public abstract class AbstractMessageSendingTemplate<D> implements MessageSendin
MessageHeaders messageHeaders = (headers != null) ? new MessageHeaders(headers) : null; MessageHeaders messageHeaders = (headers != null) ? new MessageHeaders(headers) : null;
Message<?> message = this.converter.toMessage(payload, messageHeaders); Message<?> message = this.converter.toMessage(payload, messageHeaders);
if (message == null) {
String payloadType = (payload != null) ? payload.getClass().getName() : null;
throw new MessageConversionException("Unable to convert payload type '"
+ payloadType + "', Content-Type=" + messageHeaders.get(MessageHeaders.CONTENT_TYPE)
+ ", converter=" + this.converter, null);
}
if (postProcessor != null) { if (postProcessor != null) {
message = postProcessor.postProcessMessage(message); message = postProcessor.postProcessMessage(message);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -20,6 +20,7 @@ import java.util.Map;
import org.springframework.messaging.Message; import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders; import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.converter.MessageConversionException;
/** /**
* An extension of {@link AbstractMessageSendingTemplate} that adds support for * An extension of {@link AbstractMessageSendingTemplate} that adds support for
@ -116,9 +117,18 @@ public abstract class AbstractMessagingTemplate<D> extends AbstractMessageSendin
MessageHeaders messageHeaders = (headers != null) ? new MessageHeaders(headers) : null; MessageHeaders messageHeaders = (headers != null) ? new MessageHeaders(headers) : null;
Message<?> requestMessage = getMessageConverter().toMessage(request, messageHeaders); Message<?> requestMessage = getMessageConverter().toMessage(request, messageHeaders);
if (requestMessage == null) {
String payloadType = (request != null) ? request.getClass().getName() : null;
throw new MessageConversionException("Unable to convert payload type '"
+ payloadType + "', Content-Type=" + messageHeaders.get(MessageHeaders.CONTENT_TYPE)
+ ", converter=" + getMessageConverter(), null);
}
if (postProcessor != null) { if (postProcessor != null) {
requestMessage = postProcessor.postProcessMessage(requestMessage); requestMessage = postProcessor.postProcessMessage(requestMessage);
} }
Message<?> replyMessage = this.sendAndReceive(destination, requestMessage); Message<?> replyMessage = this.sendAndReceive(destination, requestMessage);
return (replyMessage != null) ? (T) getMessageConverter().fromMessage(replyMessage, targetClass) : null; return (replyMessage != null) ? (T) getMessageConverter().fromMessage(replyMessage, targetClass) : null;
} }

View File

@ -16,14 +16,20 @@
package org.springframework.messaging.core; package org.springframework.messaging.core;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.messaging.Message; import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.converter.*;
import org.springframework.messaging.support.GenericMessage; import org.springframework.messaging.support.GenericMessage;
import org.springframework.util.MimeType;
import org.springframework.util.MimeTypeUtils;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -45,7 +51,8 @@ public class MessageSendingTemplateTests {
public void setup() { public void setup() {
this.template = new TestMessageSendingTemplate(); this.template = new TestMessageSendingTemplate();
this.postProcessor = new TestMessagePostProcessor(); this.postProcessor = new TestMessagePostProcessor();
this.headers = Collections.<String, Object>singletonMap("key", "value"); this.headers = new HashMap<>();
this.headers.put("key", "value");
} }
@Test @Test
@ -144,6 +151,17 @@ public class MessageSendingTemplateTests {
assertSame(this.template.message, this.postProcessor.getMessage()); assertSame(this.template.message, this.postProcessor.getMessage());
} }
@Test(expected = MessageConversionException.class)
public void convertAndSendNoMatchingConverter() {
MessageConverter converter = new CompositeMessageConverter(
Arrays.asList(new MappingJackson2MessageConverter()), new DefaultContentTypeResolver());
this.template.setMessageConverter(converter);
this.headers.put(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_XML);
this.template.convertAndSend("home", "payload", new MessageHeaders(this.headers));
}
private static class TestMessageSendingTemplate extends AbstractMessageSendingTemplate<String> { private static class TestMessageSendingTemplate extends AbstractMessageSendingTemplate<String> {