fix-tests-cleanup
This commit is contained in:
@@ -4,9 +4,9 @@ import javax.mail.Authenticator;
|
||||
import javax.mail.Message;
|
||||
import javax.mail.PasswordAuthentication;
|
||||
import javax.mail.Session;
|
||||
import javax.mail.Transport;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import com.sun.mail.smtp.SMTPTransport;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.awt.SystemTray;
|
||||
@@ -17,24 +17,24 @@ import java.util.Properties;
|
||||
|
||||
@Slf4j
|
||||
public record NotificationService(Config cfg) {
|
||||
|
||||
|
||||
// Extra convenience constructor: raw string → Config
|
||||
public NotificationService(String raw) {
|
||||
this(Config.parse(raw));
|
||||
}
|
||||
|
||||
|
||||
public void sendNotification(String msg, String title, int prio) {
|
||||
if (cfg.useDesktop()) sendDesktop(title, msg, prio);
|
||||
if (cfg.useEmail()) sendEmail(title, msg, prio);
|
||||
}
|
||||
|
||||
|
||||
private void sendDesktop(String title, String msg, int prio) {
|
||||
try {
|
||||
if (!SystemTray.isSupported()) {
|
||||
log.info("Desktop not supported: {}", title);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
var tray = SystemTray.getSystemTray();
|
||||
var icon = new TrayIcon(
|
||||
Toolkit.getDefaultToolkit().createImage(new byte[0]),
|
||||
@@ -42,10 +42,10 @@ public record NotificationService(Config cfg) {
|
||||
);
|
||||
icon.setImageAutoSize(true);
|
||||
tray.add(icon);
|
||||
|
||||
|
||||
var type = prio > 0 ? TrayIcon.MessageType.WARNING : TrayIcon.MessageType.INFO;
|
||||
icon.displayMessage(title, msg, type);
|
||||
|
||||
|
||||
// Remove tray icon asynchronously to avoid blocking the caller
|
||||
int delayMs = Integer.getInteger("auctiora.desktop.delay.ms", 0);
|
||||
if (delayMs <= 0) {
|
||||
@@ -77,8 +77,10 @@ public record NotificationService(Config cfg) {
|
||||
log.warn("Desktop failed: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void sendEmail(String title, String msg, int prio) {
|
||||
SMTPTransport transport = null;
|
||||
boolean sent = false;
|
||||
try {
|
||||
var props = new Properties();
|
||||
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.ssl.trust", "smtp.gmail.com");
|
||||
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)
|
||||
int smtpTimeoutMs = Integer.getInteger("auctiora.smtp.timeout.ms", isUnderTest() ? 200 : 10000);
|
||||
String t = String.valueOf(smtpTimeoutMs);
|
||||
props.put("mail.smtp.connectiontimeout", t);
|
||||
props.put("mail.smtp.timeout", t);
|
||||
props.put("mail.smtp.writetimeout", t);
|
||||
|
||||
|
||||
var session = Session.getInstance(props, new Authenticator() {
|
||||
|
||||
@Override
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(cfg.smtpUsername(), cfg.smtpPassword());
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
var m = new MimeMessage(session);
|
||||
m.setFrom(new InternetAddress(cfg.smtpUsername()));
|
||||
m.setRecipients(Message.RecipientType.TO, InternetAddress.parse(cfg.toEmail()));
|
||||
m.setSubject("[Troostwijk] " + title);
|
||||
m.setText(msg);
|
||||
m.setSentDate(new Date());
|
||||
|
||||
|
||||
if (prio > 0) {
|
||||
m.setHeader("X-Priority", "1");
|
||||
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);
|
||||
String resp = transport.getLastServerResponse();
|
||||
if (resp != null) {
|
||||
log.debug("SMTP response: {}", resp);
|
||||
}
|
||||
} catch (javax.mail.AuthenticationFailedException e) {
|
||||
log.warn("Email authentication failed - check Gmail App Password: {}", e.getMessage());
|
||||
} 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) {
|
||||
log.warn("Email failed: {}", e.getMessage());
|
||||
} finally {
|
||||
if (transport != null) {
|
||||
try {
|
||||
transport.close();
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user