use of groovy.text.Template in project cuba by cuba-platform.
the class UserManagementServiceBean method loadDefaultTemplate.
protected Template loadDefaultTemplate(String templatePath, SimpleTemplateEngine templateEngine) {
String defaultTemplateContent = resources.getResourceAsString(templatePath);
if (defaultTemplateContent == null) {
throw new IllegalStateException("Not found default email template for reset passwords operation");
}
// noinspection UnnecessaryLocalVariable
Template template = getTemplate(templateEngine, defaultTemplateContent);
return template;
}
use of groovy.text.Template in project cuba by cuba-platform.
the class UserManagementServiceBean method getResetPasswordTemplate.
protected EmailTemplate getResetPasswordTemplate(User user, SimpleTemplateEngine templateEngine, String resetPasswordSubjectTemplate, String resetPasswordBodyTemplate, Template subjectDefaultTemplate, Template bodyDefaultTemplate, Map<String, Template> localizedSubjectTemplates, Map<String, Template> localizedBodyTemplates) {
boolean userLocaleIsUnknown = StringUtils.isEmpty(user.getLanguage());
String locale = userLocaleIsUnknown ? messageTools.getDefaultLocale().getLanguage() : user.getLanguage();
Template bodyTemplate;
if (userLocaleIsUnknown) {
bodyTemplate = bodyDefaultTemplate;
} else {
if (localizedBodyTemplates.containsKey(locale))
bodyTemplate = localizedBodyTemplates.get(locale);
else {
String templateString = getLocalizedTemplateContent(resetPasswordBodyTemplate, locale);
if (templateString == null) {
log.debug("Reset passwords: Not found email body template for locale: '{}'", locale);
bodyTemplate = bodyDefaultTemplate;
} else {
bodyTemplate = getTemplate(templateEngine, templateString);
}
localizedBodyTemplates.put(locale, bodyTemplate);
}
}
Template subjectTemplate;
if (userLocaleIsUnknown) {
subjectTemplate = subjectDefaultTemplate;
} else {
if (localizedSubjectTemplates.containsKey(locale))
subjectTemplate = localizedSubjectTemplates.get(locale);
else {
String templateString = getLocalizedTemplateContent(resetPasswordSubjectTemplate, locale);
if (templateString == null) {
log.debug("Reset passwords: Not found email subject template for locale '{}'", locale);
subjectTemplate = subjectDefaultTemplate;
} else {
subjectTemplate = getTemplate(templateEngine, templateString);
}
localizedSubjectTemplates.put(locale, subjectTemplate);
}
}
return new EmailTemplate(subjectTemplate, bodyTemplate);
}
use of groovy.text.Template in project coprhd-controller by CoprHD.
the class TemplateEngine method getTemplate.
/**
* Returns a cached template file
*/
private static synchronized Template getTemplate(File templateFile) {
if (templates.containsKey(templateFile.getName())) {
return templates.get(templateFile.getName());
}
SimpleTemplateEngine engine = new SimpleTemplateEngine();
try {
String templateContents = IOUtils.toString(new FileInputStream(templateFile));
templateContents = preprocessTemplate(templateContents);
Template template = engine.createTemplate(templateContents);
templates.put(templateFile.getName(), template);
return template;
} catch (Exception e) {
throw new RuntimeException("Unable to process template" + templateFile, e);
}
}
use of groovy.text.Template in project coprhd-controller by CoprHD.
the class TemplateEngine method generateStringFromTemplate.
/**
* Call the template with the parameters, but return the response as a string
*/
public static String generateStringFromTemplate(File templateFile, Map<String, Object> parameters) {
try {
Template template = getTemplate(templateFile);
Writable finishedTemplate = template.make(parameters);
return finishedTemplate.toString();
} catch (Exception e) {
DocReporter.printError("Error whilst generating page from template " + templateFile);
DocReporter.printError(e.getMessage());
throw new RuntimeException(e);
}
}
use of groovy.text.Template in project groovy-core by groovy.
the class TemplateServlet method getTemplate.
/**
* Gets the template created by the underlying engine parsing the request.
*
* <p>
* This method looks up a simple (weak) hash map for an existing template
* object that matches the source URL. If there is no cache entry, a new one is
* created by the underlying template engine. This new instance is put
* to the cache for consecutive calls.
*
* @return The template that will produce the response text.
* @param url The URL containing the template source..
* @throws ServletException If the request specified an invalid template source URL
*/
protected Template getTemplate(URL url) throws ServletException {
String key = url.toString();
Template template = findCachedTemplate(key, null);
// Template not cached or the source file changed - compile new template!
if (template == null) {
try {
template = createAndStoreTemplate(key, url.openConnection().getInputStream(), null);
} catch (Exception e) {
throw new ServletException("Creation of template failed: " + e, e);
}
}
return template;
}
Aggregations