From b6f289cd8fe37eb5ffc52485ac9efbc67a8e7772 Mon Sep 17 00:00:00 2001 From: Mohammad Iqbal Date: Tue, 26 Mar 2024 09:37:43 +0000 Subject: [PATCH] [PRMT-4678] - Added try-with-resources block in httpClient --- .../services/ehr_repo/EhrRepoClient.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/uk/nhs/prm/repo/ehrtransferservice/services/ehr_repo/EhrRepoClient.java b/src/main/java/uk/nhs/prm/repo/ehrtransferservice/services/ehr_repo/EhrRepoClient.java index 5eb6cbdee..d513f6aa3 100644 --- a/src/main/java/uk/nhs/prm/repo/ehrtransferservice/services/ehr_repo/EhrRepoClient.java +++ b/src/main/java/uk/nhs/prm/repo/ehrtransferservice/services/ehr_repo/EhrRepoClient.java @@ -83,14 +83,16 @@ public StoreMessageResponseBody confirmMessageStored(ParsedMessage parsedMessage .header("traceId", tracer.getTraceId()) .POST(jsonPayload).build(); - var response = HttpClient.newHttpClient() - .send(request, HttpResponse.BodyHandlers.ofString()); - + try (var httpClient = HttpClient.newHttpClient()) { + var response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); - if (response.statusCode() != 201) { - throw new HttpException(String.format("Unexpected response from EHR while checking if a message was stored: %d", response.statusCode())); + if (response.statusCode() != 201) { + throw new HttpException(String.format("Unexpected response from EHR while checking if a message was stored: %d", response.statusCode())); + } + return parseResponse(response); + } catch (Exception exception) { + throw new RuntimeException("Error encountered when uploading message to S3", exception); } - return parseResponse(response); } public void softDeleteEhrRecord(String nhsNumber) {