Skip to content

Commit

Permalink
litt refaktorering med Content type
Browse files Browse the repository at this point in the history
  • Loading branch information
alpet committed Oct 16, 2023
1 parent 1b89991 commit 9e252b7
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 15 deletions.
3 changes: 1 addition & 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 @@ -14,7 +14,6 @@ import io.ktor.server.routing.*
import no.nav.emottak.ebms.model.*
import no.nav.emottak.ebms.validation.MimeValidationException
import no.nav.emottak.ebms.validation.asParseAsSoapFault
import no.nav.emottak.ebms.validation.parseContentType
import no.nav.emottak.ebms.validation.validateMime
import no.nav.emottak.ebms.validation.validateMimeAttachment
import no.nav.emottak.ebms.validation.validateMimeSoapEnvelope
Expand Down Expand Up @@ -62,7 +61,7 @@ fun Application.myApplicationModule() {
val allParts = call.receiveMultipart().readAllParts()
try {
val dokument = allParts.find {
it.contentType?.contentType + "/" + it.contentType?.contentSubtype == "text/xml" && it.contentDisposition == null
it.contentType?.withoutParameters() == ContentType.parse("text/xml") && it.contentDisposition == null
}.also { it?.validateMimeSoapEnvelope() ?: throw MimeValidationException("Unable to find soap envelope multipart") }!!.payload()
val attachments =
allParts.filter { it.contentDisposition?.disposition == ContentDisposition.Attachment.disposition }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ fun ApplicationRequest.validateContentType() {

//KRAV 5.5.2.3 Valideringsdokument
fun PartData.validateMimeSoapEnvelope() {
this.contentType?.takeIf {
it.contentType + "/" + it.contentSubtype == "text/xml" } ?: throw MimeValidationException("Content type is missing or wrong ")
this.contentType?.withoutParameters().takeIf {it == ContentType.parse("text/xml") } ?: throw MimeValidationException("Content type is missing or wrong ")

this.headers[MimeHeaders.CONTENT_ID].takeUnless { it.isNullOrBlank() }
?: throw MimeValidationException("Content ID is missing")
Expand All @@ -81,8 +80,8 @@ fun PartData.validateMimeSoapEnvelope() {
fun PartData.validateMimeAttachment() {
takeIf { this.contentDisposition?.disposition == "attachment"} ?: throw MimeValidationException("This is not attachment")
takeIf { this.headers[MimeHeaders.CONTENT_TRANSFER_ENCODING] == "base64" } ?: throw MimeValidationException("Feil content transfer encoding")
this.contentType?.takeIf {
it.contentType + "/" + it.contentSubtype == "application/pkcs7-mime"
this.contentType?.withoutParameters().takeIf {
it == ContentType.parse("application/pkcs7-mime")
}?: throw MimeValidationException("Incompatible content type on attachment")
}

Expand All @@ -97,15 +96,6 @@ private fun Headers.validateMimeHeaders() {
}






fun String.parseContentType(): String {
if (this == "text/xml") return this
return ContentTypeRegex.CONTENT_TYPE.find(this)?.groups?.get("contentType")?.value ?: throw MimeValidationException("Missing content-type")
}

class MimeValidationException(message:String,cause: Throwable? = null) : Exception(message,cause)

fun MimeValidationException.asParseAsSoapFault() : String {
Expand Down

0 comments on commit 9e252b7

Please sign in to comment.