From a078b8bf661dacf8619b23b4643f046fffe1a549 Mon Sep 17 00:00:00 2001 From: dankito Date: Tue, 26 Nov 2024 06:10:01 +0100 Subject: [PATCH] Implemented configuring connect timeout --- .../net/codinux/invoicing/email/EmailsFetcher.kt | 15 ++++++++------- .../codinux/invoicing/email/FetchEmailsOptions.kt | 5 +++-- .../invoicing/email/ListenForNewMailsOptions.kt | 4 +++- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/EmailsFetcher.kt b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/EmailsFetcher.kt index ac80653..3779c9d 100644 --- a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/EmailsFetcher.kt +++ b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/EmailsFetcher.kt @@ -43,7 +43,7 @@ open class EmailsFetcher( open fun listenForNewEmails(account: EmailAccount, options: ListenForNewMailsOptions) = runBlocking { try { - connect(account) { store -> + connect(account, options) { store -> val folder = store.getFolder(options.emailFolderName) as IMAPFolder folder.open(Folder.READ_ONLY) @@ -89,7 +89,7 @@ open class EmailsFetcher( open fun fetchAllEmails(account: EmailAccount, options: FetchEmailsOptions = FetchEmailsOptions()): FetchEmailsResult { try { - return connect(account) { store -> + return connect(account, options) { store -> val inbox = store.getFolder(options.emailFolderName) inbox.open(Folder.READ_ONLY) @@ -274,8 +274,8 @@ open class EmailsFetcher( date.toInstant() - protected open fun connect(account: EmailAccount, connected: (Store) -> T): T { - val properties = mapAccountToJavaMailProperties(account) + protected open fun connect(account: EmailAccount, options: FetchEmailsOptions, connected: (Store) -> T): T { + val properties = mapAccountToJavaMailProperties(account, options) val session = Session.getInstance(properties) session.getStore("imap").use { store -> @@ -285,15 +285,16 @@ open class EmailsFetcher( } } - protected open fun mapAccountToJavaMailProperties(account: EmailAccount) = Properties().apply { + protected open fun mapAccountToJavaMailProperties(account: EmailAccount, options: FetchEmailsOptions) = 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", "10000") - put("mail.imap.timeout", "10000") + val timeout = (options.connectTimeoutSeconds * 1000).toString() + put("mail.imap.connectiontimeout", timeout) + put("mail.imap.timeout", timeout) } } \ No newline at end of file diff --git a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsOptions.kt b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsOptions.kt index a449cf8..bb62191 100644 --- a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsOptions.kt +++ b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsOptions.kt @@ -2,7 +2,6 @@ package net.codinux.invoicing.email import net.codinux.invoicing.email.model.Email import java.io.File -import java.nio.file.Files open class FetchEmailsOptions( val downloadMessageBody: Boolean = false, @@ -11,7 +10,9 @@ open class FetchEmailsOptions( */ val downloadAttachmentsWithExtensions: List = DefaultDownloadedAttachmentsWithExtensions, val attachmentsDownloadDirectory: File = DefaultAttachmentsDownloadDirectory, + val emailFolderName: String = "INBOX", + val connectTimeoutSeconds: Int = 5, val onError: ((FetchEmailsError) -> Unit)? = null, val onEmailReceived: ((Email) -> Unit)? = null @@ -21,7 +22,7 @@ open class FetchEmailsOptions( val DefaultAttachmentsDownloadDirectory: File = File(System.getProperty("java.io.tmpdir"), "eInvoices").also { it.mkdirs() } - val IllegalFileCharacters = listOf('\\', '/', ':', '*', '?', '"', '<', '>', '|', '\u0000') + val IllegalFileCharacters = listOf('\\', '/', ':', '*', '?', '"', '<', '>', '|', '\u0000') // TODO: make generic for attachment filename } fun emailReceived(email: Email) { diff --git a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/ListenForNewMailsOptions.kt b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/ListenForNewMailsOptions.kt index 60eddd7..c49421a 100644 --- a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/ListenForNewMailsOptions.kt +++ b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/ListenForNewMailsOptions.kt @@ -10,8 +10,10 @@ open class ListenForNewMailsOptions( downloadMessageBody: Boolean = false, downloadAttachmentsWithExtensions: List = DefaultDownloadedAttachmentsWithExtensions, attachmentsDownloadDirectory: File = DefaultAttachmentsDownloadDirectory, + emailFolderName: String = "INBOX", + connectTimeoutSeconds: Int = 5, onError: ((FetchEmailsError) -> Unit)? = null, onEmailReceived: (Email) -> Unit -) : FetchEmailsOptions(downloadMessageBody, downloadAttachmentsWithExtensions, attachmentsDownloadDirectory, emailFolderName, onError, onEmailReceived) \ No newline at end of file +) : FetchEmailsOptions(downloadMessageBody, downloadAttachmentsWithExtensions, attachmentsDownloadDirectory, emailFolderName, connectTimeoutSeconds, onError, onEmailReceived) \ No newline at end of file