001/*
002 * Copyright 2002-2016 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.messaging.converter;
018
019import org.springframework.core.convert.ConversionException;
020import org.springframework.core.convert.ConversionService;
021import org.springframework.core.convert.support.DefaultConversionService;
022import org.springframework.messaging.Message;
023import org.springframework.util.Assert;
024import org.springframework.util.ClassUtils;
025
026/**
027 * An extension of the {@link SimpleMessageConverter} that uses a
028 * {@link ConversionService} to convert the payload of the message
029 * to the requested type.
030 *
031 * <p>Return {@code null} if the conversion service cannot convert
032 * from the payload type to the requested type.
033 *
034 * @author Stephane Nicoll
035 * @since 4.1
036 * @see ConversionService
037 */
038public class GenericMessageConverter extends SimpleMessageConverter {
039
040        private final ConversionService conversionService;
041
042
043        /**
044         * Create a new instance with a default {@link ConversionService}.
045         */
046        public GenericMessageConverter() {
047                this.conversionService = DefaultConversionService.getSharedInstance();
048        }
049
050        /**
051         * Create a new instance with the given {@link ConversionService}.
052         */
053        public GenericMessageConverter(ConversionService conversionService) {
054                Assert.notNull(conversionService, "ConversionService must not be null");
055                this.conversionService = conversionService;
056        }
057
058
059        @Override
060        public Object fromMessage(Message<?> message, Class<?> targetClass) {
061                Object payload = message.getPayload();
062                if (targetClass == null) {
063                        return payload;
064                }
065                if (payload != null && this.conversionService.canConvert(payload.getClass(), targetClass)) {
066                        try {
067                                return this.conversionService.convert(payload, targetClass);
068                        }
069                        catch (ConversionException ex) {
070                                throw new MessageConversionException(message, "Failed to convert message payload '" +
071                                                payload + "' to '" + targetClass.getName() + "'", ex);
072                        }
073                }
074                return (ClassUtils.isAssignableValue(targetClass, payload) ? payload : null);
075        }
076
077}