From 89fc324cb47661d11085ddb4ff7b73b2debcb4d4 Mon Sep 17 00:00:00 2001 From: dankl Date: Sat, 5 Oct 2019 22:44:12 +0200 Subject: [PATCH] - Implemented closing dialogs - Implemented setting message header and customerSystemId send by bank on messages - Renamed getBankInfo() to synchronizeCustomerSystemId() --- .../kotlin/net/dankito/fints/FinTsClient.kt | 87 +++++++++++++++---- .../dankito/fints/messages/MessageBuilder.kt | 2 +- .../net/dankito/fints/model/DialogData.kt | 5 ++ .../fints/messages/MessageBuilderTest.kt | 2 +- 4 files changed, 77 insertions(+), 19 deletions(-) diff --git a/fints4javaLib/src/main/kotlin/net/dankito/fints/FinTsClient.kt b/fints4javaLib/src/main/kotlin/net/dankito/fints/FinTsClient.kt index c7e8ca1f..fdfca493 100644 --- a/fints4javaLib/src/main/kotlin/net/dankito/fints/FinTsClient.kt +++ b/fints4javaLib/src/main/kotlin/net/dankito/fints/FinTsClient.kt @@ -3,39 +3,90 @@ package net.dankito.fints import net.dankito.fints.messages.MessageBuilder import net.dankito.fints.model.BankData import net.dankito.fints.model.CustomerData +import net.dankito.fints.model.DialogData import net.dankito.fints.model.ProductData +import net.dankito.fints.response.InstituteSegmentId +import net.dankito.fints.response.Response +import net.dankito.fints.response.ResponseParser +import net.dankito.fints.response.segments.ReceivedSynchronization import net.dankito.fints.util.IBase64Service import net.dankito.utils.web.client.IWebClient import net.dankito.utils.web.client.OkHttpWebClient import net.dankito.utils.web.client.RequestParameters import net.dankito.utils.web.client.WebClientResponse +import org.slf4j.LoggerFactory open class FinTsClient( protected val base64Service: IBase64Service, protected val webClient: IWebClient = OkHttpWebClient(), - protected val messageBuilder: MessageBuilder = MessageBuilder() + protected val messageBuilder: MessageBuilder = MessageBuilder(), + protected val responseParser: ResponseParser = ResponseParser() ) { - - fun getAnonymousBankInfo(bank: BankData, product: ProductData) { - val requestBody = messageBuilder.createAnonymousDialogInitMessage(bank, product) - - val response = getResponseForMessage(requestBody, bank) - - handleResponse(response) + companion object { + private val log = LoggerFactory.getLogger(FinTsClient::class.java) } - fun getBankInfo(bank: BankData, customer: CustomerData, product: ProductData) { - val requestBody = messageBuilder.createDialogInitMessage(bank, customer, product) - val response = getResponseForMessage(requestBody, bank) + open fun getAnonymousBankInfo(bank: BankData, product: ProductData): Response { + val dialogData = DialogData() - handleResponse(response) + val requestBody = messageBuilder.createAnonymousDialogInitMessage(bank, product, dialogData) + + val response = getAndHandleResponseForMessage(requestBody, bank) + + if (response.successful) { + dialogData.increaseMessageNumber() + response.messageHeader?.let { header -> dialogData.dialogId = header.dialogId } + + val dialogEndRequestBody = messageBuilder.createAnonymousDialogEndMessage(bank, dialogData) + + getResponseForMessage(dialogEndRequestBody, bank) + } + + return response } + open fun synchronizeCustomerSystemId(bank: BankData, customer: CustomerData, product: ProductData, + dialogData: DialogData = DialogData()): Response { + + val requestBody = messageBuilder.createSynchronizeCustomerSystemIdMessage(bank, customer, product, dialogData) + + val response = getAndHandleResponseForMessage(requestBody, bank) + + if (response.successful) { + response.messageHeader?.let { header -> dialogData.dialogId = header.dialogId } + + response.getFirstSegmentById(InstituteSegmentId.Synchronization)?.customerSystemId?.let { customerSystemId -> + customer.customerSystemId = customerSystemId + } + + closeDialog(bank, customer, dialogData) + } + + return response + } + + protected open fun closeDialog(bank: BankData, customer: CustomerData, dialogData: DialogData) { + dialogData.increaseMessageNumber() + + val dialogEndRequestBody = messageBuilder.createDialogEndMessage(bank, customer, dialogData) + + getResponseForMessage(dialogEndRequestBody, bank) + } + + + protected open fun getAndHandleResponseForMessage(requestBody: String, bank: BankData): Response { + val webResponse = getResponseForMessage(requestBody, bank) + + return handleResponse(webResponse) + } + protected open fun getResponseForMessage(requestBody: String, bank: BankData): WebClientResponse { + log.debug("Sending message:\n$requestBody") + val encodedRequestBody = base64Service.encode(requestBody) return webClient.post( @@ -43,17 +94,19 @@ open class FinTsClient( ) } - protected open fun handleResponse(response: WebClientResponse) { - val responseBody = response.body + protected open fun handleResponse(webResponse: WebClientResponse): Response { + val responseBody = webResponse.body - if (response.isSuccessful && responseBody != null) { + if (webResponse.isSuccessful && responseBody != null) { val decodedResponse = decodeBase64Response(responseBody) - if (decodedResponse != null) { + log.debug("Received message:\n$decodedResponse") - } + return responseParser.parse(decodedResponse) } + + return Response(false, false, error = webResponse.error) } protected open fun decodeBase64Response(responseBody: String): String { diff --git a/fints4javaLib/src/main/kotlin/net/dankito/fints/messages/MessageBuilder.kt b/fints4javaLib/src/main/kotlin/net/dankito/fints/messages/MessageBuilder.kt index 74b77849..01e5bb5a 100644 --- a/fints4javaLib/src/main/kotlin/net/dankito/fints/messages/MessageBuilder.kt +++ b/fints4javaLib/src/main/kotlin/net/dankito/fints/messages/MessageBuilder.kt @@ -58,7 +58,7 @@ open class MessageBuilder(protected val generator: ISegmentNumberGenerator = Seg } - open fun createDialogInitMessage(bank: BankData, customer: CustomerData, product: ProductData, dialogData: DialogData): String { + open fun createSynchronizeCustomerSystemIdMessage(bank: BankData, customer: CustomerData, product: ProductData, dialogData: DialogData): String { return createMessage(true, true, bank, customer, dialogData, listOf( IdentifikationsSegment(generator.resetSegmentNumber(2), bank, customer), diff --git a/fints4javaLib/src/main/kotlin/net/dankito/fints/model/DialogData.kt b/fints4javaLib/src/main/kotlin/net/dankito/fints/model/DialogData.kt index 62f15986..0561a502 100644 --- a/fints4javaLib/src/main/kotlin/net/dankito/fints/model/DialogData.kt +++ b/fints4javaLib/src/main/kotlin/net/dankito/fints/model/DialogData.kt @@ -10,4 +10,9 @@ open class DialogData( val DialogInitDialogData = DialogData("0", 1) } + + fun increaseMessageNumber() { + messageNumber++ + } + } \ No newline at end of file diff --git a/fints4javaLib/src/test/kotlin/net/dankito/fints/messages/MessageBuilderTest.kt b/fints4javaLib/src/test/kotlin/net/dankito/fints/messages/MessageBuilderTest.kt index 349bb34c..e6a1685a 100644 --- a/fints4javaLib/src/test/kotlin/net/dankito/fints/messages/MessageBuilderTest.kt +++ b/fints4javaLib/src/test/kotlin/net/dankito/fints/messages/MessageBuilderTest.kt @@ -59,7 +59,7 @@ class MessageBuilderTest : FinTsTestBase() { fun createDialogInitMessage() { // when - val result = underTest.createDialogInitMessage(Bank, Customer, Product, DialogData.DialogInitDialogData) + val result = underTest.createSynchronizeCustomerSystemIdMessage(Bank, Customer, Product, DialogData.DialogInitDialogData) // then assertThat(normalizeBinaryData(result)).isEqualTo(normalizeBinaryData(