Skip to content

Commit

Permalink
#414: avoid NPE evaluating response mimetype when response entity is …
Browse files Browse the repository at this point in the history
…null using HttpClient5
  • Loading branch information
Christian Ohr committed Oct 31, 2024
1 parent ff58761 commit a6a6301
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected IHttpRequest createHttpRequest(Map<String, List<String>> 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);
}

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -123,8 +122,10 @@ public List<String> 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
Expand Down

0 comments on commit a6a6301

Please sign in to comment.