Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2002-2013 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.messaging.converter;
 
 import java.util.List;
 
 
Abstract base class for MessageConverter implementations including support for common properties and a partial implementation of the conversion methods mainly to check if the converter supports the conversion based on the payload class and MIME type.

Author(s):
Rossen Stoyanchev
Since:
4.0
 
 public abstract class AbstractMessageConverter implements MessageConverter {
 
 	protected final Log logger = LogFactory.getLog(getClass());
 
 
 	private final List<MimeTypesupportedMimeTypes;
 
 
 	private boolean strictContentTypeMatch = false;
 
 	private Class<?> serializedPayloadClass = byte[].class;


Construct an AbstractMessageConverter supporting a single MIME type.

Parameters:
supportedMimeType the supported MIME type
 
 	protected AbstractMessageConverter(MimeType supportedMimeType) {
 		Assert.notNull(supportedMimeType"supportedMimeType is required");
 		this. = Collections.<MimeType>singletonList(supportedMimeType);
 	}

Construct an AbstractMessageConverter supporting multiple MIME types.

Parameters:
supportedMimeTypes the supported MIME types
 
 	protected AbstractMessageConverter(Collection<MimeTypesupportedMimeTypes) {
 		Assert.notNull(supportedMimeTypes"SupportedMimeTypes must not be null");
 		this. = new ArrayList<MimeType>(supportedMimeTypes);
 	}


Return the supported MIME types.
 
 		return Collections.unmodifiableList(this.);
 	}

Configure the ContentTypeResolver to use to resolve the content type of an input message.

Note that if no resolver is configured, then strictContentTypeMatch should be left as false (the default) or otherwise this converter will ignore all messages.

By default, a DefaultContentTypeResolver instance is used.

 
 	public void setContentTypeResolver(ContentTypeResolver resolver) {
 		this. = resolver;
 	}

Return the configured ContentTypeResolver.
 
		return this.;
	}

Whether this converter should convert messages for which no content type could be resolved through the configured ContentTypeResolver. A converter can configured to be strict only when a contentTypeResolver is configured and the list of supportedMimeTypes is not be empty. then requires the content type of a message to be resolved 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) {
		if (strictContentTypeMatch) {
					"A strict converter requires a non-empty list of supported mime types");
					"A strict converter requires a ContentTypeResolver");
		}
		this. = strictContentTypeMatch;
	}

Whether content type resolution must produce a value that matches one of the supported MIME types.
	public boolean isStrictContentTypeMatch() {
	}

Configure the preferred serialization class to use (byte[] or String) when converting an Object payload to a org.springframework.messaging.Message.

The default value is byte[].

Parameters:
payloadClass either byte[] or String
	public void setSerializedPayloadClass(Class<?> payloadClass) {
		Assert.isTrue(byte[].class.equals(payloadClass) || String.class.equals(payloadClass),
				"Payload class must be byte[] or String: " + payloadClass);
		this. = payloadClass;
	}

Return the configured preferred serialization payload class.
	}


Returns the default content type for the payload. Called when toMessage(java.lang.Object,org.springframework.messaging.MessageHeaders) is invoked without message headers or without a content type header.

By default, this returns the first element of the supportedMimeTypes, if any. Can be overridden in sub-classes.

Parameters:
payload the payload being converted to message
Returns:
the content type, or null if not known
	protected MimeType getDefaultContentType(Object payload) {
		return (!mimeTypes.isEmpty() ? mimeTypes.get(0) : null);
	}

Whether the given class is supported by this converter.

Parameters:
clazz the class to test for support
Returns:
true if supported; false otherwise
	protected abstract boolean supports(Class<?> clazz);
	public final Object fromMessage(Message<?> messageClass<?> targetClass) {
		if (!canConvertFrom(messagetargetClass)) {
			return null;
		}
		return convertFromInternal(messagetargetClass);
	}
	protected boolean canConvertFrom(Message<?> messageClass<?> targetClass) {
		return (supports(targetClass) && supportsMimeType(message.getHeaders()));
	}

Convert the message payload from serialized form to an Object.
	public abstract Object convertFromInternal(Message<?> messageClass<?> targetClass);
	public final Message<?> toMessage(Object payloadMessageHeaders headers) {
		if (!canConvertTo(payloadheaders)) {
			return null;
		}
		payload = convertToInternal(payloadheaders);
		MessageBuilder<?> builder = MessageBuilder.withPayload(payload);
		if (headers != null) {
			builder.copyHeaders(headers);
		}
		MimeType mimeType = getDefaultContentType(payload);
		if (mimeType != null) {
		}
		return builder.build();
	}
	protected boolean canConvertTo(Object payloadMessageHeaders headers) {
		Class<?> clazz = (payload != null) ? payload.getClass() : null;
		return (supports(clazz) && supportsMimeType(headers));
	}

Convert the payload object to serialized form.
	public abstract Object convertToInternal(Object payloadMessageHeaders headers);
	protected boolean supportsMimeType(MessageHeaders headers) {
			return true;
		}
		MimeType mimeType = getMimeType(headers);
		if (mimeType == null) {
				return false;
			}
			else {
				return true;
			}
		}
		for (MimeType supported : getSupportedMimeTypes()) {
			if (supported.getType().equals(mimeType.getType()) &&
					supported.getSubtype().equals(mimeType.getSubtype())) {
				return true;
			}
		}
		return false;
	}
	protected MimeType getMimeType(MessageHeaders headers) {
		return (this. != null) ? this..resolve(headers) : null;
	}
New to GrepCode? Check out our FAQ X