|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2014 the original author or authors. |
|
|
|
* Copyright 2002-2015 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
@ -32,10 +32,10 @@ import org.springframework.util.Assert; |
|
|
|
import org.springframework.util.MimeType; |
|
|
|
import org.springframework.util.MimeType; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Abstract base class for {@link MessageConverter} implementations including support for |
|
|
|
* Abstract base class for {@link MessageConverter} implementations including support |
|
|
|
* common properties and a partial implementation of the conversion methods mainly to |
|
|
|
* for common properties and a partial implementation of the conversion methods, |
|
|
|
* check if the converter supports the conversion based on the payload class and MIME |
|
|
|
* mainly to check if the converter supports the conversion based on the payload class |
|
|
|
* type. |
|
|
|
* and MIME type. |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Rossen Stoyanchev |
|
|
|
* @author Rossen Stoyanchev |
|
|
|
* @since 4.0 |
|
|
|
* @since 4.0 |
|
|
@ -68,7 +68,7 @@ public abstract class AbstractMessageConverter implements MessageConverter { |
|
|
|
* @param supportedMimeTypes the supported MIME types |
|
|
|
* @param supportedMimeTypes the supported MIME types |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected AbstractMessageConverter(Collection<MimeType> supportedMimeTypes) { |
|
|
|
protected AbstractMessageConverter(Collection<MimeType> supportedMimeTypes) { |
|
|
|
Assert.notNull(supportedMimeTypes, "SupportedMimeTypes must not be null"); |
|
|
|
Assert.notNull(supportedMimeTypes, "supportedMimeTypes must not be null"); |
|
|
|
this.supportedMimeTypes = new ArrayList<MimeType>(supportedMimeTypes); |
|
|
|
this.supportedMimeTypes = new ArrayList<MimeType>(supportedMimeTypes); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -83,13 +83,11 @@ public abstract class AbstractMessageConverter implements MessageConverter { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Configure the {@link ContentTypeResolver} to use to resolve the content |
|
|
|
* Configure the {@link ContentTypeResolver} to use to resolve the content |
|
|
|
* type of an input message. |
|
|
|
* type of an input message. |
|
|
|
* <p> |
|
|
|
* <p>Note that if no resolver is configured, then |
|
|
|
* Note that if no resolver is configured, then |
|
|
|
|
|
|
|
* {@link #setStrictContentTypeMatch(boolean) strictContentTypeMatch} should |
|
|
|
* {@link #setStrictContentTypeMatch(boolean) strictContentTypeMatch} should |
|
|
|
* be left as {@code false} (the default) or otherwise this converter will |
|
|
|
* be left as {@code false} (the default) or otherwise this converter will |
|
|
|
* ignore all messages. |
|
|
|
* ignore all messages. |
|
|
|
* <p> |
|
|
|
* <p>By default, a {@code DefaultContentTypeResolver} instance is used. |
|
|
|
* By default, a {@code DefaultContentTypeResolver} instance is used. |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void setContentTypeResolver(ContentTypeResolver resolver) { |
|
|
|
public void setContentTypeResolver(ContentTypeResolver resolver) { |
|
|
|
this.contentTypeResolver = resolver; |
|
|
|
this.contentTypeResolver = resolver; |
|
|
@ -106,20 +104,17 @@ public abstract class AbstractMessageConverter implements MessageConverter { |
|
|
|
* Whether this converter should convert messages for which no content type |
|
|
|
* Whether this converter should convert messages for which no content type |
|
|
|
* could be resolved through the configured |
|
|
|
* could be resolved through the configured |
|
|
|
* {@link org.springframework.messaging.converter.ContentTypeResolver}. |
|
|
|
* {@link org.springframework.messaging.converter.ContentTypeResolver}. |
|
|
|
* A converter can configured to be strict only when a |
|
|
|
* <p>A converter can configured to be strict only when a |
|
|
|
* {@link #setContentTypeResolver(ContentTypeResolver) contentTypeResolver} |
|
|
|
* {@link #setContentTypeResolver contentTypeResolver} is configured and the |
|
|
|
* is configured and the list of {@link #getSupportedMimeTypes() supportedMimeTypes} |
|
|
|
* list of {@link #getSupportedMimeTypes() supportedMimeTypes} is not be empty. |
|
|
|
* is not be empty. |
|
|
|
* <p>When this flag is set to {@code true}, {@link #supportsMimeType(MessageHeaders)} |
|
|
|
* |
|
|
|
* will return {@code false} if the {@link #setContentTypeResolver contentTypeResolver} |
|
|
|
* then requires the content type of a message to be resolved |
|
|
|
* is not defined or if no content-type header is present. |
|
|
|
* |
|
|
|
|
|
|
|
* When set to true, #supportsMimeType(MessageHeaders) will return false if the |
|
|
|
|
|
|
|
* contentTypeResolver is not defined or if no content-type header is present. |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void setStrictContentTypeMatch(boolean strictContentTypeMatch) { |
|
|
|
public void setStrictContentTypeMatch(boolean strictContentTypeMatch) { |
|
|
|
if (strictContentTypeMatch) { |
|
|
|
if (strictContentTypeMatch) { |
|
|
|
Assert.notEmpty(getSupportedMimeTypes(), "Strict match requires non-empty list of supported mime types."); |
|
|
|
Assert.notEmpty(getSupportedMimeTypes(), "Strict match requires non-empty list of supported mime types"); |
|
|
|
Assert.notNull(getContentTypeResolver(), "Strict match requires ContentTypeResolver."); |
|
|
|
Assert.notNull(getContentTypeResolver(), "Strict match requires ContentTypeResolver"); |
|
|
|
} |
|
|
|
} |
|
|
|
this.strictContentTypeMatch = strictContentTypeMatch; |
|
|
|
this.strictContentTypeMatch = strictContentTypeMatch; |
|
|
|
} |
|
|
|
} |
|
|
@ -166,14 +161,6 @@ public abstract class AbstractMessageConverter implements MessageConverter { |
|
|
|
return (!mimeTypes.isEmpty() ? mimeTypes.get(0) : null); |
|
|
|
return (!mimeTypes.isEmpty() ? mimeTypes.get(0) : null); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Whether the given class is supported by this converter. |
|
|
|
|
|
|
|
* @param clazz the class to test for support |
|
|
|
|
|
|
|
* @return {@code true} if supported; {@code false} otherwise |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
protected abstract boolean supports(Class<?> clazz); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public final Object fromMessage(Message<?> message, Class<?> targetClass) { |
|
|
|
public final Object fromMessage(Message<?> message, Class<?> targetClass) { |
|
|
|
if (!canConvertFrom(message, targetClass)) { |
|
|
|
if (!canConvertFrom(message, targetClass)) { |
|
|
@ -186,14 +173,8 @@ public abstract class AbstractMessageConverter implements MessageConverter { |
|
|
|
return (supports(targetClass) && supportsMimeType(message.getHeaders())); |
|
|
|
return (supports(targetClass) && supportsMimeType(message.getHeaders())); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Convert the message payload from serialized form to an Object. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public abstract Object convertFromInternal(Message<?> message, Class<?> targetClass); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public final Message<?> toMessage(Object payload, MessageHeaders headers) { |
|
|
|
public final Message<?> toMessage(Object payload, MessageHeaders headers) { |
|
|
|
|
|
|
|
|
|
|
|
if (!canConvertTo(payload, headers)) { |
|
|
|
if (!canConvertTo(payload, headers)) { |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
@ -218,15 +199,10 @@ public abstract class AbstractMessageConverter implements MessageConverter { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected boolean canConvertTo(Object payload, MessageHeaders headers) { |
|
|
|
protected boolean canConvertTo(Object payload, MessageHeaders headers) { |
|
|
|
Class<?> clazz = (payload != null) ? payload.getClass() : null; |
|
|
|
Class<?> clazz = (payload != null ? payload.getClass() : null); |
|
|
|
return (supports(clazz) && supportsMimeType(headers)); |
|
|
|
return (supports(clazz) && supportsMimeType(headers)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Convert the payload object to serialized form. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public abstract Object convertToInternal(Object payload, MessageHeaders headers); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected boolean supportsMimeType(MessageHeaders headers) { |
|
|
|
protected boolean supportsMimeType(MessageHeaders headers) { |
|
|
|
if (getSupportedMimeTypes().isEmpty()) { |
|
|
|
if (getSupportedMimeTypes().isEmpty()) { |
|
|
|
return true; |
|
|
|
return true; |
|
|
@ -249,7 +225,26 @@ public abstract class AbstractMessageConverter implements MessageConverter { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected MimeType getMimeType(MessageHeaders headers) { |
|
|
|
protected MimeType getMimeType(MessageHeaders headers) { |
|
|
|
return (this.contentTypeResolver != null) ? this.contentTypeResolver.resolve(headers) : null; |
|
|
|
return (this.contentTypeResolver != null ? this.contentTypeResolver.resolve(headers) : null); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Whether the given class is supported by this converter. |
|
|
|
|
|
|
|
* @param clazz the class to test for support |
|
|
|
|
|
|
|
* @return {@code true} if supported; {@code false} otherwise |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
protected abstract boolean supports(Class<?> clazz); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Convert the message payload from serialized form to an Object. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public abstract Object convertFromInternal(Message<?> message, Class<?> targetClass); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Convert the payload object to serialized form. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public abstract Object convertToInternal(Object payload, MessageHeaders headers); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|