andymc12 commented on a change in pull request #697:
URL: https://github.com/apache/cxf/pull/697#discussion_r493968775
##########
File path:
rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ResponseImpl.java
##########
@@ -137,22 +143,70 @@ public Object getActualEntity() {
return lastEntity != null ? lastEntity : entity;
}
+ @Override
public Object getEntity() {
return InjectionUtils.getEntity(getActualEntity());
}
+ @Override
public boolean hasEntity() {
- return getActualEntity() != null;
+ // per spec, need to check if the stream exists and if it has data.
+ Object actualEntity = getActualEntity();
+ if (actualEntity == null) {
+ return false;
+ } else if (actualEntity instanceof InputStream) {
+ final InputStream is = (InputStream) actualEntity;
+ try {
+ if (is.markSupported()) {
+ is.mark(1);
+ int i = is.read();
+ is.reset();
+ return i != -1;
+ } else {
+ try {
+ if (is.available() > 0) {
+ return true;
+ }
+ } catch (IOException ioe) {
+ //Do nothing
+ }
+ int b = is.read();
+ if (b == -1) {
+ return false;
+ }
+ PushbackInputStream pbis;
+ if (is instanceof PushbackInputStream) {
+ pbis = (PushbackInputStream) is;
+ } else {
+ pbis = new PushbackInputStream(is, 1);
+ if (lastEntity != null) {
Review comment:
I believe that `lastEntity` is used for optimizing the case where the
entity is an InputStream and a user calls `readEntity(SomeObject.class)` -
lastEntity will then be set to the instance of `SomeObject` so that subsequent
readEntity calls can just return that object rather then re-reading it from the
stream. But since it is also possible to read an entity as an InputStream,
this code covers cases like:
```
response.readEntity(InputStream.class);
if (response.hasEntity()) {
response.readEntity(InputStream.class); // or
response.readEntity(SomeObject.class), etc
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]