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.web.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.context.request.NativeWebRequest; 025 026/** 027 * A {@code ContentNegotiationStrategy} that returns a fixed content type. 028 * 029 * @author Rossen Stoyanchev 030 * @since 3.2 031 */ 032public class FixedContentNegotiationStrategy implements ContentNegotiationStrategy { 033 034 private final List<MediaType> contentTypes; 035 036 037 /** 038 * Constructor with a single default {@code MediaType}. 039 */ 040 public FixedContentNegotiationStrategy(MediaType contentType) { 041 this(Collections.singletonList(contentType)); 042 } 043 044 /** 045 * Constructor with an ordered List of default {@code MediaType}'s to return 046 * for use in applications that support a variety of content types. 047 * <p>Consider appending {@link MediaType#ALL} at the end if destinations 048 * are present which do not support any of the other default media types. 049 * @since 5.0 050 */ 051 public FixedContentNegotiationStrategy(List<MediaType> contentTypes) { 052 Assert.notNull(contentTypes, "'contentTypes' must not be null"); 053 this.contentTypes = Collections.unmodifiableList(contentTypes); 054 } 055 056 057 /** 058 * Return the configured list of media types. 059 */ 060 public List<MediaType> getContentTypes() { 061 return this.contentTypes; 062 } 063 064 065 @Override 066 public List<MediaType> resolveMediaTypes(NativeWebRequest request) { 067 return this.contentTypes; 068 } 069 070}