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:net.groupbuy.service.impl.MailServiceImpl.java

public void send(String smtpFromMail, String smtpHost, Integer smtpPort, String smtpUsername,
        String smtpPassword, String toMail, String subject, String templatePath, Map<String, Object> model,
        boolean async) {
    Assert.hasText(smtpFromMail);//w  ww. j  av  a2 s  .  co m
    Assert.hasText(smtpHost);
    Assert.notNull(smtpPort);
    Assert.hasText(smtpUsername);
    Assert.hasText(smtpPassword);
    Assert.hasText(toMail);
    Assert.hasText(subject);
    Assert.hasText(templatePath);
    try {
        Setting setting = SettingUtils.get();
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        Template template = configuration.getTemplate(templatePath);
        String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
        javaMailSender.setHost(smtpHost);
        javaMailSender.setPort(smtpPort);
        javaMailSender.setUsername(smtpUsername);
        javaMailSender.setPassword(smtpPassword);
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, false, "utf-8");
        mimeMessageHelper.setFrom(MimeUtility.encodeWord(setting.getSiteName()) + " <" + smtpFromMail + ">");
        mimeMessageHelper.setSubject(subject);
        mimeMessageHelper.setTo(toMail);
        mimeMessageHelper.setText(text, true);
        if (async) {
            addSendTask(mimeMessage);
        } else {
            javaMailSender.send(mimeMessage);
        }
    } catch (TemplateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (MessagingException e) {
        e.printStackTrace();
    }
}

From source file:edu.sampleu.common.FreemarkerUtil.java

/**
 *
 * @param file//  w w  w.java 2  s . co  m
 * @param template
 * @param props
 * @return
 * @throws IOException
 * @throws freemarker.template.TemplateException
 */
protected static File writeTemplateToFile(File file, Template template, Properties props)
        throws IOException, TemplateException {
    String output = FreeMarkerTemplateUtils.processTemplateIntoString(template, props);
    FileUtils.writeStringToFile(file, output);

    return file;
}

From source file:net.shopxx.service.impl.SmsServiceImpl.java

