FreemarkerTemplatingServiceImpl.java

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

  17. import java.io.StringReader;
  18. import java.io.StringWriter;
  19. import java.util.Map;

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

  23. import freemarker.ext.beans.BeansWrapper;
  24. import freemarker.template.Configuration;
  25. import freemarker.template.Template;
  26. import freemarker.template.TemplateExceptionHandler;

  27. /**
  28.  * Templating with Freemarker.
  29.  *
  30.  * @author Matija Obreza
  31.  */
  32. @Service
  33. public class FreemarkerTemplatingServiceImpl implements FreemarkerTemplating, InitializingBean {

  34.     private Configuration freeMarkerCfg;

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

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

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

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

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

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

  53.         this.freeMarkerCfg = cfg;
  54.     }

  55.     /**
  56.      * Process template.
  57.      *
  58.      * @param template the template
  59.      * @param root the root
  60.      * @return the string
  61.      * @throws FreemarkerException the template exception
  62.      */
  63.     @Override
  64.     public String processTemplate(final String template, final Map<?, ?> root) throws FreemarkerException {
  65.         try {
  66.             final Template temp = new Template("template-" + template.hashCode(), new StringReader(template), this.freeMarkerCfg);
  67.             final StringWriter out = new StringWriter(2048);
  68.             temp.process(root, out);
  69.             return out.toString();
  70.         } catch (final Throwable e) {
  71.             throw new FreemarkerException(e);
  72.         }
  73.     }

  74.     /**
  75.      * Process template resource.
  76.      *
  77.      * @param template the template
  78.      * @param root the root
  79.      * @return the string
  80.      * @throws FreemarkerException obviously
  81.      */
  82.     @Override
  83.     public String processTemplateResource(final String template, final Map<?, ?> root) throws FreemarkerException {
  84.         try {
  85.             final Template temp = freeMarkerCfg.getTemplate(template);
  86.             final StringWriter out = new StringWriter(2048);
  87.             temp.process(root, out);
  88.             return out.toString();
  89.         } catch (final Throwable e) {
  90.             throw new FreemarkerException(e);
  91.         }
  92.     }
  93. }