Search in sources :

Example 1 with UnexpectedSpineResponseException

use of uk.nhs.adaptors.scr.exceptions.UnexpectedSpineResponseException 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 UnexpectedSpineResponseException

use of uk.nhs.adaptors.scr.exceptions.UnexpectedSpineResponseException 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 UnexpectedSpineResponseException

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

the class UploadScrService method uploadScr.

@LogExecutionTime
public void uploadScr(RequestData requestData) {
    Bundle bundle = fhirParser.parseResource(requestData.getBody(), Bundle.class);
    var spineRequest = bundleMapper.map(bundle, requestData.getNhsdAsid());
    checkPermission(bundle, requestData.getNhsdAsid(), requestData.getClientIp());
    var response = spineClient.sendScrData(spineRequest, requestData.getNhsdAsid(), requestData.getNhsdIdentity(), requestData.getNhsdSessionUrid());
    String contentLocation;
    long retryAfter;
    try {
        contentLocation = getHeader(response.getHeaders(), CONTENT_LOCATION);
        retryAfter = parseLong(getHeader(response.getHeaders(), RETRY_AFTER));
    } catch (Exception ex) {
        throw new UnexpectedSpineResponseException("Unable to extract required headers", ex);
    }
    var processingResult = spineClient.getScrProcessingResult(contentLocation, retryAfter, requestData.getNhsdAsid(), requestData.getNhsdIdentity(), requestData.getNhsdSessionUrid());
    validateProcessingResult(processingResult);
}
Also used : UnexpectedSpineResponseException(uk.nhs.adaptors.scr.exceptions.UnexpectedSpineResponseException) Bundle(org.hl7.fhir.r4.model.Bundle) XPathExpressionException(javax.xml.xpath.XPathExpressionException) UnexpectedSpineResponseException(uk.nhs.adaptors.scr.exceptions.UnexpectedSpineResponseException) NonSuccessSpineProcessingResultException(uk.nhs.adaptors.scr.exceptions.NonSuccessSpineProcessingResultException) LogExecutionTime(uk.nhs.adaptors.scr.logging.LogExecutionTime)

Example 4 with UnexpectedSpineResponseException

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

the class SpineClient method sendGetScrId.

@SneakyThrows
@Override
@LogExecutionTime
public Response<Document> sendGetScrId(String requestBody, String nhsdAsid) {
    LOGGER.info("Sending GET SCR ID Spine request");
    LOGGER.debug("Body: {}", requestBody);
    var request = new HttpPost(spineConfiguration.getUrl() + spineConfiguration.getPsisQueriesEndpoint());
    setSoapHeaders(request, PSIS_EVENT_LIST_QUERY, TEXT_XML_VALUE);
    request.setEntity(new StringEntity(requestBody, UTF_8));
    var response = spineHttpClient.sendRequest(request, xmlResponseHandler);
    var statusCode = response.getStatusCode();
    if (statusCode != OK.value()) {
        LOGGER.error("Unexpected spine GET SCR ID response: {} {}", statusCode, serialize(response.getBody()));
        throw new UnexpectedSpineResponseException("Unexpected spine send response " + statusCode);
    }
    LOGGER.info("Received Spine {} interaction response: HTTP status {}", PSIS_EVENT_LIST_QUERY, 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 5 with UnexpectedSpineResponseException

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

the class SpineClient method sendAcsData.

@SneakyThrows
@Override
@LogExecutionTime
public Response<Document> sendAcsData(String requestBody, String nhsdAsid) {
    var url = spineConfiguration.getUrl() + spineConfiguration.getAcsEndpoint();
    LOGGER.info("Sending ACS Set Permission Spine request. URL: {}", url);
    LOGGER.debug("Body: {}", requestBody);
    var request = new HttpPost(url);
    setSoapHeaders(request, SET_PERMISSION_SOAP_ACTION, TEXT_XML_VALUE);
    request.setHeader(NHSD_ASID, nhsdAsid);
    request.setEntity(new StringEntity(requestBody, UTF_8));
    var response = spineHttpClient.sendRequest(request, xmlResponseHandler);
    var statusCode = response.getStatusCode();
    if (statusCode != OK.value()) {
        LOGGER.error("Unexpected spine ACS set permission response: {}", response);
        throw new UnexpectedSpineResponseException("Unexpected spine 'send data' response " + statusCode);
    }
    LOGGER.info("Received Spine {} interaction response: HTTP status {}", SET_PERMISSION_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)

Aggregations

UnexpectedSpineResponseException (uk.nhs.adaptors.scr.exceptions.UnexpectedSpineResponseException)8 LogExecutionTime (uk.nhs.adaptors.scr.logging.LogExecutionTime)6 SneakyThrows (lombok.SneakyThrows)5 HttpPost (org.apache.http.client.methods.HttpPost)4 StringEntity (org.apache.http.entity.StringEntity)4 XPathExpressionException (javax.xml.xpath.XPathExpressionException)2 NonSuccessSpineProcessingResultException (uk.nhs.adaptors.scr.exceptions.NonSuccessSpineProcessingResultException)2 HttpGet (org.apache.http.client.methods.HttpGet)1 Bundle (org.hl7.fhir.r4.model.Bundle)1 OperationOutcome (org.hl7.fhir.r4.model.OperationOutcome)1 HttpStatus (org.springframework.http.HttpStatus)1 BackOffInterruptedException (org.springframework.retry.backoff.BackOffInterruptedException)1 NodeList (org.w3c.dom.NodeList)1 BadRequestException (uk.nhs.adaptors.scr.exceptions.BadRequestException)1 NoSpineResultException (uk.nhs.adaptors.scr.exceptions.NoSpineResultException)1 ScrTimeoutException (uk.nhs.adaptors.scr.exceptions.ScrTimeoutException)1