eclipse-ee4j / eclipse-ee4j/jersey

InboundMessageContext.hasEntity() throws IllegalStateException when stream has been closed

Open
#5,383 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
730
Forks
382
PR merge metrics
No merged PRs in 30d

Description

Relevant code:
```
public boolean hasEntity() {
entityContent.ensureNotClosed();

try {
return entityContent.isBuffered() || !entityContent.isEmpty();
} catch (IllegalStateException ex) {
// input stream has been closed.
return false;
}
}
```

It appears that we explicitly check to see if the stream is closed here:
https://github.com/eclipse-ee4j/jersey/blob/2.x/core-common/src/main/java/org/glassfish/jersey/message/internal/InboundMessageContext.java#L814

It looks like we handle the exception and return false here:
https://github.com/eclipse-ee4j/jersey/blob/2.x/core-common/src/main/java/org/glassfish/jersey/message/internal/InboundMessageContext.java#L820

Stacktrace with personal lines scrubbed out:
```
Entity input stream has already been closed.
org.glassfish.jersey.message.internal.EntityInputStream.ensureNotClosed(EntityInputStream.java:205)
org.glassfish.jersey.message.internal.InboundMessageContext.hasEntity(InboundMessageContext.java:780)
...
```

In this particular case, I think it makes sense to move the `entityContent.ensureNotClosed();` statement into the try block below it.

To work around this issue, I have to handle my invocation of hasEntity() myself outside of an if-statement for legibility sake.

Sample work around:
```
boolean hasEntity = false;
try {
hasEntity = requestContext.hasEntity();
} catch (IllegalStateException e) {
}

if (hasEntity) {
...
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.