chemistry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From f...@apache.org
Subject svn commit: r1458508 - in /chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server: impl/atompub/ impl/browser/ shared/
Date Tue, 19 Mar 2013 20:55:20 GMT
Author: fmui
Date: Tue Mar 19 20:55:19 2013
New Revision: 1458508

URL: http://svn.apache.org/r1458508
Log:
always close input streams from clients

Modified:
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/AtomEntryParser.java
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/ObjectService.java
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/BrowserBindingUtils.java
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/ObjectService.java
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/VersioningService.java
    chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/ThresholdOutputStream.java

Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/AtomEntryParser.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/AtomEntryParser.java?rev=1458508&r1=1458507&r2=1458508&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/AtomEntryParser.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/AtomEntryParser.java
Tue Mar 19 20:55:19 2013
@@ -410,24 +410,29 @@ public class AtomEntryParser {
 
         XMLUtils.next(parser);
 
-        while (true) {
-            int event = parser.getEventType();
-            if (event == XMLStreamReader.END_ELEMENT) {
-                break;
-            } else if (event == XMLStreamReader.CHARACTERS) {
-                String s = parser.getText();
-                if (s != null) {
-                    byte[] bytes = s.getBytes("UTF-8");
-                    bufferStream.write(bytes);
-                    cappedStream.deductBytes(bytes.length);
+        try {
+            while (true) {
+                int event = parser.getEventType();
+                if (event == XMLStreamReader.END_ELEMENT) {
+                    break;
+                } else if (event == XMLStreamReader.CHARACTERS) {
+                    String s = parser.getText();
+                    if (s != null) {
+                        byte[] bytes = s.getBytes("UTF-8");
+                        bufferStream.write(bytes);
+                        cappedStream.deductBytes(bytes.length);
+                    }
+                } else if (event == XMLStreamReader.START_ELEMENT) {
+                    throw new RuntimeException("Unexpected tag: " + parser.getName());
                 }
-            } else if (event == XMLStreamReader.START_ELEMENT) {
-                throw new RuntimeException("Unexpected tag: " + parser.getName());
-            }
 
-            if (!XMLUtils.next(parser)) {
-                break;
+                if (!XMLUtils.next(parser)) {
+                    break;
+                }
             }
+        } catch (Exception e) {
+            bufferStream.destroy(); // remove temp file
+            throw e;
         }
 
         XMLUtils.next(parser);

Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/ObjectService.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/ObjectService.java?rev=1458508&r1=1458507&r2=1458508&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/ObjectService.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/ObjectService.java
Tue Mar 19 20:55:19 2013
@@ -32,6 +32,7 @@ import static org.apache.chemistry.openc
 
 import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.PrintWriter;
@@ -109,8 +110,13 @@ public final class ObjectService {
 
         if (objectId == null) {
             // create
-            newObjectId = service.create(repositoryId, parser.getProperties(), folderId,
parser.getContentStream(),
-                    versioningState, parser.getPolicyIds(), null);
+            ContentStream contentStream = parser.getContentStream();
+            try {
+                newObjectId = service.create(repositoryId, parser.getProperties(), folderId,
contentStream,
+                        versioningState, parser.getPolicyIds(), null);
+            } finally {
+                closeContentStream(contentStream);
+            }
         } else {
             if ((sourceFolderId == null) || (sourceFolderId.trim().length() == 0)) {
                 // addObjectToFolder
@@ -490,8 +496,13 @@ public final class ObjectService {
         Holder<String> objectIdHolder = new Holder<String>(objectId);
 
         if ((checkin != null) && (checkin.booleanValue())) {
-            service.checkIn(repositoryId, objectIdHolder, major, parser.getProperties(),
parser.getContentStream(),
-                    checkinComment, parser.getPolicyIds(), null, null, null);
+            ContentStream contentStream = parser.getContentStream();
+            try {
+                service.checkIn(repositoryId, objectIdHolder, major, parser.getProperties(),
contentStream,
+                        checkinComment, parser.getPolicyIds(), null, null, null);
+            } finally {
+                closeContentStream(contentStream);
+            }
         } else {
             String changeToken = extractChangeToken(parser.getProperties());
 
@@ -615,4 +626,14 @@ public final class ObjectService {
 
         return ((PropertyString) changeLogProperty).getFirstValue();
     }
+
+    public static void closeContentStream(ContentStream contentStream) {
+        if (contentStream != null && contentStream.getStream() != null) {
+            try {
+                contentStream.getStream().close();
+            } catch (IOException e) {
+                // we tried our best
+            }
+        }
+    }
 }

Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/BrowserBindingUtils.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/BrowserBindingUtils.java?rev=1458508&r1=1458507&r2=1458508&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/BrowserBindingUtils.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/BrowserBindingUtils.java
Tue Mar 19 20:55:19 2013
@@ -437,6 +437,16 @@ public final class BrowserBindingUtils {
         return result;
     }
 
+    public static void closeContentStream(ContentStream contentStream) {
+        if (contentStream != null && contentStream.getStream() != null) {
+            try {
+                contentStream.getStream().close();
+            } catch (IOException e) {
+                // we tried our best
+            }
+        }
+    }
+
     protected static ObjectData getSimpleObject(CmisService service, String repositoryId,
String objectId) {
         return service.getObject(repositoryId, objectId, null, false, IncludeRelationships.NONE,
"cmis:none", false,
                 false, null);

Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/ObjectService.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/ObjectService.java?rev=1458508&r1=1458507&r2=1458508&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/ObjectService.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/ObjectService.java
Tue Mar 19 20:55:19 2013
@@ -38,6 +38,7 @@ import static org.apache.chemistry.openc
 import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.compileUrl;
 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.CONTEXT_OBJECT_ID;
 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.CONTEXT_OBJECT_TYPE_ID;
+import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.closeContentStream;
 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.createAddAcl;
 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.createContentStream;
 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.createCookieValue;
@@ -122,9 +123,15 @@ public final class ObjectService {
         ControlParser cp = new ControlParser(request);
         TypeCache typeCache = new ServerTypeCacheImpl(repositoryId, service);
 
-        String newObjectId = service.createDocument(repositoryId, createNewProperties(cp,
typeCache), folderId,
-                createContentStream(request), versioningState, createPolicies(cp), createAddAcl(cp),
-                createRemoveAcl(cp), null);
+        ContentStream contentStream = createContentStream(request);
+
+        String newObjectId = null;
+        try {
+            newObjectId = service.createDocument(repositoryId, createNewProperties(cp, typeCache),
folderId,
+                    contentStream, versioningState, createPolicies(cp), createAddAcl(cp),
createRemoveAcl(cp), null);
+        } finally {
+            closeContentStream(contentStream);
+        }
 
         ObjectData object = getSimpleObject(service, repositoryId, newObjectId);
         if (object == null) {
@@ -692,8 +699,14 @@ public final class ObjectService {
         // execute
         Holder<String> objectIdHolder = new Holder<String>(objectId);
         Holder<String> changeTokenHolder = (changeToken == null ? null : new Holder<String>(changeToken));
-        service.setContentStream(repositoryId, objectIdHolder, overwriteFlag, changeTokenHolder,
-                createContentStream(request), null);
+        ContentStream contentStream = createContentStream(request);
+
+        try {
+            service.setContentStream(repositoryId, objectIdHolder, overwriteFlag, changeTokenHolder,
contentStream,
+                    null);
+        } finally {
+            closeContentStream(contentStream);
+        }
 
         String newObjectId = (objectIdHolder.getValue() == null ? objectId : objectIdHolder.getValue());
 
@@ -729,8 +742,14 @@ public final class ObjectService {
         // execute
         Holder<String> objectIdHolder = new Holder<String>(objectId);
         Holder<String> changeTokenHolder = (changeToken == null ? null : new Holder<String>(changeToken));
-        service.appendContentStream(repositoryId, objectIdHolder, changeTokenHolder, createContentStream(request),
-                isLastChunk, null);
+        ContentStream contentStream = createContentStream(request);
+
+        try {
+            service.appendContentStream(repositoryId, objectIdHolder, changeTokenHolder,
contentStream, isLastChunk,
+                    null);
+        } finally {
+            closeContentStream(contentStream);
+        }
 
         String newObjectId = (objectIdHolder.getValue() == null ? objectId : objectIdHolder.getValue());
 

Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/VersioningService.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/VersioningService.java?rev=1458508&r1=1458507&r2=1458508&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/VersioningService.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/VersioningService.java
Tue Mar 19 20:55:19 2013
@@ -28,12 +28,13 @@ import static org.apache.chemistry.openc
 import static org.apache.chemistry.opencmis.server.impl.atompub.AtomPubUtils.compileUrl;
 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.CONTEXT_OBJECT_ID;
 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.CONTEXT_OBJECT_TYPE_ID;
+import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.closeContentStream;
 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.createAddAcl;
 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.createContentStream;
 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.createCookieValue;
 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.createPolicies;
-import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.createUpdateProperties;
 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.createRemoveAcl;
+import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.createUpdateProperties;
 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.getSimpleObject;
 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.setCookie;
 import static org.apache.chemistry.opencmis.server.impl.browser.BrowserBindingUtils.setStatus;
@@ -48,6 +49,7 @@ import java.util.List;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.chemistry.opencmis.commons.data.ContentStream;
 import org.apache.chemistry.opencmis.commons.data.ObjectData;
 import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
 import org.apache.chemistry.opencmis.commons.impl.Constants;
@@ -134,11 +136,15 @@ public final class VersioningService {
         ControlParser cp = new ControlParser(request);
         TypeCache typeCache = new ServerTypeCacheImpl(repositoryId, service);
         Holder<String> objectIdHolder = new Holder<String>(objectId);
+        ContentStream contentStream = createContentStream(request);
 
-        service.checkIn(repositoryId, objectIdHolder, major,
-                createUpdateProperties(cp, typeId, null, Collections.singletonList(objectId),
typeCache),
-                createContentStream(request), checkinComment, createPolicies(cp), createAddAcl(cp),
-                createRemoveAcl(cp), null);
+        try {
+            service.checkIn(repositoryId, objectIdHolder, major,
+                    createUpdateProperties(cp, typeId, null, Collections.singletonList(objectId),
typeCache),
+                    contentStream, checkinComment, createPolicies(cp), createAddAcl(cp),
createRemoveAcl(cp), null);
+        } finally {
+            closeContentStream(contentStream);
+        }
 
         String newObjectId = (objectIdHolder.getValue() == null ? objectId : objectIdHolder.getValue());
 

Modified: chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/ThresholdOutputStream.java
URL: http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/ThresholdOutputStream.java?rev=1458508&r1=1458507&r2=1458508&view=diff
==============================================================================
--- chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/ThresholdOutputStream.java
(original)
+++ chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/ThresholdOutputStream.java
Tue Mar 19 20:55:19 2013
@@ -36,6 +36,8 @@ import javax.crypto.KeyGenerator;
 import javax.crypto.spec.IvParameterSpec;
 
 import org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * An OutputStream that stores the data in main memory until it reaches a
@@ -47,6 +49,9 @@ import org.apache.chemistry.opencmis.com
  * InputStream isn't required!
  */
 public class ThresholdOutputStream extends OutputStream {
+
+    private static final Logger LOG = LoggerFactory.getLogger(ThresholdOutputStream.class);
+
     private static final int MAX_GROW = 10 * 1024 * 1024; // 10 MiB
     private static final int DEFAULT_THRESHOLD = 4 * 1024 * 1024; // 4 MiB
 
@@ -258,7 +263,10 @@ public class ThresholdOutputStream exten
         }
 
         if (tempFile != null) {
-            tempFile.delete();
+            boolean isDeleted = tempFile.delete();
+            if (!isDeleted) {
+                LOG.warn("Temp file " + tempFile.getAbsolutePath() + " could not be deleted!");
+            }
         }
 
         buf = null;
@@ -451,6 +459,7 @@ public class ThresholdOutputStream exten
                     cipher = Cipher.getInstance(TRANSFORMATION);
                     cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
                 } catch (Exception e) {
+                    delete();
                     throw new IOException("Cannot initialize decryption cipher!", e);
                 }
             } else {
@@ -535,13 +544,8 @@ public class ThresholdOutputStream exten
 
             int b = stream.read();
 
-            if (b == -1 && !isDeleted) {
-                try {
-                    stream.close();
-                    isClosed = true;
-                } catch (Exception e) {
-                }
-                isDeleted = tempFile.delete();
+            if (b == -1) {
+                delete();
             }
 
             return b;
@@ -560,13 +564,8 @@ public class ThresholdOutputStream exten
 
             int n = super.read(b, off, len);
 
-            if (n == -1 && !isDeleted) {
-                try {
-                    stream.close();
-                    isClosed = true;
-                } catch (Exception e) {
-                }
-                isDeleted = tempFile.delete();
+            if (n == -1) {
+                delete();
             }
 
             return n;
@@ -574,16 +573,24 @@ public class ThresholdOutputStream exten
 
         @Override
         public void close() throws IOException {
+            delete();
+        }
+
+        protected void delete() {
             if (!isClosed) {
                 try {
                     stream.close();
                     isClosed = true;
                 } catch (Exception e) {
+                    // ignore
                 }
             }
 
             if (!isDeleted) {
                 isDeleted = tempFile.delete();
+                if (!isDeleted) {
+                    LOG.warn("Temp file " + tempFile.getAbsolutePath() + " could not be deleted!");
+                }
             }
         }
     }



Mime
View raw message