FreemarkerTemplatingServiceImpl.java

/*
 * Copyright 2018 Global Crop Diversity Trust
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.genesys.server.service.impl;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.Map;

import org.genesys.server.service.FreemarkerTemplating;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import freemarker.ext.beans.BeansWrapper;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;

/**
 * Templating with Freemarker.
 *
 * @author Matija Obreza
 */
@Service
public class FreemarkerTemplatingServiceImpl implements FreemarkerTemplating, InitializingBean {

	private Configuration freeMarkerCfg;

	@Override
	public void afterPropertiesSet() throws Exception {
		// Create your Configuration instance, and specify if up to what FreeMarker
		// version (here 2.3.27) do you want to apply the fixes that are not 100%
		// backward-compatible. See the Configuration JavaDoc for details.
		final Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);

		cfg.setClassForTemplateLoading(this.getClass(), "/freemarker/");

		// Set the preferred charset template files are stored in. UTF-8 is
		// a good choice in most applications:
		cfg.setDefaultEncoding("UTF-8");

		// Sets how errors will appear.
		// During web page *development* TemplateExceptionHandler.HTML_DEBUG_HANDLER is
		// better.
		cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

		// Don't log exceptions inside FreeMarker that it will thrown at you anyway:
		cfg.setLogTemplateExceptions(false);

		BeansWrapper objectWrapper = (BeansWrapper) cfg.getObjectWrapper();
		cfg.setSharedVariable("statics", objectWrapper.getStaticModels());

		this.freeMarkerCfg = cfg;
	}

	/**
	 * Process template.
	 *
	 * @param template the template
	 * @param root the root
	 * @return the string
	 * @throws FreemarkerException the template exception
	 */
	@Override
	public String processTemplate(final String template, final Map<?, ?> root) throws FreemarkerException {
		try {
			final Template temp = new Template("template-" + template.hashCode(), new StringReader(template), this.freeMarkerCfg);
			final StringWriter out = new StringWriter(2048);
			temp.process(root, out);
			return out.toString();
		} catch (final Throwable e) {
			throw new FreemarkerException(e);
		}
	}

	/**
	 * Process template resource.
	 *
	 * @param template the template
	 * @param root the root
	 * @return the string
	 * @throws FreemarkerException obviously
	 */
	@Override
	public String processTemplateResource(final String template, final Map<?, ?> root) throws FreemarkerException {
		try {
			final Template temp = freeMarkerCfg.getTemplate(template);
			final StringWriter out = new StringWriter(2048);
			temp.process(root, out);
			return out.toString();
		} catch (final Throwable e) {
			throw new FreemarkerException(e);
		}
	}
}