Skip to content
This repository has been archived by the owner on May 28, 2018. It is now read-only.

ClientResponse.hasEntity() should not throw a NullPointerException #2113

Open
glassfishrobot opened this issue Apr 11, 2013 · 7 comments
Open

Comments

@glassfishrobot
Copy link

Similar to #1944 should not throw a NullPointerException"), the hasEntity method throws a null pointer exception when entity is null (like for a 500 response).
When you have filters which look at possible JSON entities, it would be great to not get a null pointer.

The workaround is to check for getEntityInputStream() != null first, which is kind of awkward.

Affected Versions

[1.17]

@glassfishrobot
Copy link
Author

Reported by codingfabian

@glassfishrobot
Copy link
Author

Issue-Links:
is related to
JERSEY-1672

@glassfishrobot
Copy link
Author

mfuksa said:
Hi,

can you please provide more details? I cannot reproduce the NPE. I tried with 500 and 204 responses and never get NPE in ClientResponse.hasEntity(). I have tested it against 1.17 and with latest snapshot version. Exception stack trace and code example would help. The best would be simple maven project with test simulating the bug. Thanks.

I have the same problem with #1944 should not throw a NullPointerException") as I cannot reproduce it.

@glassfishrobot
Copy link
Author

mfuksa said:
I cannot reproduce the issue (I would need exception stack trace at least).

@glassfishrobot
Copy link
Author

pabstec said:
ClientResponse clientResponse = client.resource(url).delete(ClientResponse.class);

java.lang.NullPointerException
at com.sun.jersey.api.client.ClientResponse.hasEntity(ClientResponse.java:518)
at com.sun.jersey.api.client.filter.GZIPContentEncodingFilter.handle(GZIPContentEncodingFilter.java:125)
at com.sun.jersey.api.client.Client.handle(Client.java:652)

@glassfishrobot
Copy link
Author

pabstec said:
A work-around is to add this filter very first to the Client (which requires creating a ClientResponseWrapper class that simply delegates).

import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientRequest;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.filter.ClientFilter;

/**
 * A Client Filter that works around a NPE issue with {@link ClientResponse#hasEntity()}.
 * See <a href="https://java.net/jira/browse/JERSEY-1841">JERSEY-1841</a> and <a href="https://java.net/jira/browse/JERSEY-1672">JERSEY-1672</a>
 *
 * @author pabstec on 9/7/16.
 */
public class RobustClientResponseFilter extends ClientFilter {
  @Override
  public ClientResponse handle(ClientRequest request) throws ClientHandlerException {
    return new ClientResponseWrapper(getNext().handle(request)) {
      @Override
      public boolean hasEntity() {
        return getEntityInputStream() != null && super.hasEntity();
      }

      @Override
      public void close() throws ClientHandlerException {
        if (getEntityInputStream() != null) {
          super.close();
        }
      }
    };
  }
}

@glassfishrobot
Copy link
Author

This issue was imported from java.net JIRA JERSEY-1841

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants