2026-09-08 11:26:20 +08:00
|
|
|
package com.gzzn.omms.msgexchange.codec
|
|
|
|
|
|
2026-09-09 17:53:08 +08:00
|
|
|
import com.fasterxml.jackson.databind.DeserializationFeature
|
|
|
|
|
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
|
|
|
|
|
import com.fasterxml.jackson.dataformat.xml.XmlFactory
|
|
|
|
|
import com.fasterxml.jackson.dataformat.xml.XmlMapper
|
2026-09-08 11:26:20 +08:00
|
|
|
import com.gzzn.omms.msgexchange.domain.DecodedMessage
|
|
|
|
|
import com.gzzn.omms.msgexchange.domain.ErrorClass
|
|
|
|
|
import com.gzzn.omms.msgexchange.domain.MetaFields
|
|
|
|
|
import com.gzzn.omms.msgexchange.domain.MsgKind
|
|
|
|
|
import jakarta.inject.Singleton
|
2026-09-09 17:53:08 +08:00
|
|
|
import javax.xml.stream.XMLInputFactory
|
2026-09-08 11:26:20 +08:00
|
|
|
|
2026-09-09 19:48:45 +08:00
|
|
|
/** XML codec for the SIS wire contract; XML DTO binding is kept separate from domain payloads. */
|
2026-09-08 11:26:20 +08:00
|
|
|
@Singleton
|
|
|
|
|
class JacksonXmlCodec : XmlCodec {
|
2026-09-09 17:53:08 +08:00
|
|
|
private val mapper: XmlMapper = XmlMapper(
|
|
|
|
|
XmlFactory(
|
|
|
|
|
XMLInputFactory.newInstance().apply {
|
|
|
|
|
setProperty(XMLInputFactory.SUPPORT_DTD, false)
|
|
|
|
|
setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false)
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
).apply {
|
|
|
|
|
registerKotlinModule()
|
2026-09-09 19:48:45 +08:00
|
|
|
// SIS explicitly permits fields unused by a subsystem; wire DTOs document that policy.
|
2026-09-09 17:53:08 +08:00
|
|
|
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
|
2026-09-08 11:26:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun decode(rawXml: String): DecodeResult {
|
|
|
|
|
val trimmed = rawXml.trim()
|
|
|
|
|
if (trimmed.isEmpty() || !trimmed.startsWith("<")) {
|
|
|
|
|
return DecodeResult.Err(DecodeFailure(ErrorClass.MALFORMED, "empty-or-non-xml"))
|
|
|
|
|
}
|
2026-09-09 17:53:08 +08:00
|
|
|
val msg = try {
|
2026-09-09 19:48:45 +08:00
|
|
|
mapper.readValue(trimmed, SisMessageXml::class.java)
|
2026-09-08 11:26:20 +08:00
|
|
|
} catch (e: Exception) {
|
2026-09-09 17:53:08 +08:00
|
|
|
return DecodeResult.Err(
|
|
|
|
|
DecodeFailure(ErrorClass.MALFORMED, "xml-parse:${e.message?.take(200) ?: e.javaClass.simpleName}"),
|
|
|
|
|
)
|
2026-09-08 11:26:20 +08:00
|
|
|
}
|
2026-09-09 17:53:08 +08:00
|
|
|
val meta = msg.meta
|
|
|
|
|
?: return DecodeResult.Err(DecodeFailure(ErrorClass.MALFORMED, "missing-meta"))
|
|
|
|
|
val sndr = meta.sndr?.trim()?.takeIf { it.isNotEmpty() }
|
|
|
|
|
?: return DecodeResult.Err(DecodeFailure(ErrorClass.MALFORMED, "missing-sndr"))
|
|
|
|
|
val seqn = meta.seqn
|
|
|
|
|
?: return DecodeResult.Err(DecodeFailure(ErrorClass.MALFORMED, "missing-or-invalid-seqn"))
|
|
|
|
|
val dttm = meta.dttm
|
|
|
|
|
?: return DecodeResult.Err(DecodeFailure(ErrorClass.MALFORMED, "missing-or-invalid-dttm"))
|
2026-09-09 19:48:45 +08:00
|
|
|
val type = meta.type?.trim()?.uppercase()
|
2026-09-09 17:53:08 +08:00
|
|
|
?: return DecodeResult.Err(DecodeFailure(ErrorClass.MALFORMED, "missing-type"))
|
2026-09-09 19:48:45 +08:00
|
|
|
val styp = meta.styp?.trim()?.uppercase()
|
2026-09-09 17:53:08 +08:00
|
|
|
?: return DecodeResult.Err(DecodeFailure(ErrorClass.MALFORMED, "missing-styp"))
|
|
|
|
|
|
|
|
|
|
val kind = kindOf(type, styp)
|
|
|
|
|
val body: Any? = when (kind) {
|
2026-09-09 19:48:45 +08:00
|
|
|
is MsgKind.Schd -> msg.schd?.let(SisWireMapper::scheduleBody)
|
|
|
|
|
is MsgKind.Flop -> msg.flop?.let(SisWireMapper::flopPayload)
|
|
|
|
|
MsgKind.Fdel -> msg.flop?.let(SisWireMapper::flopPayload)
|
2026-09-09 17:53:08 +08:00
|
|
|
is MsgKind.Unsupported -> null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return DecodeResult.Ok(
|
|
|
|
|
DecodedMessage(
|
|
|
|
|
meta = MetaFields(sndr = sndr, type = type, styp = styp, seqn = seqn, dttm = dttm),
|
|
|
|
|
kind = kind,
|
|
|
|
|
rawXml = trimmed,
|
|
|
|
|
body = body,
|
|
|
|
|
),
|
|
|
|
|
)
|
2026-09-08 11:26:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun encodeRqrd(kind: String, rangeJson: String): String =
|
|
|
|
|
"""<?xml version="1.0" encoding="UTF-8"?><MSG><META><TYPE>RQFD</TYPE></META></MSG>"""
|
|
|
|
|
|
|
|
|
|
private fun kindOf(type: String, styp: String): MsgKind = when (type) {
|
|
|
|
|
"SCHD" -> MsgKind.Schd(
|
|
|
|
|
when (styp) {
|
|
|
|
|
"RESP" -> MsgKind.SchdSubtype.RESP
|
|
|
|
|
"ADFT" -> MsgKind.SchdSubtype.ADFT
|
|
|
|
|
else -> MsgKind.SchdSubtype.DNLD
|
|
|
|
|
},
|
|
|
|
|
)
|
2026-09-09 19:48:45 +08:00
|
|
|
"FLOP" -> if (styp == "FDEL") MsgKind.Fdel else MsgKind.Flop(styp)
|
|
|
|
|
else -> MsgKind.Unsupported("$type-$styp")
|
2026-09-08 11:26:20 +08:00
|
|
|
}
|
|
|
|
|
}
|