@Async @Override public void enviarMailCambioContraseña(String nombre, String email, String token, Locale locale) { try { final Context ctx = new Context(locale); ctx.setVariable("nombre", nombre); ctx.setVariable("token", token); ctx.setVariable("lenguaje", locale.getLanguage()); final MimeMessage mimeMessage = mailSender.createMimeMessage(); final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "UTF-8"); message.setSubject(messageSource.getMessage("reset.email.titulo", null, locale)); message.setFrom("*****@*****.**", "Cudú"); message.setTo(email); final String htmlContent = templateEngine.process("resetpassword", ctx); message.setText(htmlContent, true); mailSender.send(mimeMessage); } catch (Exception e) { String correlationId = UUID.randomUUID().toString(); Marker marker = MarkerFactory.getMarker("ENVIO_EMAIL"); logger.error( marker, "Error enviando email. Token: " + token + ", CorrelationId: " + correlationId, e); eventPublisher.publishEvent(new EmailErrorApplicationEvent(email, correlationId)); } }
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { TemplateEngine engine = TemplateEngineUtil.getTemplateEngine(request.getServletContext()); WebContext context = new WebContext(request, response, request.getServletContext()); context.setVariable("recipient", "World"); engine.process("index.html", context, response.getWriter()); }
public PasswordResetToken createPasswordResetToken(PasswordResetTokenCreateRequest request) { User user = userRepository.findByEmail(request.getEmail()); if (user == null) { throw new EmailNotFoundException(); } LocalDateTime now = LocalDateTime.now(); PasswordResetToken passwordResetToken = new PasswordResetToken(); passwordResetToken.setUser(user); passwordResetToken.setEmail(user.getEmail()); passwordResetToken.setExpiredAt(now.plusHours(24)); passwordResetToken.setCreatedAt(now); passwordResetToken.setCreatedBy(user.toString()); passwordResetToken.setUpdatedAt(now); passwordResetToken.setUpdatedBy(user.toString()); passwordResetToken = passwordResetTokenRepository.saveAndFlush(passwordResetToken); try { Blog blog = blogService.readBlogById(Blog.DEFAULT_ID); String blogTitle = blog.getTitle(LocaleContextHolder.getLocale().getLanguage()); ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath(); if (blog.isMultiLanguage()) { builder.path("/{language}"); } builder.path("/password-reset"); builder.path("/{token}"); Map<String, Object> urlVariables = new LinkedHashMap<>(); urlVariables.put("language", request.getLanguage()); urlVariables.put("token", passwordResetToken.getToken()); String resetLink = builder.buildAndExpand(urlVariables).toString(); Context ctx = new Context(LocaleContextHolder.getLocale()); ctx.setVariable("passwordResetToken", passwordResetToken); ctx.setVariable("resetLink", resetLink); MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8"); // true = multipart message.setSubject( MessageFormat.format( messageSourceAccessor.getMessage( "PasswordResetSubject", LocaleContextHolder.getLocale()), blogTitle)); message.setFrom(mailProperties.getProperties().get("mail.from")); message.setTo(passwordResetToken.getEmail()); String htmlContent = templateEngine.process("password-reset", ctx); message.setText(htmlContent, true); // true = isHtml mailSender.send(mimeMessage); } catch (MessagingException e) { throw new ServiceException(e); } return passwordResetToken; }
@CacheEvict(value = "users", allEntries = true) public List<UserInvitation> inviteUsers( UserInvitationCreateRequest form, BindingResult result, AuthorizedUser authorizedUser) throws MessagingException { String[] recipients = StringUtils.commaDelimitedListToStringArray(form.getInvitees()); LocalDateTime now = LocalDateTime.now(); List<UserInvitation> invitations = new ArrayList<>(); for (String recipient : recipients) { UserInvitation invitation = new UserInvitation(); invitation.setEmail(recipient); invitation.setMessage(form.getMessage()); invitation.setExpiredAt(now.plusHours(72)); invitation.setCreatedAt(now); invitation.setCreatedBy(authorizedUser.toString()); invitation.setUpdatedAt(now); invitation.setUpdatedBy(authorizedUser.toString()); invitation = userInvitationRepository.saveAndFlush(invitation); invitations.add(invitation); } Blog blog = blogService.readBlogById(Blog.DEFAULT_ID); for (UserInvitation invitation : invitations) { String websiteTitle = blog.getTitle(LocaleContextHolder.getLocale().getLanguage()); String signupLink = ServletUriComponentsBuilder.fromCurrentContextPath() .path("/_admin/signup") .queryParam("token", invitation.getToken()) .buildAndExpand() .toString(); final Context ctx = new Context(LocaleContextHolder.getLocale()); ctx.setVariable("websiteTitle", websiteTitle); ctx.setVariable("authorizedUser", authorizedUser); ctx.setVariable("signupLink", signupLink); ctx.setVariable("invitation", invitation); final MimeMessage mimeMessage = mailSender.createMimeMessage(); final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8"); // true = multipart message.setSubject( MessageFormat.format( messageSourceAccessor.getMessage( "InvitationMessageTitle", LocaleContextHolder.getLocale()), authorizedUser.toString(), websiteTitle)); message.setFrom(authorizedUser.getEmail()); message.setTo(invitation.getEmail()); final String htmlContent = templateEngine.process("user-invite", ctx); message.setText(htmlContent, true); // true = isHtml mailSender.send(mimeMessage); } return invitations; }
@Test public void renderTemplate() throws Exception { this.context.register( ThymeleafAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); TemplateEngine engine = this.context.getBean(TemplateEngine.class); Context attrs = new Context(Locale.UK, Collections.singletonMap("foo", "bar")); String result = engine.process("home", attrs); assertThat(result).isEqualTo("<html><body>bar</body></html>"); }
@Test public void useJava8TimeDialect() throws Exception { this.context.register( ThymeleafAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); TemplateEngine engine = this.context.getBean(TemplateEngine.class); Context attrs = new Context(Locale.UK); String result = engine.process("java8time-dialect", attrs); assertThat(result).isEqualTo("<html><body>2015-11-24</body></html>"); }
/** {@inheritDoc} */ @Override public void doFilter( final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { try { templateEngine.process("index", createContext(request, response), response.getWriter()); } catch (Exception e) { throw new ServletException(e); } }
@Test public void useDataDialect() throws Exception { this.context.register( ThymeleafAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); TemplateEngine engine = this.context.getBean(TemplateEngine.class); Context attrs = new Context(Locale.UK, Collections.singletonMap("foo", "bar")); String result = engine.process("data-dialect", attrs); assertEquals("<html><body data-foo=\"bar\"></body></html>", result); }
@CacheEvict(value = "users", allEntries = true) public User updatePassword(PasswordUpdateRequest request, PasswordResetToken passwordResetToken) { User user = userRepository.findByIdForUpdate(request.getUserId()); if (user == null) { throw new IllegalArgumentException("The user does not exist"); } PasswordEncoder passwordEncoder = new StandardPasswordEncoder(); user.setLoginPassword(passwordEncoder.encode(request.getPassword())); user.setUpdatedAt(LocalDateTime.now()); user.setUpdatedBy(passwordResetToken.getUser().toString()); user = userRepository.saveAndFlush(user); passwordResetTokenRepository.delete(passwordResetToken); try { Blog blog = blogService.readBlogById(Blog.DEFAULT_ID); String blogTitle = blog.getTitle(LocaleContextHolder.getLocale().getLanguage()); ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath(); if (blog.isMultiLanguage()) { builder.path("/{language}"); } builder.path("/login"); Map<String, Object> urlVariables = new LinkedHashMap<>(); urlVariables.put("language", request.getLanguage()); urlVariables.put("token", passwordResetToken.getToken()); String loginLink = builder.buildAndExpand(urlVariables).toString(); Context ctx = new Context(LocaleContextHolder.getLocale()); ctx.setVariable("passwordResetToken", passwordResetToken); ctx.setVariable("resetLink", loginLink); MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8"); // true = multipart message.setSubject( MessageFormat.format( messageSourceAccessor.getMessage( "PasswordChangedSubject", LocaleContextHolder.getLocale()), blogTitle)); message.setFrom(mailProperties.getProperties().get("mail.from")); message.setTo(passwordResetToken.getEmail()); String htmlContent = templateEngine.process("password-changed", ctx); message.setText(htmlContent, true); // true = isHtml mailSender.send(mimeMessage); } catch (MessagingException e) { throw new ServiceException(e); } return user; }
@Test public void createFromConfigClass() throws Exception { EnvironmentTestUtils.addEnvironment( this.context, "spring.thymeleaf.mode:XHTML", "spring.thymeleaf.suffix:"); this.context.register( ThymeleafAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); TemplateEngine engine = this.context.getBean(TemplateEngine.class); Context attrs = new Context(Locale.UK, Collections.singletonMap("foo", "bar")); String result = engine.process("template.txt", attrs); assertThat(result).isEqualTo("<html>bar</html>"); }
@Override public void processView(ViewEngineContext context) throws ViewEngineException { try { HttpServletRequest request = context.getRequest(); HttpServletResponse response = context.getResponse(); WebContext ctx = new WebContext(request, response, servletContext, request.getLocale()); ctx.setVariables(context.getModels()); engine.process(resolveView(context), ctx, response.getWriter()); } catch (IOException e) { throw new ViewEngineException(e); } }
@Override public String generatePageHtml(ITestPage test) { TemplateResolver templateResolver = new ClassLoaderTemplateResolver(); templateResolver.setTemplateMode("HTML5"); templateResolver.setCharacterEncoding("UTF-8"); TemplateEngine templateEngine = new TemplateEngine(); templateEngine.setTemplateResolver(templateResolver); Locale locale = LocaleUtils.toLocale("fr"); final Context ctx = new Context(locale); ctx.setVariable("test", test); String htmlOutput = templateEngine.process("new_test_report_template.html", ctx); return htmlOutput; }
@Test public void renderNonWebAppTemplate() throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( ThymeleafAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); assertThat(context.getBeanNamesForType(ViewResolver.class).length).isEqualTo(0); try { TemplateEngine engine = context.getBean(TemplateEngine.class); Context attrs = new Context(Locale.UK, Collections.singletonMap("greeting", "Hello World")); String result = engine.process("message", attrs); assertThat(result).contains("Hello World"); } finally { context.close(); } }
@Override public Content parse(String templateName, Context ctx) throws ParseException { try { LOG.debug("Parsing Thymeleaf template {} with context {}...", templateName, ctx); String result = engine.process(templateName, contextConverter.convert(ctx)); LOG.debug("Template {} successfully parsed with context {}. Result:", templateName); LOG.debug(result); return new StringContent(result); } catch (TemplateEngineException e) { throw new ParseException("Failed to parse template with thymeleaf", templateName, ctx, e); } catch (ContextException e) { throw new ParseException( "Failed to parse template with thymeleaf due to conversion error", templateName, ctx, e); } }
@Test public void testJavaScript() { TemplateEngine engine = createWisdomEngine(); Context context = new Context(); context.setVariable("test", "test"); FakeRouter router = new FakeRouter(); Controller controller = new FakeController(); router.addController(controller); context.setVariable(Routes.ROUTES_VAR, new Routes(router, controller)); String processed = engine.process("templates/javascript.thl.html", context); assertThat(processed).containsIgnoringCase("var t = 'test';"); assertThat(processed).containsIgnoringCase("var url = '/';"); assertThat(processed).containsIgnoringCase("$(document).ready(function () {"); }
public void setResponse(Pair<ABSString, ABS.StdLib.List<ABSValue>> pair) throws Exception { TemplateResolver templateResolver = new TemplateResolver(); templateResolver.setTemplateMode("HTML5"); templateResolver.setSuffix(".html"); templateResolver.setResourceResolver(new ResourceResolver()); TemplateEngine templateEngine = new TemplateEngine(); templateEngine.setTemplateResolver(templateResolver); List_Cons<ABSValue> data; if (pair != null) { String view = DataTransformer.convertABSStringToJavaString((ABSString) pair.getArg(0)); Context ctx = new Context(); if (!(pair.getArg(1) instanceof List_Nil)) { data = (List_Cons<ABSValue>) pair.getArg(1); ArrayList<Object> dataModels = DataTransformer.convertABSListToJavaList(data); if (dataModels.size() > 1) { ctx.setVariable("dataList", dataModels); } else { ctx.setVariable("data", dataModels.get(0)); } } ctx.setVariable("requestMethod", this.request.getMethod()); ctx.setVariable("requestURI", this.request.getRequestURI()); ctx.setVariable("requestContextPath", this.request.getContextPath()); ctx.setVariable( "requestRoute", this.getRoute(request.getRequestURI(), request.getContextPath())); this.response = templateEngine.process(view, ctx); } else { this.response = "<h3>ERROR 404: PAGE NOT FOUND</h3>"; this.response += "<span style='font-size:0.8em;'>ABS Server: " + new Date() + "</span><br />"; } }