001/*
002 * Copyright 2002-2015 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.web.accept;
018
019import java.util.Collections;
020import java.util.List;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024
025import org.springframework.http.MediaType;
026import org.springframework.web.context.request.NativeWebRequest;
027
028/**
029 * A {@code ContentNegotiationStrategy} that returns a fixed content type.
030 *
031 * @author Rossen Stoyanchev
032 * @since 3.2
033 */
034public class FixedContentNegotiationStrategy implements ContentNegotiationStrategy {
035
036        private static final Log logger = LogFactory.getLog(FixedContentNegotiationStrategy.class);
037
038        private final List<MediaType> contentType;
039
040
041        /**
042         * Create an instance with the given content type.
043         */
044        public FixedContentNegotiationStrategy(MediaType contentType) {
045                this.contentType = Collections.singletonList(contentType);
046        }
047
048
049        @Override
050        public List<MediaType> resolveMediaTypes(NativeWebRequest request) {
051                if (logger.isDebugEnabled()) {
052                        logger.debug("Requested media types: " + this.contentType);
053                }
054                return this.contentType;
055        }
056
057}