email sender

This commit is contained in:
Michele Artini 2023-09-22 14:09:58 +02:00
parent 01d54a3075
commit 71e268710a
4 changed files with 126 additions and 11 deletions

View File

@ -70,6 +70,12 @@
<artifactId>httpclient5</artifactId>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

View File

@ -104,20 +104,21 @@ public class CollectorService {
info.setExecutionStatus(ExecutionStatus.COMPLETED);
info.setEnd(now);
info.setExpirationDate(now.plusHours(executionDuration));
emailSender.notifySuccess(info);
} catch (final Throwable e) {
info.setExecutionStatus(ExecutionStatus.FAILED);
info.setMessage(e.getMessage() + ": " + ExceptionUtils.getStackTrace(e));
info.setEnd(LocalDateTime.now());
emailSender.notifyFailure(info);
} finally {
sc.complete();
if (StringUtils.isNotBlank(publicBasePath)) {
info.setPublicUrl(publicBasePath + "/" + jobId);
}
emailSender.notifySuccess(info);
} catch (final Throwable e) {
info.setExecutionStatus(ExecutionStatus.FAILED);
info.setMessage(e.getMessage() + ": " + ExceptionUtils.getStackTrace(e));
info.setEnd(LocalDateTime.now());
emailSender.notifyFailure(info);
} finally {
collectionInfoRepository.save(info);
sc.complete();
}
});
@ -210,7 +211,7 @@ public class CollectorService {
}
public static void cleanCollectedData(final String storageUrl) throws URISyntaxException {
log.info("[CLEAN] - Deleting: " + storageUrl);
log.info("[CLEAN] Deleting expired storage: " + storageUrl);
final URI uri = new URI(storageUrl);
final String protocol = uri.getScheme();

View File

@ -1,6 +1,24 @@
package eu.dnetlib.apps.oai.utils;
import java.util.Date;
import java.util.Properties;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import javax.annotation.PostConstruct;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import eu.dnetlib.apps.oai.model.CollectionInfo;
@ -8,6 +26,50 @@ import eu.dnetlib.apps.oai.model.CollectionInfo;
@Service
public class EmailSender {
private static final Log log = LogFactory.getLog(EmailSender.class);
private final BlockingQueue<Message> queue = new LinkedBlockingQueue<>();
@Value("${oai.conf.notification.sender.name}")
private String fromName;
@Value("${oai.conf.notification.sender.email}")
private String fromEmail;
@Value("${oai.conf.notification.smtp.host}")
private String smtpHost;
@Value("${oai.conf.notification.smtp.host}")
private long smtpPort;
@Value("${oai.conf.notification.smtp.user}")
private String smtpUser;
@Value("${oai.conf.notification.smtp.password}")
private String smtpPassword;
@PostConstruct
private void init() {
new Thread(() -> {
while (true) {
try {
final Message message = this.queue.take();
if (message != null) {
try {
log.info("Sending mail...");
Transport.send(message);
log.info("...sent");
} catch (final MessagingException e) {
log.error("Error sending email", e);
}
}
} catch (final InterruptedException e1) {
throw new RuntimeException(e1);
}
}
}).start();
}
public void notifySuccess(final CollectionInfo info) {
if (StringUtils.isNotBlank(info.getNotificationEmail())) {
sendMail(info.getNotificationEmail(), "OAI Harvesting completed", prepareSuccessMessage(info));
@ -30,8 +92,48 @@ public class EmailSender {
return null;
}
private void sendMail(final String to, final String subject, final String message) {
// TODO Auto-generated method stub
public void sendMail(final String to, final String subject, final String message) {
try {
final Session session = Session.getInstance(obtainProperties(), obtainAuthenticator());
final MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress(fromEmail, fromName));
mimeMessage.setSubject(subject);
mimeMessage.setContent(message, "text/html; charset=utf-8");
mimeMessage.setSentDate(new Date());
mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
this.queue.add(mimeMessage);
log.info("Mail to " + to + " in queue");
} catch (final Exception e) {
log.error("Error sending mail", e);
}
}
private Properties obtainProperties() {
final Properties p = new Properties();
p.put("mail.transport.protocol", "smtp");
p.put("mail.smtp.host", smtpHost);
p.put("mail.smtp.port", smtpPort);
p.put("mail.smtp.auth", Boolean.toString(StringUtils.isNotBlank(smtpUser)));
return p;
}
private Authenticator obtainAuthenticator() {
if (StringUtils.isBlank(smtpUser)) { return null; }
return new Authenticator() {
private final PasswordAuthentication authentication = new PasswordAuthentication(smtpUser, smtpPassword);
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return authentication;
}
};
}
}

View File

@ -27,4 +27,10 @@ oai.conf.maxRecords = 1000
oai.conf.enable.export.api = true
oai.conf.notification.sender.name = OAI Collector Service
oai.conf.notification.sender.email = noreply@openaire.eu
oai.conf.notification.smtp.host = localhost
oai.conf.notification.smtp.host = 587
oai.conf.notification.smtp.user =
oai.conf.notification.smtp.password =