use of io.quarkus.qute.Template in project automatiko-engine by automatiko-io.
the class SendEmailService method send.
/**
* Sends email to the given list of recipients with given subject and body that is created based on the given template.
* Optionally given attachments will be put on the message as well
*
* In case of error a <code>ServiceExecutionError</code> will be thrown with error code set to <code>sendEmailFailure</code>
* so it can be used within workflow definition to handle it
*
* In case template cannot be found with given name a <code>ServiceExecutionError</code> will
* be thrown with error code set to <code>emailTemplateNotFound</code>
*
* @param tos comma separated list of recipients
* @param subject subject of the email
* @param templateName name of the email template to be used to create the body of the email message
* @param body body of the email
* @param attachments optional attachments
*/
public void send(String tos, String subject, String templateName, Object body, File<byte[]>... attachments) {
Template template = getTemplate(templateName);
try {
Map<String, Object> templateData = new HashMap<>();
templateData.put("body", body);
String content = template.instance().data(templateData).render();
for (String to : tos.split(",")) {
Mail mail = Mail.withHtml(to, subject, content);
for (File<byte[]> attachment : attachments) {
if (attachment == null) {
continue;
}
mail.addAttachment(attachment.name(), attachment.content(), attachment.type());
}
mailer.send(mail);
}
} catch (Exception e) {
throw new ServiceExecutionError("sendEmailFailure", e.getMessage(), e);
}
}
use of io.quarkus.qute.Template in project quarkus-openapi-generator by quarkiverse.
the class QuteTemplatingEngineAdapter method compileTemplate.
@Override
public String compileTemplate(TemplatingExecutor executor, Map<String, Object> bundle, String templateFile) throws IOException {
this.cacheTemplates(executor);
Template template = engine.getTemplate(templateFile);
if (template == null) {
template = engine.parse(executor.getFullTemplateContents(templateFile));
engine.putTemplate(templateFile, template);
}
return template.data(bundle).render();
}
use of io.quarkus.qute.Template in project jbang by jbangdev.
the class Init method doCall.
@Override
public Integer doCall() throws IOException {
requireScriptArgument();
dev.jbang.catalog.Template tpl = dev.jbang.catalog.Template.get(initTemplate);
if (tpl == null) {
throw new ExitException(BaseCommand.EXIT_INVALID_INPUT, "Could not find init template named: " + initTemplate + ". Try run with --fresh to get latest catalog updates.");
}
boolean absolute = new File(scriptOrFile).isAbsolute();
Path outFile = Util.getCwd().resolve(scriptOrFile);
Path outDir = outFile.getParent();
String outName = outFile.getFileName().toString();
properties.put("scriptref", scriptOrFile);
properties.put("baseName", Util.getBaseName(Paths.get(scriptOrFile).getFileName().toString()));
properties.put("dependencies", dependencies);
List<RefTarget> refTargets = tpl.fileRefs.entrySet().stream().map(e -> new AbstractMap.SimpleEntry<>(resolveBaseName(e.getKey(), e.getValue(), outName), tpl.resolve(e.getValue()))).map(e -> RefTarget.create(tpl.catalog.catalogRef.getFile().getAbsolutePath(), e.getValue(), e.getKey())).collect(Collectors.toList());
applyTemplateProperties(tpl);
if (!force) {
// Check if any of the files already exist
for (RefTarget refTarget : refTargets) {
Path target = refTarget.to(outDir);
if (Files.exists(target)) {
warn("File " + target + " already exists. Will not initialize.");
return EXIT_GENERIC_ERROR;
}
}
}
try {
for (RefTarget refTarget : refTargets) {
if (refTarget.getSource().getOriginalResource().endsWith(".qute")) {
// TODO fix outFile path handling
Path out = refTarget.to(outDir);
renderQuteTemplate(out, refTarget.getSource(), properties);
} else {
refTarget.copy(outDir);
}
}
} catch (IOException e) {
// Clean up any files we already created
for (RefTarget refTarget : refTargets) {
Util.deletePath(refTarget.to(outDir), true);
}
}
String renderedScriptOrFile = getRenderedScriptOrFile(tpl.fileRefs, refTargets, outDir, absolute);
info("File initialized. You can now run it with 'jbang " + renderedScriptOrFile + "' or edit it using 'jbang edit --open=[editor] " + renderedScriptOrFile + "' where [editor] is your editor or IDE, e.g. '" + Edit.knownEditors[new Random().nextInt(Edit.knownEditors.length)] + "'");
return EXIT_OK;
}
use of io.quarkus.qute.Template in project jbang by jbangdev.
the class Init method renderQuteTemplate.
void renderQuteTemplate(Path outFile, String templatePath, Map<String, Object> properties) throws IOException {
Template template = TemplateEngine.instance().getTemplate(templatePath);
if (template == null) {
throw new ExitException(EXIT_INVALID_INPUT, "Could not find or load template: " + templatePath);
}
if (outFile.toString().endsWith(".java")) {
String basename = Util.getBaseName(outFile.getFileName().toString());
if (!SourceVersion.isIdentifier(basename)) {
throw new ExitException(EXIT_INVALID_INPUT, "'" + basename + "' is not a valid class name in java. Remove the special characters");
}
}
Files.createDirectories(outFile.getParent());
try (BufferedWriter writer = Files.newBufferedWriter(outFile)) {
TemplateInstance templateWithData = template.instance();
properties.forEach(templateWithData::data);
String result = templateWithData.render();
writer.write(result);
outFile.toFile().setExecutable(true);
}
}
use of io.quarkus.qute.Template in project jbang by jbangdev.
the class TrustedSources method createTrustedSources.
public static void createTrustedSources() {
Path trustedSourcesFile = Settings.getTrustedSourcesFile();
if (Files.notExists(trustedSourcesFile)) {
String templateName = "trusted-sources.qute";
Template template = TemplateEngine.instance().getTemplate(templateName);
if (template == null)
throw new ExitException(1, "Could not locate template named: '" + templateName + "'");
String result = template.render();
try {
Util.writeString(trustedSourcesFile, result);
} catch (IOException e) {
Util.errorMsg("Could not create initial trusted-sources file at " + trustedSourcesFile, e);
}
}
}
Aggregations