Permalink
Browse files

[bugfix] A regression was introduced in Git commit dbb3035 which clos…

…ed the input stream of HTTP Request bodies too early when they were binary values. This commit fixes that.
  • Loading branch information...
1 parent 00b91ca commit b5d593d1c8b25183ae15e9a99e6e9f04ac88e270 @adamretter adamretter committed Oct 13, 2013
Binary file not shown.
@@ -62,6 +62,7 @@
import org.exquery.restxq.impl.AbstractRestXqService;
import org.exquery.xdm.type.SequenceImpl;
import org.exquery.xquery.Sequence;
+import org.exquery.xquery.Type;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
@@ -236,12 +237,27 @@ public String getCacheClass() {
}
}
- //dont close the stream if its a binary value, because we will need it later for serialization
- if(is != null && !(result instanceof BinaryValue)) {
- try {
- is.close();
- } catch(final IOException ioe) {
- LOG.error(ioe.getMessage(), ioe);
+ if(is != null) {
+ /*
+ * Do NOT close the stream if its a binary value,
+ * because we will need it later for serialization
+ */
+ boolean isBinaryType = false;
+ if(result != null) {
+ try {
+ final Type type = result.head().getType();
+ isBinaryType = (type == Type.BASE64_BINARY || type == Type.HEX_BINARY);
+ } catch(final IndexOutOfBoundsException ioe) {
+ LOG.warn("Called head on an empty HTTP Request body sequence", ioe);
+ }
+ }
+
+ if(!isBinaryType) {
+ try {
+ is.close();
+ } catch(final IOException ioe) {
+ LOG.error(ioe.getMessage(), ioe);
+ }
}
}
}
@@ -76,24 +76,7 @@ public boolean hasNext() {
@Override
public TypedValue<Item> next() {
- return new TypedValue<Item>(){
-
- final Item item = getIterator().nextItem();
-
- @Override
- public Type getType() {
- return TypeAdapter.toExQueryType(item.getType());
- }
-
- @Override
- public Item getValue() {
- if(item instanceof NodeProxy) {
- return DomEnhancingNodeProxyAdapter.create((NodeProxy)item); //RESTXQ expects to find DOM Nodes not NodeProxys
- } else {
- return item;
- }
- }
- };
+ return createTypedValue(getIterator().nextItem());
}
@Override
@@ -102,7 +85,34 @@ public void remove() {
}
};
}
+
+ private TypedValue<Item> createTypedValue(final Item item) {
+ return new TypedValue<Item>(){
+ @Override
+ public Type getType() {
+ return TypeAdapter.toExQueryType(item.getType());
+ }
+ @Override
+ public Item getValue() {
+ if(item instanceof NodeProxy) {
+ return DomEnhancingNodeProxyAdapter.create((NodeProxy)item); //RESTXQ expects to find DOM Nodes not NodeProxys
+ } else {
+ return item;
+ }
+ }
+ };
+ }
+
+ @Override
+ public TypedValue<Item> head() {
+ if(sequence.isEmpty()) {
+ return Sequence.EMPTY_SEQUENCE.head();
+ } else {
+ return createTypedValue(sequence.itemAt(0));
+ }
+ }
+
@Override
public Sequence<Item> tail() {
try {

0 comments on commit b5d593d

Please sign in to comment.