You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@chemistry.apache.org by fm...@apache.org on 2014/03/27 09:41:56 UTC

svn commit: r1582221 - in /chemistry/opencmis/trunk: chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/browser/ chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/...

Author: fmui
Date: Thu Mar 27 08:41:56 2014
New Revision: 1582221

URL: http://svn.apache.org/r1582221
Log:
CMIS-771: don't try to parse an empty stream

Modified:
    chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/browser/ObjectServiceImpl.java
    chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/http/Response.java
    chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/IOUtils.java
    chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/test/java/org/apache/chemistry/opencmis/commons/impl/misc/IOUtilsTest.java

Modified: chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/browser/ObjectServiceImpl.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/browser/ObjectServiceImpl.java?rev=1582221&r1=1582220&r2=1582221&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/browser/ObjectServiceImpl.java (original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/browser/ObjectServiceImpl.java Thu Mar 27 08:41:56 2014
@@ -19,6 +19,7 @@
 package org.apache.chemistry.opencmis.client.bindings.spi.browser;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.math.BigInteger;
 import java.util.List;
@@ -40,8 +41,10 @@ import org.apache.chemistry.opencmis.com
 import org.apache.chemistry.opencmis.commons.enums.IncludeRelationships;
 import org.apache.chemistry.opencmis.commons.enums.UnfileObject;
 import org.apache.chemistry.opencmis.commons.enums.VersioningState;
+import org.apache.chemistry.opencmis.commons.exceptions.CmisConnectionException;
 import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
 import org.apache.chemistry.opencmis.commons.impl.Constants;
+import org.apache.chemistry.opencmis.commons.impl.IOUtils;
 import org.apache.chemistry.opencmis.commons.impl.JSONConverter;
 import org.apache.chemistry.opencmis.commons.impl.MimeHelper;
 import org.apache.chemistry.opencmis.commons.impl.TypeCache;
@@ -503,8 +506,15 @@ public class ObjectServiceImpl extends A
         });
 
         if (resp.hasResponseStream()) {
-            Map<String, Object> json = parseObject(resp.getStream(), resp.getCharset());
-            return JSONConverter.convertFailedToDelete(json);
+            try {
+                InputStream responseStream = IOUtils.checkForBytes(resp.getStream(), 4096);
+                if (responseStream != null) {
+                    Map<String, Object> json = parseObject(responseStream, resp.getCharset());
+                    return JSONConverter.convertFailedToDelete(json);
+                }
+            } catch (IOException e) {
+                throw new CmisConnectionException("Cannot read response!", e);
+            }
         }
 
         return new FailedToDeleteDataImpl();

