Example usage for org.springframework.ui.freemarker FreeMarkerTemplateUtils processTemplateIntoString

List of usage examples for org.springframework.ui.freemarker FreeMarkerTemplateUtils processTemplateIntoString

Introduction

In this page you can find the example usage for org.springframework.ui.freemarker FreeMarkerTemplateUtils processTemplateIntoString.

Prototype

public static String processTemplateIntoString(Template template, Object model)
        throws IOException, TemplateException 

Source Link

Document

Process the specified FreeMarker template with the given model and write the result to the given Writer.

Usage

From source file:com.onyxscheduler.util.FreemarkerUtils.java

public static String solveTemplate(Configuration freemarkerConfig, String templateName,
        ImmutableMap<String, Serializable> model) {
    try {/*from   w  w  w.  j  av  a2  s .  com*/
        Template template = freemarkerConfig.getTemplate(templateName);
        return FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
    } catch (IOException | TemplateException e) {
        throw Throwables.propagate(e);
    }
}

From source file:org.brushingbits.jnap.email.FreemarkerEmail.java

/**
 * Freemarker implementation of {@link TemplateEmail#processTemplate()}
 *//* ww  w.  java 2  s. c o  m*/
@Override
protected void processTemplate() {
    try {
        // Email content
        StringBuilder htmlMsg = new StringBuilder();

        // Header
        if (StringUtils.isNotBlank(getHeaderTemplateName())) {
            Template header = getTemplate(getHeaderTemplateName());
            htmlMsg.append(FreeMarkerTemplateUtils.processTemplateIntoString(header, values));
        }

        // Body
        Template body = getTemplate(getBodyTemplateName());
        String bodyContent = FreeMarkerTemplateUtils.processTemplateIntoString(body, values);
        htmlMsg.append(bodyContent);
        if (isMixedContent()) {
            setText(extractTextFromHtml(bodyContent));
        }

        // Footer
        if (StringUtils.isNotBlank(getFooterTemplateName())) {
            Template footer = getTemplate(getFooterTemplateName());
            htmlMsg.append(FreeMarkerTemplateUtils.processTemplateIntoString(footer, values));
        }

        // Setting the content
        if (isMixedContent()) {
            setHtmlText(htmlMsg.toString());
        } else {
            setText(htmlMsg.toString());
        }
    } catch (IOException e) {
        throw new MailPreparationException("Error trying to open template file.", e);
    } catch (TemplateException e) {
        throw new MailPreparationException("Error loading/processing template file.", e);
    }
}

From source file:it.ozimov.springboot.templating.mail.service.FreemarkerTemplateService.java

@Override
public @NonNull String mergeTemplateIntoString(final @NonNull String templateReference,
        final @NonNull Map<String, Object> model) throws IOException, TemplateException {
    checkArgument(!isNullOrEmpty(templateReference.trim()), "The given template is null, empty or blank");
    checkArgument(Objects.equals(getFileExtension(templateReference), expectedTemplateExtension()),
            "Expected a Freemarker template file with extension ftl, while %s was given",
            getFileExtension(templateReference));

    try {// ww w  .j  ava2 s  .co m
        return FreeMarkerTemplateUtils.processTemplateIntoString(
                freemarkerConfiguration.getTemplate(templateReference, Charset.forName("UTF-8").name()), model);
    } catch (freemarker.template.TemplateException e) {
        throw new TemplateException(e);
    }
}

From source file:com.bsb.cms.commons.template.freemarker.SpringFreemarkerGenerator.java

@Override
public String createString(String templatePath, Map<String, Object> dataMap) throws TemplateRuntimeException {
    String htmlText;//from   w w w. j ava  2 s  .c o  m
    try {
        htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate(templatePath),
                dataMap);
    } catch (IOException e) {
        log.error(e.getMessage());
        throw new TemplateRuntimeException(e.getMessage());
    } catch (TemplateException e) {
        log.error(e.getMessage());
        throw new TemplateRuntimeException(e.getMessage());
    }
    return htmlText;
}

From source file:it.geosolutions.opensdi2.email.OpenSDIMailer.java

/**
 * Useful class to get freemarker template from
 * // w w  w  . jav a  2 s. c o  m
 * @param template
 * @param model
 * @return
 * @throws IOException
 * @throws TemplateException
 */
