From a6a63018bc81e688c7b994313fd301ffe39fbd44 Mon Sep 17 00:00:00 2001 From: Christian Ohr Date: Thu, 31 Oct 2024 10:44:00 +0100 Subject: [PATCH] #414: avoid NPE evaluating response mimetype when response entity is null using HttpClient5 --- .../ipf/commons/ihe/fhir/ApacheHttpClient5.java | 4 ++-- .../ipf/commons/ihe/fhir/ApacheHttpRequest5.java | 2 +- .../ipf/commons/ihe/fhir/ApacheHttpResponse5.java | 9 +++++---- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/commons/ihe/fhir/core/src/main/java/org/openehealth/ipf/commons/ihe/fhir/ApacheHttpClient5.java b/commons/ihe/fhir/core/src/main/java/org/openehealth/ipf/commons/ihe/fhir/ApacheHttpClient5.java index eb90d91349..619d8c3b23 100644 --- a/commons/ihe/fhir/core/src/main/java/org/openehealth/ipf/commons/ihe/fhir/ApacheHttpClient5.java +++ b/commons/ihe/fhir/core/src/main/java/org/openehealth/ipf/commons/ihe/fhir/ApacheHttpClient5.java @@ -63,7 +63,7 @@ protected IHttpRequest createHttpRequest(Map> params) { params.forEach((key, value) -> value.stream() .map(s -> new BasicNameValuePair(key, s)) .forEach(parameters::add)); - UrlEncodedFormEntity entity = createFormEntity(parameters); + var entity = createFormEntity(parameters); return createHttpRequest(entity); } @@ -81,7 +81,7 @@ private ApacheHttpRequest5 createHttpRequest(HttpEntity entity) { } private ClassicRequestBuilder constructRequest(HttpEntity entity) { - String url = myUrl.toString(); + var url = myUrl.toString(); return switch (myRequestType) { case DELETE -> ClassicRequestBuilder.delete(url); case PATCH -> ClassicRequestBuilder.patch(url).setEntity(entity); diff --git a/commons/ihe/fhir/core/src/main/java/org/openehealth/ipf/commons/ihe/fhir/ApacheHttpRequest5.java b/commons/ihe/fhir/core/src/main/java/org/openehealth/ipf/commons/ihe/fhir/ApacheHttpRequest5.java index b83bb3d51b..b1b7532a2c 100644 --- a/commons/ihe/fhir/core/src/main/java/org/openehealth/ipf/commons/ihe/fhir/ApacheHttpRequest5.java +++ b/commons/ihe/fhir/core/src/main/java/org/openehealth/ipf/commons/ihe/fhir/ApacheHttpRequest5.java @@ -51,7 +51,7 @@ public IHttpResponse execute() throws IOException { var responseStopWatch = new StopWatch(); try { // BaseClient will close the response stream for us - ClassicHttpResponse response = httpClient.executeOpen(RoutingSupport.determineHost(request), request, null); + var response = httpClient.executeOpen(RoutingSupport.determineHost(request), request, null); return new ApacheHttpResponse5(response, responseStopWatch); } catch (HttpException e) { throw new IOException(e); diff --git a/commons/ihe/fhir/core/src/main/java/org/openehealth/ipf/commons/ihe/fhir/ApacheHttpResponse5.java b/commons/ihe/fhir/core/src/main/java/org/openehealth/ipf/commons/ihe/fhir/ApacheHttpResponse5.java index ce56b27d39..ea55267731 100644 --- a/commons/ihe/fhir/core/src/main/java/org/openehealth/ipf/commons/ihe/fhir/ApacheHttpResponse5.java +++ b/commons/ihe/fhir/core/src/main/java/org/openehealth/ipf/commons/ihe/fhir/ApacheHttpResponse5.java @@ -17,7 +17,6 @@ import ca.uhn.fhir.i18n.Msg; import ca.uhn.fhir.rest.api.Constants; -import ca.uhn.fhir.rest.client.apache.ApacheHttpResponse; import ca.uhn.fhir.rest.client.api.IHttpResponse; import ca.uhn.fhir.rest.client.impl.BaseHttpResponse; import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; @@ -38,7 +37,7 @@ public class ApacheHttpResponse5 extends BaseHttpResponse implements IHttpResponse { - private static final Logger log = LoggerFactory.getLogger(ApacheHttpResponse.class); + private static final Logger log = LoggerFactory.getLogger(ApacheHttpResponse5.class); private final ClassicHttpResponse response; private boolean entityBuffered = false; @@ -123,8 +122,10 @@ public List getHeaders(String s) { @Override public String getMimeType() { - ContentType ct = ContentType.parse(response.getEntity().getContentType()); - return ct != null ? ct.getMimeType() : null; + return Optional.ofNullable(response.getEntity()) + .map(entity -> ContentType.parse(entity.getContentType())) + .map(ContentType::getMimeType) + .orElse(null); } @Override