001/*
002 * Copyright 2002-2017 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.lang.Nullable;
023import org.springframework.messaging.Message;
024import org.springframework.util.Assert;
025import org.springframework.util.ClassUtils;
026
027/**
028 * An extension of the {@link SimpleMessageConverter} that uses a
029 * {@link ConversionService} to convert the payload of the message
030 * to the requested type.
031 *
032 * <p>Return {@code null} if the conversion service cannot convert
033 * from the payload type to the requested type.
034 *
035 * @author Stephane Nicoll
036 * @since 4.1
037 * @see ConversionService
038 */
039public class GenericMessageConverter extends SimpleMessageConverter {
040
041        private final ConversionService conversionService;
042
043
044        /**
045         * Create a new instance with a default {@link ConversionService}.
046         */
047        public GenericMessageConverter() {
048                this.conversionService = DefaultConversionService.getSharedInstance();
049        }
050
051        /**
052         * Create a new instance with the given {@link ConversionService}.
053         */
054        public GenericMessageConverter(ConversionService conversionService) {
055                Assert.notNull(conversionService, "ConversionService must not be null");
056                this.conversionService = conversionService;
057        }
058
059
060        @Override
061        @Nullable
062        public Object fromMessage(Message<?> message, Class<?> targetClass) {
063                Object payload = message.getPayload();
064                if (this.conversionService.canConvert(payload.getClass(), targetClass)) {
065                        try {
066                                return this.conversionService.convert(payload, targetClass);
067                        }
068                        catch (ConversionException ex) {
069                                throw new MessageConversionException(message, "Failed to convert message payload '" +
070                                                payload + "' to '" + targetClass.getName() + "'", ex);
071                        }
072                }
073                return (ClassUtils.isAssignableValue(targetClass, payload) ? payload : null);
074        }
075
076}