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.result.view;
018
019import java.nio.charset.Charset;
020import java.nio.charset.StandardCharsets;
021import java.util.ArrayList;
022import java.util.List;
023
024import org.springframework.core.Ordered;
025import org.springframework.http.MediaType;
026import org.springframework.util.Assert;
027
028/**
029 * Base class for {@code ViewResolver} implementations with shared properties.
030 *
031 * @author Rossen Stoyanchev
032 * @author Juergen Hoeller
033 * @since 5.0
034 */
035public abstract class ViewResolverSupport implements Ordered {
036
037        /**
038         * The default {@link MediaType content-type} for views.
039         */
040        public static final MediaType DEFAULT_CONTENT_TYPE = MediaType.parseMediaType("text/html;charset=UTF-8");
041
042
043        private List<MediaType> mediaTypes = new ArrayList<>(4);
044
045        private Charset defaultCharset = StandardCharsets.UTF_8;
046
047        private int order = Ordered.LOWEST_PRECEDENCE;
048
049
050        public ViewResolverSupport() {
051                this.mediaTypes.add(DEFAULT_CONTENT_TYPE);
052        }
053
054
055        /**
056         * Set the supported media types for this view.
057         * Default is "text/html;charset=UTF-8".
058         */
059        public void setSupportedMediaTypes(List<MediaType> supportedMediaTypes) {
060                Assert.notEmpty(supportedMediaTypes, "MediaType List must not be empty");
061                this.mediaTypes.clear();
062                this.mediaTypes.addAll(supportedMediaTypes);
063        }
064
065        /**
066         * Return the configured media types supported by this view.
067         */
068        public List<MediaType> getSupportedMediaTypes() {
069                return this.mediaTypes;
070        }
071
072        /**
073         * Set the default charset for this view, used when the
074         * {@linkplain #setSupportedMediaTypes(List) content type} does not contain one.
075         * Default is {@linkplain StandardCharsets#UTF_8 UTF 8}.
076         */
077        public void setDefaultCharset(Charset defaultCharset) {
078                Assert.notNull(defaultCharset, "Default Charset must not be null");
079                this.defaultCharset = defaultCharset;
080        }
081
082        /**
083         * Return the default charset, used when the
084         * {@linkplain #setSupportedMediaTypes(List) content type} does not contain one.
085         */
086        public Charset getDefaultCharset() {
087                return this.defaultCharset;
088        }
089
090        /**
091         * Specify the order value for this ViewResolver bean.
092         * <p>The default value is {@code Ordered.LOWEST_PRECEDENCE}, meaning non-ordered.
093         * @see org.springframework.core.Ordered#getOrder()
094         */
095        public void setOrder(int order) {
096                this.order = order;
097        }
098
099        @Override
100        public int getOrder() {
101                return this.order;
102        }
103
104}