diff --git a/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/App.kt b/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/App.kt index e0a668f1..e53aeff1 100644 --- a/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/App.kt +++ b/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/App.kt @@ -16,9 +16,11 @@ import no.nav.emottak.ebms.db.DatabaseConfig import no.nav.emottak.ebms.db.mapHikariConfig import no.nav.emottak.ebms.model.EbMSAttachment import no.nav.emottak.ebms.model.EbMSDocument +import no.nav.emottak.ebms.model.getAttachmentId +import no.nav.emottak.ebms.model.getConversationId import no.nav.emottak.ebms.processing.EbmsMessageProcessor -import no.nav.emottak.ebms.xml.EbmsMessageBuilder -import kotlin.io.encoding.ExperimentalEncodingApi +import no.nav.emottak.ebms.xml.xmlMarshaller +import org.xmlsoap.schemas.soap.envelope.Envelope fun main() { @@ -43,6 +45,27 @@ fun main() { call.respondText("Hello") } + post("/ebmsTest") { + val allParts = call.receiveMultipart().readAllParts() + val dokument = allParts.find { + it.contentType?.toString() == "text/xml" && it.contentDisposition == null + } + val envelope = xmlMarshaller.unmarshal(String((dokument as PartData.FormItem).payload()), Envelope::class.java) + + val conversationId = envelope.getConversationId() + println(conversationId) + val attachmentId = envelope.getAttachmentId() + println(attachmentId) + val attachments = allParts + .filter { it.contentDisposition == ContentDisposition.Attachment } + .filter { it.headers.get("Content-Id")?.contains(attachmentId, true) ?: false } + .map { (it as PartData.FormItem).payload() } + .first() + println( + String(attachments) + ) + call.respondText("Hello2") + } } }.start(wait = true) } diff --git a/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/model/EbmsExt.kt b/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/model/EbmsExt.kt new file mode 100644 index 00000000..9f45cc1c --- /dev/null +++ b/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/model/EbmsExt.kt @@ -0,0 +1,50 @@ +package no.nav.emottak.ebms.model + +import org.apache.commons.lang3.StringUtils.isNotBlank +import org.oasis_open.committees.ebxml_msg.schema.msg_header_2_0.AckRequested +import org.oasis_open.committees.ebxml_msg.schema.msg_header_2_0.From +import org.oasis_open.committees.ebxml_msg.schema.msg_header_2_0.Manifest +import org.oasis_open.committees.ebxml_msg.schema.msg_header_2_0.MessageHeader +import org.xmlsoap.schemas.soap.envelope.Envelope + +// TODO kan sikkert flytte alt dette til der det brukes. + +fun Envelope.getConversationId() : String { + val header = this.header.any[0] + if (header is MessageHeader) + return header.conversationId + else + throw RuntimeException("Kunne ikke finne conversation ID"); +} + +fun Envelope.getAttachmentId() : String { // TODO: egentlig kan vel det være n+1 attachments + val manifest = this.body.any.find { it is Manifest } as Manifest + return manifest.reference.map { it.href } + .first().replace("cid:", ""); // quickndirty +} + +fun Envelope.getFrom (): From { + return (this.header.any.find { it is MessageHeader } as MessageHeader).from +} + +fun Envelope.getMessageId(): String { + return this.header.any.filterIsInstance() + .stream().filter { isNotBlank(it.id) } + .map { it.id }.findFirst().get() +} + +fun Envelope.getVersion(): String { + return this.header.any.filterIsInstance() + .stream().filter { isNotBlank(it.version) } + .map { it.version }.findFirst().get() +} + +fun Envelope.getActor(): String { + return this.header.any.filterIsInstance().stream() + .filter{ isNotBlank(it.actor) }.map { it.actor }.findFirst().get() +} + +fun Envelope.getAckRequestedSigned(): Boolean { + return this.header.any.filterIsInstance().stream() + .anyMatch { it.isSigned } // Kotlin quirk. Med isSigned menes at en signed Ack er ønsket +} diff --git a/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/processing/AckRequestedProcessor.kt b/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/processing/AckRequestedProcessor.kt new file mode 100644 index 00000000..a9533e56 --- /dev/null +++ b/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/processing/AckRequestedProcessor.kt @@ -0,0 +1,44 @@ +package no.nav.emottak.ebms.processing + +import com.github.labai.jsr305x.api.NotNull +import no.nav.emottak.ebms.model.* +import no.nav.emottak.ebms.xml.xmlMarshaller +import org.apache.commons.lang3.StringUtils.isNotBlank +import org.oasis_open.committees.ebxml_msg.schema.msg_header_2_0.AckRequested +import org.oasis_open.committees.ebxml_msg.schema.msg_header_2_0.Acknowledgment +import org.oasis_open.committees.ebxml_msg.schema.msg_header_2_0.MessageHeader +import org.oasis_open.committees.ebxml_msg.schema.msg_header_2_0.ObjectFactory +import org.w3._2000._09.xmldsig_.ReferenceType +import org.xmlsoap.schemas.soap.envelope.Envelope +import java.time.Instant +import java.util.* +import kotlin.coroutines.Continuation + +class AckRequestedProcessor(): Processor { + + fun createAcknowledgement(envelope: Envelope): Acknowledgment { + val acknowledgment = Acknowledgment() + acknowledgment.id = "ACK_ID" // Identifier for Acknowledgment elementet, IKKE message ID. // TODO avklar, dette er såvidt jeg vet en arbitrær verdi? + acknowledgment.version = envelope.getVersion() + acknowledgment.isMustUnderstand = true // Alltid + acknowledgment.actor = envelope.getActor() + acknowledgment.timestamp = Date.from(Instant.now()) // TODO dette skal være message received date, hente fra context? + acknowledgment.refToMessageId = envelope.getMessageId() + acknowledgment.from = envelope.getFrom() + if(envelope.getAckRequestedSigned()) { + // TODO vi må signere responsen, kan kanskje alltid gjøres uansett? + acknowledgment.reference.addAll(getReferences()) + } + //acknowledgment.otherAttributes + return acknowledgment + } + + fun getReferences(): List<@NotNull ReferenceType> { + return emptyList() // TODO XMLDSIG elements fra signaturen vår + } + + override fun process() { + TODO("Not yet implemented") + } +} + diff --git a/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/processing/Processor.kt b/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/processing/Processor.kt new file mode 100644 index 00000000..8a0af6a1 --- /dev/null +++ b/ebms-provider/src/main/kotlin/no/nav/emottak/ebms/processing/Processor.kt @@ -0,0 +1,5 @@ +package no.nav.emottak.ebms.processing + +fun interface Processor { + fun process() // TODO kan sikkert ta imot en context. EbmsMessageContext? +} \ No newline at end of file diff --git a/ebms-provider/src/test/kotlin/no/nav/emottak/ebms/XmlMarshallerTest.kt b/ebms-provider/src/test/kotlin/no/nav/emottak/ebms/XmlMarshallerTest.kt index 5fedb698..12e982e0 100644 --- a/ebms-provider/src/test/kotlin/no/nav/emottak/ebms/XmlMarshallerTest.kt +++ b/ebms-provider/src/test/kotlin/no/nav/emottak/ebms/XmlMarshallerTest.kt @@ -3,6 +3,7 @@ */ package no.nav.emottak.ebms +import no.nav.emottak.ebms.model.getConversationId import no.nav.emottak.ebms.xml.marshal import no.nav.emottak.ebms.xml.unmarshal import org.junit.jupiter.api.Assertions.assertEquals @@ -24,7 +25,7 @@ class XmlMarshallerTest { fun testSerdeValidateEbxmlMessage() { val xmlFile = XmlMarshallerTest::class.java.classLoader - .getResourceAsStream("oppgjørsmelding/2023_08_29T12_56_58_328.xml"); + .getResourceAsStream("oppgjørsmelding/2023_08_29T12_56_58_328.xml") val envelope = unmarshal(xmlFile.reader().readText(), Envelope::class.java) assertTrue(envelope is Envelope)