-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement /convert endpoint for xml/json conversion (#165)
- Loading branch information
1 parent
0fe78c5
commit 5490b62
Showing
9 changed files
with
230 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/jvmMain/kotlin/controller/conversion/ConversionController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package controller.conversion | ||
|
||
interface ConversionController { | ||
suspend fun convertRequest(content: String, type: String? = "json", version: String? = "5.0", toType: String? = type, | ||
toVersion: String? = version, sessionId: String?): ConversionResponse | ||
} | ||
|
||
data class ConversionResponse(val response: String, val sessionId: String) |
56 changes: 56 additions & 0 deletions
56
src/jvmMain/kotlin/controller/conversion/ConversionControllerImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package controller.conversion | ||
|
||
import controller.validation.ValidationServiceFactory | ||
import model.CliContext | ||
import org.hl7.fhir.utilities.TimeTracker | ||
import org.hl7.fhir.utilities.VersionUtilities | ||
import org.hl7.fhir.validation.ValidationEngine | ||
import org.koin.core.component.KoinComponent | ||
import org.koin.core.component.inject | ||
import java.nio.file.Files | ||
import java.nio.file.Path | ||
import kotlin.io.path.deleteIfExists | ||
|
||
class ConversionControllerImpl : ConversionController, KoinComponent { | ||
|
||
private val validationServiceFactory by inject<ValidationServiceFactory>() | ||
|
||
override suspend fun convertRequest(content: String, type: String?, version: String?, toType: String?, | ||
toVersion: String?, sessionId: String?): ConversionResponse { | ||
var fromType = type ?: "json" | ||
var fromVersion = version ?: "5.0" | ||
var session = sessionId ?: "new" | ||
|
||
val cliContext = CliContext() | ||
|
||
var validator: ValidationEngine? = validationServiceFactory.getCachedValidator(session) | ||
if (validator == null || validator.version != fromVersion) { | ||
val definitions = VersionUtilities.packageForVersion(fromVersion) + "#" + | ||
VersionUtilities.getCurrentVersion(fromVersion) | ||
val timeTracker = TimeTracker() | ||
session = validationServiceFactory.getValidationService() | ||
.initializeValidator(cliContext, definitions, timeTracker, "new") | ||
validator = validationServiceFactory.getCachedValidator(session) | ||
validator?.version = fromVersion | ||
} | ||
|
||
var input: Path? = null | ||
var output: Path? = null | ||
try { | ||
input = Files.createTempFile("input", ".$fromType") | ||
Files.writeString(input.toAbsolutePath(), content) | ||
cliContext.addSource(input.toAbsolutePath().toString()) | ||
|
||
output = Files.createTempFile("convert", ".${toType ?: fromType}") | ||
|
||
cliContext.targetVer = toVersion ?: fromVersion | ||
|
||
cliContext.output = output.toAbsolutePath().toString() | ||
validationServiceFactory.getValidationService().convertSources(cliContext, validator) | ||
return ConversionResponse(Files.readString(output.toAbsolutePath()), session) | ||
} finally { | ||
input?.deleteIfExists() | ||
output?.deleteIfExists() | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/jvmMain/kotlin/controller/conversion/ConversionModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package controller.conversion | ||
|
||
import constants.CONVERSION_ENDPOINT | ||
|
||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.request.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
import org.koin.ktor.ext.inject | ||
|
||
const val NO_CONTENT_PROVIDED_MESSAGE = "No content for conversion provided in request." | ||
const val INVALID_TYPE_MESSAGE = "Invalid type parameter! Supported xml or json." | ||
const val INVALID_TO_TYPE_MESSAGE = "Invalid toType parameter! Supported xml or json." | ||
|
||
fun Route.conversionModule() { | ||
|
||
val conversionController by inject<ConversionController>() | ||
|
||
post(CONVERSION_ENDPOINT) { | ||
val logger = call.application.environment.log | ||
val content = call.receiveText() | ||
val type = call.request.queryParameters["type"]?.lowercase() ?: when { | ||
call.request.contentType() == ContentType.Application.Xml -> "xml" | ||
call.request.contentType() == ContentType.Application.Json -> "json" | ||
else -> "json" | ||
} | ||
val version = call.request.queryParameters["version"] ?: "5.0" | ||
val toVersion = call.request.queryParameters["toVersion"] ?: version | ||
val toType = call.request.queryParameters["toType"]?.lowercase() ?: type | ||
var sessionId = call.request.queryParameters["sessionId"] ?: call.request.header("Session-Id") | ||
|
||
logger.info("Received Conversion Request. Convert to $toVersion FHIR version and $toType type. " + | ||
"Memory (free/max): ${java.lang.Runtime.getRuntime().freeMemory()}/" + | ||
"${java.lang.Runtime.getRuntime().maxMemory()}") | ||
|
||
when { | ||
content.isEmpty() -> { | ||
call.respond(HttpStatusCode.BadRequest, NO_CONTENT_PROVIDED_MESSAGE) | ||
} | ||
type != "xml" && type != "json" -> { | ||
call.respond(HttpStatusCode.BadRequest, INVALID_TYPE_MESSAGE) | ||
} | ||
toType != "xml" && toType != "json" -> { | ||
call.respond(HttpStatusCode.BadRequest, INVALID_TO_TYPE_MESSAGE) | ||
} | ||
|
||
else -> { | ||
try { | ||
val (response, session) = conversionController.convertRequest(content, type, version, toType, | ||
toVersion, sessionId) | ||
val contentType = if (toType == "xml") ContentType.Application.Xml else ContentType.Application.Json | ||
call.response.headers.append("Session-Id", session) | ||
call.respondText(response, contentType, HttpStatusCode.OK) | ||
} catch (e: Exception) { | ||
logger.error(e.localizedMessage, e) | ||
call.respond(HttpStatusCode.InternalServerError, e.localizedMessage) | ||
} | ||
} | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/jvmMain/kotlin/controller/validation/ValidationServiceFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
package controller.validation | ||
|
||
import org.hl7.fhir.validation.ValidationEngine | ||
import org.hl7.fhir.validation.cli.services.ValidationService | ||
|
||
interface ValidationServiceFactory { | ||
|
||
fun getValidationService() : ValidationService | ||
|
||
fun getCachedValidator(sessionId: String?) : ValidationEngine? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters