Skip to content

Commit

Permalink
Prøver xmlmapper
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikg committed Nov 1, 2023
1 parent 7e2c2ab commit 6f16a35
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 31 deletions.
1 change: 1 addition & 0 deletions apps/etterlatte-proxy/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies {
implementation(Cxf.CxfWsSecurity)
implementation(Micrometer.Prometheus)
implementation(Jackson.jacksonDatatypejsr310)
implementation(Jackson.jacksonXml)

testImplementation(NavFelles.MockOauth2Server)
testImplementation(Ktor.ServerTests)
Expand Down
63 changes: 32 additions & 31 deletions apps/etterlatte-proxy/src/main/kotlin/routes/TilbakekrevingRoute.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
package no.nav.etterlatte.routes

import com.fasterxml.jackson.core.JsonGenerator
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.JsonSerializer
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule
import com.fasterxml.jackson.dataformat.xml.XmlMapper
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.kotlin.KotlinModule
import com.fasterxml.jackson.module.kotlin.readValue
import io.ktor.server.application.call
import io.ktor.server.application.log
import io.ktor.server.request.receive
import io.ktor.server.request.receiveText
import io.ktor.server.response.respond
import io.ktor.server.routing.Route
import io.ktor.server.routing.application
import io.ktor.server.routing.post
import jakarta.xml.bind.JAXBContext
import jakarta.xml.bind.Marshaller
import no.nav.okonomi.tilbakekrevingservice.TilbakekrevingPortType
import no.nav.okonomi.tilbakekrevingservice.TilbakekrevingsvedtakRequest
import no.nav.okonomi.tilbakekrevingservice.TilbakekrevingsvedtakResponse
import java.io.StringWriter
import javax.xml.stream.XMLInputFactory
import javax.xml.transform.stream.StreamSource
import javax.xml.datatype.XMLGregorianCalendar

/**
* Endepunkter for å integrere med tilbakekrevingstjenesten fra gcp til fss
Expand All @@ -27,38 +32,34 @@ fun Route.tilbakekrevingRoute(tilbakekrevingService: TilbakekrevingPortType) {
val request = call.receiveText()
logger.info(request)

val vedtakRequest = toTilbakekrevingsvedtakRequest(request)

val vedtakRequest: TilbakekrevingsvedtakRequest = xmlMapper.readValue(request)
logger.info("Videresender tilbakekrevingsvedtak ${vedtakRequest.tilbakekrevingsvedtak.vedtakId} til on-prem")

val response = tilbakekrevingService.tilbakekrevingsvedtak(vedtakRequest)
logger.info(toXml(response))
logger.info(xmlMapper.writeValueAsString(response))

call.respond(response)
}


}

val jaxbContext = JAXBContext.newInstance(TilbakekrevingsvedtakRequest::class.java)
val jaxbContextResponse = JAXBContext.newInstance(TilbakekrevingsvedtakResponse::class.java)
val xmlInputFactory = XMLInputFactory.newInstance()

fun toXml(response: TilbakekrevingsvedtakResponse): String {
val marshaller = jaxbContextResponse.createMarshaller()
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true)
val xmlMapper = XmlMapper(JacksonXmlModule().apply { setDefaultUseWrapper(false) }).apply {
registerModule(KotlinModule.Builder().build())
registerModule(JavaTimeModule())
registerModule(CustomXMLGregorianCalendarModule())
disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
}

val stringWriter = StringWriter()
stringWriter.use {
marshaller.marshal(response, stringWriter)
private class CustomXMLGregorianCalendarModule : SimpleModule() {
init {
addSerializer(XMLGregorianCalendar::class.java, CustomXMLGregorianCalendarSerializer())
}

return stringWriter.toString()
}
fun toTilbakekrevingsvedtakRequest(xml: String): TilbakekrevingsvedtakRequest {
val request =
jaxbContext.createUnmarshaller().unmarshal(
xmlInputFactory.createXMLStreamReader(StreamSource(xml)),
TilbakekrevingsvedtakRequest::class.java,
)
return request.value
private class CustomXMLGregorianCalendarSerializer : JsonSerializer<XMLGregorianCalendar>() {
override fun serialize(value: XMLGregorianCalendar?, gen: JsonGenerator, serializers: SerializerProvider) {
if (value != null) {
gen.writeString(value.toGregorianCalendar().toZonedDateTime().toLocalDate().toString())
}
}
}
}
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ object Ktor {

object Jackson {
const val jacksonDatatypejsr310 = "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.15.0"
const val jacksonXml = "com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.15.0"
}

object Kafka {
Expand Down

0 comments on commit 6f16a35

Please sign in to comment.