From a6ac33d8eddb8a7d8e3130e60d93c7908e3322c6 Mon Sep 17 00:00:00 2001 From: dankito Date: Thu, 21 Nov 2024 15:53:30 +0100 Subject: [PATCH] Extracted connect() --- .../net/codinux/invoicing/mail/MailReader.kt | 45 +++++++++++-------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/e-invoicing-domain/src/main/kotlin/net/codinux/invoicing/mail/MailReader.kt b/e-invoicing-domain/src/main/kotlin/net/codinux/invoicing/mail/MailReader.kt index 95cdf43..9a741dc 100644 --- a/e-invoicing-domain/src/main/kotlin/net/codinux/invoicing/mail/MailReader.kt +++ b/e-invoicing-domain/src/main/kotlin/net/codinux/invoicing/mail/MailReader.kt @@ -4,6 +4,7 @@ import jakarta.mail.BodyPart import jakarta.mail.Folder import jakarta.mail.Part import jakarta.mail.Session +import jakarta.mail.Store import jakarta.mail.internet.MimeMultipart import net.codinux.invoicing.model.Invoice import net.codinux.invoicing.reader.EInvoiceReader @@ -21,17 +22,12 @@ class MailReader( fun listAllMessagesWithEInvoice(account: MailAccount): List { - val properties = mapAccountToJavaMailProperties(account) - try { - val session = Session.getInstance(properties) - session.getStore("imap").use { store -> - store.connect(account.serverAddress, account.username, account.password) - + connect(account) { store -> val inbox = store.getFolder("INBOX") inbox.open(Folder.READ_ONLY) - return listAllMessagesWithEInvoiceInFolder(inbox).also { + listAllMessagesWithEInvoiceInFolder(inbox).also { inbox.close(false) } } @@ -42,18 +38,6 @@ class MailReader( return emptyList() } - private fun mapAccountToJavaMailProperties(account: MailAccount) = Properties().apply { - put("mail.store.protocol", "imap") - - put("mail.imap.host", account.serverAddress) - put("mail.imap.port", account.port?.toString() ?: "993") // Default IMAP over SSL - put("mail.imap.ssl.enable", "true") - - put("mail.imap.connectiontimeout", "5000") - put("mail.imap.timeout", "5000") - } - - private fun listAllMessagesWithEInvoiceInFolder(folder: Folder): List = folder.messages.mapNotNull { message -> try { if (message.isMimeType("multipart/*")) { @@ -121,4 +105,27 @@ class MailReader( private fun map(date: Date): LocalDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate() + + private fun connect(account: MailAccount, connected: (Store) -> T?): T? { + val properties = mapAccountToJavaMailProperties(account) + + val session = Session.getInstance(properties) + session.getStore("imap").use { store -> + store.connect(account.serverAddress, account.username, account.password) + + return connected(store) + } + } + + private fun mapAccountToJavaMailProperties(account: MailAccount) = Properties().apply { + put("mail.store.protocol", "imap") + + put("mail.imap.host", account.serverAddress) + put("mail.imap.port", account.port?.toString() ?: "993") // Default IMAP over SSL + put("mail.imap.ssl.enable", "true") + + put("mail.imap.connectiontimeout", "5000") + put("mail.imap.timeout", "5000") + } + } \ No newline at end of file