- Implemented closing dialogs - Implemented setting message header and customerSystemId send by bank on messages - Renamed getBankInfo() to synchronizeCustomerSystemId()

This commit is contained in:
dankl 2019-10-05 22:44:12 +02:00 committed by dankito
parent 9577e6c933
commit 89fc324cb4
4 changed files with 77 additions and 19 deletions

View File

@ -3,39 +3,90 @@ package net.dankito.fints
import net.dankito.fints.messages.MessageBuilder import net.dankito.fints.messages.MessageBuilder
import net.dankito.fints.model.BankData import net.dankito.fints.model.BankData
import net.dankito.fints.model.CustomerData import net.dankito.fints.model.CustomerData
import net.dankito.fints.model.DialogData
import net.dankito.fints.model.ProductData 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.fints.util.IBase64Service
import net.dankito.utils.web.client.IWebClient import net.dankito.utils.web.client.IWebClient
import net.dankito.utils.web.client.OkHttpWebClient import net.dankito.utils.web.client.OkHttpWebClient
import net.dankito.utils.web.client.RequestParameters import net.dankito.utils.web.client.RequestParameters
import net.dankito.utils.web.client.WebClientResponse import net.dankito.utils.web.client.WebClientResponse
import org.slf4j.LoggerFactory
open class FinTsClient( open class FinTsClient(
protected val base64Service: IBase64Service, protected val base64Service: IBase64Service,
protected val webClient: IWebClient = OkHttpWebClient(), protected val webClient: IWebClient = OkHttpWebClient(),
protected val messageBuilder: MessageBuilder = MessageBuilder() protected val messageBuilder: MessageBuilder = MessageBuilder(),
protected val responseParser: ResponseParser = ResponseParser()
) { ) {
companion object {
fun getAnonymousBankInfo(bank: BankData, product: ProductData) { private val log = LoggerFactory.getLogger(FinTsClient::class.java)
val requestBody = messageBuilder.createAnonymousDialogInitMessage(bank, product)
val response = getResponseForMessage(requestBody, bank)
handleResponse(response)
} }
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<ReceivedSynchronization>(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 { protected open fun getResponseForMessage(requestBody: String, bank: BankData): WebClientResponse {
log.debug("Sending message:\n$requestBody")
val encodedRequestBody = base64Service.encode(requestBody) val encodedRequestBody = base64Service.encode(requestBody)
return webClient.post( return webClient.post(
@ -43,17 +94,19 @@ open class FinTsClient(
) )
} }
protected open fun handleResponse(response: WebClientResponse) { protected open fun handleResponse(webResponse: WebClientResponse): Response {
val responseBody = response.body val responseBody = webResponse.body
if (response.isSuccessful && responseBody != null) { if (webResponse.isSuccessful && responseBody != null) {
val decodedResponse = decodeBase64Response(responseBody) 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 { protected open fun decodeBase64Response(responseBody: String): String {

View File

@ -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( return createMessage(true, true, bank, customer, dialogData, listOf(
IdentifikationsSegment(generator.resetSegmentNumber(2), bank, customer), IdentifikationsSegment(generator.resetSegmentNumber(2), bank, customer),

View File

@ -10,4 +10,9 @@ open class DialogData(
val DialogInitDialogData = DialogData("0", 1) val DialogInitDialogData = DialogData("0", 1)
} }
fun increaseMessageNumber() {
messageNumber++
}
} }

View File

@ -59,7 +59,7 @@ class MessageBuilderTest : FinTsTestBase() {
fun createDialogInitMessage() { fun createDialogInitMessage() {
// when // when
val result = underTest.createDialogInitMessage(Bank, Customer, Product, DialogData.DialogInitDialogData) val result = underTest.createSynchronizeCustomerSystemIdMessage(Bank, Customer, Product, DialogData.DialogInitDialogData)
// then // then
assertThat(normalizeBinaryData(result)).isEqualTo(normalizeBinaryData( assertThat(normalizeBinaryData(result)).isEqualTo(normalizeBinaryData(