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

ClientResponse.close() should not throw a NullPointerException #1944

Open
glassfishrobot opened this issue Jan 27, 2013 · 6 comments
Open

ClientResponse.close() should not throw a NullPointerException #1944

glassfishrobot opened this issue Jan 27, 2013 · 6 comments

Comments

@glassfishrobot
Copy link

Currently closing the connection is not easy.
the easiest solution would be to have a finally block with just response.close().
However, depending on the server response entity may be null in ClientResponse#613
i think close should include a null check here. and just do nothing if it does not have an entity.

Affected Versions

[1.17]

@glassfishrobot
Copy link
Author

Reported by codingfabian

@glassfishrobot
Copy link
Author

Issue-Links:
is related to
JERSEY-1841

@glassfishrobot
Copy link
Author

mfuksa said:
can you please provide more details? I cannot reproduce the NPE. 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.

@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:
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-1672

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