From dc7d7a83c0efb2dd33ab14f57956f04d507fcddb Mon Sep 17 00:00:00 2001
From: Andreas Romeyke <andreas.romeyke@slub-dresden.de>
Date: Wed, 25 Jan 2023 13:08:56 +0100
Subject: [PATCH] - refactoring, removed relative file handling

---
 .../plugin/storage/nfs/SLUBStoragePlugin.java | 76 ++++++++-----------
 1 file changed, 33 insertions(+), 43 deletions(-)

diff --git a/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java b/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java
index 243533b..4781383 100644
--- a/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java
+++ b/java/org/slub/rosetta/dps/repository/plugin/storage/nfs/SLUBStoragePlugin.java
@@ -108,14 +108,9 @@ public class SLUBStoragePlugin extends AbstractStorageHandler {
         }
         return "(unknown) Bytes/s";
     }
-    public boolean checkFixity(List<Fixity> fixities, String storedEntityIdentifier) throws Exception {
-        log.info("SLUBStoragePlugin.checkFixity()");
-        /* todo: only absolute paths */
-        return checkFixity(fixities, storedEntityIdentifier, true);
-    }
 
-    public boolean checkFixity(List<Fixity> fixities, String storedEntityIdentifier, boolean isRelativePath) throws Exception {
-        log.info("SLUBStoragePlugin.checkFixity() storedEntityIdentifier='" + storedEntityIdentifier + "' isRelativePath=" + isRelativePath);
+    public boolean checkFixity(List<Fixity> fixities, String absolute_storedEntityIdentifier) throws Exception {
+        log.info("SLUBStoragePlugin.checkFixity() storedEntityIdentifier='" + absolute_storedEntityIdentifier + "'");
         // log.info("SLUBStoragePlugin.checkFixity() all fixities=" + fixities);
         if (fixities == null) {
             log.warn("No fixity list provided");
@@ -124,12 +119,12 @@ public class SLUBStoragePlugin extends AbstractStorageHandler {
         boolean result = true;
         for (Fixity fixity : fixities) {
             fixity.setResult(Boolean.FALSE);
-            result &= checkSpecificFixity(storedEntityIdentifier, isRelativePath, fixity);
+            result &= checkSpecificFixity(absolute_storedEntityIdentifier, fixity);
         }
         return result;
     }
 
-    private boolean checkSpecificFixity(String storedEntityIdentifier, boolean isRelativePath, Fixity fixity) throws Exception {
+    private boolean checkSpecificFixity(String absolute_storedEntityIdentifier, Fixity fixity) throws Exception {
         String algorithm = fixity.getAlgorithm();
                     /* HINT: in past versions of Rosetta was a workaround for fixity.getAlgorithm() required,
                              a lowercase string was returned instead of the correct fixity code table entry */
@@ -142,17 +137,16 @@ public class SLUBStoragePlugin extends AbstractStorageHandler {
                             (!Fixity.FixityAlgorithm.SHA256.toString().equals(algorithm)) &&
                             (!Fixity.FixityAlgorithm.CRC32.toString().equals(algorithm))
             ) {
-                // log.info("SLUBStoragePlugin.checkFixity() call checkFixityByPlugin (" + fixity + "," + storedEntityIdentifier + "," + isRelativePath + ")");
-                return checkFixityByPlugin(fixity, storedEntityIdentifier, isRelativePath);
+                // log.info("SLUBStoragePlugin.checkFixity() call checkFixityByPlugin (" + fixity + "," + storedEntityIdentifier + ")");
+                return checkFixityByPlugin(fixity, absolute_storedEntityIdentifier);
             } else {
                 log.info("SLUBStoragePlugin.checkFixity() calcMD5|calcSHA1|calcCRC32|calcSHA256=true");
                 int checksummerAlgorithmIndex = getChecksummerAlgorithmIndex(algorithm);
                 log.info("SLUBStoragePlugin.checkFixity() checksummerAlgorithmIndex=" + checksummerAlgorithmIndex);
                 if (checksummerAlgorithmIndex != -1) {
-                    return checkFixityByBuiltin(fixity, storedEntityIdentifier, isRelativePath, algorithm);
+                    return checkFixityByBuiltin(fixity, absolute_storedEntityIdentifier, algorithm);
                 }
             }
-
         } catch (IOException e) {
             log.error("SLUBStoragePlugin.checkFixity(), I/O error, " + e.getMessage());
             throw e; // let Rosetta know something broke, creates technical issue in workbench
@@ -169,19 +163,18 @@ public class SLUBStoragePlugin extends AbstractStorageHandler {
     /** uses a checksummer object and check if fixity of given builtin algorithm is correct
      *
      * @param fixity concrete fixity object
-     * @param storedEntityIdentifier ROsetta identifier
-     * @param isRelativePath RelativePath
+     * @param absolute_storedEntityIdentifier ROsetta identifier
      * @param algorithm which algorithm should be used
      * @return result
      * @throws NoSuchAlgorithmException if algorithm is unknown
      * @throws IOException if I/O error
      */
-    private boolean checkFixityByBuiltin(Fixity fixity, String storedEntityIdentifier, boolean isRelativePath, String algorithm) throws NoSuchAlgorithmException, IOException {
+    private boolean checkFixityByBuiltin(Fixity fixity, String absolute_storedEntityIdentifier, String algorithm) throws NoSuchAlgorithmException, IOException {
         String oldValue = fixity.getValue();
         log.info("SLUBStoragePlugin.checkFixity() getAlgorithm (2)=" + algorithm);
         log.info("SLUBStoragePlugin.checkFixity() oldvalue=" + oldValue);
         long starttime = System.currentTimeMillis();
-        InputStream is = retrieveEntity(storedEntityIdentifier, isRelativePath);
+        InputStream is = retrieveEntity(absolute_storedEntityIdentifier);
         Checksummer checksummer = new Checksummer(is, true, true, true, true);
         fixity.setValue(checksummer.getChecksum(algorithm));
         log.info("SLUBStoragePlugin.checkFixity() newvalue=" + fixity.getValue());
@@ -189,7 +182,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler {
         boolean result = fixity.getResult();
         is.close();
         long endtime = System.currentTimeMillis();
-        var pathname = (isRelativePath ? getDirRoot() : "") + storedEntityIdentifier;
+        var pathname = absolute_storedEntityIdentifier;
         log.info("SLUBStoragePlugin.checkFixity() pathname='"+ pathname + "' result=" + result + " (builtin " + throughput(starttime, endtime, pathname)+ ")");
         return result;
     }
@@ -197,12 +190,11 @@ public class SLUBStoragePlugin extends AbstractStorageHandler {
     /** check fixity by calling its registered plugin
      *
      * @param fixity concrete fixity object
-     * @param storedEntityIdentifier path to file which should be checked
-     * @param isRelativePath indicates if path is relative
+     * @param absolute_storedEntityIdentifier path to file which should be checked
      * @return result
      * @throws Exception to inform rosetta
      */
-    private boolean checkFixityByPlugin(Fixity fixity, String storedEntityIdentifier, boolean isRelativePath) throws Exception {
+    private boolean checkFixityByPlugin(Fixity fixity, String absolute_storedEntityIdentifier) throws Exception {
         log.info("SLUBStoragePlugin.checkFixityByPlugin() another fixity");
         String pluginname = fixity.getPluginName();
         log.info("SLUBStoragePlugin.checkFixityByPlugin() pluginname=" + pluginname);
@@ -222,7 +214,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler {
         String oldValue = fixity.getValue();
         log.info("SLUBStoragePlugin.checkFixityByPlugin() oldvalue=" + oldValue);
         long starttime = System.currentTimeMillis();
-        fixity.setValue(getChecksumUsingPlugin(isRelativePath ? getLocalFilePath(storedEntityIdentifier) : storedEntityIdentifier, pluginname, oldValue));
+        fixity.setValue(getChecksumUsingPlugin(absolute_storedEntityIdentifier, pluginname, oldValue));
         /* HINT: if plugin name is still empty a java.lang.NullPointerException gets thrown
                  Rosetta will handle it and let us know (creates technical issue in workbench)
          */
@@ -230,15 +222,16 @@ public class SLUBStoragePlugin extends AbstractStorageHandler {
         log.info("SLUBStoragePlugin.checkFixityByPlugin() newvalue=" + fixity.getValue());
         boolean result = fixity.getResult();
         long endtime = System.currentTimeMillis();
-        var pathname = (isRelativePath ? getDirRoot() : "") + storedEntityIdentifier;
+        var pathname = absolute_storedEntityIdentifier;
         log.info("SLUBStoragePlugin.checkFixityByPlugin() pathname='" + pathname + "' result=" + result + " (plugins " + throughput(starttime, endtime, pathname)+ ")");
         return result;
     }
 
-    public boolean deleteEntity(String storedEntityIdentifier)
+    public boolean deleteEntity(String relative_storedEntityIdentifier)
     {
-        log.info("SLUBStoragePlugin.deleteEntity() storedEntityIdentifier='" + storedEntityIdentifier + "'");
-        File file = new File(getDirRoot() + storedEntityIdentifier);
+        log.info("SLUBStoragePlugin.deleteEntity() storedEntityIdentifier='" + relative_storedEntityIdentifier + "'");
+        var absolute_filename = getFullFilePath(relative_storedEntityIdentifier);
+        File file = new File(absolute_filename);
         try
         {
             return file.delete();
@@ -266,22 +259,19 @@ public class SLUBStoragePlugin extends AbstractStorageHandler {
             throws IOException
     {
         log.info("SLUBStoragePlugin.retrieveEntity() with '" + storedEntityIdentifier + "'");
-        return retrieveEntity(storedEntityIdentifier, true);
-    }
-    public InputStream retrieveEntity(String storedEntityIdentifier, boolean isRelative)
-            throws IOException
-    {
-        log.info("SLUBStoragePlugin.retrieveEntity() with '" + storedEntityIdentifier + "' isrelative=" + isRelative);
-        var pathname = (isRelative ? getDirRoot() : "") + storedEntityIdentifier;
         // TODO: 64k (1MB) buffer set
         // OLD, without buffering, but working: InputStream foo =  java.nio.file.Files.newInputStream( Paths.get( pathname));
-        return new BufferedInputStream( java.nio.file.Files.newInputStream(Paths.get( pathname)), getBlockSize());
+        var absolute_filename = getFullFilePath(storedEntityIdentifier);
+        var absolute_path = Paths.get(absolute_filename);
+        var is = java.nio.file.Files.newInputStream( absolute_path );
+        return new BufferedInputStream( is , getBlockSize());
     }
     public byte[] retrieveEntityByRange(String storedEntityIdentifier, long start, long end) throws Exception
     {
         log.info("SLUBStoragePlugin.retrieveEntitybyRange() with '" + storedEntityIdentifier + "' start=" + start + " end=" + end);
         /* try-with-ressource block, no close if failed needed */
-        try (RandomAccessFile file = new RandomAccessFile(getDirRoot() + storedEntityIdentifier, "r"))
+        var absolute_filename = getFullFilePath(storedEntityIdentifier);
+        try (RandomAccessFile file = new RandomAccessFile(absolute_filename, "r"))
         {
             long starttime = System.currentTimeMillis();
             file.seek(start);
@@ -317,24 +307,25 @@ public class SLUBStoragePlugin extends AbstractStorageHandler {
         if (existsDescPath != null)
         {
             destFilePath = existsDescPath;
-            isCopyFileNeeded = !checkFixity(storedEntityMetadata.getFixities(), destFilePath, false);
+            /* destFilePath is relative, checkfixity expects absolute */
+            isCopyFileNeeded = !checkFixity(storedEntityMetadata.getFixities(), getFullFilePath(destFilePath));
         }
         log.debug("SLUBStoragePlugin.storeEntity() destFilePath='" + destFilePath + "'");
         Map<String, String> paths = getStoreEntityIdentifier(storedEntityMetadata, destFilePath);
         String storedEntityIdentifier = paths.get("relativeDirectoryPath");
         log.debug("SLUBStoragePlugin.storeEntity() storedEntityIdentifier='" + storedEntityIdentifier + "'");
-        destFilePath = paths.get("destFilePath");
-        log.debug("SLUBStoragePlugin.storeEntity() destFilePath (2)='" + destFilePath + "'");
+        String absolute_destFilePath = paths.get("destFilePath");
+        log.debug("SLUBStoragePlugin.storeEntity() destFilePath (2)='" + absolute_destFilePath + "'");
         log.debug("SLUBStoragePlugin.storeEntity() isCopyFileNeeded='" + isCopyFileNeeded + "'");
         if (isCopyFileNeeded)
         {
             if (canHandleSourcePath(storedEntityMetadata.getCurrentFilePath()))
             {
-                log.debug("SLUBStoragePlugin.storeEntity() destFilePath canhandle sourcepath");
+                log.debug("SLUBStoragePlugin.storeEntity() absolute_destFilePath canhandle sourcepath");
                 if (is != null) {
                     is.close();
                 }
-                copyStream(storedEntityMetadata, destFilePath);
+                copyStream(storedEntityMetadata, absolute_destFilePath);
                 log.info("SLUBStoragePlugin.storeEntity() try copy (copyStream) was successfull");
             }
             else
@@ -345,12 +336,12 @@ public class SLUBStoragePlugin extends AbstractStorageHandler {
                     log.warn("SLUBStoragePlugin.storeEntity() InputStream is null");
                     return null;
                 }
-                try (OutputStream output = java.nio.file.Files.newOutputStream( Paths.get( destFilePath)))
+                try (OutputStream output = java.nio.file.Files.newOutputStream( Paths.get( absolute_destFilePath)))
                 {
                     long starttime = System.currentTimeMillis();
                     IOUtil.copy(is, output, getBlockSize());
                     long endtime = System.currentTimeMillis();
-                    log.info("SLUBStoragePlugin.storeEntity() try copy (IOUtil.copy) was successfull (" + throughput(starttime, endtime, destFilePath)+ ")" );
+                    log.info("SLUBStoragePlugin.storeEntity() try copy (IOUtil.copy) was successfull (" + throughput(starttime, endtime, absolute_destFilePath)+ ")" );
                 }
             }
             if (!checkFixity(storedEntityMetadata.getFixities(), storedEntityIdentifier)) {
@@ -483,7 +474,6 @@ public class SLUBStoragePlugin extends AbstractStorageHandler {
         // paths.put("relativeDirectoryPath", (relativeDirectoryPath + getNextDir(destFilePath) + File.separator + fileName));
         paths.put("relativeDirectoryPath", (relativeDirectoryPath + File.separator + fileName));
         return paths;
-
     }
     /** copied from NFS Storage Plugin, enhanced with debugging info,
      * this combines full file path and creates parent directories if needed  {@inheritDoc}
-- 
GitLab