|
@@ -2,31 +2,33 @@ package cz.senslog.watchdog.messagebroker;
|
|
|
|
|
|
|
|
import cz.senslog.watchdog.config.EmailMessageBrokerConfig;
|
|
import cz.senslog.watchdog.config.EmailMessageBrokerConfig;
|
|
|
import cz.senslog.watchdog.config.MessageBrokerType;
|
|
import cz.senslog.watchdog.config.MessageBrokerType;
|
|
|
|
|
+import cz.senslog.watchdog.provider.Record;
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
import org.apache.logging.log4j.Logger;
|
|
import org.apache.logging.log4j.Logger;
|
|
|
|
|
|
|
|
import javax.mail.*;
|
|
import javax.mail.*;
|
|
|
import javax.mail.internet.*;
|
|
import javax.mail.internet.*;
|
|
|
-import java.time.LocalDateTime;
|
|
|
|
|
-import java.util.HashMap;
|
|
|
|
|
-import java.util.Map;
|
|
|
|
|
-import java.util.Properties;
|
|
|
|
|
|
|
+import java.time.Instant;
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
|
|
|
import static javax.mail.Message.RecipientType.TO;
|
|
import static javax.mail.Message.RecipientType.TO;
|
|
|
|
|
|
|
|
public class EmailMessageBroker implements MessageBroker {
|
|
public class EmailMessageBroker implements MessageBroker {
|
|
|
|
|
|
|
|
|
|
+ private static final String BREAK_LINE = "<br />";
|
|
|
|
|
+
|
|
|
private static final Logger logger = LogManager.getLogger(EmailMessageBroker.class);
|
|
private static final Logger logger = LogManager.getLogger(EmailMessageBroker.class);
|
|
|
|
|
|
|
|
private final EmailMessageBrokerConfig config;
|
|
private final EmailMessageBrokerConfig config;
|
|
|
private final Session session;
|
|
private final Session session;
|
|
|
|
|
|
|
|
- private final Map<String, LocalDateTime> sendMessages;
|
|
|
|
|
|
|
+ private final Set<Record> sendRecords;
|
|
|
|
|
+
|
|
|
|
|
|
|
|
public EmailMessageBroker(EmailMessageBrokerConfig config) {
|
|
public EmailMessageBroker(EmailMessageBrokerConfig config) {
|
|
|
this.config = config;
|
|
this.config = config;
|
|
|
this.session = openSession(config);
|
|
this.session = openSession(config);
|
|
|
- this.sendMessages = new HashMap<>();
|
|
|
|
|
|
|
+ this.sendRecords = new HashSet<>();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private Session openSession(EmailMessageBrokerConfig config) {
|
|
private Session openSession(EmailMessageBrokerConfig config) {
|
|
@@ -45,14 +47,27 @@ public class EmailMessageBroker implements MessageBroker {
|
|
|
return Session.getInstance(props, auth);
|
|
return Session.getInstance(props, auth);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private Message createMessage(String text) throws MessagingException {
|
|
|
|
|
|
|
+ private Message createMessage(Instant created, List<Record> records) throws MessagingException {
|
|
|
Message message = new MimeMessage(session);
|
|
Message message = new MimeMessage(session);
|
|
|
message.setFrom(new InternetAddress(config.getSender()));
|
|
message.setFrom(new InternetAddress(config.getSender()));
|
|
|
message.setRecipients(TO, InternetAddress.parse(config.getRecipient()));
|
|
message.setRecipients(TO, InternetAddress.parse(config.getRecipient()));
|
|
|
message.setSubject(config.getSubject());
|
|
message.setSubject(config.getSubject());
|
|
|
|
|
|
|
|
MimeBodyPart mimeBodyPart = new MimeBodyPart();
|
|
MimeBodyPart mimeBodyPart = new MimeBodyPart();
|
|
|
- mimeBodyPart.setContent(text, "text/html");
|
|
|
|
|
|
|
+ StringBuilder content = new StringBuilder();
|
|
|
|
|
+ content.append(String.format("Data reported at the time <b>%s</b>", created))
|
|
|
|
|
+ .append(BREAK_LINE).append(BREAK_LINE);
|
|
|
|
|
+
|
|
|
|
|
+ content.append("<b>") // TODO refactor
|
|
|
|
|
+ .append("unitId").append(";")
|
|
|
|
|
+ .append("sensorId").append(";")
|
|
|
|
|
+ .append("lastObservation")
|
|
|
|
|
+ .append("</b>").append(BREAK_LINE);
|
|
|
|
|
+
|
|
|
|
|
+ for (Record record : records) {
|
|
|
|
|
+ content.append(record.getMessage()).append(BREAK_LINE);
|
|
|
|
|
+ }
|
|
|
|
|
+ mimeBodyPart.setContent(content.toString(), "text/html");
|
|
|
|
|
|
|
|
Multipart multipart = new MimeMultipart();
|
|
Multipart multipart = new MimeMultipart();
|
|
|
multipart.addBodyPart(mimeBodyPart);
|
|
multipart.addBodyPart(mimeBodyPart);
|
|
@@ -61,35 +76,60 @@ public class EmailMessageBroker implements MessageBroker {
|
|
|
return message;
|
|
return message;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private List<Record> prepareRecords(Report report) {
|
|
|
|
|
+ List<Record> readyToSend = new ArrayList<>();
|
|
|
|
|
+ for (Record record : report.getRecords()) {
|
|
|
|
|
+ if (!sendRecords.contains(record)) {
|
|
|
|
|
+ readyToSend.add(record);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return readyToSend;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
- public void send(String text, MessageBrokerHandler status) {
|
|
|
|
|
- if (sendMessages.containsKey(text)) {
|
|
|
|
|
- logger.info("The message '{}' was already send at {}.", text, sendMessages.get(text));
|
|
|
|
|
- status.handle(new MessageStatus(text)); return;
|
|
|
|
|
|
|
+ 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;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ List<Record> recordsToSend = prepareRecords(report);
|
|
|
|
|
+ if (recordsToSend.isEmpty()) {
|
|
|
|
|
+ logger.info("The same report was already send.");
|
|
|
|
|
+ status.handle(new MessageStatus(report)); return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
- Message message = createMessage(text);
|
|
|
|
|
|
|
+ Message message = createMessage(report.getCreated(), recordsToSend);
|
|
|
logger.info("Sending a message via email.");
|
|
logger.info("Sending a message via email.");
|
|
|
Transport.send(message);
|
|
Transport.send(message);
|
|
|
logger.info("The message was send successfully.");
|
|
logger.info("The message was send successfully.");
|
|
|
- sendMessages.put(text, LocalDateTime.now());
|
|
|
|
|
- status.handle(new MessageStatus(text));
|
|
|
|
|
|
|
+ sendRecords.addAll(recordsToSend);
|
|
|
|
|
+ status.handle(new MessageStatus(report));
|
|
|
} catch (MessagingException e) {
|
|
} catch (MessagingException e) {
|
|
|
logger.catching(e);
|
|
logger.catching(e);
|
|
|
- status.handle(new MessageStatus(text, e.getMessage()));
|
|
|
|
|
|
|
+ status.handle(new MessageStatus(report, e.getMessage()));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- LocalDateTime deadline = LocalDateTime.now().minusDays(1);
|
|
|
|
|
- for (Map.Entry<String, LocalDateTime> entry : sendMessages.entrySet()) {
|
|
|
|
|
- if (entry.getValue().isBefore(deadline)) {
|
|
|
|
|
- sendMessages.remove(entry.getKey());
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ vanish();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public MessageBrokerType getType() {
|
|
public MessageBrokerType getType() {
|
|
|
return MessageBrokerType.EMAIL;
|
|
return MessageBrokerType.EMAIL;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ public Record[] vanish() {
|
|
|
|
|
+ Instant deadline = Instant.now().minusSeconds(60 * 60 * 24 * config.getVanishPeriodDays());
|
|
|
|
|
+ List<Record> deletedRecords = new ArrayList<>();
|
|
|
|
|
+ Iterator<Record> iterator = sendRecords.iterator();
|
|
|
|
|
+ while(iterator.hasNext()) {
|
|
|
|
|
+ Record record = iterator.next();
|
|
|
|
|
+ if (record.getTimestamp().isBefore(deadline)) {
|
|
|
|
|
+ deletedRecords.add(record);
|
|
|
|
|
+ iterator.remove();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return deletedRecords.toArray(new Record[0]);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|