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

"Connection is still allocated" when deserializing InputStream return type #1198

Open
GhrabSaifEddine opened this issue Oct 13, 2023 · 0 comments

Comments

@GhrabSaifEddine
Copy link

Hello,

I just found a strange behaviour in ApiClient.invokeAPI, when deserializing a second response of InputStream after a first one with the same type. The error stack shows :
RESTEASY004655: Unable to invoke request .... Caused by: java.lang.IllegalStateException: Connection is still allocated
After debugging, I saw that the first response was not consumed globally wich causes the stack reason. So,I closed the response here :

else if (response.getStatusInfo().getFamily().equals(Status.Family.SUCCESSFUL)) {
      if (returnType == null) {
        return null;
      } else {
        T parsedResponse = deserialize(response, returnType);
        response.close();
        return parsedResponse;
      }
    } 

The problem seems to be resolved and I can call the second response successfully BUT a new problem showed when deserializing the response entity ( wich is the inputstream ). The method deserialize does not handle the inputstream case so it is treated like that : : T parsedResponse = response.readEntity(returnType). This assertion close the inputstream, so it cant be readed anymore and blocks the operation. So I updated the method this way :

public <T> T deserialize(Response response, GenericType<T> returnType) throws ApiException {
    if (response == null || returnType == null) {
      return null;
    }

    if ("byte[]".equals(returnType.toString())) {
      // Handle binary response (byte array).
      return (T) response.readEntity(byte[].class);
    } else if (returnType.equals(File.class)) {
      // Handle file downloading.
      @SuppressWarnings("unchecked")
      T file = (T) downloadFileFromResponse(response);
      return file;
    }
    else if (returnType.getType().equals(InputStream.class)) {
      return downloadInputStreamFromResponse(response);
    }

    String contentType = null;
    List<Object> contentTypes = response.getHeaders().get("Content-Type");
    if (contentTypes != null && !contentTypes.isEmpty()) {
      contentType = String.valueOf(contentTypes.get(0));
    }
    if (contentType == null) {
      throw new ApiException(500, "missing Content-Type in response");
    }

    T parsedResponse = response.readEntity(returnType);
    return parsedResponse;
  }
private <T> T downloadInputStreamFromResponse(Response response) {
 File file = downloadFileFromResponse(response);
 try {
   return (T) new FileInputStream(file);
 } catch (FileNotFoundException e) {
   throw new RuntimeException(e);
 } finally {
   FileUtils.deleteQuietly(file);
 }
}

Then everything went so good.
I suggest adding those updates, and if anyone have the same problem, he can add it to his ApiClient.mustache or generate his own ApiClient.java.

Best regards,

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

No branches or pull requests

1 participant