From d6ba8fcc2796fb1abc92d1f840a0dd8959987e5c Mon Sep 17 00:00:00 2001
From: Andreas Romeyke <art1@andreas-romeyke.de>
Date: Tue, 23 Aug 2022 13:38:04 +0200
Subject: [PATCH] - simplified checkFixity() outsourcing inner loop -
 simplified checkFixityByBuiltin() - simplified checkFixityByPlugin()

---
 .../plugin/storage/nfs/SLUBStoragePlugin.java | 124 ++++++++----------
 1 file changed, 53 insertions(+), 71 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 0b4ae07..c9bf904 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
@@ -82,87 +82,78 @@ public class SLUBStoragePlugin extends AbstractStorageHandler {
         log.info("SLUBStoragePlugin.checkFixity()");
         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);
         // log.info("SLUBStoragePlugin.checkFixity() all fixities=" + fixities);
+        if (fixities != null) {
+            log.warn("No fixity list provided");
+            return true;
+        }
         boolean result = true;
-        if (fixities != null)
-        {
-            InputStream is = null;
-            try {
-                is = retrieveEntity(storedEntityIdentifier, isRelativePath);
-                Checksummer checksummer = new Checksummer(is, true, true, true,true);
-                for (Fixity fixity : fixities) {
-                    fixity.setResult(Boolean.FALSE);
-                    String algorithm = fixity.getAlgorithm();
+        for (Fixity fixity : fixities) {
+            fixity.setResult(Boolean.FALSE);
+            result &= checkSpecificFixity(storedEntityIdentifier, isRelativePath, fixity);
+        }
+        return result;
+    }
+
+    private boolean checkSpecificFixity(String storedEntityIdentifier, boolean isRelativePath, Fixity fixity) throws IOException, NoSuchAlgorithmException, 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 */
-                    log.info("SLUBStoragePlugin.checkFixity() getAlgorithm=" + algorithm);
-                    // log.info("SLUBStoragePlugin.checkFixity() FixityAlgorithm.MD5=" + Fixity.FixityAlgorithm.MD5.toString());
-                    // log.info("SLUBStoragePlugin.checkFixity() FixityAlgorithm.SHA1=" + Fixity.FixityAlgorithm.SHA1.toString());
-                    // log.info("SLUBStoragePlugin.checkFixity() FixityAlgorithm.SHA256=" + Fixity.FixityAlgorithm.SHA256.toString());
-                    // log.info("SLUBStoragePlugin.checkFixity() FixityAlgorithm.CRC32=" + Fixity.FixityAlgorithm.CRC32.toString());
-                    if (
-                            (!Fixity.FixityAlgorithm.MD5.toString().equals(algorithm)) &&
+        log.info("SLUBStoragePlugin.checkFixity() getAlgorithm=" + algorithm);
+        try {
+
+            if (
+                    (!Fixity.FixityAlgorithm.MD5.toString().equals(algorithm)) &&
                             (!Fixity.FixityAlgorithm.SHA1.toString().equals(algorithm)) &&
                             (!Fixity.FixityAlgorithm.SHA256.toString().equals(algorithm)) &&
                             (!Fixity.FixityAlgorithm.CRC32.toString().equals(algorithm))
-                    ) {
-                        // log.info("SLUBStoragePlugin.checkFixity() call checkFixityByPlugin (" + fixity + "," + storedEntityIdentifier + "," + isRelativePath + "," + result + ")");
-                        result = checkFixityByPlugin(fixity, storedEntityIdentifier, isRelativePath, result);
-                    } else {
-                        log.info("SLUBStoragePlugin.checkFixity() calcMD5|calcSHA1|calcCRC32|calcSHA256=true");
-                        int checksummerAlgorithmIndex = getChecksummerAlgorithmIndex(algorithm);
-                        log.info("SLUBStoragePlugin.checkFixity() checksummerAlgorithmIndex=" + checksummerAlgorithmIndex);
-                        if (checksummerAlgorithmIndex != -1)
-                            result = checkFixityByBuiltin(fixity, algorithm, checksummer, result);
-                    }
+            ) {
+                // log.info("SLUBStoragePlugin.checkFixity() call checkFixityByPlugin (" + fixity + "," + storedEntityIdentifier + "," + isRelativePath + "," + result + ")");
+                return checkFixityByPlugin(fixity, storedEntityIdentifier, isRelativePath);
+            } 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);
                 }
             }
