001/*
002 * Copyright 2012-2017 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 org.springframework.boot.context.properties.ConfigurationProperties;
020import org.springframework.core.Ordered;
021import org.springframework.util.Assert;
022import org.springframework.web.servlet.view.AbstractTemplateViewResolver;
023
024/**
025 * Base class for {@link ConfigurationProperties} of a
026 * {@link AbstractTemplateViewResolver}.
027 *
028 * @author Andy Wilkinson
029 * @since 1.1.0
030 */
031public abstract class AbstractTemplateViewResolverProperties
032                extends AbstractViewResolverProperties {
033
034        /**
035         * Prefix that gets prepended to view names when building a URL.
036         */
037        private String prefix;
038
039        /**
040         * Suffix that gets appended to view names when building a URL.
041         */
042        private String suffix;
043
044        /**
045         * Name of the RequestContext attribute for all views.
046         */
047        private String requestContextAttribute;
048
049        /**
050         * Whether all request attributes should be added to the model prior to merging with
051         * the template.
052         */
053        private boolean exposeRequestAttributes = false;
054
055        /**
056         * Whether all HttpSession attributes should be added to the model prior to merging
057         * with the template.
058         */
059        private boolean exposeSessionAttributes = false;
060
061        /**
062         * Whether HttpServletRequest attributes are allowed to override (hide) controller
063         * generated model attributes of the same name.
064         */
065        private boolean allowRequestOverride = false;
066
067        /**
068         * Whether to expose a RequestContext for use by Spring's macro library, under the
069         * name "springMacroRequestContext".
070         */
071        private boolean exposeSpringMacroHelpers = true;
072
073        /**
074         * Whether HttpSession attributes are allowed to override (hide) controller generated
075         * model attributes of the same name.
076         */
077        private boolean allowSessionOverride = false;
078
079        protected AbstractTemplateViewResolverProperties(String defaultPrefix,
080                        String defaultSuffix) {
081                this.prefix = defaultPrefix;
082                this.suffix = defaultSuffix;
083        }
084
085        public String getPrefix() {
086                return this.prefix;
087        }
088
089        public void setPrefix(String prefix) {
090                this.prefix = prefix;
091        }
092
093        public String getSuffix() {
094                return this.suffix;
095        }
096
097        public void setSuffix(String suffix) {
098                this.suffix = suffix;
099        }
100
101        public String getRequestContextAttribute() {
102                return this.requestContextAttribute;
103        }
104
105        public void setRequestContextAttribute(String requestContextAttribute) {
106                this.requestContextAttribute = requestContextAttribute;
107        }
108
109        public boolean isExposeRequestAttributes() {
110                return this.exposeRequestAttributes;
111        }
112
113        public void setExposeRequestAttributes(boolean exposeRequestAttributes) {
114                this.exposeRequestAttributes = exposeRequestAttributes;
115        }
116
117        public boolean isExposeSessionAttributes() {
118                return this.exposeSessionAttributes;
119        }
120
121        public void setExposeSessionAttributes(boolean exposeSessionAttributes) {
122                this.exposeSessionAttributes = exposeSessionAttributes;
123        }
124
125        public boolean isAllowRequestOverride() {
126                return this.allowRequestOverride;
127        }
128
129        public void setAllowRequestOverride(boolean allowRequestOverride) {
130                this.allowRequestOverride = allowRequestOverride;
131        }
132
133        public boolean isAllowSessionOverride() {
134                return this.allowSessionOverride;
135        }
136
137        public void setAllowSessionOverride(boolean allowSessionOverride) {
138                this.allowSessionOverride = allowSessionOverride;
139        }
140
141        public boolean isExposeSpringMacroHelpers() {
142                return this.exposeSpringMacroHelpers;
143        }
144
145        public void setExposeSpringMacroHelpers(boolean exposeSpringMacroHelpers) {
146                this.exposeSpringMacroHelpers = exposeSpringMacroHelpers;
147        }
148
149        /**
150         * Apply the given properties to a {@link AbstractTemplateViewResolver}. Use Object in
151         * signature to avoid runtime dependency on MVC, which means that the template engine
152         * can be used in a non-web application.
153         * @param viewResolver the resolver to apply the properties to.
154         */
155        public void applyToMvcViewResolver(Object viewResolver) {
156                Assert.isInstanceOf(AbstractTemplateViewResolver.class, viewResolver,
157                                "ViewResolver is not an instance of AbstractTemplateViewResolver :"
158                                                + viewResolver);
159                AbstractTemplateViewResolver resolver = (AbstractTemplateViewResolver) viewResolver;
160                resolver.setPrefix(getPrefix());
161                resolver.setSuffix(getSuffix());
162                resolver.setCache(isCache());
163                if (getContentType() != null) {
164                        resolver.setContentType(getContentType().toString());
165                }
166                resolver.setViewNames(getViewNames());
167                resolver.setExposeRequestAttributes(isExposeRequestAttributes());
168                resolver.setAllowRequestOverride(isAllowRequestOverride());
169                resolver.setAllowSessionOverride(isAllowSessionOverride());
170                resolver.setExposeSessionAttributes(isExposeSessionAttributes());
171                resolver.setExposeSpringMacroHelpers(isExposeSpringMacroHelpers());
172                resolver.setRequestContextAttribute(getRequestContextAttribute());
173                // The resolver usually acts as a fallback resolver (e.g. like a
174                // InternalResourceViewResolver) so it needs to have low precedence
175                resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 5);
176        }
177
178}