001/*
002 * Copyright 2002-2019 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.core.codec;
018
019import java.util.Arrays;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.reactivestreams.Publisher;
026import reactor.core.publisher.Mono;
027
028import org.springframework.core.ResolvableType;
029import org.springframework.core.io.buffer.DataBuffer;
030import org.springframework.lang.Nullable;
031import org.springframework.util.MimeType;
032
033/**
034 * Abstract base class for {@link Decoder} implementations.
035 *
036 * @author Sebastien Deleuze
037 * @author Arjen Poutsma
038 * @since 5.0
039 * @param <T> the element type
040 */
041public abstract class AbstractDecoder<T> implements Decoder<T> {
042
043        private final List<MimeType> decodableMimeTypes;
044
045        protected Log logger = LogFactory.getLog(getClass());
046
047
048        protected AbstractDecoder(MimeType... supportedMimeTypes) {
049                this.decodableMimeTypes = Arrays.asList(supportedMimeTypes);
050        }
051
052
053        /**
054         * Set an alternative logger to use than the one based on the class name.
055         * @param logger the logger to use
056         * @since 5.1
057         */
058        public void setLogger(Log logger) {
059                this.logger = logger;
060        }
061
062        /**
063         * Return the currently configured Logger.
064         * @since 5.1
065         */
066        public Log getLogger() {
067                return logger;
068        }
069
070
071        @Override
072        public List<MimeType> getDecodableMimeTypes() {
073                return this.decodableMimeTypes;
074        }
075
076        @Override
077        public boolean canDecode(ResolvableType elementType, @Nullable MimeType mimeType) {
078                if (mimeType == null) {
079                        return true;
080                }
081                for (MimeType candidate : this.decodableMimeTypes) {
082                        if (candidate.isCompatibleWith(mimeType)) {
083                                return true;
084                        }
085                }
086                return false;
087        }
088
089        @Override
090        public Mono<T> decodeToMono(Publisher<DataBuffer> inputStream, ResolvableType elementType,
091                        @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
092
093                throw new UnsupportedOperationException();
094        }
095
096}