-            catch (IOException e) {
-                log.error("SLUBStoragePlugin.checkFixity(), I/O error, " + e.getMessage());
-                throw e; // let Rosetta know something broke, creates technical issue in workbench
-            }
-            catch (NoSuchAlgorithmException e) {
-                log.error( "SLUBStoragePlugin.checkFixity(), missed Algorithm, " + e.getMessage());
-                throw e; // let Rosetta know something broke, creates technical issue in workbench
-            }
-            catch (Exception e) {
-                log.error( "SLUBStoragePlugin.checkFixity(), unknown problem, " + e.getMessage());
-                throw e; // let Rosetta know something broke, creates technical issue in workbench
-            }
-            finally
-            {
-                // log.info("SLUBStoragePlugin.checkFixity() finally called");
-                if (is != null) {
-                    try {
-                        is.close();
-                    } catch (Exception e) {
-                        log.warn("SLUBStoragePlugin.checkFixity(), failed closing file, " + e.getMessage());
-                    }
-                }
-            }
-        }
-        return result;
+
+        } catch (IOException e) {
+            log.error("SLUBStoragePlugin.checkFixity(), I/O error, " + e.getMessage());
+            throw e; // let Rosetta know something broke, creates technical issue in workbench
+        } catch (NoSuchAlgorithmException e) {
+            log.error("SLUBStoragePlugin.checkFixity(), missed Algorithm, " + e.getMessage());
+            throw e; // let Rosetta know something broke, creates technical issue in workbench
+        } catch (Exception e) {
+            log.error("SLUBStoragePlugin.checkFixity(), unknown problem, " + e.getMessage());
+            throw e; // let Rosetta know something broke, creates technical issue in workbench
+        }
+        return false;
     }
 
     /** uses a checksummer object and check if fixity of given builtin algorithm is correct
      *
      * @param fixity concrete fixity object
+     * @param storedEntityIdentifier
+     * @param is RelativePath
      * @param algorithm which algorithm should be used
-     * @param checksummer checksummer object of given input stream
-     * @param result result is boolean variable to hold states of all fixity checks
      * @return result
      * @throws NoSuchAlgorithmException
      */
-    private boolean checkFixityByBuiltin(Fixity fixity, String algorithm, Checksummer checksummer, boolean result) throws NoSuchAlgorithmException {
+    private boolean checkFixityByBuiltin(Fixity fixity, String storedEntityIdentifier, boolean isRelativePath, String algorithm) throws NoSuchAlgorithmException, IOException {
         String oldValue = fixity.getValue();
         log.info("SLUBStoragePlugin.checkFixity() getAlgorithm (2)=" + algorithm);
         log.info("SLUBStoragePlugin.checkFixity() oldvalue=" + oldValue);
+        InputStream is = retrieveEntity(storedEntityIdentifier, isRelativePath);
+        Checksummer checksummer = new Checksummer(is, true, true, true, true);
         fixity.setValue(checksummer.getChecksum(algorithm));
         log.info("SLUBStoragePlugin.checkFixity() newvalue=" + fixity.getValue());
         fixity.setResult((oldValue == null) || (oldValue.equalsIgnoreCase(fixity.getValue())));
-        result &= fixity.getResult();
+        boolean result = fixity.getResult();
         log.info("SLUBStoragePlugin.checkFixity() result=" + result);
         return result;
     }
@@ -176,7 +167,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler {
      * @return result
      * @throws Exception
      */
-    private boolean checkFixityByPlugin(Fixity fixity, String storedEntityIdentifier, boolean isRelativePath, boolean result) throws Exception {
+    private boolean checkFixityByPlugin(Fixity fixity, String storedEntityIdentifier, boolean isRelativePath) throws Exception {
         log.info("SLUBStoragePlugin.checkFixityByPlugin() another fixity");
         String pluginname = "";
         try {
@@ -218,7 +209,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler {
          */
         fixity.setResult((oldValue == null) || (oldValue.equals(fixity.getValue())));
         log.info("SLUBStoragePlugin.checkFixityByPlugin() newvalue=" + fixity.getValue());
-        result &= fixity.getResult();
+        boolean result = fixity.getResult();
         log.info("SLUBStoragePlugin.checkFixityByPlugin() result=" + result);
         return result;
     }
@@ -267,13 +258,14 @@ public class SLUBStoragePlugin extends AbstractStorageHandler {
     public byte[] retrieveEntityByRange(String storedEntityIdentifier, long start, long end) throws Exception
     {
         log.info("SLUBStoragePlugin.retrieveEntitybyRange() with '" + storedEntityIdentifier + "' start=" + start + " end=" + end);
-        byte[] bytes = new byte[(int)(end - start + 1L)];
-        RandomAccessFile file = null;
+        byte[] bytes = null;
         try
         {
-            file = new RandomAccessFile(this.parameters.get("DIR_ROOT") + storedEntityIdentifier, "r");
+            RandomAccessFile file = new RandomAccessFile(this.parameters.get("DIR_ROOT") + storedEntityIdentifier, "r");
             file.seek(start);
+            bytes = new byte[(int)(end - start + 1L)];
             file.readFully(bytes, 0, (int)(end - start + 1L));
+            file.close();
         }
         catch (FileNotFoundException e) {
             log.error("SLUBStoragePlugin.retrieveEntitybyRange(), file not found, " + e.getMessage());
@@ -287,17 +279,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler {
             log.error("SLUBStoragePlugin.retrieveEntityByRange(), unknown problem, " + e.getMessage());
             throw e;
         }
-        finally
-        {
-            // log.info("SLUBStoragePlugin.retrieveEntityByRange() finally called");
-            if (file != null) {
-                try {
-                    file.close();
-                } catch (Exception e) {
-                    log.warn("SLUBStoragePlugin.retrieveEntitybyRange(), failed closing file, " + e.getMessage());
-                }
-            }
-        }
+
         return bytes;
     }
 
-- 
GitLab