Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.util.HashMap;
import java.util.Map;

import static com.google.common.base.Preconditions.checkNotNull;

public class HttpResponseImpl implements HttpResponse {

private static final Logger LOG = LoggerFactory.getLogger(HttpResponseImpl.class);
Expand Down Expand Up @@ -123,8 +125,13 @@ public Map<String, String> headers() {
@Override
public <T> T readEntity(Class<T> typeToReadAs) {
HttpEntity entity = response.getEntity();
if (entity == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use checkNotNull(entity) here, this will make it more compact and readable, what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because checkNotNull(entity) would throw a NullPointerException if entity == null, which is exactly the issue #825. The goal of this pull request is to accept without error the case where entity == null, e.g. for HEAD requests.

For example, in Jersey2 connector, readEntity will return null for a HEAD request.

// Normal case if the response has no content, e.g. for a HEAD request
return null;
}
try {
return ObjectMapperSingleton.getContext(typeToReadAs).reader(typeToReadAs).readValue(entity.getContent());
InputStream content = checkNotNull(entity.getContent(), "Entity content should not be null.");
return ObjectMapperSingleton.getContext(typeToReadAs).readerFor(typeToReadAs).readValue(content);
} catch (Exception e) {
LOG.error(e.getMessage(), e);
throw new ClientResponseException(e.getMessage(), 0, e);
Expand Down