andymc12 commented on a change in pull request #697:
URL: https://github.com/apache/cxf/pull/697#discussion_r493915066
##########
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();
Review comment:
This does read one byte of the stream. If it is -1, then it simply
returns false (no entity). If there is data on the stream, then we construct
the PushbackInputStream to unread the byte we checked before. I think this
approach is optimal for the case where there is no entity, but not harmful in
the case where there is. If the stream supports marking (fair uncommon imo) or
the available method (much more common), then we'll never get this far.
Unless you object, I'd like to leave it this way, but I can add a unit test
to ensure the different expectations and complete coverage (input stream
supporting marking, available, 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]