-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged in classes from IOPS-Utilty repository
This is used to cache IG's in AWS S3 to reduce application start up time
- Loading branch information
1 parent
1dfb33a
commit 943c2a8
Showing
8 changed files
with
761 additions
and
4 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
79 changes: 79 additions & 0 deletions
79
src/main/kotlin/uk/nhs/england/fhirvalidator/awsProvider/AWSBinary.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,79 @@ | ||
package uk.nhs.england.fhirvalidator.awsProvider | ||
|
||
import ca.uhn.fhir.context.FhirContext | ||
import ca.uhn.fhir.rest.api.MethodOutcome | ||
import ca.uhn.fhir.rest.client.api.IGenericClient | ||
import org.hl7.fhir.instance.model.api.IBaseBundle | ||
import org.hl7.fhir.r4.model.* | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.beans.factory.annotation.Qualifier | ||
import org.springframework.stereotype.Component | ||
import uk.nhs.england.fhirvalidator.configuration.FHIRServerProperties | ||
import uk.nhs.england.fhirvalidator.configuration.MessageProperties | ||
import uk.nhs.england.fhirvalidator.interceptor.CognitoAuthInterceptor | ||
import java.io.File | ||
import java.nio.file.Files | ||
import java.util.* | ||
|
||
@Component | ||
class AWSBinary(val messageProperties: MessageProperties, val awsClient: IGenericClient, | ||
//sqs: AmazonSQS?, | ||
@Qualifier("R4") val ctx: FhirContext, | ||
val fhirServerProperties: FHIRServerProperties, | ||
private val cognitoAuthInterceptor: CognitoAuthInterceptor | ||
) { | ||
|
||
private val log = LoggerFactory.getLogger("FHIRAudit") | ||
|
||
fun get(url : String): ImplementationGuide? { | ||
var bundle: Bundle? = null | ||
var retry = 3 | ||
while (retry > 0) { | ||
try { | ||
bundle = awsClient | ||
.search<IBaseBundle>() | ||
.forResource(ImplementationGuide::class.java) | ||
.where( | ||
ImplementationGuide.URL.matches().value(url) | ||
) | ||
.returnBundle(Bundle::class.java) | ||
.execute() | ||
break | ||
} catch (ex: Exception) { | ||
// do nothing | ||
log.error(ex.message) | ||
retry-- | ||
if (retry == 0) throw ex | ||
} | ||
} | ||
if (bundle == null || !bundle.hasEntry()) return null | ||
return bundle.entryFirstRep.resource as ImplementationGuide | ||
} | ||
|
||
fun get(internalId: IdType): Binary { | ||
val binary = Binary() | ||
var path = internalId.value | ||
if (!path.startsWith("/")) path = "/" + path | ||
val location = cognitoAuthInterceptor.getBinaryLocation(path) | ||
println(location.getString("presignedGetUrl")) | ||
binary.id = location.getString("id") | ||
binary.contentType = location.getString("contentType") | ||
val conn = cognitoAuthInterceptor.getBinary(location.getString("presignedGetUrl")) | ||
binary.data = conn.inputStream.readAllBytes() | ||
return binary | ||
} | ||
|
||
public fun create(fileName : String): MethodOutcome? { | ||
|
||
var response: MethodOutcome? = null | ||
val binary = Binary() | ||
binary.contentType = "application/gzip" | ||
val json = cognitoAuthInterceptor.postBinaryLocation(binary) | ||
val location = json.getString("presignedPutUrl") | ||
binary.id = json.getString("id") | ||
var file = File(fileName) | ||
val fileContent: ByteArray = Files.readAllBytes(file.toPath()) | ||
cognitoAuthInterceptor.postBinary(location,fileContent) | ||
return MethodOutcome().setResource(binary) | ||
} | ||
} |
144 changes: 144 additions & 0 deletions
144
src/main/kotlin/uk/nhs/england/fhirvalidator/awsProvider/AWSImplementationGuide.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,144 @@ | ||
package uk.nhs.england.fhirvalidator.awsProvider | ||
|
||
import ca.uhn.fhir.context.FhirContext | ||
import ca.uhn.fhir.rest.api.MethodOutcome | ||
import ca.uhn.fhir.rest.client.api.IGenericClient | ||
import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException | ||
import org.hl7.fhir.instance.model.api.IBaseBundle | ||
import org.hl7.fhir.r4.model.* | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.beans.factory.annotation.Qualifier | ||
import org.springframework.stereotype.Component | ||
import uk.nhs.england.fhirvalidator.configuration.FHIRServerProperties | ||
import uk.nhs.england.fhirvalidator.configuration.MessageProperties | ||
import java.util.* | ||
|
||
@Component | ||
class AWSImplementationGuide(val messageProperties: MessageProperties, val awsClient: IGenericClient, | ||
//sqs: AmazonSQS?, | ||
@Qualifier("R4") val ctx: FhirContext, | ||
val fhirServerProperties: FHIRServerProperties, | ||
val awsAuditEvent: AWSAuditEvent | ||
) { | ||
|
||
private val log = LoggerFactory.getLogger("FHIRAudit") | ||
|
||
|
||
fun createUpdate(newImplementationGuide: ImplementationGuide): ImplementationGuide? { | ||
var awsBundle: Bundle? = null | ||
if (!newImplementationGuide.hasUrl()) throw UnprocessableEntityException("ImplementationGuide has no identifier") | ||
var nhsIdentifier = newImplementationGuide.url | ||
if (nhsIdentifier == null) throw UnprocessableEntityException("ImplementationGuide has no identifier") | ||
var retry = 3 | ||
while (retry > 0) { | ||
try { | ||
|
||
awsBundle = awsClient!!.search<IBaseBundle>().forResource(ImplementationGuide::class.java) | ||
.where( | ||
ImplementationGuide.URL.matches().value(nhsIdentifier) | ||
) | ||
.returnBundle(Bundle::class.java) | ||
.execute() | ||
break | ||
} catch (ex: Exception) { | ||
// do nothing | ||
log.error(ex.message) | ||
retry-- | ||
if (retry == 0) throw ex | ||
} | ||
} | ||
|
||
|
||
// This v3esquw data should have been processed into propoer resources so remove | ||
newImplementationGuide.contained = ArrayList() | ||
|
||
if (awsBundle!!.hasEntry() && awsBundle.entryFirstRep.hasResource() | ||
&& awsBundle.entryFirstRep.hasResource() | ||
&& awsBundle.entryFirstRep.resource is ImplementationGuide | ||
) { | ||
val diagnosticReport = awsBundle.entryFirstRep.resource as ImplementationGuide | ||
// Dont update for now - just return aws ImplementationGuide | ||
return update(diagnosticReport, newImplementationGuide)!!.resource as ImplementationGuide | ||
} else { | ||
return create(newImplementationGuide)!!.resource as ImplementationGuide | ||
} | ||
} | ||
|
||
public fun get(url : String): ImplementationGuide? { | ||
var bundle: Bundle? = null | ||
var retry = 3 | ||
while (retry > 0) { | ||
try { | ||
bundle = awsClient | ||
.search<IBaseBundle>() | ||
.forResource(ImplementationGuide::class.java) | ||
.where( | ||
ImplementationGuide.URL.matches().value(url) | ||
) | ||
.returnBundle(Bundle::class.java) | ||
.execute() | ||
break | ||
} catch (ex: Exception) { | ||
// do nothing | ||
log.error(ex.message) | ||
retry-- | ||
if (retry == 0) throw ex | ||
} | ||
} | ||
if (bundle == null || !bundle.hasEntry()) return null | ||
return bundle.entryFirstRep.resource as ImplementationGuide | ||
} | ||
|
||
private fun update(implementationGuide: ImplementationGuide, newImplementationGuide: ImplementationGuide): MethodOutcome? { | ||
var response: MethodOutcome? = null | ||
var changed: Boolean | ||
|
||
// TODO do change detection | ||
changed = true | ||
|
||
if (!changed) return MethodOutcome().setResource(implementationGuide) | ||
var retry = 3 | ||
while (retry > 0) { | ||
try { | ||
newImplementationGuide.id = implementationGuide.idElement.value | ||
response = awsClient!!.update().resource(newImplementationGuide).withId(implementationGuide.id).execute() | ||
log.info("AWS ImplementationGuide updated " + response.resource.idElement.value) | ||
val auditEvent = awsAuditEvent.createAudit(implementationGuide, AuditEvent.AuditEventAction.C) | ||
awsAuditEvent.writeAWS(auditEvent) | ||
break | ||
} catch (ex: Exception) { | ||
// do nothing | ||
log.error(ex.message) | ||
retry-- | ||
if (retry == 0) throw ex | ||
} | ||
} | ||
return response | ||
|
||
} | ||
|
||
private fun create(newImplementationGuide: ImplementationGuide): MethodOutcome? { | ||
|
||
var response: MethodOutcome? = null | ||
|
||
var retry = 3 | ||
while (retry > 0) { | ||
try { | ||
response = awsClient | ||
.create() | ||
.resource(newImplementationGuide) | ||
.execute() | ||
val diagnosticReport = response.resource as ImplementationGuide | ||
val auditEvent = awsAuditEvent.createAudit(diagnosticReport, AuditEvent.AuditEventAction.C) | ||
awsAuditEvent.writeAWS(auditEvent) | ||
break | ||
} catch (ex: Exception) { | ||
// do nothing | ||
log.error(ex.message) | ||
retry-- | ||
if (retry == 0) throw ex | ||
} | ||
} | ||
return response | ||
} | ||
} |
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
30 changes: 30 additions & 0 deletions
30
src/main/kotlin/uk/nhs/england/fhirvalidator/provider/BinaryProvider.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,30 @@ | ||
package uk.nhs.england.fhirvalidator.provider | ||
|
||
import ca.uhn.fhir.context.FhirContext | ||
import ca.uhn.fhir.rest.annotation.* | ||
import ca.uhn.fhir.rest.server.IResourceProvider | ||
import jakarta.servlet.http.HttpServletRequest | ||
import mu.KLogging | ||
import org.hl7.fhir.r4.model.* | ||
import org.springframework.beans.factory.annotation.Qualifier | ||
import org.springframework.stereotype.Component | ||
import uk.nhs.england.fhirvalidator.awsProvider.AWSBinary | ||
import uk.nhs.england.fhirvalidator.interceptor.CognitoAuthInterceptor | ||
|
||
@Component | ||
class BinaryProvider( | ||
private val awsBinary: AWSBinary, | ||
|
||
) : IResourceProvider { | ||
companion object : KLogging() | ||
|
||
override fun getResourceType(): Class<Binary> { | ||
return Binary::class.java | ||
} | ||
|
||
@Read | ||
fun read(httpRequest : HttpServletRequest, @IdParam internalId: IdType): Binary? { | ||
return awsBinary.get(internalId) | ||
} | ||
|
||
} |
Oops, something went wrong.