Finished consolidating contentType Message conversion

- Removed MessageSerializationUtils
    - Consolidated Message contentType conversion refactoring that was started with d0be34f7cb commit

At this point Message conversion is consolidated in either MessageConverters or In/Out channel interceptors configured in MessageConverterConfigurer
This commit is contained in:
Oleg Zhurakousky
2017-11-14 20:29:19 -05:00
parent 464a98aa10
commit 024e3cffcc
4 changed files with 40 additions and 74 deletions

View File

@@ -146,9 +146,14 @@ public abstract class AbstractBinder<T, C extends ConsumerProperties, P extends
return name + GROUP_INDEX_DELIMITER + (StringUtils.hasText(group) ? group : "default");
}
/**
* Deprecated as of v2.0. Doesn't do anything other then returns an instance
* of {@link MessageValues} built from {@link Message}. Remains primarily for
* backward compatibility and will be removed in the next major release.
*/
@Deprecated
protected final MessageValues serializePayloadIfNecessary(Message<?> message) {
return MessageSerializationUtils.serializePayload(message);
return new MessageValues(message);
}
@Deprecated

View File

@@ -16,7 +16,6 @@
package org.springframework.cloud.stream.binder;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
@@ -43,7 +42,6 @@ import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.SubscribableChannel;
import org.springframework.messaging.support.ChannelInterceptorAdapter;
import org.springframework.util.Assert;
import org.springframework.util.MimeType;
/**
* {@link AbstractBinder} that serves as base class for {@link MessageChannel} binders.
@@ -595,11 +593,11 @@ public abstract class AbstractMessageChannelBinder<C extends ConsumerProperties,
Object contentType = transformed.get(MessageHeaders.CONTENT_TYPE);
// transform content type headers to String, so that they can be properly
// embedded in JSON
if (contentType instanceof MimeType) {
if (contentType != null) {
transformed.put(MessageHeaders.CONTENT_TYPE, contentType.toString());
}
Object originalContentType = transformed.get(BinderHeaders.BINDER_ORIGINAL_CONTENT_TYPE);
if (originalContentType instanceof MimeType) {
if (originalContentType != null) {
transformed.put(BinderHeaders.BINDER_ORIGINAL_CONTENT_TYPE, originalContentType.toString());
}
payload = EmbeddedHeaderUtils.embedHeaders(transformed, this.embeddedHeaders);

View File

@@ -1,69 +0,0 @@
/*
* Copyright 2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.stream.binder;
import java.nio.charset.StandardCharsets;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
* Utility class for serializing and de-serializing the message payload.
*
* @author Soby Chacko
* @author Vinicius Carvalho
* @author Oleg Zhurakousky
*/
@Deprecated
public abstract class MessageSerializationUtils {
/**
* Serialize the message payload unless it is a byte array.
*
* @param message the message with the payload to serialize
* @return the Message with the serialized payload
*/
public static MessageValues serializePayload(Message<?> message) {
Object originalPayload = message.getPayload();
boolean setOriginalContentType = (originalPayload instanceof String);
Assert.isTrue(originalPayload instanceof byte[] || originalPayload instanceof String,
"Failed to convert message's payload. No suitable converter found for provided contentType: "
+ message.getHeaders().get(MessageHeaders.CONTENT_TYPE) + " and paylod: " + originalPayload);
Object originalContentType = message.getHeaders().get(MessageHeaders.CONTENT_TYPE);
// Pass content type as String since some transport adapters will exclude
// CONTENT_TYPE Header otherwise
String contentType = null;
if (originalContentType != null) {
contentType = setOriginalContentType ? JavaClassMimeTypeUtils.mimeTypeFromObject(originalPayload,
ObjectUtils.nullSafeToString(originalContentType)).toString() : originalContentType.toString() ;
}
Object payload = originalPayload instanceof byte[] ? originalPayload : ((String) originalPayload).getBytes(StandardCharsets.UTF_8);
MessageValues messageValues = new MessageValues(message);
messageValues.setPayload(payload);
if (StringUtils.hasText(contentType)) {
messageValues.put(MessageHeaders.CONTENT_TYPE, contentType);
if (originalContentType != null && !originalContentType.toString().equals(contentType.toString())) {
messageValues.put(BinderHeaders.BINDER_ORIGINAL_CONTENT_TYPE, originalContentType.toString());
}
}
return messageValues;
}
}

View File

@@ -26,6 +26,8 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.cloud.stream.binder.BinderException;
import org.springframework.cloud.stream.binder.BinderHeaders;
import org.springframework.cloud.stream.binder.ConsumerProperties;
import org.springframework.cloud.stream.binder.JavaClassMimeTypeUtils;
import org.springframework.cloud.stream.binder.MessageValues;
import org.springframework.cloud.stream.binder.PartitionHandler;
import org.springframework.cloud.stream.binder.PartitionKeyExtractorStrategy;
import org.springframework.cloud.stream.binder.PartitionSelectorStrategy;
@@ -52,6 +54,7 @@ import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.MimeType;
import org.springframework.util.MimeTypeUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
@@ -354,9 +357,38 @@ public class MessageConverterConfigurer
.copyHeaders(headers)
.build();
}
postProcessedMessage = this.finishPreSend(postProcessedMessage);
}
return postProcessedMessage;
}
/**
* This is strictly to support 1.3 semantics where BINDER_ORIGINAL_CONTENT_TYPE header
* needs to be set for certain cases and String payloads needs to be converted to byte[].
*
* Factored out of what was left of MessageSerializationUtils.
*/
// deprecated at the get go as a reminder to remove at v3.0
@Deprecated
private Message<?> finishPreSend(Message<?> message) {
String oct = message.getHeaders().containsKey(MessageHeaders.CONTENT_TYPE) ? message.getHeaders().get(MessageHeaders.CONTENT_TYPE).toString() : null;
String ct = oct;
if (message.getPayload() instanceof String) {
ct = JavaClassMimeTypeUtils.mimeTypeFromObject(message.getPayload(), ObjectUtils.nullSafeToString(oct)).toString();
}
MessageValues messageValues = new MessageValues(message);
Object payload = message.getPayload();
if (payload instanceof String) {
payload = ((String)payload).getBytes(StandardCharsets.UTF_8);
}
messageValues.setPayload(payload);
if (ct != null && !ct.equals(oct)) {
messageValues.put(MessageHeaders.CONTENT_TYPE, ct);
messageValues.put(BinderHeaders.BINDER_ORIGINAL_CONTENT_TYPE, oct);
}
return messageValues.toMessage();
}
}