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