Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Phoebus Olog Email Notifier Module

This is an optional module for the Phoebus Olog electronic logbook service, see https://github.com/Olog/phoebus-olog. Its purpose is to send an email when a log is sent based on a list of emails mapped to the logbook entry's tag.
This is an optional module for the Phoebus Olog electronic logbook service, see https://github.com/Olog/phoebus-olog. Its purpose is to send an email when a log is sent based on a list of emails mapped to the logbook entry's tag.

The module uses Simple Java Mail to send emails (Email java library: https://www.simplejavamail.org/).

Expand All @@ -9,36 +9,46 @@ The module uses Simple Java Mail to send emails (Email java library: https://www
#### Add these to phoebus olog:

pom.xml
```

```xml
<!-- Email Notifier Module -->
<dependency>
<groupId>org.phoebus</groupId>
<artifactId>olog-email-notifier-module</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>jakarta.mail</artifactId>
<version>2.0.1</version>
<groupId>jakarta.mail</groupId>
<artifactId>jakarta.mail-api</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>org.eclipse.angus</groupId>
<artifactId>angus-mail</artifactId>
<version>2.0.3</version>
</dependency>
```

Example tagsToEmails.json
```

```json
{
"tagsToEmails":
"tagsToEmails":
{
"tag1": ["email1@example.com"],
"tag2": ["email2@example.com", "email3@example.com"]
}
}
}
```

Add the following to olog application.properties
Properties file:

```
olog.tagsToEmailsFilePath=/pathToTagsToEmails.json

Expand All @@ -49,4 +59,4 @@ simplejavamail.defaults.from.name=From Default
simplejavamail.defaults.from.address=from@default.com
```

See: https://www.simplejavamail.org/configuration.html#section-available-properties for more simplejavamail properties
See: https://www.simplejavamail.org/configuration.html#section-available-properties for more simplejavamail properties
56 changes: 27 additions & 29 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.phoebus</groupId>
<artifactId>olog-email-notifier-module</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.2.1</version>
<name>Olog Email Notifier Module</name>


Expand All @@ -19,28 +19,28 @@
<dependency>
<groupId>org.phoebus</groupId>
<artifactId>service-olog</artifactId>
<version>4.7.7</version>
<version>5.1.2</version>
</dependency>
<dependency>
<groupId>org.simplejavamail</groupId>
<artifactId>simple-java-mail</artifactId>
<version>8.11.2</version>
<groupId>org.simplejavamail</groupId>
<artifactId>simple-java-mail</artifactId>
<version>8.11.2</version>
</dependency>
<dependency>
<groupId>org.simplejavamail</groupId>
<artifactId>spring-module</artifactId>
<version>8.11.2</version>
<groupId>org.simplejavamail</groupId>
<artifactId>spring-module</artifactId>
<version>8.11.2</version>
</dependency>
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>2.1.3</version>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>jakarta.mail</artifactId>
<version>2.0.1</version>
</dependency>
<groupId>org.eclipse.angus</groupId>
<artifactId>angus-mail</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
Expand All @@ -60,13 +60,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.12.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>5.2.0</version>
<version>5.21.0</version>
<scope>test</scope>
</dependency>

Expand All @@ -87,14 +81,17 @@
</dependencies>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<pluginManagement> <!-- lock down plugins versions to avoid using Maven defaults (may be moved to
parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<!-- clean lifecycle, see
https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<!-- default lifecycle, jar packaging: see
https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
Expand All @@ -119,7 +116,8 @@
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<!-- site lifecycle, see
https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
Expand All @@ -131,4 +129,4 @@
</plugins>
</pluginManagement>
</build>
</project>
</project>
90 changes: 65 additions & 25 deletions src/main/java/org/phoebus/olog/email/EmailNotifier.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
package org.phoebus.olog.email;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.SortedSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import org.phoebus.olog.entity.Attachment;
import org.phoebus.olog.entity.Log;
import org.phoebus.olog.entity.State;
import org.phoebus.olog.entity.Tag;
import org.phoebus.olog.notification.LogEntryNotifier;
import org.simplejavamail.api.email.AttachmentResource;
import org.simplejavamail.api.email.Email;
import org.simplejavamail.api.mailer.Mailer;
import org.simplejavamail.config.ConfigLoader;
Expand All @@ -14,30 +30,23 @@

import com.google.auto.service.AutoService;

import org.phoebus.olog.entity.Tag;
import org.phoebus.olog.entity.State;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import jakarta.activation.DataSource;
import jakarta.mail.util.ByteArrayDataSource;

@AutoService(LogEntryNotifier.class)
@Component
@Import(SimpleJavaMailSpringSupport.class)
public class EmailNotifier implements LogEntryNotifier {

private Logger logger = Logger.getLogger(EmailNotifier.class.getName());

@Autowired
private TagEmailMapPreferences tagEmailMapPreferences;

@Autowired
private Mailer mailer;

private Map<String,List<String>> tagEmailMap;
private Map<String, List<String>> tagEmailMap = new HashMap<>();

@Override
public void notify(Log log) {
Expand All @@ -46,9 +55,34 @@ public void notify(Log log) {
List<String> tagNames = new ArrayList<>();

String logTitle = log.getTitle();
String owner = log.getOwner();
String senderEmail = ConfigLoader.getStringProperty(Property.DEFAULT_FROM_ADDRESS);
String senderName = ConfigLoader.getStringProperty(Property.DEFAULT_FROM_NAME);

SortedSet<Attachment> logAttachments = log.getAttachments();
logger.log(Level.INFO, "Retrieved " + logAttachments.size() + " log attachments");
List<AttachmentResource> attachmentResources = logAttachments.stream()
.map(attachment -> {
logger.log(Level.INFO, "Processing attachment: " + attachment.getFilename());
String fname = attachment.getFilename();
String description = attachment.getFileMetadataDescription();
DataSource dataSource;
try (InputStream attachmentStream = attachment.getAttachment().getInputStream()) {
dataSource = new ByteArrayDataSource(attachmentStream, "application/octet-stream");
} catch (Exception e) {
logger.log(Level.SEVERE, "Error reading attachment stream", e);
return null;
}
AttachmentResource resource = new AttachmentResource(fname, dataSource, description);
logger.log(Level.INFO, "Retrieved attachment resource: " + fname);
return resource;
})
.filter(Objects::nonNull)
.collect(Collectors.toList());
if (attachmentResources.size() != logAttachments.size()) {
logger.log(Level.SEVERE, "Failed to read some attachments");
}

if (senderEmail == null) {
logger.log(Level.WARNING, "Default sender email was not set in properties file");
senderEmail = "olog-email@email.com";
Expand All @@ -58,15 +92,15 @@ public void notify(Log log) {
senderName = "olog emailer";
}

logger.log(Level.INFO,"Email From " + senderName + " " + senderEmail);
logger.log(Level.INFO,"Logbook Email Started");
logger.log(Level.INFO, "Email From " + senderName + " " + senderEmail);
logger.log(Level.INFO, "Logbook Email Started");

// Get Tag to Email Map
try {
tagEmailMap = tagEmailMapPreferences.tagEmailMap();
} catch (Exception e) {
logger.log(Level.SEVERE, "There was an error reading tag email JSON", e);
}
}

// Send Email
try {
Expand All @@ -75,10 +109,14 @@ public void notify(Log log) {
if (tag.getState() == State.Active) {
String tagName = tag.getName();
tagNames.add(tagName);
emailList.addAll(tagEmailMap.get(tagName));
if (tagEmailMap.get(tagName) != null) {
emailList.addAll(tagEmailMap.get(tagName));
}
}
}
Email logbookEmail = createLogEmail(emailList, senderName, senderEmail, logTitle, log.getDescription(), tagNames);
Email logbookEmail = createLogEmail(emailList, senderName, senderEmail, owner, logTitle,
log.getDescription(),
tagNames, attachmentResources);
if (!emailList.isEmpty()) {
logger.log(Level.INFO, "Email being sent to: " + emailList);
mailer.sendMail(logbookEmail);
Expand All @@ -90,13 +128,15 @@ public void notify(Log log) {
}
}

public Email createLogEmail(List<String> emailList, String senderName, String senderEmail, String subject, String body, List<String> tagNames) {
public Email createLogEmail(List<String> emailList, String senderName, String senderEmail, String owner,
String subject, String body, List<String> tagNames, List<AttachmentResource> attachmentList) {
return EmailBuilder.startingBlank()
.from(senderName , senderEmail)
.toMultiple(emailList)
.withSubject(subject)
.withPlainText(tagNames.toString())
.withPlainText(body)
.buildEmail();
.from(senderName + " [" + owner + "]", senderEmail)
.toMultiple(emailList)
.withSubject(subject)
.withPlainText(tagNames.toString())
.withPlainText(body)
.withAttachments(attachmentList)
.buildEmail();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.phoebus.olog.email;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;
import com.fasterxml.jackson.databind.ObjectMapper;

/** Preference settings for tag to email list
Expand Down
Loading