summaryrefslogtreecommitdiff
path: root/ebics/src/main/kotlin/ebics_h005/Ebics3Response.kt
blob: dd4b18a2215a7777823245c61223215000e17bb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package tech.libeufin.ebics.ebics_h005

import org.apache.xml.security.binding.xmldsig.SignatureType
import tech.libeufin.common.crypto.CryptoUtil
import tech.libeufin.ebics.ebics_h004.EbicsTypes
import java.math.BigInteger
import javax.xml.bind.annotation.*
import javax.xml.bind.annotation.adapters.CollapsedStringAdapter
import javax.xml.bind.annotation.adapters.NormalizedStringAdapter
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter
import kotlin.math.min

@XmlAccessorType(XmlAccessType.NONE)
@XmlType(name = "", propOrder = ["header", "authSignature", "body"])
@XmlRootElement(name = "ebicsResponse")
class Ebics3Response {
    @get:XmlAttribute(name = "Version", required = true)
    @get:XmlJavaTypeAdapter(CollapsedStringAdapter::class)
    lateinit var version: String

    @get:XmlAttribute(name = "Revision")
    var revision: Int? = null

    @get:XmlElement(required = true)
    lateinit var header: Header

    @get:XmlElement(name = "AuthSignature", required = true)
    lateinit var authSignature: SignatureType

    @get:XmlElement(required = true)
    lateinit var body: Body

    @XmlAccessorType(XmlAccessType.NONE)
    @XmlType(name = "", propOrder = ["_static", "mutable"])
    class Header {
        @get:XmlElement(name = "static", required = true)
        lateinit var _static: StaticHeaderType

        @get:XmlElement(required = true)
        lateinit var mutable: MutableHeaderType

        @get:XmlAttribute(name = "authenticate", required = true)
        var authenticate: Boolean = false
    }

    @XmlAccessorType(XmlAccessType.NONE)
    @XmlType(name = "", propOrder = ["dataTransfer", "returnCode", "timestampBankParameter"])
    class Body {
        @get:XmlElement(name = "DataTransfer")
        var dataTransfer: DataTransferResponseType? = null

        @get:XmlElement(name = "ReturnCode", required = true)
        lateinit var returnCode: ReturnCode

        @get:XmlElement(name = "TimestampBankParameter")
        var timestampBankParameter: EbicsTypes.TimestampBankParameter? = null
    }


    @XmlAccessorType(XmlAccessType.NONE)
    @XmlType(
        name = "",
        propOrder = ["transactionPhase", "segmentNumber", "orderID", "returnCode", "reportText"]
    )
    class MutableHeaderType {
        @get:XmlElement(name = "TransactionPhase", required = true)
        @get:XmlSchemaType(name = "token")
        lateinit var transactionPhase: EbicsTypes.TransactionPhaseType

        @get:XmlElement(name = "SegmentNumber")
        var segmentNumber: EbicsTypes.SegmentNumber? = null

        @get:XmlElement(name = "OrderID")
        @get:XmlJavaTypeAdapter(CollapsedStringAdapter::class)
        @get:XmlSchemaType(name = "token")
        var orderID: String? = null

        @get:XmlElement(name = "ReturnCode", required = true)
        @get:XmlJavaTypeAdapter(CollapsedStringAdapter::class)
        @get:XmlSchemaType(name = "token")
        lateinit var returnCode: String

        @get:XmlElement(name = "ReportText", required = true)
        @get:XmlJavaTypeAdapter(NormalizedStringAdapter::class)
        @get:XmlSchemaType(name = "normalizedString")
        lateinit var reportText: String
    }

    @XmlAccessorType(XmlAccessType.NONE)
    class OrderData {
        @get:XmlValue
        lateinit var value: String
    }

    @XmlAccessorType(XmlAccessType.NONE)
    class ReturnCode {
        @get:XmlValue
        @get:XmlJavaTypeAdapter(CollapsedStringAdapter::class)
        lateinit var value: String

        @get:XmlAttribute(name = "authenticate", required = true)
        var authenticate: Boolean = false
    }

