Search in sources :

Example 1 with LogExecutionTime

use of uk.nhs.adaptors.scr.logging.LogExecutionTime in project summary-care-record-api by NHSDigital.

the class SpineClient method sendScrData.

@SneakyThrows
@Override
@LogExecutionTime
public Response<String> sendScrData(String requestBody, String nhsdAsid, String nhsdIdentity, String nhsdSessionUrid) {
    var url = spineConfiguration.getUrl() + spineConfiguration.getScrEndpoint();
    LOGGER.info("Sending SCR Upload request to SPINE. URL: {}", url);
    LOGGER.debug("Body: {}", requestBody);
    var request = new HttpPost(url);
    setUploadScrHeaders(request, nhsdAsid, nhsdIdentity, nhsdSessionUrid);
    request.setEntity(new StringEntity(requestBody, UTF_8));
    var response = spineHttpClient.sendRequest(request, stringResponseHandler);
    var statusCode = response.getStatusCode();
    if (statusCode != ACCEPTED.value()) {
        LOGGER.error("Unexpected spine SCR POST response: {} {}", statusCode, response.getBody());
        throw new UnexpectedSpineResponseException("Unexpected spine 'send data' response " + statusCode);
    }
    LOGGER.info("Received Spine {} interaction response: HTTP status {}", UPLOAD_SCR_SOAP_ACTION, response.getStatusCode());
    return response;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) UnexpectedSpineResponseException(uk.nhs.adaptors.scr.exceptions.UnexpectedSpineResponseException) LogExecutionTime(uk.nhs.adaptors.scr.logging.LogExecutionTime) SneakyThrows(lombok.SneakyThrows)

Example 2 with LogExecutionTime

use of uk.nhs.adaptors.scr.logging.LogExecutionTime in project summary-care-record-api by NHSDigital.

the class SpineClient method getScrProcessingResult.

@Override
@LogExecutionTime
public ProcessingResult getScrProcessingResult(String contentLocation, long initialWaitTime, String nhsdAsid, String nhsdIdentity, String nhsdSessionUrid) {
    var repeatTimeout = spineConfiguration.getScrResultRepeatTimeout();
    var template = RetryTemplate.builder().withinMillis(repeatTimeout).customBackoff(new ScrRetryBackoffPolicy()).retryOn(NoSpineResultException.class).build();
    LOGGER.info("Starting polling result. First request in {}ms", initialWaitTime);
    try {
        Thread.sleep(initialWaitTime);
    } catch (InterruptedException e) {
        throw new ScrTimeoutException(e);
    }
    return template.execute(ctx -> {
        LOGGER.info("Fetching SCR processing result. RetryCount={}", ctx.getRetryCount());
        var request = new HttpGet(spineConfiguration.getUrl() + contentLocation);
        setCommonHeaders(request, nhsdAsid, nhsdIdentity, nhsdSessionUrid);
        var result = spineHttpClient.sendRequest(request, stringResponseHandler);
        int statusCode = result.getStatusCode();
        if (statusCode == OK.value()) {
            LOGGER.info("{} processing result received.", statusCode);
            return ProcessingResult.parseProcessingResult(result.getBody());
        } else if (statusCode == ACCEPTED.value()) {
            var nextRetryAfter = Long.parseLong(SpineHttpClient.getHeader(result.getHeaders(), RETRY_AFTER));
            LOGGER.info("{} received. NextRetry in {}ms", statusCode, nextRetryAfter);
            throw new NoSpineResultException(nextRetryAfter);
        } else {
            LOGGER.error("Unexpected spine polling response: {} {}", statusCode, result.getBody());
            throw new UnexpectedSpineResponseException("Unexpected spine polling response " + statusCode);
        }
    });
}
Also used : UnexpectedSpineResponseException(uk.nhs.adaptors.scr.exceptions.UnexpectedSpineResponseException) HttpGet(org.apache.http.client.methods.HttpGet) NoSpineResultException(uk.nhs.adaptors.scr.exceptions.NoSpineResultException) BackOffInterruptedException(org.springframework.retry.backoff.BackOffInterruptedException) ScrTimeoutException(uk.nhs.adaptors.scr.exceptions.ScrTimeoutException) LogExecutionTime(uk.nhs.adaptors.scr.logging.LogExecutionTime)

Example 3 with LogExecutionTime

use of uk.nhs.adaptors.scr.logging.LogExecutionTime in project summary-care-record-api by NHSDigital.

the class GetScrController method getScrId.

