Search in sources :

Example 96 with Context

use of org.thymeleaf.context.Context in project be5 by DevelopmentOnTheEdge.

the class TemplateProcessor method generate.

@Override
public void generate(Request req, Response res, Injector injector) {
    UserAwareMeta userAwareMeta = injector.get(UserAwareMeta.class);
    String title = userAwareMeta.getColumnTitle("index", "page", "title");
    String description = userAwareMeta.getColumnTitle("index", "page", "description");
    Context context = new Context();
    context.setVariable("lang", UserInfoHolder.getLanguage());
    context.setVariable("title", title);
    context.setVariable("description", description);
    String reqWithoutContext = req.getRequestUri().replaceFirst(req.getContextPath(), "");
    if (!reqWithoutContext.endsWith("/"))
        reqWithoutContext += "/";
    context.setVariable("baseUrl", req.getContextPath() + reqWithoutContext);
    context.setVariable("baseUrlWithoutContext", reqWithoutContext);
    res.sendHtml(templateEngine.process(reqWithoutContext + "index", context));
}
Also used : ServletContext(javax.servlet.ServletContext) Context(org.thymeleaf.context.Context) UserAwareMeta(com.developmentontheedge.be5.api.helpers.UserAwareMeta)

Example 97 with Context

use of org.thymeleaf.context.Context in project cetc by DiscoverForever.

the class MailService method sendEmailFromTemplate.

@Async
public void sendEmailFromTemplate(User user, String templateName, String titleKey) {
    Locale locale = Locale.forLanguageTag(user.getLangKey());
    Context context = new Context(locale);
    context.setVariable(USER, user);
    context.setVariable(BASE_URL, jHipsterProperties.getMail().getBaseUrl());
    String content = templateEngine.process(templateName, context);
    String subject = messageSource.getMessage(titleKey, null, locale);
    sendEmail(user.getEmail(), subject, content, false, true);
}
Also used : Locale(java.util.Locale) Context(org.thymeleaf.context.Context) Async(org.springframework.scheduling.annotation.Async)

Example 98 with Context

use of org.thymeleaf.context.Context in project irida by phac-nml.

the class EmailControllerImpl method sendSubscriptionUpdateEmail.

/**
 * {@inheritDoc}
 */
@Override
public void sendSubscriptionUpdateEmail(User user, List<ProjectEvent> events) {
    logger.debug("Sending subscription email to " + user.getEmail());
    final Context ctx = new Context();
    ctx.setVariable("ngsEmail", serverEmail);
    ctx.setVariable("serverURL", serverURL);
    ctx.setVariable("user", user);
    Locale locale = Locale.forLanguageTag(user.getLocale());
    ctx.setVariable("dateFormat", messageSource.getMessage("locale.date.long", null, locale));
    List<Map<String, Object>> eventsList = buildEventsListFromCollection(events);
    ctx.setVariable("events", eventsList);
    final String htmlContent = templateEngine.process(SUBSCRIPTION_TEMPLATE, ctx);
    try {
        final MimeMessage mimeMessage = this.javaMailSender.createMimeMessage();
        final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "UTF-8");
        message.setSubject(messageSource.getMessage("email.subscription.title", null, locale));
        message.setFrom(serverEmail);
        message.setTo(user.getEmail());
        message.setText(htmlContent, true);
        javaMailSender.send(mimeMessage);
    } catch (Exception e) {
        logger.error("Error trying to send subcription email.", e);
        throw new MailSendException("Failed to send e-mail for project event subscription.", e);
    }
}
Also used : Context(org.thymeleaf.context.Context) Locale(java.util.Locale) MailSendException(org.springframework.mail.MailSendException) MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) MessagingException(javax.mail.MessagingException) MailSendException(org.springframework.mail.MailSendException)

Example 99 with Context

use of org.thymeleaf.context.Context in project irida by phac-nml.

the class EmailControllerImpl method sendPasswordResetLinkEmail.

/**
 * {@inheritDoc}
 */
@Override
public void sendPasswordResetLinkEmail(User user, PasswordReset passwordReset) throws MailSendException {
    logger.debug("Sending password reset email to " + user.getEmail());
    final Context ctx = new Context();
    ctx.setVariable("ngsEmail", serverEmail);
    ctx.setVariable("serverURL", serverURL);
    Locale locale = LocaleContextHolder.getLocale();
    // add the reset information
    ctx.setVariable("passwordReset", passwordReset);
    ctx.setVariable("user", user);
    try {
        final MimeMessage mimeMessage = this.javaMailSender.createMimeMessage();
        final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "UTF-8");
        message.setSubject(messageSource.getMessage("email.reset.subject", null, locale));
        message.setFrom(serverEmail);
        message.setTo(user.getEmail());
        final String htmlContent = templateEngine.process(RESET_TEMPLATE, ctx);
        message.setText(htmlContent, true);
        javaMailSender.send(mimeMessage);
    } catch (Exception e) {
        logger.error("Error trying to send a password reset link email.", e);
        throw new MailSendException("Failed to send e-mail when doing password reset.", e);
    }
}
Also used : Context(org.thymeleaf.context.Context) Locale(java.util.Locale) MailSendException(org.springframework.mail.MailSendException) MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) MessagingException(javax.mail.MessagingException) MailSendException(org.springframework.mail.MailSendException)

Example 100 with Context

use of org.thymeleaf.context.Context in project dubion by valsamiq.

the class MailService method sendEmailFromTemplate.

@Async
public void sendEmailFromTemplate(User user, String templateName, String titleKey) {
    Locale locale = Locale.forLanguageTag(user.getLangKey());
    Context context = new Context(locale);
    context.setVariable(USER, user);
    context.setVariable(BASE_URL, jHipsterProperties.getMail().getBaseUrl());
    String content = templateEngine.process(templateName, context);
    String subject = messageSource.getMessage(titleKey, null, locale);
    sendEmail(user.getEmail(), subject, content, false, true);
}
Also used : Locale(java.util.Locale) Context(org.thymeleaf.context.Context) Async(org.springframework.scheduling.annotation.Async)

Aggregations

Context (org.thymeleaf.context.Context)242 Test (org.junit.Test)179 Album (org.thymeleaf.spring5.reactive.data.Album)141 ReactiveDataDriverContextVariable (org.thymeleaf.spring5.context.webflux.ReactiveDataDriverContextVariable)130 TemplateEngine (org.thymeleaf.TemplateEngine)30 Locale (java.util.Locale)24 Async (org.springframework.scheduling.annotation.Async)21 ClassLoaderTemplateResolver (org.thymeleaf.templateresolver.ClassLoaderTemplateResolver)13 SpringTemplateEngine (org.thymeleaf.spring5.SpringTemplateEngine)11 MockServletContext (org.springframework.mock.web.MockServletContext)10 RequestContext (org.springframework.web.servlet.support.RequestContext)10 Test (org.junit.jupiter.api.Test)9 WebContext (org.thymeleaf.context.WebContext)9 MessagingException (javax.mail.MessagingException)7 StringTemplateResolver (org.thymeleaf.templateresolver.StringTemplateResolver)7 DefaultTemplateResolver (org.thymeleaf.templateresolver.DefaultTemplateResolver)6 ServletContext (jakarta.servlet.ServletContext)5 ArrayList (java.util.ArrayList)5 MimeMessage (javax.mail.internet.MimeMessage)5 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)5