From cc5cfa25fffc25fd30740b173f0ac9f19f6fb71b Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Tue, 16 Jan 2018 12:57:34 +0000
Subject: [PATCH] SSDM-6019: Fixing a bug reveal in openBIS manual tests:
 Custom import failed. The fix doesn't check authorization for custom import
 because the data set owner isn't known.

SVN: 39100
---
 .../cisd/etlserver/api/v1/PutDataSetExecutor.java         | 7 ++++---
 .../systemsx/cisd/etlserver/api/v1/PutDataSetService.java | 8 ++++----
 .../api/v1/PutDataSetTopLevelDataSetHandler.java          | 7 ++++---
 .../ch/systemsx/cisd/etlserver/api/v1/PutDataSetUtil.java | 6 +++++-
 .../api/v1/PutDataSetTopLevelDataSetHandlerTest.java      | 4 ++--
 5 files changed, 19 insertions(+), 13 deletions(-)

diff --git a/datastore_server/source/java/ch/systemsx/cisd/etlserver/api/v1/PutDataSetExecutor.java b/datastore_server/source/java/ch/systemsx/cisd/etlserver/api/v1/PutDataSetExecutor.java
index 9759da99dd5..8ca00943510 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/etlserver/api/v1/PutDataSetExecutor.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/etlserver/api/v1/PutDataSetExecutor.java
@@ -162,11 +162,12 @@ class PutDataSetExecutor implements IDataSetHandlerRpc
     /**
      * Run the put command; does *not* close the input stream &mdash; clients of the executor are expected to close the input stream when appropriate.
      * 
+     * @param noOwnerAllowed <code>true</code> if missing data set owner is allowed. This is the case for custom import.
      * @throws IOException
      */
-    public List<DataSetInformation> execute() throws UserFailureException, IOException
+    public List<DataSetInformation> execute(boolean noOwnerAllowed) throws UserFailureException, IOException
     {
-        PutDataSetUtil.checkAccess(sessionToken, getOpenBisService(), newDataSet);
+        PutDataSetUtil.checkAccess(sessionToken, getOpenBisService(), newDataSet, noOwnerAllowed);
 
         writeDataSetToTempDirectory();
         overrideOrNull = null;
@@ -186,7 +187,7 @@ class PutDataSetExecutor implements IDataSetHandlerRpc
      */
     public List<DataSetInformation> executeWithoutWriting() throws UserFailureException
     {
-        PutDataSetUtil.checkAccess(sessionToken, getOpenBisService(), newDataSet);
+        PutDataSetUtil.checkAccess(sessionToken, getOpenBisService(), newDataSet, false);
 
         overrideOrNull = null;
 
diff --git a/datastore_server/source/java/ch/systemsx/cisd/etlserver/api/v1/PutDataSetService.java b/datastore_server/source/java/ch/systemsx/cisd/etlserver/api/v1/PutDataSetService.java
index af25b6b502f..c5e75e0e6f6 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/etlserver/api/v1/PutDataSetService.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/etlserver/api/v1/PutDataSetService.java
@@ -156,13 +156,13 @@ public class PutDataSetService
                         new PutDataSetExecutor(this,
                                 ((PutDataSetServerPluginHolder) registrator).getPlugin(),
                                 sessionToken, createNewDataSetDTO(customImportFile),
-                                getAsInputStream(customImportFile)).execute();
+                                getAsInputStream(customImportFile)).execute(true);
             } else
             {
                 infos =
                         new PutDataSetTopLevelDataSetHandler(this, registrator, sessionToken,
                                 createNewDataSetDTO(customImportFile),
-                                getAsInputStream(customImportFile)).execute();
+                                getAsInputStream(customImportFile)).execute(true);
             }
             StringBuilder sb = new StringBuilder();
             for (DataSetInformation info : infos)
@@ -220,12 +220,12 @@ public class PutDataSetService
                 infos =
                         new PutDataSetExecutor(this,
                                 ((PutDataSetServerPluginHolder) registrator).getPlugin(),
-                                sessionToken, newDataSet, inputStream).execute();
+                                sessionToken, newDataSet, inputStream).execute(false);
             } else
             {
                 infos =
                         new PutDataSetTopLevelDataSetHandler(this, registrator, sessionToken,
-                                newDataSet, inputStream).execute();
+                                newDataSet, inputStream).execute(false);
             }
             StringBuilder sb = new StringBuilder();
             for (DataSetInformation info : infos)