public String applyTemplate(String template, @SuppressWarnings("rawtypes") Map model)
        throws IOException, TemplateException {
    String result = null;
    try {
        result = FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate(template), model);
    } catch (IOException e) {
        LOGGER.error("unable to read template file" + template, e);
        throw e;

    } catch (TemplateException e) {
        LOGGER.error("unable to use freMarkerTemplate:" + template);
        throw e;
    }
    return result;
}

From source file:com.jrzmq.core.email.SimpleMailService.java

/**
 * ?/*from ww  w  . j a v  a  2s  .c  om*/
 * @param templateName        ???
 * @param map                 ?
 * @return
 * @throws MessagingException
 */
private String generateContent(String templateName, Map<String, Object> map) throws MessagingException {

    try {
        return FreeMarkerTemplateUtils.processTemplateIntoString(
                freemarkerConfiguration.getTemplate(templateName, DEFAULT_ENCODING), map);
    } catch (IOException e) {
        logger.error("?, FreeMarker??", e);
        throw new MessagingException("FreeMarker??", e);
    } catch (TemplateException e) {
        logger.error("?, FreeMarker?", e);
        throw new MessagingException("FreeMarker?", e);
    }
}

From source file:com.teradata.benchto.driver.loader.SqlStatementGenerator.java

private String generateQuerySqlStatement(String sqlTemplate, Map<String, ?> attributes) {
    try {//from ww w  .  jav  a 2s.co  m
        // template name must be unique to ensure correct templates caching
        String templateName = UUID.randomUUID().toString();
        Template queryTemplate = new Template(templateName, new StringReader(sqlTemplate),
                freemarkerConfiguration);

        return FreeMarkerTemplateUtils.processTemplateIntoString(queryTemplate, attributes);
    } catch (IOException | TemplateException e) {
        throw new BenchmarkExecutionException(e);
    }
}

From source file:io.gravitee.management.service.impl.EmailServiceImpl.java

@Async
public void sendEmailNotification(final EmailNotification emailNotification) {
    try {/*from   w  w  w.jav  a 2s.c om*/
        final Template template = freemarkerConfiguration.getTemplate(emailNotification.getContent());
        final String htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(template,
                emailNotification.getParams());

        final String from = emailNotification.getFrom();
        if (from != null && !from.isEmpty()) {
            mailMessage.setFrom(from);
        }

        mailMessage.setTo(emailNotification.getTo());
        mailMessage.setSubject(emailNotification.getSubject());

        final String html = addResourcesInMessage(mailMessage, htmlText);

        LOGGER.debug("Sending an email to: {}\nSubject: {}\nMessage: {}", emailNotification.getTo(),
                emailNotification.getSubject(), html);

        mailSender.send(mailMessage.getMimeMessage());
    } catch (final Exception ioe) {
        LOGGER.error("Error while sending email notification", ioe);
    }
}

From source file:com.it.j2ee.modules.email.MimeMailService.java

/**
 * Freemarker?html?./*from  w w w  . ja  v  a  2s.  c o  m*/
 */
private String generateContent(String userName) throws MessagingException {

    try {
        Map context = Collections.singletonMap("userName", userName);
        return FreeMarkerTemplateUtils.processTemplateIntoString(template, context);
    } catch (IOException e) {
        logger.error("?, FreeMarker??", e);
        throw new MessagingException("FreeMarker??", e);
    } catch (TemplateException e) {
        logger.error("?, FreeMarker?", e);
        throw new MessagingException("FreeMarker?", e);
    }
}

From source file:com.bsb.cms.commons.template.freemarker.SpringFreemarkerGenerator.java

@Override
public void createFile(String ftlTemplateFile, Map<String, Object> dataMap, String filePath)
        throws TemplateRuntimeException {
    Writer out = null;/*from  w w  w. ja v  a2 s  .c  om*/
    try {
        String htmlText = FreeMarkerTemplateUtils
                .processTemplateIntoString(configuration.getTemplate(ftlTemplateFile), dataMap);

        filePath = filePath.replace("\\", "/");
        filePath = filePath.replace("//", "/");
        String savePath = StringUtils.substringBeforeLast(filePath, "/");
        File file = new File(savePath);
        if (!file.exists()) {
            file.mkdirs();
        }

        out = new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8");
        out.write(htmlText);
    } catch (IOException e) {
        log.error(e.getMessage());
        throw new TemplateRuntimeException(e.getMessage());
    } catch (TemplateException e) {
        log.error(e.getMessage());
        throw new TemplateRuntimeException(e.getMessage());
    } finally {
        if (out != null) {
            try {
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}