Support text/* contentType sent by non-SCSt applications

- When deserializing the payload at the consumer endpoint, the non-byte stream payload type requires to use `String` object when the underlying message content-type is of any `text` type contentType (text/plain, text/xml and text/html).
 - This fix is only needed to support any non-SCSt applications that will have the 'text/*` contentType of the message that is being deserialized
 - Add test

This resolves #403
This commit is contained in:
Ilayaperumal Gopinathan
2016-03-07 11:34:34 +05:30
parent 6acb825ad5
commit 4d8fa19a3a
2 changed files with 14 additions and 2 deletions

View File

@@ -111,6 +111,19 @@ public class MessageChannelBinderSupportTests {
assertNull(reconstructed.get(MessageHeaders.CONTENT_TYPE));
}
@Test
public void testStringXML() throws IOException {
Message<?> message = MessageBuilder
.withPayload("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><test></test>")
.setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_XML)
.build();
Message<?> converted = binder.serializePayloadIfNecessary(message).toMessage();
assertEquals(MimeTypeUtils.TEXT_PLAIN, contentTypeResolver.resolve(converted.getHeaders()));
MessageValues reconstructed = binder.deserializePayloadIfNecessary(converted);
assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><test></test>", reconstructed.getPayload());
assertEquals(MimeTypeUtils.TEXT_XML.toString(), reconstructed.get(MessageHeaders.CONTENT_TYPE));
}
@Test
public void testContentTypePreserved() throws IOException {
Message<String> inbound = MessageBuilder.withPayload("{\"foo\":\"foo\"}")

View File

@@ -18,7 +18,6 @@ package org.springframework.cloud.stream.binder;
import static org.springframework.util.MimeTypeUtils.APPLICATION_JSON;
import static org.springframework.util.MimeTypeUtils.APPLICATION_OCTET_STREAM;
import static org.springframework.util.MimeTypeUtils.TEXT_PLAIN;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -426,7 +425,7 @@ public abstract class AbstractBinder<T> implements ApplicationContextAware, Init
}
private Object deserializePayload(byte[] bytes, MimeType contentType) {
if (TEXT_PLAIN.equals(contentType) || APPLICATION_JSON.equals(contentType)) {
if ("text".equalsIgnoreCase(contentType.getType()) || APPLICATION_JSON.equals(contentType)) {
try {
return new String(bytes, "UTF-8");
}