    @XmlAccessorType(XmlAccessType.NONE)
    @XmlType(name = "DataTransferResponseType", propOrder = ["dataEncryptionInfo", "orderData"])
    class DataTransferResponseType {
        @get:XmlElement(name = "DataEncryptionInfo")
        var dataEncryptionInfo: Ebics3Types.DataEncryptionInfo? = null

        @get:XmlElement(name = "OrderData", required = true)
        lateinit var orderData: OrderData
    }

    @XmlAccessorType(XmlAccessType.NONE)
    @XmlType(name = "ResponseStaticHeaderType", propOrder = ["transactionID", "numSegments"])
    class StaticHeaderType {
        @get:XmlElement(name = "TransactionID")
        var transactionID: String? = null

        @get:XmlElement(name = "NumSegments")
        @get:XmlSchemaType(name = "positiveInteger")
        var numSegments: BigInteger? = null
    }

    companion object {

        fun createForUploadWithError(
            errorText: String, errorCode: String, phase: EbicsTypes.TransactionPhaseType
        ): Ebics3Response {
            val resp = Ebics3Response().apply {
                this.version = "H005"
                this.revision = 1
                this.header = Header().apply {
                    this.authenticate = true
                    this.mutable = MutableHeaderType().apply {
                        this.reportText = errorText
                        this.returnCode = errorCode
                        this.transactionPhase = phase
                    }
                    _static = StaticHeaderType()
                }
                this.authSignature = SignatureType()
                this.body = Body().apply {
                    this.returnCode = ReturnCode().apply {
                        this.authenticate = true
                        this.value = errorCode
                    }
                }
            }
            return resp
        }

        fun createForUploadInitializationPhase(transactionID: String, orderID: String): Ebics3Response {
            return Ebics3Response().apply {
                this.version = "H005"
                this.revision = 1
                this.header = Header().apply {
                    this.authenticate = true
                    this._static = StaticHeaderType().apply {
                        this.transactionID = transactionID
                    }
                    this.mutable = MutableHeaderType().apply {
                        this.transactionPhase =
                            EbicsTypes.TransactionPhaseType.INITIALISATION
                        this.orderID = orderID
                        this.reportText = "[EBICS_OK] OK"
                        this.returnCode = "000000"
                    }
                }
                this.authSignature = SignatureType()
                this.body = Body().apply {
                    this.returnCode = ReturnCode().apply {
                        this.authenticate = true
                        this.value = "000000"
                    }
                }
            }
        }

        fun createForDownloadReceiptPhase(transactionID: String, positiveAck: Boolean): Ebics3Response {
            return Ebics3Response().apply {
                this.version = "H005"
                this.revision = 1
                this.header = Header().apply {
                    this.authenticate = true
                    this._static = StaticHeaderType().apply {
                        this.transactionID = transactionID
                    }
                    this.mutable = MutableHeaderType().apply {
                        this.transactionPhase =
                            EbicsTypes.TransactionPhaseType.RECEIPT
                        if (positiveAck) {
                            this.reportText = "[EBICS_DOWNLOAD_POSTPROCESS_DONE] Received positive receipt"
                            this.returnCode = "011000"
                        } else {
                            this.reportText = "[EBICS_DOWNLOAD_POSTPROCESS_SKIPPED] Received negative receipt"
                            this.returnCode = "011001"
                        }
                    }
                }
                this.authSignature = SignatureType()
                this.body = Body().apply {
                    this.returnCode = ReturnCode().apply {
                        this.authenticate = true
                        this.value = "000000"
                    }
                }
            }
        }

        fun createForUploadTransferPhase(
            transactionID: String,
            segmentNumber: Int,
            lastSegment: Boolean,
            orderID: String
        ): Ebics3Response {
            return Ebics3Response().apply {
                this.version = "H005"
                this.revision = 1
                this.header = Header().apply {
                    this.authenticate = true
                    this._static = StaticHeaderType().apply {
                        this.transactionID = transactionID
                    }
                    this.mutable = MutableHeaderType().apply {
                        this.transactionPhase =
                            EbicsTypes.TransactionPhaseType.TRANSFER
                        this.segmentNumber = EbicsTypes.SegmentNumber().apply {
                            this.value = BigInteger.valueOf(segmentNumber.toLong())
                            if (lastSegment) {
                                this.lastSegment = true
                            }
                        }
                        this.orderID = orderID
                        this.reportText = "[EBICS_OK] OK"
                        this.returnCode = "000000"
                    }
                }
                this.authSignature = SignatureType()
                this.body = Body().apply {
                    this.returnCode = ReturnCode().apply {
                        this.authenticate = true
                        this.value = "000000"
                    }
                }
            }
        }

        /**
         * @param requestedSegment requested segment as a 1-based index
         */
        fun createForDownloadTransferPhase(
            transactionID: String,
            numSegments: Int,
            segmentSize: Int,
            encodedData: String,
            requestedSegment: Int
        ): Ebics3Response {
            return Ebics3Response().apply {
                this.version = "H005"
                this.revision = 1
                this.header = Header().apply {
                    this.authenticate = true
                    this._static = StaticHeaderType().apply {
                        this.transactionID = transactionID
                        this.numSegments = BigInteger.valueOf(numSegments.toLong())
                    }
                    this.mutable = MutableHeaderType().apply {
                        this.transactionPhase =
                            EbicsTypes.TransactionPhaseType.TRANSFER
                        this.segmentNumber = EbicsTypes.SegmentNumber().apply {
                            this.lastSegment = numSegments == requestedSegment
                            this.value = BigInteger.valueOf(requestedSegment.toLong())
                        }
                        this.reportText = "[EBICS_OK] OK"
                        this.returnCode = "000000"
                    }
                }
                this.authSignature = SignatureType()
                this.body = Body().apply {
                    this.returnCode = ReturnCode().apply {
                        this.authenticate = true
                        this.value = "000000"
                    }
                    this.dataTransfer = DataTransferResponseType().apply {
                        this.orderData = OrderData().apply {
                            val start = segmentSize * (requestedSegment - 1)
                            this.value = encodedData.substring(start, min(start + segmentSize, encodedData.length))
                        }
                    }
                }
            }
        }
        fun createForDownloadInitializationPhase(
            transactionID: String,
            numSegments: Int,
            segmentSize: Int,
            enc: CryptoUtil.EncryptionResult,
            encodedData: String
        ): Ebics3Response {
            return Ebics3Response().apply {
                this.version = "H005"
                this.revision = 1
                this.header = Header().apply {
                    this.authenticate = true
                    this._static = StaticHeaderType().apply {
                        this.transactionID = transactionID
                        this.numSegments = BigInteger.valueOf(numSegments.toLong())
                    }
                    this.mutable = MutableHeaderType().apply {
                        this.transactionPhase =
                            EbicsTypes.TransactionPhaseType.INITIALISATION
                        this.segmentNumber = EbicsTypes.SegmentNumber().apply {
                            this.lastSegment = (numSegments == 1)
                            this.value = BigInteger.valueOf(1)
                        }
                        this.reportText = "[EBICS_OK] OK"
                        this.returnCode = "000000"
                    }
                }
                this.authSignature = SignatureType()
                this.body = Body().apply {
                    this.returnCode = ReturnCode().apply {
                        this.authenticate = true
                        this.value = "000000"
                    }
                    this.dataTransfer = DataTransferResponseType().apply {
                        this.dataEncryptionInfo = Ebics3Types.DataEncryptionInfo().apply {
                            this.authenticate = true
                            this.encryptionPubKeyDigest = Ebics3Types.PubKeyDigest()
                                .apply {
                                this.algorithm = "http://www.w3.org/2001/04/xmlenc#sha256"
                                this.version = "E002"
                                this.value = enc.pubKeyDigest
                            }
                            this.transactionKey = enc.encryptedTransactionKey
                        }
                        this.orderData = OrderData().apply {
                            this.value = encodedData.substring(0, min(segmentSize, encodedData.length))
                        }
                    }
                }
            }
        }
    }
}