From fb404e19e60477b012460ff30ee3b0eb13122dea Mon Sep 17 00:00:00 2001
From: vkovtun <vkovtun@ethz.ch>
Date: Wed, 29 Nov 2023 17:15:25 +0100
Subject: [PATCH] BIS-772: Work in progress.

---
 .../v3/executor/exporter/ExportExecutor.java      | 15 ++++++++++-----
 .../v3/executor/exporter/ExportExecutorTest.java  |  8 ++++++++
 2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/server-application-server/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/exporter/ExportExecutor.java b/server-application-server/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/exporter/ExportExecutor.java
index 1bb61cc0509..56ddbcb6edb 100644
--- a/server-application-server/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/exporter/ExportExecutor.java
+++ b/server-application-server/source/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/exporter/ExportExecutor.java
@@ -129,13 +129,11 @@ import ch.ethz.sis.openbis.generic.asapi.v3.dto.sample.fetchoptions.SampleTypeFe
 import ch.ethz.sis.openbis.generic.asapi.v3.dto.sample.search.SampleTypeSearchCriteria;
 import ch.ethz.sis.openbis.generic.asapi.v3.dto.space.Space;
 import ch.ethz.sis.openbis.generic.asapi.v3.exceptions.NotFetchedException;
-import ch.ethz.sis.openbis.generic.dssapi.v3.IDataStoreServerApi;
 import ch.ethz.sis.openbis.generic.dssapi.v3.dto.datasetfile.DataSetFile;
 import ch.ethz.sis.openbis.generic.dssapi.v3.dto.datasetfile.fetchoptions.DataSetFileFetchOptions;
 import ch.ethz.sis.openbis.generic.dssapi.v3.dto.datasetfile.search.DataSetFileSearchCriteria;
 import ch.ethz.sis.openbis.generic.server.asapi.v3.IApplicationServerInternalApi;
 import ch.ethz.sis.openbis.generic.server.asapi.v3.executor.IOperationContext;
-import ch.ethz.sis.openbis.generic.server.asapi.v3.executor.common.search.AbstractSearchObjectsOperationExecutor;
 import ch.ethz.sis.openbis.generic.server.xls.export.ExportableKind;
 import ch.ethz.sis.openbis.generic.server.xls.export.ExportablePermId;
 import ch.ethz.sis.openbis.generic.server.xls.export.XLSExport;
@@ -222,11 +220,13 @@ public class ExportExecutor implements IExportExecutor
 
     private static final Logger OPERATION_LOG = LogFactory.getLogger(LogCategory.OPERATION, ExportExecutor.class);
 
+    /** All characters except the ones we consider safe as a folder name. */
+    private static final String UNSAFE_CHARACTERS_REGEXP = "[^\\w $!#%'()+,\\-.;=@\\[\\]^{}_~]";
+
     @Autowired
     private ISessionWorkspaceProvider sessionWorkspaceProvider;
 
-    @Autowired
-    private IDataStoreServerApi v3Dss;
+//    private IDataStoreServerApi v3Dss = (IDataStoreServerApi) ServiceProvider.getDssServiceV3().getService();
 
     @Resource(name = ExposablePropertyPlaceholderConfigurer.PROPERTY_CONFIGURER_BEAN_NAME)
     private ExposablePropertyPlaceholderConfigurer configurer;
@@ -974,13 +974,18 @@ public class ExportExecutor implements IExportExecutor
     {
         try
         {
-            return entity.getVarcharProperty(NAME_PROPERTY_NAME);
+            return escapeUnsafeCharacters(entity.getVarcharProperty(NAME_PROPERTY_NAME));
         } catch (final NotFetchedException e)
         {
             return null;
         }
     }
 
+    static String escapeUnsafeCharacters(final String name)
+    {
+        return name.replaceAll(UNSAFE_CHARACTERS_REGEXP, "_");
+    }
+
     private static void exportXls(final ZipOutputStream zos, final BufferedOutputStream bos, final XLSExport.PrepareWorkbookResult xlsExportResult,
             final Workbook wb, final Collection<String> warnings) throws IOException
     {
diff --git a/server-application-server/sourceTest/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/exporter/ExportExecutorTest.java b/server-application-server/sourceTest/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/exporter/ExportExecutorTest.java
index 963456e1209..e47d75c2c79 100644
--- a/server-application-server/sourceTest/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/exporter/ExportExecutorTest.java
+++ b/server-application-server/sourceTest/java/ch/ethz/sis/openbis/generic/server/asapi/v3/executor/exporter/ExportExecutorTest.java
@@ -347,4 +347,12 @@ public class ExportExecutorTest
         ExportExecutor.getFolderName(prefix, spaceCode, projectCode, containerCode, entityCode, entityName);
     }
 
+    @Test()
+    public void testEscapeUnsafeCharacters()
+    {
+        final String input = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 $!#%'()+,-.;=@[]^_{}~\\/:*?\"<>|`";
+        final String expectedOutput = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 $!#%'()+,-.;=@[]^_{}~__________";
+        assertEquals(ExportExecutor.escapeUnsafeCharacters(input), expectedOutput);
+    }
+
 }
\ No newline at end of file
-- 
GitLab