aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/notification/MailTemplating.java
blob: 1c05330702ec227367fa4d4075c1c5748c0707eb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.notification;

import com.yahoo.config.provision.TenantName;
import com.yahoo.vespa.hosted.controller.api.integration.ConsoleUrls;
import com.yahoo.vespa.hosted.controller.tenant.PendingMailVerification;
import com.yahoo.yolean.Exceptions;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
import org.apache.velocity.runtime.resource.util.StringResourceRepository;
import org.apache.velocity.tools.generic.EscapeTool;

import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;

/**
 * @author bjorncs
 */
public class MailTemplating {

    public enum Template {
        MAIL("mail"),  DEFAULT_MAIL_CONTENT("default-mail-content"), NOTIFICATION_MESSAGE("notification-message"),
        CLOUD_TRIAL_NOTIFICATION("cloud-trial-notification"), MAIL_VERIFICATION("mail-verification");

        public static Optional<Template> fromId(String id) {
            return Arrays.stream(values()).filter(t -> t.id.equals(id)).findAny();
        }

        private final String id;

        Template(String id) { this.id = id; }

        public String getId() { return id; }
    }

    private final VelocityEngine velocity;
    private final EscapeTool escapeTool = new EscapeTool();
    private final ConsoleUrls consoleUrls;

    public MailTemplating(ConsoleUrls consoleUrls) {
        this.velocity = createTemplateEngine();
        this.consoleUrls = consoleUrls;
    }

    public String generateDefaultMailHtml(Template mailBodyTemplate, Map<String, Object> params, TenantName tenant) {
        var ctx = createVelocityContext();
        ctx.put("accountNotificationLink", consoleUrls.tenantNotifications(tenant));
        ctx.put("privacyPolicyLink", "https://legal.yahoo.com/xw/en/yahoo/privacy/topic/b2bprivacypolicy/index.html");
        ctx.put("termsOfServiceLink", consoleUrls.termsOfService());
        ctx.put("supportLink", consoleUrls.support());
        ctx.put("mailBodyTemplate", mailBodyTemplate.getId());
        params.forEach(ctx::put);
        return render(ctx, Template.MAIL);
    }

    public String generateMailVerificationHtml(PendingMailVerification pmf) {
        var ctx = createVelocityContext();
        ctx.put("verifyLink", consoleUrls.verifyEmail(pmf.getVerificationCode()));
        ctx.put("email", pmf.getMailAddress());
        return render(ctx, Template.MAIL_VERIFICATION);
    }

    public String escapeHtml(String s) { return escapeTool.html(s); }

    private VelocityContext createVelocityContext() {
        var ctx = new VelocityContext();
        ctx.put("esc", escapeTool);
        return ctx;
    }

    private String render(VelocityContext ctx, Template template) {
        var writer = new StringWriter();
        // Ignoring return value - implementation either returns 'true' or throws, never 'false'
        velocity.mergeTemplate(template.getId(), StandardCharsets.UTF_8.name(), ctx, writer);
        return writer.toString();
    }

    private static VelocityEngine createTemplateEngine() {
        var v = new VelocityEngine();
        v.setProperty(Velocity.RESOURCE_LOADERS, "string");
        v.setProperty(Velocity.RESOURCE_LOADER + ".string.class", StringResourceLoader.class.getName());
        v.setProperty(Velocity.RESOURCE_LOADER + ".string.repository.static", "false");
        v.init();
        var repo = (StringResourceRepository) v.getApplicationAttribute(StringResourceLoader.REPOSITORY_NAME_DEFAULT);
        Arrays.stream(Template.values()).forEach(t -> registerTemplate(repo, t.getId()));
        return v;
    }

    private static void registerTemplate(StringResourceRepository repo, String name) {
        var templateStr = Exceptions.uncheck(() -> {
            var in = MailTemplating.class.getResourceAsStream("/mail/%s.vm".formatted(name));
            return new String(in.readAllBytes());
        });
        repo.putStringResource(name, templateStr);
    }
}