fix-tests-cleanup
This commit is contained in:
@@ -4,9 +4,9 @@ import javax.mail.Authenticator;
|
|||||||
import javax.mail.Message;
|
import javax.mail.Message;
|
||||||
import javax.mail.PasswordAuthentication;
|
import javax.mail.PasswordAuthentication;
|
||||||
import javax.mail.Session;
|
import javax.mail.Session;
|
||||||
import javax.mail.Transport;
|
|
||||||
import javax.mail.internet.InternetAddress;
|
import javax.mail.internet.InternetAddress;
|
||||||
import javax.mail.internet.MimeMessage;
|
import javax.mail.internet.MimeMessage;
|
||||||
|
import com.sun.mail.smtp.SMTPTransport;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import java.awt.SystemTray;
|
import java.awt.SystemTray;
|
||||||
@@ -17,24 +17,24 @@ import java.util.Properties;
|
|||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public record NotificationService(Config cfg) {
|
public record NotificationService(Config cfg) {
|
||||||
|
|
||||||
// Extra convenience constructor: raw string → Config
|
// Extra convenience constructor: raw string → Config
|
||||||
public NotificationService(String raw) {
|
public NotificationService(String raw) {
|
||||||
this(Config.parse(raw));
|
this(Config.parse(raw));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendNotification(String msg, String title, int prio) {
|
public void sendNotification(String msg, String title, int prio) {
|
||||||
if (cfg.useDesktop()) sendDesktop(title, msg, prio);
|
if (cfg.useDesktop()) sendDesktop(title, msg, prio);
|
||||||
if (cfg.useEmail()) sendEmail(title, msg, prio);
|
if (cfg.useEmail()) sendEmail(title, msg, prio);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendDesktop(String title, String msg, int prio) {
|
private void sendDesktop(String title, String msg, int prio) {
|
||||||
try {
|
try {
|
||||||
if (!SystemTray.isSupported()) {
|
if (!SystemTray.isSupported()) {
|
||||||
log.info("Desktop not supported: {}", title);
|
log.info("Desktop not supported: {}", title);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var tray = SystemTray.getSystemTray();
|
var tray = SystemTray.getSystemTray();
|
||||||
var icon = new TrayIcon(
|
var icon = new TrayIcon(
|
||||||
Toolkit.getDefaultToolkit().createImage(new byte[0]),
|
Toolkit.getDefaultToolkit().createImage(new byte[0]),
|
||||||
@@ -42,10 +42,10 @@ public record NotificationService(Config cfg) {
|
|||||||
);
|
);
|
||||||
icon.setImageAutoSize(true);
|
icon.setImageAutoSize(true);
|
||||||
tray.add(icon);
|
tray.add(icon);
|
||||||
|
|
||||||
var type = prio > 0 ? TrayIcon.MessageType.WARNING : TrayIcon.MessageType.INFO;
|
var type = prio > 0 ? TrayIcon.MessageType.WARNING : TrayIcon.MessageType.INFO;
|
||||||
icon.displayMessage(title, msg, type);
|
icon.displayMessage(title, msg, type);
|
||||||
|
|
||||||
// Remove tray icon asynchronously to avoid blocking the caller
|
// Remove tray icon asynchronously to avoid blocking the caller
|
||||||
int delayMs = Integer.getInteger("auctiora.desktop.delay.ms", 0);
|
int delayMs = Integer.getInteger("auctiora.desktop.delay.ms", 0);
|
||||||
if (delayMs <= 0) {
|
if (delayMs <= 0) {
|
||||||
@@ -77,8 +77,10 @@ public record NotificationService(Config cfg) {
|
|||||||
log.warn("Desktop failed: {}", e.getMessage());
|
log.warn("Desktop failed: {}", e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendEmail(String title, String msg, int prio) {
|
private void sendEmail(String title, String msg, int prio) {
|
||||||
|
SMTPTransport transport = null;
|
||||||
|
boolean sent = false;
|
||||||
try {
|
try {
|
||||||
var props = new Properties();
|
var props = new Properties();
|
||||||
props.put("mail.smtp.auth", "true");
|
props.put("mail.smtp.auth", "true");
|
||||||
@@ -88,42 +90,63 @@ public record NotificationService(Config cfg) {
|
|||||||
props.put("mail.smtp.port", "587");
|
props.put("mail.smtp.port", "587");
|
||||||
props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
|
props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
|
||||||
props.put("mail.smtp.ssl.protocols", "TLSv1.2");
|
props.put("mail.smtp.ssl.protocols", "TLSv1.2");
|
||||||
|
// Avoid waiting for QUIT reply (prevents "Exception reading response" after successful send)
|
||||||
|
props.put("mail.smtp.quitwait", "false");
|
||||||
|
|
||||||
// Connection timeouts (configurable; short during tests, longer otherwise)
|
// Connection timeouts (configurable; short during tests, longer otherwise)
|
||||||
int smtpTimeoutMs = Integer.getInteger("auctiora.smtp.timeout.ms", isUnderTest() ? 200 : 10000);
|
int smtpTimeoutMs = Integer.getInteger("auctiora.smtp.timeout.ms", isUnderTest() ? 200 : 10000);
|
||||||
String t = String.valueOf(smtpTimeoutMs);
|
String t = String.valueOf(smtpTimeoutMs);
|
||||||
props.put("mail.smtp.connectiontimeout", t);
|
props.put("mail.smtp.connectiontimeout", t);
|
||||||
props.put("mail.smtp.timeout", t);
|
props.put("mail.smtp.timeout", t);
|
||||||
props.put("mail.smtp.writetimeout", t);
|
props.put("mail.smtp.writetimeout", t);
|
||||||
|
|
||||||
var session = Session.getInstance(props, new Authenticator() {
|
var session = Session.getInstance(props, new Authenticator() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected PasswordAuthentication getPasswordAuthentication() {
|
protected PasswordAuthentication getPasswordAuthentication() {
|
||||||
return new PasswordAuthentication(cfg.smtpUsername(), cfg.smtpPassword());
|
return new PasswordAuthentication(cfg.smtpUsername(), cfg.smtpPassword());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var m = new MimeMessage(session);
|
var m = new MimeMessage(session);
|
||||||
m.setFrom(new InternetAddress(cfg.smtpUsername()));
|
m.setFrom(new InternetAddress(cfg.smtpUsername()));
|
||||||
m.setRecipients(Message.RecipientType.TO, InternetAddress.parse(cfg.toEmail()));
|
m.setRecipients(Message.RecipientType.TO, InternetAddress.parse(cfg.toEmail()));
|
||||||
m.setSubject("[Troostwijk] " + title);
|
m.setSubject("[Troostwijk] " + title);
|
||||||
m.setText(msg);
|
m.setText(msg);
|
||||||
m.setSentDate(new Date());
|
m.setSentDate(new Date());
|
||||||
|
|
||||||
if (prio > 0) {
|
if (prio > 0) {
|
||||||
m.setHeader("X-Priority", "1");
|
m.setHeader("X-Priority", "1");
|
||||||
m.setHeader("Importance", "High");
|
m.setHeader("Importance", "High");
|
||||||
}
|
}
|
||||||
|
|
||||||
Transport.send(m);
|
transport = (SMTPTransport) session.getTransport("smtp");
|
||||||
|
transport.connect("smtp.gmail.com", cfg.smtpUsername(), cfg.smtpPassword());
|
||||||
|
transport.sendMessage(m, m.getAllRecipients());
|
||||||
|
sent = true;
|
||||||
|
|
||||||
log.info("Email notification sent: {}", title);
|
log.info("Email notification sent: {}", title);
|
||||||
|
String resp = transport.getLastServerResponse();
|
||||||
|
if (resp != null) {
|
||||||
|
log.debug("SMTP response: {}", resp);
|
||||||
|
}
|
||||||
} catch (javax.mail.AuthenticationFailedException e) {
|
} catch (javax.mail.AuthenticationFailedException e) {
|
||||||
log.warn("Email authentication failed - check Gmail App Password: {}", e.getMessage());
|
log.warn("Email authentication failed - check Gmail App Password: {}", e.getMessage());
|
||||||
} catch (javax.mail.MessagingException e) {
|
} catch (javax.mail.MessagingException e) {
|
||||||
log.warn("Email connection failed (network/firewall issue?): {}", e.getMessage());
|
if (sent) {
|
||||||
|
// Message accepted by server; subsequent connection close caused exception
|
||||||
|
log.info("Email sent (connection closed early): {}", e.getMessage());
|
||||||
|
} else {
|
||||||
|
log.warn("Email connection failed (network/firewall issue?): {}", e.getMessage());
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("Email failed: {}", e.getMessage());
|
log.warn("Email failed: {}", e.getMessage());
|
||||||
|
} finally {
|
||||||
|
if (transport != null) {
|
||||||
|
try {
|
||||||
|
transport.close();
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user