@GetMapping(path = "/DocumentReference", produces = { APPLICATION_FHIR_JSON_VALUE })
@SuppressWarnings("checkstyle:parameternumber")
@LogExecutionTime
public String getScrId(@RequestHeader(NHSD_ASID) @NotNull String nhsdAsid, @RequestHeader(CLIENT_IP) @NotNull String clientIp, @RequestParam("patient") @NotNull @PatientId String patient, @RequestParam(required = false) @TypeCode String type, @RequestParam(name = "_sort", required = false) @SortMethod String sort, @RequestParam(name = "_count", required = false) @RecordCount Integer count) {
    LOGGER.info("Received GET SCR ID request");
    String nhsNumber = extractNhsNumber(patient);
    Bundle bundle = getScrService.getScrId(nhsNumber, nhsdAsid, clientIp);
    return fhirParser.encodeToJson(bundle);
}
Also used : Bundle(org.hl7.fhir.r4.model.Bundle) LogExecutionTime(uk.nhs.adaptors.scr.logging.LogExecutionTime) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 4 with LogExecutionTime

use of uk.nhs.adaptors.scr.logging.LogExecutionTime in project summary-care-record-api by NHSDigital.

the class GetScrService method getScr.

@LogExecutionTime
public Bundle getScr(String nhsNumber, String compositionId, String nhsdAsid, String clientIp) {
    Document scrIdXml = getScrIdRawXml(nhsNumber, nhsdAsid, clientIp);
    checkDetectedIssues(scrIdXml);
    EventListQueryResponse response = eventListQueryResponseParser.parseXml(scrIdXml);
    if (StringUtils.equals(response.getLatestScrId(), compositionId)) {
        Document document = getScrRawXml(response.getLatestScrId(), nhsNumber, nhsdAsid, clientIp);
        logXml("Received SCR XML: {}", document);
        checkDetectedIssues(document);
        var bundle = interactionMapper.map(document);
        Patient patient = recordTargetMapper.mapPatient(document);
        Stream.of(gpSummaryMapper, diagnosisMapper, findingMapper).map(mapper -> mapper.map(document)).flatMap(resources -> resources.stream()).peek(it -> setPatientReferences(it, patient)).map(resource -> getBundleEntryComponent(resource)).forEach(bundle::addEntry);
        bundle.addEntry(getBundleEntryComponent(patient));
        bundle.setTotal(bundle.getEntry().size());
        return bundle;
    } else {
        return interactionMapper.mapToEmpty();
    }
}
Also used : SpineClientContract(uk.nhs.adaptors.scr.clients.spine.SpineClientContract) TemplateUtils(uk.nhs.adaptors.scr.utils.TemplateUtils) Mustache(com.github.mustachejava.Mustache) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Identifier(org.hl7.fhir.r4.model.Identifier) Response(uk.nhs.adaptors.scr.clients.spine.SpineHttpClient.Response) CodeableConcept(org.hl7.fhir.r4.model.CodeableConcept) DocumentReferenceContextComponent(org.hl7.fhir.r4.model.DocumentReference.DocumentReferenceContextComponent) Autowired(org.springframework.beans.factory.annotation.Autowired) EventListQueryResponse(uk.nhs.adaptors.scr.models.EventListQueryResponse) Reference(org.hl7.fhir.r4.model.Reference) OffsetDateTime.now(java.time.OffsetDateTime.now) StringUtils(org.apache.commons.lang3.StringUtils) RelatedPerson(org.hl7.fhir.r4.model.RelatedPerson) EventListQueryResponseParser(uk.nhs.adaptors.scr.models.EventListQueryResponseParser) GpSummaryMapper(uk.nhs.adaptors.scr.mappings.from.hl7.GpSummaryMapper) Attachment(org.hl7.fhir.r4.model.Attachment) DocumentReferenceContentComponent(org.hl7.fhir.r4.model.DocumentReference.DocumentReferenceContentComponent) Document(org.w3c.dom.Document) Arrays.asList(java.util.Arrays.asList) BundleEntryComponent(org.hl7.fhir.r4.model.Bundle.BundleEntryComponent) SpineConfiguration(uk.nhs.adaptors.scr.config.SpineConfiguration) CURRENT(org.hl7.fhir.r4.model.Enumerations.DocumentReferenceStatus.CURRENT) HtmlParser.serialize(uk.nhs.adaptors.scr.mappings.from.hl7.HtmlParser.serialize) Patient(org.hl7.fhir.r4.model.Patient) DiagnosisMapper(uk.nhs.adaptors.scr.mappings.from.hl7.DiagnosisMapper) MATCH(org.hl7.fhir.r4.model.Bundle.SearchEntryMode.MATCH) TemplateUtils.loadTemplate(uk.nhs.adaptors.scr.utils.TemplateUtils.loadTemplate) DocumentReference(org.hl7.fhir.r4.model.DocumentReference) ScrConfiguration(uk.nhs.adaptors.scr.config.ScrConfiguration) InteractionMapper(uk.nhs.adaptors.scr.mappings.from.hl7.InteractionMapper) Resource(org.hl7.fhir.r4.model.Resource) SEARCHSET(org.hl7.fhir.r4.model.Bundle.BundleType.SEARCHSET) Composition(org.hl7.fhir.r4.model.Composition) EventQueryParams(uk.nhs.adaptors.scr.models.EventQueryParams) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) Stream(java.util.stream.Stream) FindingMapper(uk.nhs.adaptors.scr.mappings.from.hl7.FindingMapper) Coding(org.hl7.fhir.r4.model.Coding) MDC(org.slf4j.MDC) EventListQueryParams(uk.nhs.adaptors.scr.models.EventListQueryParams) DateTimeFormatter(java.time.format.DateTimeFormatter) UTC(java.time.ZoneOffset.UTC) FhirHelper.randomUUID(uk.nhs.adaptors.scr.utils.FhirHelper.randomUUID) Bundle(org.hl7.fhir.r4.model.Bundle) RecordTargetMapper(uk.nhs.adaptors.scr.mappings.from.hl7.RecordTargetMapper) LogExecutionTime(uk.nhs.adaptors.scr.logging.LogExecutionTime) EventListQueryResponse(uk.nhs.adaptors.scr.models.EventListQueryResponse) Patient(org.hl7.fhir.r4.model.Patient) Document(org.w3c.dom.Document) LogExecutionTime(uk.nhs.adaptors.scr.logging.LogExecutionTime)

