Skip to content

Commit

Permalink
feat(#2056): Make email templates configurable in UI (#2057)
Browse files Browse the repository at this point in the history
* feat(#2056): Make email templates configurable in UI

* Suppress checkstyle warning for HTML templates

* Remove obsolete code

* Fix content type in rest interface

* Move email default template to resources, use syntax highlighting in UI

* Fix misleading comment
  • Loading branch information
dominikriemer authored Oct 20, 2023
1 parent f1d8639 commit 7ca3a88
Show file tree
Hide file tree
Showing 31 changed files with 774 additions and 316 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.streampipes.mail;

import org.apache.streampipes.mail.template.AccountActiviationMailTemplate;
import org.apache.streampipes.mail.template.CustomMailTemplate;
import org.apache.streampipes.mail.template.InitialPasswordMailTemplate;
import org.apache.streampipes.mail.template.PasswordRecoveryMailTemplate;
import org.apache.streampipes.mail.utils.MailUtils;
Expand All @@ -29,11 +30,14 @@

public class MailSender extends AbstractMailer {

public void sendEmail(SpEmail mail) {
public void sendEmail(SpEmail mail) throws IOException {
Email email = baseEmail()
.withRecipients(toSimpleRecipientList(mail.getRecipients()))
.withSubject(mail.getSubject())
.appendText(mail.getMessage())
.appendTextHTML(new CustomMailTemplate(
mail.getSubject(),
mail.getPreheader(),
mail.getMessage()).generateTemplate())
.buildEmail();

deliverMail(email);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.apache.streampipes.mail.template.generation.MailTemplateBuilder;
import org.apache.streampipes.mail.template.part.BaseUrlPart;
import org.apache.streampipes.mail.template.part.LogoPart;
import org.apache.streampipes.mail.template.part.MailTemplatePart;
import org.apache.streampipes.storage.management.StorageDispatcher;

import java.io.IOException;
import java.util.HashMap;
Expand All @@ -35,22 +35,27 @@ public abstract class AbstractMailTemplate {

protected abstract void addPlaceholders(Map<String, String> placeholders);

protected abstract void addTemplateParts(Map<String, MailTemplatePart> templateParts);
protected abstract void configureTemplate(MailTemplateBuilder builder);

public String generateTemplate() throws IOException {
Map<String, MailTemplatePart> templateParts = new HashMap<>();
Map<String, String> placeholders = new HashMap<>();
addTemplateParts(templateParts);
addPlaceholders(placeholders);

return MailTemplateBuilder.create(MailTemplatePart.MAIL_TEMPLATE_OUTER)
.addSubpart(DefaultPlaceholders.FOOTER, MailTemplatePart.MAIL_TEMPLATE_FOOTER)
.addSubparts(templateParts)
var template = StorageDispatcher.INSTANCE
.getNoSqlStore()
.getSpCoreConfigurationStorage()
.get()
.getEmailTemplateConfig()
.getTemplate();

var builder = MailTemplateBuilder.create(template)
.withPlaceholder(DefaultPlaceholders.TITLE, getTitle())
.withPlaceholder(DefaultPlaceholders.PREHEADER, getPreHeader())
.withPlaceholder(DefaultPlaceholders.LOGO, new LogoPart().generate())
.withPlaceholder(DefaultPlaceholders.BASE_URL, new BaseUrlPart().generate())
.withPlaceholders(placeholders)
.generateHtmlTemplate();
.withPlaceholders(placeholders);

configureTemplate(builder);
return builder.generateHtmlTemplate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.streampipes.mail.template;

import org.apache.streampipes.mail.template.generation.DefaultPlaceholders;
import org.apache.streampipes.mail.template.generation.MailTemplateBuilder;
import org.apache.streampipes.mail.template.part.LinkPart;
import org.apache.streampipes.mail.template.part.MailTemplatePart;
import org.apache.streampipes.mail.utils.MailUtils;
Expand Down Expand Up @@ -54,8 +55,8 @@ protected void addPlaceholders(Map<String, String> placeholders) {
}

@Override
protected void addTemplateParts(Map<String, MailTemplatePart> templateParts) {
templateParts.put(DefaultPlaceholders.INNER.key(), MailTemplatePart.MAIL_TEMPLATE_INNER_BUTTON);
protected void configureTemplate(MailTemplateBuilder builder) {
builder.withInnerPart(MailTemplatePart.MAIL_TEMPLATE_INNER_BUTTON);
}

private String makeLink() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.apache.streampipes.mail.template;

import org.apache.streampipes.mail.template.generation.MailTemplateBuilder;

import java.util.Map;

public class CustomMailTemplate extends AbstractMailTemplate {

private final String title;
private final String preheader;
private final String content;

public CustomMailTemplate(String title,
String preheader,
String content) {
this.title = title;
this.preheader = preheader;
this.content = content;
}

@Override
protected String getTitle() {
return title;
}

@Override
protected String getPreHeader() {
return preheader;
}

@Override
protected void addPlaceholders(Map<String, String> placeholders) {
}

@Override
protected void configureTemplate(MailTemplateBuilder builder) {
builder.withInnerPart(content);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.streampipes.mail.template;

import org.apache.streampipes.mail.template.generation.DefaultPlaceholders;
import org.apache.streampipes.mail.template.generation.MailTemplateBuilder;
import org.apache.streampipes.mail.template.part.LinkPart;
import org.apache.streampipes.mail.template.part.MailTemplatePart;
import org.apache.streampipes.mail.utils.MailUtils;
Expand Down Expand Up @@ -54,8 +55,8 @@ protected void addPlaceholders(Map<String, String> placeholders) {
}

@Override
protected void addTemplateParts(Map<String, MailTemplatePart> templateParts) {
templateParts.put(DefaultPlaceholders.INNER.key(), MailTemplatePart.MAIL_TEMPLATE_INNER_BUTTON);
protected void configureTemplate(MailTemplateBuilder builder) {
builder.withInnerPart(MailTemplatePart.MAIL_TEMPLATE_INNER_BUTTON);
}

private String makeLink() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.streampipes.mail.template;

import org.apache.streampipes.mail.template.generation.DefaultPlaceholders;
import org.apache.streampipes.mail.template.generation.MailTemplateBuilder;
import org.apache.streampipes.mail.template.part.LinkPart;
import org.apache.streampipes.mail.template.part.MailTemplatePart;
import org.apache.streampipes.mail.utils.MailUtils;
Expand Down Expand Up @@ -54,9 +55,8 @@ protected void addPlaceholders(Map<String, String> placeholders) {
}

@Override
protected void addTemplateParts(Map<String, MailTemplatePart> templateParts) {
templateParts.put(DefaultPlaceholders.INNER.key(), MailTemplatePart.MAIL_TEMPLATE_INNER_BUTTON);
templateParts.put(DefaultPlaceholders.FOOTER.key(), MailTemplatePart.MAIL_TEMPLATE_FOOTER);
protected void configureTemplate(MailTemplateBuilder builder) {
builder.withInnerPart(MailTemplatePart.MAIL_TEMPLATE_INNER_BUTTON);
}

private String makeLink() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.streampipes.mail.template;

import org.apache.streampipes.mail.template.generation.DefaultPlaceholders;
import org.apache.streampipes.mail.template.generation.MailTemplateBuilder;
import org.apache.streampipes.mail.template.part.MailTemplatePart;
import org.apache.streampipes.mail.utils.MailUtils;

Expand All @@ -41,8 +42,8 @@ protected void addPlaceholders(Map<String, String> placeholders) {
}

@Override
protected void addTemplateParts(Map<String, MailTemplatePart> templateParts) {
templateParts.put(DefaultPlaceholders.INNER.key(), MailTemplatePart.MAIL_TEMPLATE_INNER_PLAIN);
protected void configureTemplate(MailTemplateBuilder builder) {
builder.withInnerPart(MailTemplatePart.MAIL_TEMPLATE_INNER_PLAIN);
}

private String makeText() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ public enum DefaultPlaceholders {

TITLE("TITLE"),
PREHEADER("PREHEADER"),
FOOTER("FOOTER"),
FOOTER_TEXT("FOOTER_TEXT"),
INNER("INNER"),

BUTTON_TEXT("BUTTON_TEXT"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,37 @@
package org.apache.streampipes.mail.template.generation;

import org.apache.streampipes.mail.template.part.MailTemplatePart;
import org.apache.streampipes.mail.utils.MailUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class MailTemplateBuilder {

private final MailTemplatePart outerPart;
private final Map<String, MailTemplatePart> innerParts;
private final String outerTemplate;
private String innerPart;
private final Map<String, String> placeholders;

private MailTemplateBuilder(MailTemplatePart outerPart) {
this.outerPart = outerPart;
this.innerParts = new HashMap<>();
private MailTemplateBuilder(String outerTemplate) {
this.outerTemplate = outerTemplate;
this.placeholders = new HashMap<>();
}

public static MailTemplateBuilder create(MailTemplatePart outerPart) {
return new MailTemplateBuilder(outerPart);
public static MailTemplateBuilder create(String outerTemplate) {
return new MailTemplateBuilder(outerTemplate);
}

public MailTemplateBuilder addSubpart(String placeholder, MailTemplatePart templatePart) {
this.innerParts.put(placeholder, templatePart);

public MailTemplateBuilder withInnerPart(MailTemplatePart innerPart) {
try {
this.innerPart = innerPart.getContent();
} catch (IOException e) {
throw new RuntimeException(e);
}
return this;
}

public MailTemplateBuilder addSubpart(DefaultPlaceholders placeholder, MailTemplatePart templatePart) {
return addSubpart(placeholder.key(), templatePart);

}

public MailTemplateBuilder addSubparts(Map<String, MailTemplatePart> templateParts) {
this.innerParts.putAll(templateParts);
public MailTemplateBuilder withInnerPart(String content) {
this.innerPart = content;
return this;
}

Expand All @@ -72,8 +68,8 @@ public MailTemplateBuilder withPlaceholder(DefaultPlaceholders placeholder, Stri
return withPlaceholder(placeholder.key(), content);
}

public String generateHtmlTemplate() throws IOException {
String fullTemplate = getAndApplyPlaceholders(outerPart);
public String generateHtmlTemplate() {
String fullTemplate = getAndApplyPlaceholders(outerTemplate);

for (String key : placeholders.keySet()) {
String placeholder = makeKey(key);
Expand All @@ -83,30 +79,15 @@ public String generateHtmlTemplate() throws IOException {
return fullTemplate;
}

private String readFileContentsToString(MailTemplatePart part) throws IOException {
return MailUtils.readResourceFileToString(part.getTemplateFilename());
private String getAndApplyPlaceholders(String outerTemplate) {
return applyInnerTemplate(outerTemplate);
}

private String getAndApplyPlaceholders(MailTemplatePart partContent) throws IOException {
String partContentAsString = readFileContentsToString(partContent);
for (String innerPartKey : innerParts.keySet()) {
String templateKey = makeKey(innerPartKey);
if (hasPlaceholder(partContentAsString, templateKey)) {
partContentAsString =
partContentAsString.replaceAll(templateKey, getAndApplyPlaceholders(innerParts.get(innerPartKey)));
}
}

return partContentAsString;
}

private boolean hasPlaceholder(String content, String placeholder) {
return content.contains(placeholder);
private String applyInnerTemplate(String content) {
return content.replaceAll(makeKey(DefaultPlaceholders.INNER.key()), innerPart);
}

private String makeKey(String key) {
return "###" + key + "###";
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,22 @@
*/
package org.apache.streampipes.mail.template.part;

import org.apache.streampipes.mail.utils.MailUtils;

import java.io.IOException;

public enum MailTemplatePart {

MAIL_TEMPLATE_OUTER("mail-template-outer.html"),
MAIL_TEMPLATE_INNER_BUTTON("mail-template-inner-button.html"),
MAIL_TEMPLATE_INNER_PLAIN("mail-template-inner-plain.html"),
MAIL_TEMPLATE_FOOTER("mail-template-footer.html");
MAIL_TEMPLATE_INNER_PLAIN("mail-template-inner-plain.html");

private String templateFilename;
private final String templateFilename;

MailTemplatePart(String templateFilename) {
this.templateFilename = templateFilename;
}

public String getTemplateFilename() {
return this.templateFilename;
public String getContent() throws IOException {
return MailUtils.readResourceFileToString(templateFilename);
}
}
39 changes: 0 additions & 39 deletions streampipes-mail/src/main/resources/mail-template-footer.html

This file was deleted.

Loading

0 comments on commit 7ca3a88

Please sign in to comment.