Open Kilda Java Documentation
VelocityTemplateService.java
Go to the documentation of this file.
1 package org.usermanagement.service.impl;
2 
3 import java.io.StringWriter;
4 import java.util.HashMap;
5 import java.util.Map;
6 
7 import javax.annotation.PostConstruct;
8 
9 import org.apache.velocity.VelocityContext;
10 import org.apache.velocity.app.VelocityEngine;
11 import org.apache.velocity.runtime.RuntimeConstants;
12 import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
14 
15 import org.springframework.stereotype.Service;
16 
17 
18 @Service
19 public class VelocityTemplateService implements TemplateService {
20 
21  private VelocityEngine velocityEngine;
22  private Map<Template, String> templates = new HashMap<TemplateService.Template, String>();
23 
27  @PostConstruct
28  private void init() {
29  velocityEngine = new VelocityEngine();
30  velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
31  velocityEngine.setProperty("classpath.resource.loader.class",
32  ClasspathResourceLoader.class.getName());
33  velocityEngine.init();
34 
35  templates.put(Template.RESET_ACCOUNT_PASSWORD, "ui/templates/mail/resetAccountPassword.vm");
36  templates.put(Template.ACCOUNT_USERNAME, "ui/templates/mail/accountUsername.vm");
37  templates.put(Template.ACCOUNT_PASSWORD, "ui/templates/mail/accountPassword.vm");
38  templates.put(Template.RESET_2FA, "ui/templates/mail/reset2fa.vm");
39  templates.put(Template.CHANGE_PASSWORD, "ui/templates/mail/changePassword.vm");
40 
41  }
42 
43  @Override
44  public String mergeTemplateToString(Template template, Map<String, Object> model) {
45  org.apache.velocity.Template velocityTemplate =
46  velocityEngine.getTemplate(templates.get(template));
47  VelocityContext velocityContext = new VelocityContext(model);
48  StringWriter writer = new StringWriter();
49  velocityTemplate.merge(velocityContext, writer);
50  return writer.toString();
51  }
52 }
String mergeTemplateToString(Template template, Map< String, Object > model)