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.freemarker;
018
019import java.io.IOException;
020import java.util.List;
021
022import freemarker.cache.ClassTemplateLoader;
023import freemarker.cache.TemplateLoader;
024import freemarker.template.Configuration;
025import freemarker.template.TemplateException;
026
027import org.springframework.beans.factory.InitializingBean;
028import org.springframework.context.ResourceLoaderAware;
029import org.springframework.lang.Nullable;
030import org.springframework.ui.freemarker.FreeMarkerConfigurationFactory;
031import org.springframework.util.Assert;
032
033/**
034 * Configures FreeMarker for web usage via the "configLocation" and/or
035 * "freemarkerSettings" and/or "templateLoaderPath" properties.
036 * The simplest way to use this class is to specify just a "templateLoaderPath"
037 * (e.g. "classpath:templates"); you do not need any further configuration then.
038 *
039 * <p>This bean must be included in the application context of any application
040 * using {@link FreeMarkerView}. It exists purely to configure FreeMarker.
041 * It is not meant to be referenced by application components but just internally
042 * by {@code FreeMarkerView}. Implements {@link FreeMarkerConfig} to be found by
043 * {@code FreeMarkerView} without depending on the bean name of the configurer.
044 *
045 * <p>Note that you can also refer to a pre-configured FreeMarker Configuration
046 * instance via the "configuration" property. This allows to share a FreeMarker
047 * Configuration for web and email usage for example.
048 *
049 * <p>This configurer registers a template loader for this package, allowing to
050 * reference the "spring.ftl" macro library contained in this package:
051 *
052 * <pre class="code">
053 * &lt;#import "/spring.ftl" as spring/&gt;
054 * &lt;@spring.bind "person.age"/&gt;
055 * age is ${spring.status.value}</pre>
056 *
057 * Note: Spring's FreeMarker support requires FreeMarker 2.3 or higher.
058 *
059 * @author Rossen Stoyanchev
060 * @since 5.0
061 */
062public class FreeMarkerConfigurer extends FreeMarkerConfigurationFactory
063                implements FreeMarkerConfig, InitializingBean, ResourceLoaderAware {
064
065        @Nullable
066        private Configuration configuration;
067
068
069        public FreeMarkerConfigurer() {
070                setDefaultEncoding("UTF-8");
071        }
072
073
074        /**
075         * Set a pre-configured Configuration to use for the FreeMarker web config,
076         * e.g. a shared one for web and email usage. If this is not set,
077         * FreeMarkerConfigurationFactory's properties (inherited by this class)
078         * have to be specified.
079         */
080        public void setConfiguration(Configuration configuration) {
081                this.configuration = configuration;
082        }
083
084
085        /**
086         * Initialize FreeMarkerConfigurationFactory's Configuration
087         * if not overridden by a pre-configured FreeMarker Configuration.
088         * <p>Sets up a ClassTemplateLoader to use for loading Spring macros.
089         * @see #createConfiguration
090         * @see #setConfiguration
091         */
092        @Override
093        public void afterPropertiesSet() throws IOException, TemplateException {
094                if (this.configuration == null) {
095                        this.configuration = createConfiguration();
096                }
097        }
098
099        /**
100         * This implementation registers an additional ClassTemplateLoader
101         * for the Spring-provided macros, added to the end of the list.
102         */
103        @Override
104        protected void postProcessTemplateLoaders(List<TemplateLoader> templateLoaders) {
105                templateLoaders.add(new ClassTemplateLoader(FreeMarkerConfigurer.class, ""));
106        }
107
108
109        /**
110         * Return the Configuration object wrapped by this bean.
111         */
112        @Override
113        public Configuration getConfiguration() {
114                Assert.state(this.configuration != null, "No Configuration available");
115                return this.configuration;
116        }
117
118}