Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #825 - NullPointerException in HttpClient connector #826

Merged
merged 1 commit into from Sep 27, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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