|
|
@@ -1,124 +1,124 @@
|
|
|
-package cz.senslog.watchdog.messagebroker.broker;
|
|
|
-
|
|
|
-import cz.senslog.watchdog.config.EmailMessageBrokerConfig;
|
|
|
-import cz.senslog.watchdog.config.MessageBrokerType;
|
|
|
-import cz.senslog.watchdog.core.EmailServerConnection;
|
|
|
-import cz.senslog.watchdog.domain.*;
|
|
|
-import cz.senslog.watchdog.messagebroker.MessageBroker;
|
|
|
-import cz.senslog.watchdog.messagebroker.MessageBrokerHandler;
|
|
|
-import cz.senslog.watchdog.messagebroker.MessageStatus;
|
|
|
-import cz.senslog.watchdog.messagebroker.writer.HtmlTableWriter;
|
|
|
-import cz.senslog.watchdog.messagebroker.writer.TableWriter;
|
|
|
-import org.apache.logging.log4j.LogManager;
|
|
|
-import org.apache.logging.log4j.Logger;
|
|
|
-
|
|
|
-import javax.mail.*;
|
|
|
-import java.util.*;
|
|
|
-
|
|
|
-import static java.time.format.DateTimeFormatter.ofPattern;
|
|
|
-
|
|
|
-public class EmailMessageBroker implements MessageBroker {
|
|
|
-
|
|
|
- private static final String BREAK_LINE = "<br />";
|
|
|
-
|
|
|
- private static final Logger logger = LogManager.getLogger(EmailMessageBroker.class);
|
|
|
-
|
|
|
- private final EmailServerConnection serverConnection;
|
|
|
- private final EmailMessageBrokerConfig messageConfig;
|
|
|
-
|
|
|
- public EmailMessageBroker(EmailServerConnection serverConnection, EmailMessageBrokerConfig messageConfig) {
|
|
|
- this.serverConnection = serverConnection;
|
|
|
- this.messageConfig = messageConfig;
|
|
|
- }
|
|
|
-
|
|
|
- private String createMessage(Report report) {
|
|
|
- StringBuilder content = new StringBuilder();
|
|
|
- final String rowStyle = "border: 1px solid #dddddd; text-align: left; padding: 8px;";
|
|
|
-
|
|
|
- boolean isMessages = !report.getMessages().isEmpty();
|
|
|
- boolean isRecords = !report.getReports().isEmpty();
|
|
|
-
|
|
|
- if (!report.getOperationProperties().isEmpty()) {
|
|
|
-
|
|
|
- TableWriter tableSourceWriter = HtmlTableWriter.createWithHeader("width: 100%;", "background-color: #dddddd")
|
|
|
- .cell("Operation Type").cell("Operation Value").end();
|
|
|
-
|
|
|
- for (Map.Entry<String, String> operationEntry : report.getOperationProperties().entrySet()) {
|
|
|
- tableSourceWriter.row(rowStyle)
|
|
|
- .cell(operationEntry.getKey(), rowStyle)
|
|
|
- .cell(operationEntry.getValue(), rowStyle)
|
|
|
- .end();
|
|
|
- }
|
|
|
-
|
|
|
- content.append(tableSourceWriter.table()).append(BREAK_LINE);
|
|
|
- }
|
|
|
-
|
|
|
- if (isMessages || !isRecords) {
|
|
|
- TableWriter tableMsgWriter = HtmlTableWriter.createWithHeader("width: 100%;", "background-color: #dddddd")
|
|
|
- .cell("Messages").end();
|
|
|
-
|
|
|
- for (String message : report.getMessages()) {
|
|
|
- tableMsgWriter.row(rowStyle).cell(message, rowStyle).end();
|
|
|
- }
|
|
|
-
|
|
|
- if (!isRecords) {
|
|
|
- tableMsgWriter.row(rowStyle).cell("All received observations are valid.");
|
|
|
- }
|
|
|
-
|
|
|
- content.append(tableMsgWriter.table()).append(BREAK_LINE);
|
|
|
- }
|
|
|
-
|
|
|
- if (isRecords) {
|
|
|
- TableWriter tableReportWriter = HtmlTableWriter.createWithHeader("width: 100%;", "background-color: #dddddd")
|
|
|
- .cell("unitName (unitId)").cell("sensorName (sensorId)").cell("timestamp").cell("reported").cell("status").end();
|
|
|
-
|
|
|
- String reportedTime = report.getCreated().format(ofPattern("yyyy-MM-dd HH:mm:ss"));
|
|
|
-
|
|
|
- report.getReports().sort(Comparator.comparing(SimpleReport::getStatus).reversed());
|
|
|
- for (SimpleReport simpleReport : report.getReports()) {
|
|
|
- boolean isOk = simpleReport.getStatus().equals(StatusReport.OK);
|
|
|
- if (simpleReport.getRecord() instanceof ObservationInfo) {
|
|
|
- ObservationInfo observation = (ObservationInfo) simpleReport.getRecord();
|
|
|
- Source source = observation.getSource();
|
|
|
-
|
|
|
- String unitCell = String.format("%s (%s)", source.getUnit().getName(), source.getUnit().getId());
|
|
|
- String sensorCell = String.format("%s (%s)", source.getSensor().getName(), source.getSensor().getId());
|
|
|
-
|
|
|
- tableReportWriter.row(rowStyle + "background-color: " + (isOk ? "#CCFFCC" : "#FFCCCC"))
|
|
|
- .cell(unitCell, rowStyle)
|
|
|
- .cell(sensorCell, rowStyle)
|
|
|
- .cell(observation.getTimestamp().toString(), rowStyle)
|
|
|
- .cell(reportedTime, rowStyle)
|
|
|
- .cell(simpleReport.getStatus().name(), rowStyle)
|
|
|
- .end();
|
|
|
- }
|
|
|
- }
|
|
|
- content.append(tableReportWriter.table()).append(BREAK_LINE);
|
|
|
- }
|
|
|
-
|
|
|
- return content.toString();
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void send(Report report, MessageBrokerHandler status) {
|
|
|
- if (report == null) {
|
|
|
- logger.info("Nothing to send. The receive report is null.");
|
|
|
- status.handle(new MessageStatus(null, "No report to send.")); return;
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- logger.info("Sending a message via email.");
|
|
|
- serverConnection.send(createMessage(report), messageConfig);
|
|
|
- logger.info("The message was send successfully.");
|
|
|
- status.handle(new MessageStatus(report));
|
|
|
- } catch (MessagingException e) {
|
|
|
- logger.catching(e);
|
|
|
- status.handle(new MessageStatus(report, e.getMessage()));
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public MessageBrokerType getType() {
|
|
|
- return MessageBrokerType.EMAIL;
|
|
|
- }
|
|
|
-}
|
|
|
+package cz.senslog.watchdog.messagebroker.broker;
|
|
|
+
|
|
|
+import cz.senslog.watchdog.config.EmailMessageBrokerConfig;
|
|
|
+import cz.senslog.watchdog.config.MessageBrokerType;
|
|
|
+import cz.senslog.watchdog.core.connection.EmailServerConnection;
|
|
|
+import cz.senslog.watchdog.domain.*;
|
|
|
+import cz.senslog.watchdog.messagebroker.MessageBroker;
|
|
|
+import cz.senslog.watchdog.messagebroker.MessageBrokerHandler;
|
|
|
+import cz.senslog.watchdog.messagebroker.MessageStatus;
|
|
|
+import cz.senslog.watchdog.messagebroker.writer.HtmlTableWriter;
|
|
|
+import cz.senslog.watchdog.messagebroker.writer.TableWriter;
|
|
|
+import org.apache.logging.log4j.LogManager;
|
|
|
+import org.apache.logging.log4j.Logger;
|
|
|
+
|
|
|
+import javax.mail.*;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+import static java.time.format.DateTimeFormatter.ofPattern;
|
|
|
+
|
|
|
+public class EmailMessageBroker implements MessageBroker {
|
|
|
+
|
|
|
+ private static final String BREAK_LINE = "<br />";
|
|
|
+
|
|
|
+ private static final Logger logger = LogManager.getLogger(EmailMessageBroker.class);
|
|
|
+
|
|
|
+ private final EmailServerConnection serverConnection;
|
|
|
+ private final EmailMessageBrokerConfig messageConfig;
|
|
|
+
|
|
|
+ public EmailMessageBroker(EmailServerConnection serverConnection, EmailMessageBrokerConfig messageConfig) {
|
|
|
+ this.serverConnection = serverConnection;
|
|
|
+ this.messageConfig = messageConfig;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String createMessage(Report report) {
|
|
|
+ StringBuilder content = new StringBuilder();
|
|
|
+ final String rowStyle = "border: 1px solid #dddddd; text-align: left; padding: 8px;";
|
|
|
+
|
|
|
+ boolean isMessages = !report.getMessages().isEmpty();
|
|
|
+ boolean isRecords = !report.getReports().isEmpty();
|
|
|
+
|
|
|
+ if (!report.getOperationProperties().isEmpty()) {
|
|
|
+
|
|
|
+ TableWriter tableSourceWriter = HtmlTableWriter.createWithHeader("width: 100%;", "background-color: #dddddd")
|
|
|
+ .cell("Operation Type").cell("Operation Value").end();
|
|
|
+
|
|
|
+ for (Map.Entry<String, String> operationEntry : report.getOperationProperties().entrySet()) {
|
|
|
+ tableSourceWriter.row(rowStyle)
|
|
|
+ .cell(operationEntry.getKey(), rowStyle)
|
|
|
+ .cell(operationEntry.getValue(), rowStyle)
|
|
|
+ .end();
|
|
|
+ }
|
|
|
+
|
|
|
+ content.append(tableSourceWriter.table()).append(BREAK_LINE);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isMessages || !isRecords) {
|
|
|
+ TableWriter tableMsgWriter = HtmlTableWriter.createWithHeader("width: 100%;", "background-color: #dddddd")
|
|
|
+ .cell("Messages").end();
|
|
|
+
|
|
|
+ for (String message : report.getMessages()) {
|
|
|
+ tableMsgWriter.row(rowStyle).cell(message, rowStyle).end();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!isRecords) {
|
|
|
+ tableMsgWriter.row(rowStyle).cell("All received observations are valid.");
|
|
|
+ }
|
|
|
+
|
|
|
+ content.append(tableMsgWriter.table()).append(BREAK_LINE);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isRecords) {
|
|
|
+ TableWriter tableReportWriter = HtmlTableWriter.createWithHeader("width: 100%;", "background-color: #dddddd")
|
|
|
+ .cell("unitName (unitId)").cell("sensorName (sensorId)").cell("timestamp").cell("reported").cell("status").end();
|
|
|
+
|
|
|
+ String reportedTime = report.getCreated().format(ofPattern("yyyy-MM-dd HH:mm:ss"));
|
|
|
+
|
|
|
+ report.getReports().sort(Comparator.comparing(SimpleReport::getStatus).reversed());
|
|
|
+ for (SimpleReport simpleReport : report.getReports()) {
|
|
|
+ boolean isOk = simpleReport.getStatus().equals(StatusReport.OK);
|
|
|
+ if (simpleReport.getRecord() instanceof ObservationInfo) {
|
|
|
+ ObservationInfo observation = (ObservationInfo) simpleReport.getRecord();
|
|
|
+ Source source = observation.getSource();
|
|
|
+
|
|
|
+ String unitCell = String.format("%s (%s)", source.getUnit().getName(), source.getUnit().getId());
|
|
|
+ String sensorCell = String.format("%s (%s)", source.getSensor().getName(), source.getSensor().getId());
|
|
|
+
|
|
|
+ tableReportWriter.row(rowStyle + "background-color: " + (isOk ? "#CCFFCC" : "#FFCCCC"))
|
|
|
+ .cell(unitCell, rowStyle)
|
|
|
+ .cell(sensorCell, rowStyle)
|
|
|
+ .cell(observation.getTimestamp().toString(), rowStyle)
|
|
|
+ .cell(reportedTime, rowStyle)
|
|
|
+ .cell(simpleReport.getStatus().name(), rowStyle)
|
|
|
+ .end();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ content.append(tableReportWriter.table()).append(BREAK_LINE);
|
|
|
+ }
|
|
|
+
|
|
|
+ return content.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void send(Report report, MessageBrokerHandler status) {
|
|
|
+ if (report == null) {
|
|
|
+ logger.info("Nothing to send. The receive report is null.");
|
|
|
+ status.handle(new MessageStatus(null, "No report to send.")); return;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ logger.info("Sending a message via email.");
|
|
|
+ serverConnection.send(createMessage(report), messageConfig);
|
|
|
+ logger.info("The message was send successfully.");
|
|
|
+ status.handle(new MessageStatus(report));
|
|
|
+ } catch (MessagingException e) {
|
|
|
+ logger.catching(e);
|
|
|
+ status.handle(new MessageStatus(report, e.getMessage()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public MessageBrokerType getType() {
|
|
|
+ return MessageBrokerType.EMAIL;
|
|
|
+ }
|
|
|
+}
|