Source:
protected final Message<?> transformOutboundIfNecessary(Message<?> message, MediaType to) { Message<?> messageToSend = message; Object originalPayload = message.getPayload(); Object payload = null; Object originalContentType = message.getHeaders().get(MessageHeaders.CONTENT_TYPE); String originalContentTypeString = null; if (originalContentType instanceof MediaType) { originalContentTypeString = originalContentType.toString(); } else if (originalContentType instanceof String) { originalContentTypeString = (String) originalContentType; } String contentType = originalContentTypeString; if (to.equals(MediaType.ALL)) { return message; } else if (to.equals(MediaType.APPLICATION_OCTET_STREAM)) { if (originalPayload instanceof byte[]) { payload = originalPayload; contentType = XD_OCTET_STREAM_VALUE; } else if (originalPayload instanceof String) { try { payload = ((String) originalPayload).getBytes("UTF-8"); if (contentType == null) { contentType = XD_TEXT_PLAIN_UTF8_VALUE; } } catch (UnsupportedEncodingException e) { logger.error("Could not convert String to bytes", e); } } else { payload = this.jsonMapper.toBytes(originalPayload); contentType = XD_JSON_OCTET_STREAM_VALUE; } } else { throw new IllegalArgumentException("'to' can only be 'ALL' or 'APPLICATION_OCTET_STREAM'"); } if (payload != null) { MessageBuilder<Object> messageBuilder = MessageBuilder.withPayload(payload).copyHeaders( message.getHeaders()).setHeader(MessageHeaders.CONTENT_TYPE, contentType); if (originalContentTypeString != null) { messageBuilder.setHeader(ORIGINAL_CONTENT_TYPE_HEADER, originalContentTypeString); } messageToSend = messageBuilder.build(); } return messageToSend; }

Target:
protected final Message<?> transformOutboundIfNecessary(Message<?> message, MediaType to) { Message<?> messageToSend = message; Object originalPayload = message.getPayload(); Object payload = null; Object originalContentType = message.getHeaders().get(MessageHeaders.CONTENT_TYPE); String originalContentTypeString = null; if (originalContentType instanceof MediaType) { originalContentTypeString = originalContentType.toString(); } else if (originalContentType instanceof String) { originalContentTypeString = (String) originalContentType; } String contentType = originalContentTypeString; if (to.equals(MediaType.ALL)) { return message; } else if (to.equals(MediaType.APPLICATION_OCTET_STREAM)) { if (originalPayload instanceof byte[]) { payload = originalPayload; contentType = XD_OCTET_STREAM_VALUE; } else if (originalPayload instanceof String) { try { payload = ((String) originalPayload).getBytes("UTF-8"); if (MediaType.APPLICATION_JSON_VALUE.equals(contentType)) { contentType = XD_JSON_TEXT_VALUE; } else { contentType = XD_TEXT_PLAIN_UTF8_VALUE; } } catch (UnsupportedEncodingException e) { logger.error("Could not convert String to bytes", e); } } else { payload = this.jsonMapper.toBytes(originalPayload); contentType = XD_JSON_OCTET_STREAM_VALUE; } } else { throw new IllegalArgumentException("'to' can only be 'ALL' or 'APPLICATION_OCTET_STREAM'"); } if (payload != null) { MessageBuilder<Object> messageBuilder = MessageBuilder.withPayload(payload).copyHeaders( message.getHeaders()).setHeader(MessageHeaders.CONTENT_TYPE, contentType); if (originalContentTypeString != null) { messageBuilder.setHeader(ORIGINAL_CONTENT_TYPE_HEADER, originalContentTypeString); } messageToSend = messageBuilder.build(); } return messageToSend; }