Skip to content

Commit

Permalink
Notification emails merging and localization
Browse files Browse the repository at this point in the history
Signed-off-by: Stepan Ermakov <sermakov@orionsoft.ru>
  • Loading branch information
sermakov-orion authored and michalskrivanek committed Apr 13, 2023
1 parent f78c60f commit 76a07d3
Show file tree
Hide file tree
Showing 12 changed files with 956 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,40 +1,52 @@
package org.ovirt.engine.core.notifier.transport.smtp;

import java.util.Date;
import java.util.Locale;

import org.ovirt.engine.core.notifier.filter.AuditLogEvent;

/**
* Creates a simple message subject and body using helper class {@linkplain MessageHelper} to determine <br>
* the structure of the message subject and body
* Creates a simple message subject and body using helper class {@linkplain MessageHelper} or
* {@linkplain LocalizedMessageHelper} to determine the structure of the message subject and body
*/
public class EventMessageContent {
private String subject;
private String body;

public EventMessageContent() {
}

private void prepareMessageSubject(String hostName,
AuditLogEvent event) {
subject = MessageHelper.prepareMessageSubject(event.getType(), hostName, event.getMessage());
AuditLogEvent event,
Locale locale) {
subject = (locale == null) ? MessageHelper.prepareMessageSubject(event.getType(), hostName, event.getMessage()) :
LocalizedMessageHelper.prepareMessageSubject(event.getType(), hostName, event.getMessage(), locale);
}

public EventMessageContent(String subject, String body) {
this.subject = subject;
this.body = body;
}

private void prepareMessageBody(AuditLogEvent event,
boolean isBodyHtml) {
boolean isBodyHtml,
Locale locale) {
MessageBody messageBody = new MessageBody();
messageBody.setUserInfo(event.getUserName());
messageBody.setVmInfo(event.getVmName());
messageBody.setHostInfo(event.getVdsName());
messageBody.setTemplateInfo(event.getVmTemplateName());
messageBody.setDatacenterInfo(event.getStoragePoolName());
messageBody.setStorageDomainInfo(event.getStorageDomainName());
final Date logTime = event.getLogTime();
messageBody.setLogTime(logTime != null ? logTime.toString() : "");
messageBody.setSeverity(String.valueOf(event.getSeverity()));
messageBody.setLogTime(event.getLogTime());
messageBody.setSeverity(event.getSeverity());
messageBody.setMessage(event.getMessage());

if (isBodyHtml) {
this.body = MessageHelper.prepareHTMLMessageBody(messageBody);
this.body = (locale == null) ? MessageHelper.prepareHTMLMessageBody(messageBody) :
LocalizedMessageHelper.prepareHTMLMessageBody(messageBody, locale);
} else {
this.body = MessageHelper.prepareMessageBody(messageBody);
this.body = (locale == null) ? MessageHelper.prepareMessageBody(messageBody) :
LocalizedMessageHelper.prepareMessageBody(messageBody, locale);
}
}

Expand Down Expand Up @@ -63,11 +75,12 @@ public String getMessageSubject() {
* @param hostName the host name on which the subject will refer to
* @param event associated event which the message will be created by
* @param isBodyHtml defines the format of message body
* @param locale locale for the message content
*/
public void prepareMessage(String hostName, AuditLogEvent event,
boolean isBodyHtml) {
prepareMessageSubject(hostName, event);
prepareMessageBody(event, isBodyHtml);
boolean isBodyHtml, Locale locale) {
prepareMessageSubject(hostName, event, locale);
prepareMessageBody(event, isBodyHtml, locale);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package org.ovirt.engine.core.notifier.transport.smtp;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang.StringUtils;
import org.ovirt.engine.core.common.AuditLogSeverity;
import org.ovirt.engine.core.notifier.filter.AuditLogEventType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A helper class designed to construct localized message parts in static structure
*/
public class LocalizedMessageHelper {
private static final Logger log = LoggerFactory.getLogger(LocalizedMessageHelper.class);
private static final Map<Locale, ResourceBundle> RESOURCES = new ConcurrentHashMap<>();

/**
* Constructs a formatted message body based on provided message body elements content.<br>
* If any of message body element is empty or missing, it will not appear in the formatted message body text
* @param messageBody
* the message body values for populate a formatted message body
* @param locale
* locale for the message content
* @return a formatted message body
*/
public static String prepareMessageBody(MessageBody messageBody, Locale locale) {
StringBuilder sb = new StringBuilder();

appendPlainMessage(sb, "smtp.message.body.plain.time", getLogTime(messageBody, locale), locale);
appendPlainMessage(sb, "smtp.message.body.plain.message", messageBody.getMessage(), locale);
appendPlainMessage(sb, "smtp.message.body.plain.severity", getSeverity(messageBody, locale), locale);

appendPlainMessage(sb, "smtp.message.body.plain.user.info", messageBody.getUserInfo(), locale);
appendPlainMessage(sb, "smtp.message.body.plain.vm.info", messageBody.getVmInfo(), locale);
appendPlainMessage(sb, "smtp.message.body.plain.host.info", messageBody.getHostInfo(), locale);
appendPlainMessage(sb, "smtp.message.body.plain.template.info", messageBody.getTemplateInfo(), locale);
appendPlainMessage(sb, "smtp.message.body.plain.dc.info", messageBody.getDatacenterInfo(), locale);
appendPlainMessage(sb, "smtp.message.body.plain.storage.domain.info", messageBody.getStorageDomainInfo(), locale);

return sb.toString();
}

/**
* Construct a formatted message based on predefined template:<br>
* {@code "Issue Solved"/"Alert Notification" (host name), [message details]}
* @param type
* determines the prefix of the subject
* @param hostName
* the machine names associated with this event
* @param message
* the content of the message to convey
* @param locale
* locale for the message content
* @return a formatted message subject
*/
public static String prepareMessageSubject(AuditLogEventType type, String hostName, String message, Locale locale) {
String auditLogEventType = type.name();
switch (type) {
case alertMessage:
auditLogEventType = getResourceString("smtp.message.audit.log.event.type.alert", locale, auditLogEventType);
break;
case resolveMessage:
auditLogEventType = getResourceString("smtp.message.audit.log.event.type.resolve", locale, auditLogEventType);
break;
}
return String.format("%s (%s), [%s]", auditLogEventType, hostName, message);

}

/**
* Constructs a formatted message body based on provided message body elements content in HTML format.<br>
* If any of message body element is empty or missing, it will not appear in the formatted message body text
* @param messageBody
* the message body values for populate a formatted message body
* @param locale
* locale for the message content
* @return a formatted HTML message body
*/
public static String prepareHTMLMessageBody(MessageBody messageBody, Locale locale) {
StringBuilder sb = new StringBuilder();

appendHtmlMessage(sb, "smtp.message.body.html.time", getLogTime(messageBody, locale), locale);
appendHtmlMessage(sb, "smtp.message.body.html.message", messageBody.getMessage(), locale);
appendHtmlMessage(sb, "smtp.message.body.html.severity", getSeverity(messageBody, locale), locale);

appendHtmlMessage(sb, "smtp.message.body.html.user.info", messageBody.getUserInfo(), locale);
appendHtmlMessage(sb, "smtp.message.body.html.vm.info", messageBody.getVmInfo(), locale);
appendHtmlMessage(sb, "smtp.message.body.html.host.info", messageBody.getHostInfo(), locale);
appendHtmlMessage(sb, "smtp.message.body.html.template.info", messageBody.getTemplateInfo(), locale);
appendHtmlMessage(sb, "smtp.message.body.html.dc.info", messageBody.getDatacenterInfo(), locale);
appendHtmlMessage(sb, "smtp.message.body.html.storage.domain.info", messageBody.getStorageDomainInfo(), locale);

return sb.toString();
}

private static String getLogTime(MessageBody messageBody, Locale locale) {
Date logTime = messageBody.getLogTime();
if (logTime == null) {
return "";
}
return SimpleDateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, locale).format(logTime);
}

private static String getSeverity(MessageBody messageBody, Locale locale) {
AuditLogSeverity severity = messageBody.getSeverity();
if (severity == null) {
return "";
}
String auditLogSeverity = severity.name();
switch (severity) {
case NORMAL:
auditLogSeverity = getResourceString("smtp.message.audit.log.severity.normal", locale, auditLogSeverity);
break;
case WARNING:
auditLogSeverity = getResourceString("smtp.message.audit.log.severity.warning", locale, auditLogSeverity);
break;
case ERROR:
auditLogSeverity = getResourceString("smtp.message.audit.log.severity.error", locale, auditLogSeverity);
break;
case ALERT:
auditLogSeverity = getResourceString("smtp.message.audit.log.severity.alert", locale, auditLogSeverity);
break;

}
return auditLogSeverity;
}

private static void appendPlainMessage(StringBuilder body, String messageId, String messageValue, Locale locale) {
appendMessage(body, messageId, messageValue, System.lineSeparator(), locale);
}
private static void appendHtmlMessage(StringBuilder body, String messageId, String messageValue, Locale locale) {
appendMessage(body, messageId, messageValue, "<br>", locale);
}
private static void appendMessage(StringBuilder body, String messageId, String messageValue, String messageSuffix, Locale locale) {
if (StringUtils.isNotEmpty(messageValue)) {
String message = getResourceString(messageId, locale, "");
body.append(String.format(message, messageValue))
.append(messageSuffix);
}
}

private static String getResourceString(String id, Locale locale, String defaultValue) {
try {
return RESOURCES.computeIfAbsent(locale, lc -> ResourceBundle.getBundle("smtp-messages", lc))
.getString(id);
} catch (MissingResourceException mre) {
log.debug("Failed to load resource string {}: {}", id, mre.getMessage(), mre);
return defaultValue;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.ovirt.engine.core.notifier.transport.smtp;

import java.util.Date;

import org.ovirt.engine.core.common.AuditLogSeverity;

/**
* Describes a message content
*/
Expand All @@ -10,9 +14,9 @@ public class MessageBody{
private String templateInfo;
private String datacenterInfo;
private String storageDomainInfo;
private String logTime;
private Date logTime;
private String message;
private String severity;
private AuditLogSeverity severity;

public String getUserInfo() {
return userInfo;
Expand Down Expand Up @@ -53,19 +57,19 @@ public void setStorageDomainInfo(String storageDomainInfo) {
this.storageDomainInfo = storageDomainInfo;
}

public String getLogTime() {
public Date getLogTime() {
return logTime;
}

public void setLogTime(String logTime) {
public void setLogTime(Date logTime) {
this.logTime = logTime;
}

public String getSeverity() {
public AuditLogSeverity getSeverity() {
return severity;
}

public void setSeverity(String severity) {
public void setSeverity(AuditLogSeverity severity) {
this.severity = severity;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.ovirt.engine.core.notifier.transport.smtp;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.ovirt.engine.core.notifier.filter.AuditLogEventType;

Expand All @@ -19,7 +21,7 @@ public static String prepareMessageBody(MessageBody messageBody) {
StringBuilder sb = new StringBuilder();

sb.append(String.format("Time:%s%nMessage:%s%nSeverity:%s%n",
messageBody.getLogTime(),
getLogTime(messageBody),
messageBody.getMessage(),
messageBody.getSeverity()));

Expand Down Expand Up @@ -77,7 +79,7 @@ public static String prepareHTMLMessageBody(MessageBody messageBody) {
StringBuilder sb = new StringBuilder();

sb.append(String.format("<b>Time:</b> %s<br><b>Message:</b> %s<br><b>Severity:</b> %s<p>",
messageBody.getLogTime(),
getLogTime(messageBody),
messageBody.getMessage(),
messageBody.getSeverity()));

Expand Down Expand Up @@ -107,4 +109,9 @@ public static String prepareHTMLMessageBody(MessageBody messageBody) {
}
return sb.toString();
}

private static String getLogTime(MessageBody messageBody) {
Date logTime = messageBody.getLogTime();
return logTime == null ? "" : logTime.toString();
}
}
Loading

0 comments on commit 76a07d3

Please sign in to comment.