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.seedboxer.mule.processor.postaction.SSHCommandSender.java

private String processTemplate(String command, Map<String, Object> templateVars)
        throws IOException, TemplateException {

    Template cmdTemplate = new Template("command", new StringReader(command), freemarkerConfiguration);
    return FreeMarkerTemplateUtils.processTemplateIntoString(cmdTemplate, templateVars);
}

From source file:uk.org.rbc1b.roms.scheduled.PersonChangesScheduledService.java

private void createEmailForRecipient(EmailRecipient mailRecipient) throws IOException, TemplateException {
    Configuration conf = emailFreemarkerConfigurer.getConfiguration();

    Map<String, Person> model = new HashMap<String, Person>();
    Person person = personDao.findPerson(mailRecipient.getPerson().getPersonId());
    model.put("person", person);

    String text = FreeMarkerTemplateUtils.processTemplateIntoString(conf.getTemplate(EMAIL_TEMPLATE), model);

    Email email = new Email();
    email.setRecipient(person.getEmail());
    email.setSubject(SUBJECT);/*from  w ww . j a v a2s.c o  m*/
    email.setText(text);
    emailDao.save(email);
}

From source file:edu.monash.merc.mail.impl.FreeMarkerMailServiceImpl.java

/**
 * Create a mail body base on the mail template file and template values.
 *
 * @param templateValueMap//  w  w  w .  j  a  va  2s .c  o m
 * @param templateFile
 * @return
 * @throws Exception
 */
private String createMailBody(Map<String, String> templateValueMap, String templateFile) throws Exception {
    String htmlText = "";
    Template tpl = mailFreeMarker.getConfiguration().getTemplate(templateFile);
    htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(tpl, templateValueMap);
    return htmlText;
}

From source file:com.gnizr.web.action.user.RequestPasswordReset.java

private boolean sendPasswordResetEmail(String token, User user) {
    Map<String, Object> model = new HashMap<String, Object>();
    model.put("token", token);
    model.put("username", user.getUsername());
    model.put("gnizrConfiguration", getGnizrConfiguration());

    if (getVerifyResetTemplate() == null) {
        logger.error("RequestPasswordReset: templateMessge bean is not defined");
        addActionError(String.valueOf(ActionErrorCode.ERROR_CONFIG));
        return false;
    }//from  w w  w. j  a  v  a 2s .  co  m
    String toEmail = user.getEmail();
    if (toEmail == null) {
        logger.error("RequestPasswordReset: the email of user " + user.getUsername() + " is not defined");
        addActionError(String.valueOf(ActionErrorCode.ERROR_EMAIL_UNDEF));
        return false;
    }
    SimpleMailMessage msg = new SimpleMailMessage(getVerifyResetTemplate());
    msg.setTo(toEmail);

    if (msg.getFrom() == null) {
        String contactEmail = getGnizrConfiguration().getSiteContactEmail();
        if (contactEmail != null) {
            msg.setFrom(contactEmail);
        } else {
            msg.setFrom("help@localhost");
        }
    }

    Template fmTemplate = null;
    String text = null;
    try {
        fmTemplate = freemarkerEngine.getTemplate("login/notifyreset-template.ftl");
        text = FreeMarkerTemplateUtils.processTemplateIntoString(fmTemplate, model);
    } catch (Exception e) {
        logger.error("RequestPasswordReset: error creating message template from Freemarker engine");
    }

    msg.setText(text);

    if (getMailSender() == null) {
        logger.error("RequestPasswordReset: mailSender bean is not defined");
        addActionError(String.valueOf(ActionErrorCode.ERROR_CONFIG));
        return false;
    }
    try {
        getMailSender().send(msg);
        return true;
    } catch (Exception e) {
        logger.error("RequestPasswordReset: send mail error. " + e);
        addActionError(String.valueOf(ActionErrorCode.ERROR_INTERNAL));
    }
    return false;
}

From source file:uk.org.rbc1b.roms.controller.volunteer.contactdetails.VolunteerContactDetailsEmailGenerator.java

/**
 * Generate the email template for the volunteer passed into this
 * generator class./* w  ww  . j a  v a2s .  c  om*/
 *
 * @param volunteer the volunteer (recipient) to be used in the template
 * @return Email full email with injected template
 * @throws IOException if we can't find the template
 * @throws TemplateException if free marker has trouble
 */
public Email generateEmailForVolunteers(Volunteer volunteer) throws IOException, TemplateException {

    Configuration conf = emailFreemarkerConfigurer.getConfiguration();
    Map<String, Object> model = new HashMap<>();
    model.put("volunteer", volunteer);
    populatePersonModel(model, volunteer.getPerson());
    populateEmergencyContact(model, volunteer);

    List<Assignment> assignments = volunteerDao.findAssignments(volunteer.getPersonId());
    populateAssignments(model, assignments);

    List<VolunteerQualification> qualifications = volunteerDao.findQualifications(volunteer.getPersonId());
    model.put("qualifications", volunteerModelFactory.generateVolunteerQualificationsModel(qualifications));

    List<VolunteerSkill> skills = volunteerDao.findSkills(volunteer.getPersonId());
    model.put("skills", volunteerModelFactory.generateVolunteerSkillsModel(skills));

    model.put("edificeHomeUrl", edificeProperty.getProperty(SERVER_URL));
    model.put("confirmationUrl", generateConfirmationUrl(volunteer.getPersonId()));

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

    return email;
}