public void send(String[] mobiles, String templatePath, Map<String, Object> model, Date sendTime,
        boolean async) {
    Assert.notEmpty(mobiles);//w w w  .j av a2s .c o  m
    Assert.hasText(templatePath);

    try {
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        Template template = configuration.getTemplate(templatePath);
        String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
        send(mobiles, content, sendTime, async);
    } catch (TemplateException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:uk.org.rbc1b.roms.controller.volunteer.update.VolunteerUpdateEmailGenerator.java

/**
 * Prepares an email to send to the volunteer with the link to the form where they can update
 * their information./*w ww .  j a v a2s  . c o m*/
 *
 * @param volunteer the volunteer to send
 * @return email
 * @throws TemplateException on failure process the template
 * @throws IOException on failure to look up the template
 */
public Email generateVolunteerUpdateRequestEmail(Volunteer volunteer) throws IOException, TemplateException {
    String uri = generateSecureUri(volunteer);

    Configuration conf = emailFreemarkerConfigurer.getConfiguration();
    Map<String, String> model = new HashMap<String, String>();
    model.put("forename", volunteer.getPerson().getForename());
    model.put("httpsurl", uri);
    String text = FreeMarkerTemplateUtils.processTemplateIntoString(conf.getTemplate(UPDATE_REQUEST_TEMPLATE),
            model);
    Email email = new Email();
    email.setRecipient(volunteer.getPerson().getEmail());
    email.setSubject(SUBJECT);
    email.setText(text);

    return email;
}

From source file:uk.org.rbc1b.roms.controller.volunteer.ldc.SubmitLDCFormVolunteerEmailGenerator.java

/**
 * Generate email for a volunteer with the correct message.
 *
 * @param volunteer the volunteer/* w  ww.ja v a 2  s .  c om*/
 * @return Email object
 * @throws IOException if we can't find the email template
 * @throws TemplateException if Freemarker's had enough
 */
public Email generateEmailForVolunteers(Volunteer volunteer) throws IOException, TemplateException {
    Configuration conf = emailFreemarkerConfigurer.getConfiguration();
    Map<String, Object> model = new HashMap<>();
    model.put("volunteer", volunteer);

    Date date = volunteer.getFormDate();
    if (date == null) {
        model.put("message", SubmitLDCFormEmailMessageConstants.FORM_DATE_UNKNOWN.getMessage());
    } else {
        DateTime formDate = DataConverterUtil.toDateTime(date);
        DateTime todayDate = new DateTime();
        Period period = new Period(formDate, todayDate);

        if ((period.getYears() == 2) && (period.getMonths() >= 6)) {
            model.put("message", SubmitLDCFormEmailMessageConstants.FORM_DATE_TWO_HALF_YRS.getMessage());
        } else if (period.getYears() >= 3) {
            model.put("message", SubmitLDCFormEmailMessageConstants.FORM_DATE_THREE_YRS.getMessage());
        }
    }

    Email email = new Email();
    email.setRecipient(volunteer.getPerson().getEmail());
    email.setSubject(SUBJECT);
    email.setText(FreeMarkerTemplateUtils.processTemplateIntoString(conf.getTemplate(SUBMIT_LDC_FORM_TEMPLATE),
            model));

    return email;
}

From source file:com.springstudy.utils.email.MimeMailService.java

/**
 * Freemarker?html?.//from   ww w.  j a v a  2  s .  c  o m
 */
private String generateContent(String template, Map context) throws MessagingException {
    try {
        Template template1 = freeMarkerConfigurer.getConfiguration().getTemplate(template, DEFAULT_ENCODING);
        return FreeMarkerTemplateUtils.processTemplateIntoString(template1, 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.seedboxer.seedboxer.mule.processor.notification.EmailNotification.java

/**
 * Process a Freemarker template and return the String created with it.
 * @param template//from  ww w  . jav a 2s.c o m
 * @param templateVars
 * @return
 */
private String getProcessedTemplate(String template, Map<String, Object> templateVars) {
    try {
        return FreeMarkerTemplateUtils.processTemplateIntoString(freemarkerConfiguration.getTemplate(template),
                templateVars);
    } catch (IOException e) {
        LOGGER.warn("Error at processing template", e);
    } catch (TemplateException e) {
        LOGGER.warn("Error at processing template", e);
    }
    return null;
}

From source file:org.hdiv.web.servlet.view.freemarker.FreeMarkerConfigurerTests.java

public void testFreemarkerConfigurationFactoryBeanWithNonFileResourceLoaderPath()
        throws IOException, TemplateException {
    FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean();
    fcfb.setTemplateLoaderPath("file:/mydir");
    Properties settings = new Properties();
    settings.setProperty("localized_lookup", "false");
    fcfb.setFreemarkerSettings(settings);
    fcfb.setResourceLoader(new ResourceLoader() {
        public Resource getResource(String location) {
            if (!("file:/mydir".equals(location) || "file:/mydir/test".equals(location))) {
                throw new IllegalArgumentException(location);
            }/*from  ww  w . j  a  v  a  2 s  . c o  m*/
            return new ByteArrayResource("test".getBytes(), "test");
        }

        public ClassLoader getClassLoader() {
            return getClass().getClassLoader();
        }
    });
    fcfb.afterPropertiesSet();
    assertTrue(fcfb.getObject() instanceof Configuration);
    Configuration fc = (Configuration) fcfb.getObject();
    Template ft = fc.getTemplate("test");
    assertEquals("test", FreeMarkerTemplateUtils.processTemplateIntoString(ft, new HashMap()));
}

From source file:uk.org.rbc1b.roms.controller.volunteer.update.VolunteerUpdateEmailGenerator.java

/**
 * Generate the email send to the volunteer to confirm they have updated their details.
 * @param volunteer volunteer/*from w w w  .j av  a2  s.  co  m*/
 * @return email
 * @throws TemplateException on failure process the template
 * @throws IOException on failure to look up the template
 */
public Email generateVolunteerUpdateConfirmationEmail(Volunteer volunteer)
        throws IOException, TemplateException {
    Configuration conf = emailFreemarkerConfigurer.getConfiguration();
    Map<String, String> model = new HashMap<String, String>();
    model.put("forename", volunteer.getPerson().getForename());
    String text = FreeMarkerTemplateUtils
            .processTemplateIntoString(conf.getTemplate(UPDATE_CONFIRMATION_TEMPLATE), model);
    Email email = new Email();
    email.setRecipient(volunteer.getPerson().getEmail());
    email.setSubject(SUBJECT + " - Confirmation");
    email.setText(text);

    return email;
}

From source file:com.sisrni.service.FreeMarkerMailServiceImpl.java

public String geFreeMarkerTemplateContent(Map<String, Object> model) {
    StringBuffer content = new StringBuffer();
    try {//w  w  w .jav  a2s. c om
        content.append(FreeMarkerTemplateUtils
                .processTemplateIntoString(freemarkerConfiguration.getTemplate(getNameTemplate()), model));
        return content.toString();
    } catch (Exception e) {
        System.out.println("Exception occured while processing fmtemplate:" + e.getMessage());
    }
    return "";
}