001/*
002 * Copyright 2002-2014 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.messaging.MessageHeaders;
020import org.springframework.util.MimeType;
021
022/**
023 * A default {@link ContentTypeResolver} that checks the
024 * {@link MessageHeaders#CONTENT_TYPE} header or falls back to a default value.
025 *
026 * <p>The header value is expected to be a {@link org.springframework.util.MimeType}
027 * or a {@code String} that can be parsed into a {@code MimeType}.
028 *
029 * @author Rossen Stoyanchev
030 * @since 4.0
031 */
032public class DefaultContentTypeResolver implements ContentTypeResolver {
033
034        private MimeType defaultMimeType;
035
036
037        /**
038         * Set the default MIME type to use when there is no
039         * {@link MessageHeaders#CONTENT_TYPE} header present.
040         * <p>This property does not have a default value.
041         */
042        public void setDefaultMimeType(MimeType defaultMimeType) {
043                this.defaultMimeType = defaultMimeType;
044        }
045
046        /**
047         * Return the default MIME type to use if no
048         * {@link MessageHeaders#CONTENT_TYPE} header is present.
049         */
050        public MimeType getDefaultMimeType() {
051                return this.defaultMimeType;
052        }
053
054
055        @Override
056        public MimeType resolve(MessageHeaders headers) {
057                if (headers == null || headers.get(MessageHeaders.CONTENT_TYPE) == null) {
058                        return this.defaultMimeType;
059                }
060                Object value = headers.get(MessageHeaders.CONTENT_TYPE);
061                if (value instanceof MimeType) {
062                        return (MimeType) value;
063                }
064                else if (value instanceof String) {
065                        return MimeType.valueOf((String) value);
066                }
067                else {
068                        throw new IllegalArgumentException(
069                                        "Unknown type for contentType header value: " + value.getClass());
070                }
071        }
072
073        @Override
074        public String toString() {
075                return "DefaultContentTypeResolver[" + "defaultMimeType=" + this.defaultMimeType + "]";
076        }
077
078}