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");
* you may not use this file except in compliance with the License.
@@ -106,4 +106,9 @@ public class CompositeMessageConverter implements MessageConverter {
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");
* 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;
}
@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");
* 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.MessageHeaders;
import org.springframework.messaging.MessagingException;
import org.springframework.messaging.converter.MessageConversionException;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.converter.SimpleMessageConverter;
import org.springframework.util.Assert;
@@ -130,6 +131,14 @@ public abstract class AbstractMessageSendingTemplate<D> implements MessageSendin
MessageHeaders messageHeaders = (headers != null) ? new MessageHeaders(headers) : null;
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) {
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");
* 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.MessageHeaders;
import org.springframework.messaging.converter.MessageConversionException;
/**
* 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;
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) {
requestMessage = postProcessor.postProcessMessage(requestMessage);
}
Message<?> replyMessage = this.sendAndReceive(destination, requestMessage);
return (replyMessage != null) ? (T) getMessageConverter().fromMessage(replyMessage, targetClass) : null;
}