diff --git a/fints4k/src/main/kotlin/net/dankito/fints/FinTsClient.kt b/fints4k/src/main/kotlin/net/dankito/fints/FinTsClient.kt index 6a323c85..5a794ce3 100644 --- a/fints4k/src/main/kotlin/net/dankito/fints/FinTsClient.kt +++ b/fints4k/src/main/kotlin/net/dankito/fints/FinTsClient.kt @@ -35,6 +35,7 @@ import net.dankito.utils.web.client.WebClientResponse import org.slf4j.LoggerFactory import java.math.BigDecimal import java.util.* +import java.util.concurrent.CopyOnWriteArrayList open class FinTsClient @JvmOverloads constructor( @@ -55,6 +56,12 @@ open class FinTsClient @JvmOverloads constructor( } + protected val messageLogField = CopyOnWriteArrayList() + + open val messageLog: List + get() = ArrayList(messageLogField) + + /** * Retrieves information about bank (e.g. supported HBCI versions, FinTS server address, * supported jobs, ...). @@ -568,6 +575,8 @@ open class FinTsClient @JvmOverloads constructor( } protected open fun getAndHandleResponseForMessage(requestBody: String, dialogContext: DialogContext): Response { + addMessageLog(requestBody, MessageLogEntryType.Sent, dialogContext) + val webResponse = getResponseForMessage(requestBody, dialogContext.bank.finTs3ServerAddress) val response = handleResponse(webResponse, dialogContext) @@ -580,8 +589,6 @@ open class FinTsClient @JvmOverloads constructor( } protected open fun getResponseForMessage(requestBody: String, finTs3ServerAddress: String): WebClientResponse { - log.debug("Sending message:\n${prettyPrintHbciMessage(requestBody)}") - val encodedRequestBody = base64Service.encode(requestBody) return webClient.post( @@ -597,7 +604,7 @@ open class FinTsClient @JvmOverloads constructor( try { val decodedResponse = decodeBase64Response(responseBody) - log.debug("Received message:\n${prettyPrintHbciMessage(decodedResponse)}") + addMessageLog(decodedResponse, MessageLogEntryType.Received, dialogContext) return responseParser.parse(decodedResponse) } catch (e: Exception) { @@ -618,10 +625,49 @@ open class FinTsClient @JvmOverloads constructor( return base64Service.decode(responseBody.replace("\r", "").replace("\n", "")) } + + protected open fun addMessageLog(message: String, type: MessageLogEntryType, dialogContext: DialogContext) { + val timeStamp = Date() + val prettyPrintMessage = prettyPrintHbciMessage(message) + + val prettyPrintMessageWithoutSensitiveData = removeSensitiveDataFromMessage(prettyPrintMessage, dialogContext) + + + log.debug("${if (type == MessageLogEntryType.Sent) "Sending" else "Received"} message:\n$prettyPrintMessage") + + messageLogField.add(MessageLogEntry(prettyPrintMessageWithoutSensitiveData, timeStamp, dialogContext.customer)) + } + + protected open fun removeSensitiveDataFromMessage(prettyPrintMessage: String, dialogContext: DialogContext): String { + var prettyPrintMessageWithoutSensitiveData = prettyPrintMessage + .replace(dialogContext.customer.customerId, "") + .replace("+" + dialogContext.customer.pin, "+") + + if (dialogContext.customer.name.isNotBlank()) { // TODO: log after response is parsed as otherwise this information may is not available + prettyPrintMessageWithoutSensitiveData = prettyPrintMessageWithoutSensitiveData + .replace(dialogContext.customer.name, "", true) + } + + dialogContext.customer.accounts.forEach { account -> + prettyPrintMessageWithoutSensitiveData = prettyPrintMessageWithoutSensitiveData + .replace(account.accountIdentifier, "") + + if (account.accountHolderName.isNotBlank()) { + prettyPrintMessageWithoutSensitiveData = prettyPrintMessageWithoutSensitiveData + .replace(account.accountHolderName, "", true) + } + } + + // TODO: remove account transactions + + return prettyPrintMessageWithoutSensitiveData + } + protected fun prettyPrintHbciMessage(message: String): String { return message.replace("'", "'\r\n") } + protected open fun handleMayRequiredTan(response: Response, dialogContext: DialogContext): Response { // TODO: use response from DialogContext if (response.isStrongAuthenticationRequired) { diff --git a/fints4k/src/main/kotlin/net/dankito/fints/model/MessageLogEntry.kt b/fints4k/src/main/kotlin/net/dankito/fints/model/MessageLogEntry.kt new file mode 100644 index 00000000..47d02a35 --- /dev/null +++ b/fints4k/src/main/kotlin/net/dankito/fints/model/MessageLogEntry.kt @@ -0,0 +1,16 @@ +package net.dankito.fints.model + +import java.util.* + + +open class MessageLogEntry( + val message: String, + val time: Date, + val customer: CustomerData +) { + + override fun toString(): String { + return message + } + +} \ No newline at end of file diff --git a/fints4k/src/main/kotlin/net/dankito/fints/model/MessageLogEntryType.kt b/fints4k/src/main/kotlin/net/dankito/fints/model/MessageLogEntryType.kt new file mode 100644 index 00000000..29e5ca8e --- /dev/null +++ b/fints4k/src/main/kotlin/net/dankito/fints/model/MessageLogEntryType.kt @@ -0,0 +1,10 @@ +package net.dankito.fints.model + + +enum class MessageLogEntryType { + + Sent, + + Received + +} \ No newline at end of file