001/*
002 * Copyright 2012-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 *      http://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.boot.autoconfigure.template;
018
019import java.nio.charset.Charset;
020import java.nio.charset.StandardCharsets;
021import java.util.LinkedHashMap;
022import java.util.Map;
023
024import org.springframework.boot.context.properties.ConfigurationProperties;
025import org.springframework.util.MimeType;
026import org.springframework.web.servlet.ViewResolver;
027
028/**
029 * Base class for {@link ConfigurationProperties} of a {@link ViewResolver}.
030 *
031 * @author Andy Wilkinson
032 * @author Stephane Nicoll
033 * @since 1.2.0
034 * @see AbstractTemplateViewResolverProperties
035 */
036public abstract class AbstractViewResolverProperties {
037
038        private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html");
039
040        private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
041
042        /**
043         * Whether to enable MVC view resolution for this technology.
044         */
045        private boolean enabled = true;
046
047        /**
048         * Whether to enable template caching.
049         */
050        private boolean cache;
051
052        /**
053         * Content-Type value.
054         */
055        private MimeType contentType = DEFAULT_CONTENT_TYPE;
056
057        /**
058         * Template encoding.
059         */
060        private Charset charset = DEFAULT_CHARSET;
061
062        /**
063         * White list of view names that can be resolved.
064         */
065        private String[] viewNames;
066
067        /**
068         * Whether to check that the templates location exists.
069         */
070        private boolean checkTemplateLocation = true;
071
072        public void setEnabled(boolean enabled) {
073                this.enabled = enabled;
074        }
075
076        public boolean isEnabled() {
077                return this.enabled;
078        }
079
080        public void setCheckTemplateLocation(boolean checkTemplateLocation) {
081                this.checkTemplateLocation = checkTemplateLocation;
082        }
083
084        public boolean isCheckTemplateLocation() {
085                return this.checkTemplateLocation;
086        }
087
088        public String[] getViewNames() {
089                return this.viewNames;
090        }
091
092        public void setViewNames(String[] viewNames) {
093                this.viewNames = viewNames;
094        }
095
096        public boolean isCache() {
097                return this.cache;
098        }
099
100        public void setCache(boolean cache) {
101                this.cache = cache;
102        }
103
104        public MimeType getContentType() {
105                if (this.contentType.getCharset() == null) {
106                        Map<String, String> parameters = new LinkedHashMap<>();
107                        parameters.put("charset", this.charset.name());
108                        parameters.putAll(this.contentType.getParameters());
109                        return new MimeType(this.contentType, parameters);
110                }
111                return this.contentType;
112        }
113
114        public void setContentType(MimeType contentType) {
115                this.contentType = contentType;
116        }
117
118        public Charset getCharset() {
119                return this.charset;
120        }
121
122        public String getCharsetName() {
123                return (this.charset != null) ? this.charset.name() : null;
124        }
125
126        public void setCharset(Charset charset) {
127                this.charset = charset;
128        }
129
130}