Skip to content

Commit 02e8198

Browse files
committed
MessageConversionException offers constructor without cause argument now, plus related polishing
Issue: SPR-11653 (cherry picked from commit 2888775)
1 parent 090ab4b commit 02e8198

File tree

6 files changed

+96
-127
lines changed

6 files changed

+96
-127
lines changed

spring-messaging/src/main/java/org/springframework/messaging/converter/MessageConversionException.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,9 @@
2828
@SuppressWarnings("serial")
2929
public class MessageConversionException extends MessagingException {
3030

31+
public MessageConversionException(String description) {
32+
super(description);
33+
}
3134

3235
public MessageConversionException(String description, Throwable cause) {
3336
super(description, cause);

spring-messaging/src/main/java/org/springframework/messaging/core/AbstractDestinationResolvingMessagingTemplate.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,9 +25,9 @@
2525
* An extension of {@link AbstractMessagingTemplate} that adds operations for sending
2626
* messages to a resolvable destination name as defined by the following interfaces:
2727
* <ul>
28-
* <li>{@link DestinationResolvingMessageSendingOperations}</li>
29-
* <li>{@link DestinationResolvingMessageReceivingOperations}</li>
30-
* <li>{@link DestinationResolvingMessageRequestReplyOperations}</li>
28+
* <li>{@link DestinationResolvingMessageSendingOperations}</li>
29+
* <li>{@link DestinationResolvingMessageReceivingOperations}</li>
30+
* <li>{@link DestinationResolvingMessageRequestReplyOperations}</li>
3131
* </ul>
3232
*
3333
* @author Mark Fisher
@@ -65,36 +65,31 @@ public DestinationResolver<D> getDestinationResolver() {
6565
@Override
6666
public void send(String destinationName, Message<?> message) {
6767
D destination = resolveDestination(destinationName);
68-
this.doSend(destination, message);
68+
doSend(destination, message);
6969
}
7070

7171
protected final D resolveDestination(String destinationName) {
72-
Assert.state(this.destinationResolver != null, "destinationResolver is required to resolve destination names");
72+
Assert.state(this.destinationResolver != null, "DestinationResolver is required to resolve destination names");
7373
return this.destinationResolver.resolveDestination(destinationName);
7474
}
7575

7676
@Override
7777
public <T> void convertAndSend(String destinationName, T payload) {
78-
Map<String, Object> headers = null;
79-
this.convertAndSend(destinationName, payload, headers);
78+
convertAndSend(destinationName, payload, null, null);
8079
}
8180

8281
@Override
8382
public <T> void convertAndSend(String destinationName, T payload, Map<String, Object> headers) {
84-
MessagePostProcessor postProcessor = null;
85-
this.convertAndSend(destinationName, payload, headers, postProcessor);
83+
convertAndSend(destinationName, payload, headers, null);
8684
}
8785

8886
@Override
8987
public <T> void convertAndSend(String destinationName, T payload, MessagePostProcessor postProcessor) {
90-
Map<String, Object> headers = null;
91-
this.convertAndSend(destinationName, payload, headers, postProcessor);
88+
convertAndSend(destinationName, payload, null, postProcessor);
9289
}
9390

9491
@Override
95-
public <T> void convertAndSend(String destinationName, T payload, Map<String, Object> headers,
96-
MessagePostProcessor postProcessor) {
97-
92+
public <T> void convertAndSend(String destinationName, T payload, Map<String, Object> headers, MessagePostProcessor postProcessor) {
9893
D destination = resolveDestination(destinationName);
9994
super.convertAndSend(destination, payload, headers, postProcessor);
10095
}

spring-messaging/src/main/java/org/springframework/messaging/core/AbstractMessageSendingTemplate.java

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.apache.commons.logging.Log;
2222
import org.apache.commons.logging.LogFactory;
23+
2324
import org.springframework.messaging.Message;
2425
import org.springframework.messaging.MessageHeaders;
2526
import org.springframework.messaging.MessagingException;
@@ -37,7 +38,7 @@
3738
*/
3839
public abstract class AbstractMessageSendingTemplate<D> implements MessageSendingOperations<D> {
3940

40-
protected final Log logger = LogFactory.getLog(this.getClass());
41+
protected final Log logger = LogFactory.getLog(getClass());
4142

4243
private volatile D defaultDestination;
4344

@@ -80,79 +81,75 @@ public MessageConverter getMessageConverter() {
8081

8182
@Override
8283
public void send(Message<?> message) {
83-
this.send(getRequiredDefaultDestination(), message);
84+
send(getRequiredDefaultDestination(), message);
8485
}
8586

8687
protected final D getRequiredDefaultDestination() {
87-
Assert.state(this.defaultDestination != null, "No 'defaultDestination' configured.");
88+
Assert.state(this.defaultDestination != null, "No 'defaultDestination' configured");
8889
return this.defaultDestination;
8990
}
9091

9192
@Override
9293
public void send(D destination, Message<?> message) {
93-
this.doSend(destination, message);
94+
doSend(destination, message);
9495
}
9596

9697
protected abstract void doSend(D destination, Message<?> message);
9798

9899

99100
@Override
100101
public void convertAndSend(Object payload) throws MessagingException {
101-
this.convertAndSend(getRequiredDefaultDestination(), payload);
102+
convertAndSend(getRequiredDefaultDestination(), payload);
102103
}
103104

104105
@Override
105106
public void convertAndSend(D destination, Object payload) throws MessagingException {
106-
this.convertAndSend(destination, payload, (Map<String, Object>) null);
107+
convertAndSend(destination, payload, (Map<String, Object>) null);
107108
}
108109

109110
@Override
110111
public void convertAndSend(D destination, Object payload, Map<String, Object> headers) throws MessagingException {
111-
MessagePostProcessor postProcessor = null;
112-
this.convertAndSend(destination, payload, headers, postProcessor);
112+
convertAndSend(destination, payload, headers, null);
113113
}
114114

115115
@Override
116116
public void convertAndSend(Object payload, MessagePostProcessor postProcessor) throws MessagingException {
117-
this.convertAndSend(getRequiredDefaultDestination(), payload, postProcessor);
117+
convertAndSend(getRequiredDefaultDestination(), payload, postProcessor);
118118
}
119119

120120
@Override
121121
public void convertAndSend(D destination, Object payload, MessagePostProcessor postProcessor)
122122
throws MessagingException {
123123

124-
Map<String, Object> headers = null;
125-
this.convertAndSend(destination, payload, headers, postProcessor);
124+
convertAndSend(destination, payload, null, postProcessor);
126125
}
127126

128127
@Override
129128
public void convertAndSend(D destination, Object payload, Map<String, Object> headers,
130129
MessagePostProcessor postProcessor) throws MessagingException {
131130

132-
headers = processHeadersToSend(headers);
133-
MessageHeaders messageHeaders = (headers != null) ? new MessageHeaders(headers) : null;
134-
Message<?> message = this.converter.toMessage(payload, messageHeaders);
131+
Map<String, Object> headersToUse = processHeadersToSend(headers);
132+
MessageHeaders messageHeaders = (headersToUse != null ? new MessageHeaders(headersToUse) : null);
135133

134+
Message<?> message = getMessageConverter().toMessage(payload, messageHeaders);
136135
if (message == null) {
137-
String payloadType = (payload != null) ? payload.getClass().getName() : null;
138-
throw new MessageConversionException("Unable to convert payload type '"
139-
+ payloadType + "', Content-Type=" + messageHeaders.get(MessageHeaders.CONTENT_TYPE)
140-
+ ", converter=" + this.converter, null);
136+
String payloadType = (payload != null ? payload.getClass().getName() : null);
137+
Object contentType = (messageHeaders != null ? messageHeaders.get(MessageHeaders.CONTENT_TYPE) : null);
138+
throw new MessageConversionException("Unable to convert payload with type='" + payloadType +
139+
"', contentType='" + contentType + "', converter=[" + getMessageConverter() + "]");
141140
}
142-
143141
if (postProcessor != null) {
144142
message = postProcessor.postProcessMessage(message);
145143
}
146-
this.send(destination, message);
144+
send(destination, message);
147145
}
148146

149147
/**
150-
* Provides access to the map of headers before a send operation.
151-
* Implementations can modify the headers by returning a different map.
152-
* This implementation returns the map that was passed in (i.e. without any changes).
153-
*
154-
* @param headers the headers to send, possibly {@code null}
155-
* @return the actual headers to send
148+
* Provides access to the map of input headers before a send operation. Sub-classes
149+
* can modify the headers and then return the same or a different map.
150+
* <p>This default implementation in this class returns the input map.
151+
* @param headers the headers to send or {@code null}
152+
* @return the actual headers to send or {@code null}
156153
*/
157154
protected Map<String, Object> processHeadersToSend(Map<String, Object> headers) {
158155
return headers;

spring-messaging/src/main/java/org/springframework/messaging/core/AbstractMessagingTemplate.java

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,26 @@ public abstract class AbstractMessagingTemplate<D> extends AbstractMessageSendin
3737

3838
@Override
3939
public Message<?> receive() {
40-
return this.receive(getRequiredDefaultDestination());
40+
return receive(getRequiredDefaultDestination());
4141
}
4242

4343
@Override
4444
public Message<?> receive(D destination) {
45-
return this.doReceive(destination);
45+
return doReceive(destination);
4646
}
4747

4848
protected abstract Message<?> doReceive(D destination);
4949

5050

5151
@Override
5252
public <T> T receiveAndConvert(Class<T> targetClass) {
53-
return this.receiveAndConvert(getRequiredDefaultDestination(), targetClass);
53+
return receiveAndConvert(getRequiredDefaultDestination(), targetClass);
5454
}
5555

5656
@SuppressWarnings("unchecked")
5757
@Override
5858
public <T> T receiveAndConvert(D destination, Class<T> targetClass) {
59-
Message<?> message = this.doReceive(destination);
59+
Message<?> message = doReceive(destination);
6060
if (message != null) {
6161
return (T) getMessageConverter().fromMessage(message, targetClass);
6262
}
@@ -67,70 +67,63 @@ public <T> T receiveAndConvert(D destination, Class<T> targetClass) {
6767

6868
@Override
6969
public Message<?> sendAndReceive(Message<?> requestMessage) {
70-
return this.sendAndReceive(getRequiredDefaultDestination(), requestMessage);
70+
return sendAndReceive(getRequiredDefaultDestination(), requestMessage);
7171
}
7272

7373
@Override
7474
public Message<?> sendAndReceive(D destination, Message<?> requestMessage) {
75-
return this.doSendAndReceive(destination, requestMessage);
75+
return doSendAndReceive(destination, requestMessage);
7676
}
7777

7878
protected abstract Message<?> doSendAndReceive(D destination, Message<?> requestMessage);
7979

8080

8181
@Override
8282
public <T> T convertSendAndReceive(Object request, Class<T> targetClass) {
83-
return this.convertSendAndReceive(getRequiredDefaultDestination(), request, targetClass);
83+
return convertSendAndReceive(getRequiredDefaultDestination(), request, targetClass);
8484
}
8585

8686
@Override
8787
public <T> T convertSendAndReceive(D destination, Object request, Class<T> targetClass) {
88-
Map<String, Object> headers = null;
89-
return this.convertSendAndReceive(destination, request, headers, targetClass);
88+
return convertSendAndReceive(destination, request, null, targetClass);
9089
}
9190

9291
@Override
93-
public <T> T convertSendAndReceive(D destination, Object request, Map<String, Object> headers,
94-
Class<T> targetClass) {
95-
96-
MessagePostProcessor postProcessor = null;
97-
return this.convertSendAndReceive(destination, request, headers, targetClass, postProcessor);
92+
public <T> T convertSendAndReceive(D destination, Object request, Map<String, Object> headers, Class<T> targetClass) {
93+
return convertSendAndReceive(destination, request, headers, targetClass, null);
9894
}
9995

10096
@Override
10197
public <T> T convertSendAndReceive(Object request, Class<T> targetClass, MessagePostProcessor postProcessor) {
102-
return this.convertSendAndReceive(getRequiredDefaultDestination(), request, targetClass, postProcessor);
98+
return convertSendAndReceive(getRequiredDefaultDestination(), request, targetClass, postProcessor);
10399
}
104100

105101
@Override
106-
public <T> T convertSendAndReceive(D destination, Object request, Class<T> targetClass,
107-
MessagePostProcessor postProcessor) {
108-
109-
Map<String, Object> headers = null;
110-
return this.convertSendAndReceive(destination, request, headers, targetClass, postProcessor);
102+
public <T> T convertSendAndReceive(D destination, Object request, Class<T> targetClass, MessagePostProcessor postProcessor) {
103+
return convertSendAndReceive(destination, request, null, targetClass, postProcessor);
111104
}
112105

113106
@SuppressWarnings("unchecked")
114107
@Override
115108
public <T> T convertSendAndReceive(D destination, Object request, Map<String, Object> headers,
116109
Class<T> targetClass, MessagePostProcessor postProcessor) {
117110

118-
MessageHeaders messageHeaders = (headers != null) ? new MessageHeaders(headers) : null;
111+
MessageHeaders messageHeaders = (headers != null ? new MessageHeaders(headers) : null);
119112
Message<?> requestMessage = getMessageConverter().toMessage(request, messageHeaders);
120113

121114
if (requestMessage == null) {
122-
String payloadType = (request != null) ? request.getClass().getName() : null;
123-
throw new MessageConversionException("Unable to convert payload type '"
124-
+ payloadType + "', Content-Type=" + messageHeaders.get(MessageHeaders.CONTENT_TYPE)
125-
+ ", converter=" + getMessageConverter(), null);
115+
String payloadType = (request != null ? request.getClass().getName() : null);
116+
Object contentType = (messageHeaders != null ? messageHeaders.get(MessageHeaders.CONTENT_TYPE) : null);
117+
throw new MessageConversionException("Unable to convert payload with type '" + payloadType +
118+
"', contentType='" + contentType + "', converter=[" + getMessageConverter() + "]");
126119
}
127120

128121
if (postProcessor != null) {
129122
requestMessage = postProcessor.postProcessMessage(requestMessage);
130123
}
131124

132-
Message<?> replyMessage = this.sendAndReceive(destination, requestMessage);
133-
return (replyMessage != null) ? (T) getMessageConverter().fromMessage(replyMessage, targetClass) : null;
125+
Message<?> replyMessage = sendAndReceive(destination, requestMessage);
126+
return (replyMessage != null ? (T) getMessageConverter().fromMessage(replyMessage, targetClass) : null);
134127
}
135128

136129
}

0 commit comments

Comments
 (0)