From source file:com.github.dactiv.common.spring.mail.JavaMailService.java

/**
 * ?freemarker?/* w w  w. j ava2  s  .  c o  m*/
 * 
 * @param templateName ???
 * @param model freemarker???el??
 * 
 * @return String
 * 
 * @throws IOException
 * @throws TemplateException
 */
private String getTemplateString(String templateName, Map<String, ?> model)
        throws IOException, TemplateException {
    Template template = freemarkerConfiguration.getTemplate(templateName, encoding);
    return FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
}

From source file:com.edgenius.core.service.impl.MailEngineService.java

private String generateContent(String templateName, Map map) {
    try {//from w  ww. java 2 s.  c o  m
        mailTemplateEngine.getConfiguration().setLocale(Global.getDefaultLocale());
        Template t = mailTemplateEngine.getConfiguration().getTemplate(templateName);
        return FreeMarkerTemplateUtils.processTemplateIntoString(t, map);
    } catch (TemplateException e) {
        log.error("Error while processing FreeMarker template ", e);
    } catch (FileNotFoundException e) {
        log.error("Error while open template file ", e);
    } catch (IOException e) {
        log.error("Error while generate Email Content ", e);
    }
    return null;
}

From source file:com.github.dactiv.fear.user.service.account.AccountService.java

/**
 * ???/*from w w  w  .  j  a  v  a2s  .  co m*/
 *
 * @param mail ???
 * @throws Exception
 */
public void sendValidMail(String mail) throws Exception {

    Boolean flag = Apis.invoke("accountService", "isUsernameUnique", mail);

    if (!flag) {
        throw new ServiceException("" + mail + "");
    }

    Map<String, Object> principal = Subjects.getPrincipal();
    String id = new Md5Hash(mail + principal.get("id") + System.currentTimeMillis()).toHex();

    ValidToken entity = new MailValidToken(id, new Date(), mail, principal);
    Cache cache = cacheManager.getCache(validMailCacheName);

    cache.put(id, entity);

    Template template = freemarkerConfiguration.getTemplate("registration.ftl", "UTF-8");
    String message = FreeMarkerTemplateUtils.processTemplateIntoString(template, entity);

    Mail mailEntity = new Mail(mail, "?", message, Boolean.TRUE);
    Apis.invoke("messageService", "sendMail", mailEntity);
}

From source file:com.gnizr.web.action.user.ApproveUserAccount.java

private boolean sendNotificationEmail(User user) {
    Map<String, Object> model = new HashMap<String, Object>();
    model.put("token", token);
    model.put("username", user.getUsername());
    model.put("email", user.getEmail());
    model.put("createdOn", user.getCreatedOn());
    model.put("gnizrConfiguration", getGnizrConfiguration());

    if (getWelcomeEmailTemplate() == null) {
        logger.error("ApproveUserAccount: welcomeEmailTemplate bean is not defined");
        addActionError(String.valueOf(ActionErrorCode.ERROR_CONFIG));
        return false;
    }// w  w w .  jav  a 2  s .  co  m

    String toUserEmail = user.getEmail();
    if (toUserEmail == null) {
        logger.error("ApproveUserAccount: the email of user " + user.getUsername() + " is not defined");
        addActionError(String.valueOf(ActionErrorCode.ERROR_EMAIL_UNDEF));
        return false;
    }

    SimpleMailMessage notifyMsg = new SimpleMailMessage(getWelcomeEmailTemplate());
    notifyMsg.setTo(toUserEmail);

    if (notifyMsg.getFrom() == null) {
        String contactEmail = getGnizrConfiguration().getSiteContactEmail();
        if (contactEmail != null) {
            notifyMsg.setFrom(contactEmail);
        } else {
            notifyMsg.setFrom("no-reply@localhost");
        }
    }

    Template fmTemplate1 = null;
    String text1 = null;
    try {
        fmTemplate1 = freemarkerEngine.getTemplate("login/welcome-template.ftl");
        text1 = FreeMarkerTemplateUtils.processTemplateIntoString(fmTemplate1, model);
    } catch (Exception e) {
        logger.error("ApproveUserAccount: error creating message template from Freemarker engine");
    }
    notifyMsg.setText(text1);

    if (getMailSender() == null) {
        logger.error("ApproveUserAccount: mailSender bean is not defined");
        addActionError(String.valueOf(ActionErrorCode.ERROR_CONFIG));
        return false;
    }
    try {
        getMailSender().send(notifyMsg);
        return true;
    } catch (Exception e) {
        logger.error("ApproveUserAccount: send mail error. " + e);
        addActionError(String.valueOf(ActionErrorCode.ERROR_INTERNAL));
    }

    return false;
}

From source file:edu.sampleu.admin.XmlIngester.java

/**
 * writes processed template  to file/*from  w  w w .  ja v a2s  . c  om*/
 *
 * @param file
 * @param template
 * @param props
 * @return
 * @throws IOException
 * @throws TemplateException
 */
private File writeTemplateToFile(File file, Template template, Properties props)
        throws IOException, TemplateException {
    String output = FreeMarkerTemplateUtils.processTemplateIntoString(template, props);
    LOG.debug("Generated File Output: " + output);
    FileUtils.writeStringToFile(file, output);
    return file;
}