diff --git a/datastore_server/source/java/ch/systemsx/cisd/etlserver/api/v1/PutDataSetTopLevelDataSetHandler.java b/datastore_server/source/java/ch/systemsx/cisd/etlserver/api/v1/PutDataSetTopLevelDataSetHandler.java
index 74c7015ad95..abc85f9fb6e 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/etlserver/api/v1/PutDataSetTopLevelDataSetHandler.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/etlserver/api/v1/PutDataSetTopLevelDataSetHandler.java
@@ -173,11 +173,12 @@ class PutDataSetTopLevelDataSetHandler
     /**
      * Run the put command; does *not* close the input stream &mdash; clients of the executor are expected to close the input stream when appropriate.
      * 
+     * @param noOwnerAllowed <code>true</code> if missing data set owner is allowed. This is the case for custom import.
      * @throws IOException
      */
-    public List<DataSetInformation> execute() throws UserFailureException, IOException
+    public List<DataSetInformation> execute(boolean noOwnerAllowed) throws UserFailureException, IOException
     {
-        PutDataSetUtil.checkAccess(sessionToken, getOpenBisService(), newDataSet);
+        PutDataSetUtil.checkAccess(sessionToken, getOpenBisService(), newDataSet, noOwnerAllowed);
 
         writeDataSetToTempDirectory();
 
@@ -198,7 +199,7 @@ class PutDataSetTopLevelDataSetHandler
      */
     public List<DataSetInformation> executeWithoutWriting() throws UserFailureException
     {
-        PutDataSetUtil.checkAccess(sessionToken, getOpenBisService(), newDataSet);
+        PutDataSetUtil.checkAccess(sessionToken, getOpenBisService(), newDataSet, false);
 
         // Register the data set
         try
diff --git a/datastore_server/source/java/ch/systemsx/cisd/etlserver/api/v1/PutDataSetUtil.java b/datastore_server/source/java/ch/systemsx/cisd/etlserver/api/v1/PutDataSetUtil.java
index 558dd67e92a..e4e52ccca45 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/etlserver/api/v1/PutDataSetUtil.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/etlserver/api/v1/PutDataSetUtil.java
@@ -27,7 +27,7 @@ import ch.systemsx.cisd.openbis.dss.generic.shared.api.v1.NewDataSetDTO.DataSetO
 public class PutDataSetUtil
 {
 
-    public static void checkAccess(String sessionToken, IEncapsulatedOpenBISService service, NewDataSetDTO newDataSet)
+    public static void checkAccess(String sessionToken, IEncapsulatedOpenBISService service, NewDataSetDTO newDataSet, boolean noOwnerAllowed)
     {
         if (newDataSet == null)
         {
@@ -38,6 +38,10 @@ public class PutDataSetUtil
 
         if (owner == null)
         {
+            if (noOwnerAllowed)
+            {
+                return;
+            }
             throw new UserFailureException("Owner of a new data set cannot be null");
         }
 
diff --git a/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/api/v1/PutDataSetTopLevelDataSetHandlerTest.java b/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/api/v1/PutDataSetTopLevelDataSetHandlerTest.java
index 6966523d5fb..e833e853cf1 100644
--- a/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/api/v1/PutDataSetTopLevelDataSetHandlerTest.java
+++ b/datastore_server/sourceTest/java/ch/systemsx/cisd/etlserver/api/v1/PutDataSetTopLevelDataSetHandlerTest.java
@@ -182,7 +182,7 @@ public class PutDataSetTopLevelDataSetHandlerTest extends AbstractFileSystemTest
                 new PutDataSetTopLevelDataSetHandler(putDataSetService, registrator, SESSION_TOKEN,
                         newDataSet, inputStream);
 
-        handler.execute();
+        handler.execute(false);
 
         assertEquals("MY-TYPE", dataSetInfoMatcher.recordedObject().getDataSetType().getCode());
         List<NewProperty> dataSetProperties =
@@ -230,7 +230,7 @@ public class PutDataSetTopLevelDataSetHandlerTest extends AbstractFileSystemTest
                 new PutDataSetTopLevelDataSetHandler(putDataSetService, registrator, SESSION_TOKEN,
                         newDataSet, inputStream);
 
-        handler.execute();
+        handler.execute(false);
 
         assertEquals(null, dataSetInfoMatcher.recordedObject().getDataSetType());
         assertEquals(0, dataSetInfoMatcher.recordedObject().getDataSetProperties().size());
-- 
GitLab