From c5b726ecbd660876ea1b089d6a8f3b3ec19277ae Mon Sep 17 00:00:00 2001
From: felmer <franz-josef.elmer@id.ethz.ch>
Date: Mon, 26 Feb 2018 10:25:54 +0100
Subject: [PATCH] SSDM-5101: Same small refactorings in PutDataSetService

---
 .../etlserver/api/v1/PutDataSetService.java   | 67 ++++++++-----------
 1 file changed, 27 insertions(+), 40 deletions(-)

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 8bd0b279faa..757582998b6 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
@@ -274,26 +274,12 @@ public class PutDataSetService implements IPutDataSetService
             doInitialization();
         }
 
-        if (StringUtils.isBlank(sessionToken))
-        {
-            throw new UserFailureException("Session token cannot be null or empty");
-        }
-        if (sessionToken.contains("/"))
-        {
-            throw new UserFailureException("Session token must not contain '/'");
-        }
+        validateSessionToken(sessionToken);
+        validateUploadId(uploadId);
         if (newDataSet == null)
         {
             throw new UserFailureException("New data set cannot be null");
         }
-        if (StringUtils.isBlank(uploadId))
-        {
-            throw new UserFailureException("Upload id cannot be null or empty");
-        }
-        if (uploadId.contains("/"))
-        {
-            throw new UserFailureException("Upload id must not contain '/'");
-        }
 
         ServiceProvider.getOpenBISService().checkSession(sessionToken);
 
@@ -346,14 +332,8 @@ public class PutDataSetService implements IPutDataSetService
 
         try
         {
-            if (StringUtils.isBlank(sessionToken))
-            {
-                throw new UserFailureException("Session token cannot be null or empty");
-            }
-            if (sessionToken.contains("/"))
-            {
-                throw new UserFailureException("Session token must not contain '/'");
-            }
+            validateSessionToken(sessionToken);
+            validateUploadId(uploadId);
             if (StringUtils.isBlank(filePath))
             {
                 throw new UserFailureException("File path cannot be null or empty");
@@ -370,14 +350,6 @@ public class PutDataSetService implements IPutDataSetService
             {
                 throw new UserFailureException("Data set type cannot be null or empty");
             }
-            if (StringUtils.isBlank(uploadId))
-            {
-                throw new UserFailureException("Upload id cannot be null or empty");
-            }
-            if (uploadId.contains("/"))
-            {
-                throw new UserFailureException("Upload id must not contain '/'");
-            }
             if (inputStream == null)
             {
                 throw new UserFailureException("Input stream cannot be null");
@@ -613,14 +585,7 @@ public class PutDataSetService implements IPutDataSetService
             doInitialization();
         }
 
-        if (StringUtils.isBlank(sessionToken))
-        {
-            throw new IllegalArgumentException("Session token cannot be null or empty");
-        }
-        if (sessionToken.contains("/"))
-        {
-            throw new UserFailureException("Session token must not contain '/'");
-        }
+        validateSessionToken(sessionToken);
 
         Collection<TopLevelDataSetRegistratorGlobalState> states = getThreadGlobalStates();
 
@@ -646,6 +611,28 @@ public class PutDataSetService implements IPutDataSetService
         }
     }
 
+    private void validateSessionToken(String sessionToken)
+    {
+        validate(sessionToken, "Session token");
+    }
+
+    private void validateUploadId(String uploadId)
+    {
+        validate(uploadId, "Upload id");
+    }
+
+    private void validate(String uploadId, String name)
+    {
+        if (StringUtils.isBlank(uploadId))
+        {
+            throw new UserFailureException(name + " cannot be null or empty");
+        }
+        if (uploadId.contains("/"))
+        {
+            throw new UserFailureException(name + " must not contain '/'");
+        }
+    }
+
 }
 
 /**
-- 
GitLab