Skip to content

Commit

Permalink
Litt Acknowledgement generering
Browse files Browse the repository at this point in the history
  • Loading branch information
RettIProd committed Sep 21, 2023
1 parent 9ef3b77 commit f299878
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 3 deletions.
27 changes: 25 additions & 2 deletions ebms-provider/src/main/kotlin/no/nav/emottak/ebms/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand All @@ -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)
}
Expand Down
50 changes: 50 additions & 0 deletions ebms-provider/src/main/kotlin/no/nav/emottak/ebms/model/EbmsExt.kt
Original file line number Diff line number Diff line change
@@ -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<MessageHeader>()
.stream().filter { isNotBlank(it.id) }
.map { it.id }.findFirst().get()
}

fun Envelope.getVersion(): String {
return this.header.any.filterIsInstance<MessageHeader>()
.stream().filter { isNotBlank(it.version) }
.map { it.version }.findFirst().get()
}

fun Envelope.getActor(): String {
return this.header.any.filterIsInstance<AckRequested>().stream()
.filter{ isNotBlank(it.actor) }.map { it.actor }.findFirst().get()
}

fun Envelope.getAckRequestedSigned(): Boolean {
return this.header.any.filterIsInstance<AckRequested>().stream()
.anyMatch { it.isSigned } // Kotlin quirk. Med isSigned menes at en signed Ack er ønsket
}
Original file line number Diff line number Diff line change
@@ -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")
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package no.nav.emottak.ebms.processing

fun interface Processor {
fun process() // TODO kan sikkert ta imot en context. EbmsMessageContext?
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit f299878

Please sign in to comment.