Example 5 with LogExecutionTime

use of uk.nhs.adaptors.scr.logging.LogExecutionTime in project summary-care-record-api by NHSDigital.

the class GetScrService method getScrId.

@LogExecutionTime
public Bundle getScrId(String nhsNumber, String nhsdAsid, String clientIp) {
    Document scrIdXml = getScrIdRawXml(nhsNumber, nhsdAsid, clientIp);
    checkDetectedIssues(scrIdXml);
    EventListQueryResponse response = eventListQueryResponseParser.parseXml(scrIdXml);
    Bundle bundle = buildBundle();
    if (StringUtils.isNotEmpty(response.getLatestScrId())) {
        bundle.setTotal(1);
        Patient patient = buildPatientResource(nhsNumber);
        DocumentReference documentReference = buildDocumentReference(nhsNumber, response, patient);
        bundle.addEntry(new BundleEntryComponent().setFullUrl(getScrUrl() + "/DocumentReference/" + documentReference.getId()).setResource(documentReference).setSearch(new Bundle.BundleEntrySearchComponent().setMode(MATCH)));
        bundle.addEntry(new BundleEntryComponent().setFullUrl(patient.getId()).setResource(patient));
    } else {
        bundle.setTotal(0);
    }
    return bundle;
}
Also used : BundleEntryComponent(org.hl7.fhir.r4.model.Bundle.BundleEntryComponent) Bundle(org.hl7.fhir.r4.model.Bundle) EventListQueryResponse(uk.nhs.adaptors.scr.models.EventListQueryResponse) Patient(org.hl7.fhir.r4.model.Patient) Document(org.w3c.dom.Document) DocumentReference(org.hl7.fhir.r4.model.DocumentReference) LogExecutionTime(uk.nhs.adaptors.scr.logging.LogExecutionTime)

Aggregations

LogExecutionTime (uk.nhs.adaptors.scr.logging.LogExecutionTime)12 UnexpectedSpineResponseException (uk.nhs.adaptors.scr.exceptions.UnexpectedSpineResponseException)6 SneakyThrows (lombok.SneakyThrows)5 HttpPost (org.apache.http.client.methods.HttpPost)5 StringEntity (org.apache.http.entity.StringEntity)5 Bundle (org.hl7.fhir.r4.model.Bundle)4 BundleEntryComponent (org.hl7.fhir.r4.model.Bundle.BundleEntryComponent)2 DocumentReference (org.hl7.fhir.r4.model.DocumentReference)2 Patient (org.hl7.fhir.r4.model.Patient)2 Document (org.w3c.dom.Document)2 EventListQueryResponse (uk.nhs.adaptors.scr.models.EventListQueryResponse)2 Mustache (com.github.mustachejava.Mustache)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 IOException (java.io.IOException)1 OffsetDateTime.now (java.time.OffsetDateTime.now)1 UTC (java.time.ZoneOffset.UTC)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 Arrays.asList (java.util.Arrays.asList)1 Stream (java.util.stream.Stream)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1