Author: fmui
Date: Thu Jan 15 13:04:50 2015
New Revision: 1652084

URL: http://svn.apache.org/r1652084
Log:
Server: added paranoiac content handling and stream closing 

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/MultiFilingService.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/atompub/PolicyService.java
    
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/RepositoryService.java
    
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/VersioningService.java
    
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/CmisBrowserBindingServlet.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/AbstractServiceCall.java
    
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/ThresholdOutputStream.java
    
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/AtomEntryParserTest.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=1652084&r1=1652083&r2=1652084&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
 Thu Jan 15 13:04:50 2015
@@ -194,9 +194,7 @@ public final class AtomEntryParser {
      * Parses the stream.
      */
     public void parse(InputStream stream) throws XMLStreamException, 
IOException {
-        object = null;
-        atomContentStream = null;
-        cmisContentStream = null;
+        release();
 
         if (stream == null) {
             return;
@@ -224,12 +222,48 @@ public final class AtomEntryParser {
                     break;
                 }
             }
+        } catch (XMLStreamException xse) {
+            release();
+            throw xse;
+        } catch (IOException ioe) {
+            release();
+            throw ioe;
+        } catch (RuntimeException re) {
+            release();
+            throw re;
         } finally {
             parser.close();
         }
     }
 
     /**
+     * Releases all resources.
+     */
+    public void release() {
+        object = null;
+        typeDef = null;
+        bulkUpdate = null;
+        closeAtomContentStream();
+        closeCmisContentStream();
+    }
+
+    /**
+     * Closes the Atom content stream.
+     */
+    private void closeAtomContentStream() {
+        IOUtils.closeQuietly(atomContentStream);
+        atomContentStream = null;
+    }
+
+    /**
+     * Closes the CMIS content stream.
+     */
+    private void closeCmisContentStream() {
+        IOUtils.closeQuietly(cmisContentStream);
+        cmisContentStream = null;
+    }
+
+    /**
      * Parses an Atom entry.
      */
     private void parseEntry(XMLStreamReader parser) throws XMLStreamException, 
