001/*
002 * Copyright 2002-2018 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.reactive.accept;
018
019import java.util.Collections;
020import java.util.List;
021
022import org.springframework.http.MediaType;
023import org.springframework.util.Assert;
024import org.springframework.web.server.ServerWebExchange;
025
026/**
027 * Resolver that always resolves to a fixed list of media types. This can be
028 * used as the "last in line" strategy providing a fallback for when the client
029 * has not requested any media types.
030 *
031 * @author Rossen Stoyanchev
032 * @since 5.0
033 */
034public class FixedContentTypeResolver implements RequestedContentTypeResolver {
035
036        private final List<MediaType> contentTypes;
037
038
039        /**
040         * Constructor with a single default {@code MediaType}.
041         */
042        public FixedContentTypeResolver(MediaType mediaType) {
043                this(Collections.singletonList(mediaType));
044        }
045
046        /**
047         * Constructor with an ordered List of default {@code MediaType}'s to return
048         * for use in applications that support a variety of content types.
049         * <p>Consider appending {@link MediaType#ALL} at the end if destinations
050         * are present which do not support any of the other default media types.
051         */
052        public FixedContentTypeResolver(List<MediaType> contentTypes) {
053                Assert.notNull(contentTypes, "'contentTypes' must not be null");
054                this.contentTypes = Collections.unmodifiableList(contentTypes);
055        }
056
057
058        /**
059         * Return the configured list of media types.
060         */
061        public List<MediaType> getContentTypes() {
062                return this.contentTypes;
063        }
064
065
066        @Override
067        public List<MediaType> resolveMediaTypes(ServerWebExchange exchange) {
068                return this.contentTypes;
069        }
070
071}