Modified: chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/http/Response.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/http/Response.java?rev=1582221&r1=1582220&r2=1582221&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/http/Response.java (original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-client/chemistry-opencmis-client-bindings/src/main/java/org/apache/chemistry/opencmis/client/bindings/spi/http/Response.java Thu Mar 27 08:41:56 2014
@@ -141,13 +141,7 @@ public class Response {
         } else {
             stream = new BufferedInputStream(stream, 64 * 1024);
             try {
-                stream.mark(2);
-                if (stream.read() == -1) {
-                    hasResponseStream = false;
-                } else {
-                    stream.reset();
-                    hasResponseStream = true;
-                }
+                hasResponseStream = IOUtils.checkForBytes(stream);
             } catch (IOException ioe) {
                 throw new CmisConnectionException("IO exception!", ioe);
             }

Modified: chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/IOUtils.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/IOUtils.java?rev=1582221&r1=1582220&r2=1582221&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/IOUtils.java (original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/main/java/org/apache/chemistry/opencmis/commons/impl/IOUtils.java Thu Mar 27 08:41:56 2014
@@ -18,6 +18,7 @@
  */
 package org.apache.chemistry.opencmis.commons.impl;
 
+import java.io.BufferedInputStream;
 import java.io.BufferedReader;
 import java.io.Closeable;
 import java.io.IOException;
@@ -130,6 +131,68 @@ public final class IOUtils {
     }
 
     /**
+     * Checks if a stream has more bytes. If the provided stream is not
+     * markable, it wrappes a {@link BufferedInputStream} around the stream and
+     * returns it.
+     * 
+     * @param stream
+     *            the stream
+     * @param bufferSize
+     *            the size of the buffer in bytes if a
+     *            {@link BufferedInputStream} has to be created
+     * @return {@code null} if the stream doesn't have more bytes, the provided
+     *         stream if the provided stream is markable and has more bytes, or
+     *         a {@link BufferedInputStream} if the provided stream is not
+     *         markable and has more bytes
+     * 
+     * @throws IOException
+     */
+    public static InputStream checkForBytes(InputStream stream, int bufferSize) throws IOException {
+        if (stream == null) {
+            return null;
+        }
+
+        InputStream checkStream = stream;
+
+        if (!stream.markSupported()) {
+            checkStream = new BufferedInputStream(stream, bufferSize);
+        }
+
+        if (checkForBytes(checkStream)) {
+            return checkStream;
+        }
+
+        return null;
+    }
+
+    /**
+     * Checks if a stream has more bytes.
+     * 
+     * @param stream
+     *            a markable stream
+     * @return {@code true} if the stream has more bytes, {@code false}
+     *         otherwise
+     */
+    public static boolean checkForBytes(InputStream stream) throws IOException {
+        if (stream == null) {
+            return false;
+        }
+
+        if (!stream.markSupported()) {
+            throw new IllegalArgumentException("Stream must support marks!");
+        }
+
+        stream.mark(2);
+
+        if (stream.read() != -1) {
+            stream.reset();
+            return true;
+        }
+
+        return false;
+    }
+
+    /**
      * Closes a stream and ignores any exceptions.
      * 
      * @param closeable

Modified: chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/test/java/org/apache/chemistry/opencmis/commons/impl/misc/IOUtilsTest.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/test/java/org/apache/chemistry/opencmis/commons/impl/misc/IOUtilsTest.java?rev=1582221&r1=1582220&r2=1582221&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/test/java/org/apache/chemistry/opencmis/commons/impl/misc/IOUtilsTest.java (original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-commons/chemistry-opencmis-commons-impl/src/test/java/org/apache/chemistry/opencmis/commons/impl/misc/IOUtilsTest.java Thu Mar 27 08:41:56 2014
@@ -20,10 +20,13 @@ package org.apache.chemistry.opencmis.co
 
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
+import java.io.BufferedInputStream;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.Closeable;
@@ -61,6 +64,29 @@ public class IOUtilsTest {
     }
 
     @Test
+    public void testCheckForBytes() throws Exception {
+        assertFalse(IOUtils.checkForBytes(new ByteArrayInputStream(new byte[0])));
+        assertTrue(IOUtils.checkForBytes(new ByteArrayInputStream(IOUtils.toUTF8Bytes("Hello World!"))));
+
+        assertNull(IOUtils.checkForBytes(new ByteArrayInputStream(new byte[0]), 1024));
+        assertTrue(IOUtils.checkForBytes(new ByteArrayInputStream(IOUtils.toUTF8Bytes("Hello World!")), 1024) instanceof ByteArrayInputStream);
+
+        assertNull(IOUtils.checkForBytes(new ByteArrayInputStream(new byte[0]) {
+            @Override
+            public boolean markSupported() {
+
+                return false;
+            }
+        }, 1024));
+        assertTrue(IOUtils.checkForBytes(new ByteArrayInputStream(IOUtils.toUTF8Bytes("Hello World!")) {
+            @Override
+            public boolean markSupported() {
+                return false;
+            }
+        }, 1024) instanceof BufferedInputStream);
+    }
+
+    @Test
     public void testConsumeAndClose() {
         ByteArrayInputStream stream = new ByteArrayInputStream(IOUtils.toUTF8Bytes("test"));