IOException {
@@ -310,6 +344,17 @@ public final class AtomEntryParser {
      * @throws IOException
      */
     private void parseAtomContent(XMLStreamReader parser) throws 
XMLStreamException, IOException {
+        if (atomContentStream != null) {
+            closeAtomContentStream();
+            throw new CmisInvalidArgumentException("More than one content 
provided!");
+        }
+
+        if (cmisContentStream != null) {
+            // CMIS content takes precedence (see CMIS spec)
+            XMLUtils.skip(parser);
+            return;
+        }
+
         atomContentStream = new ContentStreamImpl();
 
         // read attributes
@@ -366,6 +411,12 @@ public final class AtomEntryParser {
      * Extract the content stream.
      */
     private void parseCmisContent(XMLStreamReader parser) throws 
XMLStreamException, IOException {
+        closeAtomContentStream();
+        if (cmisContentStream != null) {
+            closeCmisContentStream();
+            throw new CmisInvalidArgumentException("More than one content 
provided!");
+        }
+
         cmisContentStream = new ContentStreamImpl();
 
         XMLUtils.next(parser);

Modified: 
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/MultiFilingService.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/MultiFilingService.java?rev=1652084&r1=1652083&r2=1652084&view=diff
==============================================================================
--- 
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/MultiFilingService.java
 (original)
+++ 
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/MultiFilingService.java
 Thu Jan 15 13:04:50 2015
@@ -58,23 +58,25 @@ public class MultiFilingService {
             parser.setIgnoreAtomContentSrc(true); // needed for some clients
             parser.parse(request.getInputStream());
 
-            String objectId = parser.getId();
-
-            if (stopBeforeService(service)) {
-                return;
-            }
-
-            if (objectId == null && removeFrom == null) {
-                // create unfiled object
-                createUnfiledObject(context, service, repositoryId, request, 
response, parser);
-                return;
-            }
-
             // execute
-            service.removeObjectFromFolder(repositoryId, objectId, removeFrom, 
null);
-
-            if (stopAfterService(service)) {
-                return;
+            String objectId = parser.getId();
+            try {
+                if (stopBeforeService(service)) {
+                    return;
+                }
+
+                if (objectId == null && removeFrom == null) {
+                    createUnfiledObject(context, service, repositoryId, 
request, response, parser);
+                    return;
+                }
+
+                service.removeObjectFromFolder(repositoryId, objectId, 
removeFrom, null);
+
+                if (stopAfterService(service)) {
+                    return;
+                }
+            } finally {
+                parser.release();
             }
 
             ObjectInfo objectInfo = service.getObjectInfo(repositoryId, 
objectId);
@@ -117,13 +119,8 @@ public class MultiFilingService {
 
             // create
             ContentStream contentStream = parser.getContentStream();
-            String newObjectId = null;
-            try {
-                newObjectId = service.create(repositoryId, 
parser.getProperties(), null, contentStream,
-                        versioningState, parser.getPolicyIds(), null);
-            } finally {
-                closeContentStream(contentStream);
-            }
+            String newObjectId = service.create(repositoryId, 
parser.getProperties(), null, contentStream,
+                    versioningState, parser.getPolicyIds(), null);
 
             if (stopAfterService(service)) {
                 return;

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=1652084&r1=1652083&r2=1652084&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
 Thu Jan 15 13:04:50 2015
@@ -97,47 +97,37 @@ public class ObjectService {
             parser.setIgnoreAtomContentSrc(true); // needed for some clients
             parser.parse(request.getInputStream());
 
-            String objectId = parser.getId();
-
             // execute
             String newObjectId = null;
-
-            if (objectId == null) {
+            String objectId = parser.getId();
+            try {
                 if (stopBeforeService(service)) {
                     return;
                 }
 
-                // create
-                ContentStream contentStream = parser.getContentStream();
-                try {
+                if (objectId == null) {
+                    // create
+                    ContentStream contentStream = parser.getContentStream();
                     newObjectId = service.create(repositoryId, 
parser.getProperties(), folderId, contentStream,
                             versioningState, parser.getPolicyIds(), null);
-                } finally {
-                    closeContentStream(contentStream);
-                }
-
-                if (stopAfterService(service)) {
-                    return;
-                }
-            } else {
-                if (stopBeforeService(service)) {
-                    return;
-                }
-
-                if (sourceFolderId == null || sourceFolderId.trim().length() 
== 0) {
-                    // addObjectToFolder
-                    service.addObjectToFolder(repositoryId, objectId, 
folderId, null, null);
-                    newObjectId = objectId;
                 } else {
-                    // move
-                    Holder<String> objectIdHolder = new 
Holder<String>(objectId);
-                    service.moveObject(repositoryId, objectIdHolder, folderId, 
sourceFolderId, null);
-                    newObjectId = objectIdHolder.getValue();
+                    if (sourceFolderId == null || 
sourceFolderId.trim().length() == 0) {
+                        // addObjectToFolder
+                        service.addObjectToFolder(repositoryId, objectId, 
folderId, null, null);
+                        newObjectId = objectId;
+                    } else {
+                        // move
+                        Holder<String> objectIdHolder = new 
Holder<String>(objectId);
+                        service.moveObject(repositoryId, objectIdHolder, 
folderId, sourceFolderId, null);
+                        newObjectId = objectIdHolder.getValue();
+                    }
                 }
 
                 if (stopAfterService(service)) {
                     return;
                 }
+            } finally {
+                parser.release();
             }
 
             ObjectInfo objectInfo = service.getObjectInfo(repositoryId, 
newObjectId);
@@ -184,15 +174,20 @@ public class ObjectService {
             AtomEntryParser parser = new 
AtomEntryParser(request.getInputStream(), streamFactory);
 
             // execute
-            if (stopBeforeService(service)) {
-                return;
-            }
+            String newObjectId = null;
+            try {
+                if (stopBeforeService(service)) {
+                    return;
+                }
 
-            String newObjectId = service.createRelationship(repositoryId, 
parser.getProperties(),
-                    parser.getPolicyIds(), null, null, null);
+                newObjectId = service.createRelationship(repositoryId, 
parser.getProperties(), parser.getPolicyIds(),
+                        null, null, null);
 
-            if (stopAfterService(service)) {
-                return;
+                if (stopAfterService(service)) {
+                    return;
+                }
+            } finally {
+                parser.release();
             }
 
             ObjectInfo objectInfo = service.getObjectInfo(repositoryId, 
newObjectId);
@@ -694,50 +689,51 @@ public class ObjectService {
             // execute
             Holder<String> objectIdHolder = new Holder<String>(objectId);
 
-            if ((checkin != null) && (checkin.booleanValue())) {
-                if (stopBeforeService(service)) {
-                    return;
-                }
+            try {
+                if ((checkin != null) && (checkin.booleanValue())) {
+                    if (stopBeforeService(service)) {
+                        return;
+                    }
 
-                ContentStream contentStream = parser.getContentStream();
-                try {
+                    ContentStream contentStream = parser.getContentStream();
                     service.checkIn(repositoryId, objectIdHolder, major, 
parser.getProperties(), contentStream,
                             checkinComment, parser.getPolicyIds(), null, null, 
null);
-                } finally {
-                    closeContentStream(contentStream);
-                }
 
-                if (stopAfterService(service)) {
-                    return;
-                }
-            } else {
-                Properties properties = parser.getProperties();
-                String changeToken = null;
-                if (properties != null) {
-                    changeToken = extractChangeToken(properties);
-                    if (changeToken != null) {
-                        properties = new PropertiesImpl(properties);
-                        ((PropertiesImpl) 
properties).removeProperty(PropertyIds.CHANGE_TOKEN);
+                    if (stopAfterService(service)) {
+                        return;
+                    }
+                } else {
+                    Properties properties = parser.getProperties();
+                    String changeToken = null;
+                    if (properties != null) {
+                        changeToken = extractChangeToken(properties);
+                        if (changeToken != null) {
+                            properties = new PropertiesImpl(properties);
+                            ((PropertiesImpl) 
properties).removeProperty(PropertyIds.CHANGE_TOKEN);
+                        }
                     }
-                }
 
-                if (changeToken == null) {
-                    // not required by the CMIS specification
-                    // -> keep for backwards compatibility with older OpenCMIS
-                    // clients
-                    changeToken = getStringParameter(request, 
Constants.PARAM_CHANGE_TOKEN);
-                }
+                    if (changeToken == null) {
+                        // not required by the CMIS specification
+                        // -> keep for backwards compatibility with older
+                        // OpenCMIS
+                        // clients
+                        changeToken = getStringParameter(request, 
Constants.PARAM_CHANGE_TOKEN);
+                    }
 
-                if (stopBeforeService(service)) {
-                    return;
-                }
+                    if (stopBeforeService(service)) {
+                        return;
+                    }
 
-                service.updateProperties(repositoryId, objectIdHolder, 
changeToken == null ? null : new Holder<String>(
-                        changeToken), properties, null);
+                    service.updateProperties(repositoryId, objectIdHolder, 
changeToken == null ? null
+                            : new Holder<String>(changeToken), properties, 
null);
 
-                if (stopAfterService(service)) {
-                    return;
+                    if (stopAfterService(service)) {
+                        return;
+                    }
                 }
+            } finally {
+                parser.release();
             }
 
             ObjectInfo objectInfo = service.getObjectInfo(repositoryId, 
objectIdHolder.getValue());
@@ -806,14 +802,28 @@ public class ObjectService {
             AtomEntryParser parser = new AtomEntryParser(streamFactory);
             parser.parse(request.getInputStream());
 
-            BulkUpdateImpl bulkUpdate = parser.getBulkUpdate();
-            if (bulkUpdate == null) {
-                throw new CmisInvalidArgumentException("Bulk update data is 
missing!");
-            }
+            // execute
+            List<BulkUpdateObjectIdAndChangeToken> result = null;
+            try {
+                BulkUpdateImpl bulkUpdate = parser.getBulkUpdate();
+                if (bulkUpdate == null) {
+                    throw new CmisInvalidArgumentException("Bulk update data 
is missing!");
+                }
 
-            List<BulkUpdateObjectIdAndChangeToken> result = 
service.bulkUpdateProperties(repositoryId,
-                    bulkUpdate.getObjectIdAndChangeToken(), 
bulkUpdate.getProperties(),
-                    bulkUpdate.getAddSecondaryTypeIds(), 
bulkUpdate.getRemoveSecondaryTypeIds(), null);
+                if (stopBeforeService(service)) {
+                    return;
+                }
+
+                result = service.bulkUpdateProperties(repositoryId, 
bulkUpdate.getObjectIdAndChangeToken(),
+                        bulkUpdate.getProperties(), 
bulkUpdate.getAddSecondaryTypeIds(),
+                        bulkUpdate.getRemoveSecondaryTypeIds(), null);
+
+                if (stopAfterService(service)) {
+                    return;
+                }
+            } finally {
+                parser.release();
+            }
 
             response.setStatus(HttpServletResponse.SC_CREATED);
             response.setContentType(Constants.MEDIATYPE_FEED);

Modified: 
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/PolicyService.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/PolicyService.java?rev=1652084&r1=1652083&r2=1652084&view=diff
==============================================================================
--- 
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/PolicyService.java
 (original)
+++ 
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/PolicyService.java
 Thu Jan 15 13:04:50 2015
@@ -159,14 +159,18 @@ public class PolicyService {
             AtomEntryParser parser = new 
AtomEntryParser(request.getInputStream(), streamFactory);
 
             // execute
-            if (stopBeforeService(service)) {
-                return;
-            }
+            try {
+                if (stopBeforeService(service)) {
+                    return;
+                }
 
-            service.applyPolicy(repositoryId, parser.getId(), objectId, null);
+                service.applyPolicy(repositoryId, parser.getId(), objectId, 
null);
 
-            if (stopAfterService(service)) {
-                return;
+                if (stopAfterService(service)) {
+                    return;
+                }
+            } finally {
+                parser.release();
             }
 
             ObjectInfo objectInfo = service.getObjectInfo(repositoryId, 
parser.getId());

Modified: 
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/RepositoryService.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/RepositoryService.java?rev=1652084&r1=1652083&r2=1652084&view=diff
==============================================================================
--- 
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/RepositoryService.java
 (original)
+++ 
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/RepositoryService.java
 Thu Jan 15 13:04:50 2015
@@ -490,14 +490,19 @@ public class RepositoryService {
             parser.parse(request.getInputStream());
 
             // execute
-            if (stopBeforeService(service)) {
-                return;
-            }
-
-            TypeDefinition newType = service.createType(repositoryId, 
parser.getTypeDefinition(), null);
-
-            if (stopAfterService(service)) {
-                return;
+            TypeDefinition newType = null;
+            try {
+                if (stopBeforeService(service)) {
+                    return;
+                }
+
+                newType = service.createType(repositoryId, 
parser.getTypeDefinition(), null);
+
+                if (stopAfterService(service)) {
+                    return;
+                }
+            } finally {
+                parser.release();
             }
 
             // set headers
@@ -535,14 +540,19 @@ public class RepositoryService {
             parser.parse(request.getInputStream());
 
             // execute
-            if (stopBeforeService(service)) {
-                return;
-            }
-
-            TypeDefinition newType = service.updateType(repositoryId, 
parser.getTypeDefinition(), null);
-
-            if (stopAfterService(service)) {
-                return;
+            TypeDefinition newType = null;
+            try {
+                if (stopBeforeService(service)) {
+                    return;
+                }
+
+                newType = service.updateType(repositoryId, 
parser.getTypeDefinition(), null);
+
+                if (stopAfterService(service)) {
+                    return;
+                }
+            } finally {
+                parser.release();
             }
 
             // set headers

Modified: 
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/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/atompub/VersioningService.java?rev=1652084&r1=1652083&r2=1652084&view=diff
==============================================================================
--- 
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/VersioningService.java
 (original)
+++ 
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/atompub/VersioningService.java
 Thu Jan 15 13:04:50 2015
@@ -60,15 +60,19 @@ public class VersioningService {
             parser.parse(request.getInputStream());
 
             // execute
-            if (stopBeforeService(service)) {
-                return;
-            }
-
             Holder<String> checkOutId = new Holder<String>(parser.getId());
-            service.checkOut(repositoryId, checkOutId, null, null);
+            try {
+                if (stopBeforeService(service)) {
+                    return;
+                }
+
+                service.checkOut(repositoryId, checkOutId, null, null);
 
-            if (stopAfterService(service)) {
-                return;
+                if (stopAfterService(service)) {
+                    return;
+                }
+            } finally {
+                parser.release();
             }
 
             ObjectInfo objectInfo = service.getObjectInfo(repositoryId, 
checkOutId.getValue());

Modified: 
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/CmisBrowserBindingServlet.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/CmisBrowserBindingServlet.java?rev=1652084&r1=1652083&r2=1652084&view=diff
==============================================================================
--- 
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/CmisBrowserBindingServlet.java
 (original)
+++ 
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/CmisBrowserBindingServlet.java
 Thu Jan 15 13:04:50 2015
@@ -75,6 +75,7 @@ import static org.apache.chemistry.openc
 import static 
org.apache.chemistry.opencmis.server.shared.Dispatcher.METHOD_POST;
 
 import java.io.IOException;
+import java.io.InputStream;
 
 import javax.servlet.ServletConfig;
 import javax.servlet.ServletException;
@@ -262,6 +263,18 @@ public class CmisBrowserBindingServlet e
                 printError(context, e, request, response);
             }
         } finally {
+            // in any case close the content stream if one has been provided
+            if (request instanceof POSTHttpServletRequestWrapper) {
+                InputStream stream = ((POSTHttpServletRequestWrapper) 
request).getStream();
+                if (stream != null) {
+                    try {
+                        stream.close();
+                    } catch (IOException e) {
+                        LOG.error("Could not close POST stream: {}", 
e.toString(), e);
+                    }
+                }
+            }
+
             // we are done.
             response.flushBuffer();
         }

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=1652084&r1=1652083&r2=1652084&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
 Thu Jan 15 13:04:50 2015
@@ -111,14 +111,8 @@ public class ObjectService {
                 return;
             }
 
-            String newObjectId = null;
-            try {
-                newObjectId = service
-                        .createDocument(repositoryId, createNewProperties(cp, 
typeCache), folderId, contentStream,
-                                versioningState, createPolicies(cp), 
createAddAcl(cp), createRemoveAcl(cp), null);
-            } finally {
-                closeContentStream(contentStream);
-            }
+            String newObjectId = service.createDocument(repositoryId, 
createNewProperties(cp, typeCache), folderId,
+                    contentStream, versioningState, createPolicies(cp), 
createAddAcl(cp), createRemoveAcl(cp), null);
 
             if (stopAfterService(service)) {
                 return;
@@ -964,12 +958,8 @@ public class ObjectService {
                 return;
             }
 
-            try {
-                service.setContentStream(repositoryId, objectIdHolder, 
overwriteFlag, changeTokenHolder, contentStream,
-                        null);
-            } finally {
-                closeContentStream(contentStream);
-            }
+            service.setContentStream(repositoryId, objectIdHolder, 
overwriteFlag, changeTokenHolder, contentStream,
+                    null);
 
             if (stopAfterService(service)) {
                 return;
@@ -1023,12 +1013,8 @@ public class ObjectService {
                 return;
             }
 
-            try {
-                service.appendContentStream(repositoryId, objectIdHolder, 
changeTokenHolder, contentStream,
-                        isLastChunk, null);
-            } finally {
-                closeContentStream(contentStream);
-            }
+            service.appendContentStream(repositoryId, objectIdHolder, 
changeTokenHolder, contentStream, isLastChunk,
+                    null);
 
             if (stopAfterService(service)) {
                 return;

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=1652084&r1=1652083&r2=1652084&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
 Thu Jan 15 13:04:50 2015
@@ -161,13 +161,9 @@ public class VersioningService {
                 return;
             }
 
-            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);
-            }
+            service.checkIn(repositoryId, objectIdHolder, major,
+                    createUpdateProperties(cp, typeId, null, 
Collections.singletonList(objectId), typeCache),
+                    contentStream, checkinComment, createPolicies(cp), 
createAddAcl(cp), createRemoveAcl(cp), null);
 
             if (stopAfterService(service)) {
                 return;

Modified: 
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/AbstractServiceCall.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/AbstractServiceCall.java?rev=1652084&r1=1652083&r2=1652084&view=diff
==============================================================================
--- 
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/AbstractServiceCall.java
 (original)
+++ 
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/main/java/org/apache/chemistry/opencmis/server/shared/AbstractServiceCall.java
 Thu Jan 15 13:04:50 2015
@@ -126,13 +126,6 @@ public abstract class AbstractServiceCal
     }
 
     /**
-     * Closes a content stream.
-     */
-    public void closeContentStream(ContentStream contentStream) {
-        IOUtils.closeQuietly(contentStream);
-    }
-
-    /**
      * Sets certain HTTP headers if the server implementation requested them.
      * 
      * @return {@code true} if the request has been served by this method (for

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=1652084&r1=1652083&r2=1652084&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
 Thu Jan 15 13:04:50 2015
@@ -159,29 +159,45 @@ public class ThresholdOutputStream exten
     private void openTempFile() throws IOException {
         tempFile = File.createTempFile("opencmis", null, tempDir);
 
-        if (encrypt) {
-            Cipher cipher;
-            try {
-                KeyGenerator keyGenerator = 
KeyGenerator.getInstance(ALGORITHM);
-                keyGenerator.init(KEY_SIZE);
-                key = keyGenerator.generateKey();
+        try {
+            if (encrypt) {
+                Cipher cipher;
+                try {
+                    KeyGenerator keyGenerator = 
KeyGenerator.getInstance(ALGORITHM);
+                    keyGenerator.init(KEY_SIZE);
+                    key = keyGenerator.generateKey();
 
-                cipher = Cipher.getInstance(TRANSFORMATION);
-                cipher.init(Cipher.ENCRYPT_MODE, key);
+                    cipher = Cipher.getInstance(TRANSFORMATION);
+                    cipher.init(Cipher.ENCRYPT_MODE, key);
 
-                iv = cipher.getIV();
-            } catch (Exception e) {
+                    iv = cipher.getIV();
+                } catch (Exception e) {
 
-                if (LOG.isErrorEnabled()) {
-                    LOG.error("Cannot initialize encryption cipher: {}", 
e.toString(), e);
+                    if (LOG.isErrorEnabled()) {
+                        LOG.error("Cannot initialize encryption cipher: {}", 
e.toString(), e);
+                    }
+
+                    throw new IOException("Cannot initialize encryption 
cipher!", e);
                 }
 
-                throw new IOException("Cannot initialize encryption cipher!", 
e);
+                tmpStream = new BufferedOutputStream(new 
CipherOutputStream(new FileOutputStream(tempFile), cipher));
+            } else {
+                tmpStream = new BufferedOutputStream(new 
FileOutputStream(tempFile));
+            }
+        } catch (IOException ioe) {
+            if (tempFile.exists()) {
+                if (!tempFile.delete()) {
+                    if (LOG.isErrorEnabled()) {
+                        LOG.error("Temp file {} could not be deleted!", 
tempFile.getAbsolutePath());
+                    }
+                }
             }
 
-            tmpStream = new BufferedOutputStream(new CipherOutputStream(new 
FileOutputStream(tempFile), cipher));
-        } else {
-            tmpStream = new BufferedOutputStream(new 
FileOutputStream(tempFile));
+            throw ioe;
+        }
+
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Created temp file: {}", tempFile.getAbsolutePath());
         }
     }
 
@@ -297,13 +313,23 @@ public class ThresholdOutputStream exten
             LOG.debug("ThresholdOutputStream destroyed." + (cause == null ? "" 
: " Cause: " + cause.toString()), cause);
         }
 
-        try {
-            if (tmpStream != null) {
+        if (tmpStream != null) {
+            try {
                 tmpStream.flush();
+            } catch (Exception e) {
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Flushing the temp file {} failed: {}", 
tempFile.getAbsolutePath(), e.toString(), e);
+                }
+            }
+            try {
                 tmpStream.close();
+            } catch (Exception e) {
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Closing the temp file {} failed: {}", 
tempFile.getAbsolutePath(), e.toString(), e);
+                }
             }
-        } catch (Exception e) {
-            // ignore
+
+            tmpStream = null;
         }
 
         if (tempFile != null) {
@@ -312,6 +338,10 @@ public class ThresholdOutputStream exten
                 if (LOG.isErrorEnabled()) {
                     LOG.error("Temp file {} could not be deleted!", 
tempFile.getAbsolutePath());
                 }
+            } else {
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Deleted temp file: {}", 
tempFile.getAbsolutePath());
+                }
             }
         }
 
@@ -654,15 +684,24 @@ public class ThresholdOutputStream exten
                 try {
                     stream.close();
                     isClosed = true;
+                    stream = null;
                 } catch (Exception e) {
-                    // ignore
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug("Closing the temp file {} failed: {}", 
tempFile.getAbsolutePath(), e.toString(), e);
+                    }
                 }
             }
 
             if (!isDeleted) {
                 isDeleted = tempFile.delete();
                 if (!isDeleted) {
-                    LOG.warn("Temp file {} could not be deleted!", 
tempFile.getAbsolutePath());
+                    if (LOG.isErrorEnabled()) {
+                        LOG.error("Temp file {} could not be deleted!", 
tempFile.getAbsolutePath());
+                    }
+                } else {
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug("Deleted temp file: {}", 
tempFile.getAbsolutePath());
+                    }
                 }
             }
         }

Modified: 
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/AtomEntryParserTest.java
URL: 
http://svn.apache.org/viewvc/chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/AtomEntryParserTest.java?rev=1652084&r1=1652083&r2=1652084&view=diff
==============================================================================
--- 
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/AtomEntryParserTest.java
 (original)
+++ 
chemistry/opencmis/trunk/chemistry-opencmis-server/chemistry-opencmis-server-bindings/src/test/java/org/apache/chemistry/opencmis/server/impl/AtomEntryParserTest.java
 Thu Jan 15 13:04:50 2015
@@ -191,6 +191,9 @@ public class AtomEntryParserTest {
                 .get(PropertyIds.NAME);
 
         assertEquals("atom.title", nameProperty.getFirstValue());
+
+        aep.release();
+        assertNull(aep.getContentStream());
     }
 
     @Test
@@ -241,6 +244,9 @@ public class AtomEntryParserTest {
         assertNotNull(contentStream.getStream());
 
         contentStream.getStream().close();
+
+        aep.release();
+        assertNull(aep.getContentStream());
     }
 
     private static byte[] parse(byte[] entry) throws Exception {
@@ -258,6 +264,9 @@ public class AtomEntryParserTest {
 
         contentStream.getStream().close();
 
+        aep.release();
+        assertNull(aep.getContentStream());
+
         return baos.toByteArray();
     }
 }


Reply via email to