From f7e97e93db977b86b72d74e2d7f80ddb2e9936b4 Mon Sep 17 00:00:00 2001
From: Andreas Romeyke <art1@andreas-romeyke.de>
Date: Wed, 24 Aug 2022 16:09:40 +0200
Subject: [PATCH] - fixed condition in no fixity check - simplified setResult()
 call - fixed javadoc - fixed missed is.close() - removed superflous try-catch
 around simple getters - added try-with block in retrieveEntityByRange() -
 added try-with block in  SLUBStoragePlugin - replaced LinkedList args with
 ArrayList

---
 .../plugin/storage/nfs/SLUBStoragePlugin.java | 82 +++++++------------
 1 file changed, 29 insertions(+), 53 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 39e2903..8894ccb 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
@@ -86,13 +86,13 @@ public class SLUBStoragePlugin extends AbstractStorageHandler {
     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) {
+        if (fixities == null) {
             log.warn("No fixity list provided");
             return true;
         }
         boolean result = true;
         for (Fixity fixity : fixities) {
-            fixity.setResult(Boolean.FALSE);
+            fixity.setResult(false);
             result &= checkSpecificFixity(storedEntityIdentifier, isRelativePath, fixity);
         }
         return result;
@@ -138,11 +138,12 @@ 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
+     * @param storedEntityIdentifier ROsetta identifier
      * @param isRelativePath RelativePath
      * @param algorithm which algorithm should be used
      * @return result
-     * @throws NoSuchAlgorithmException
+     * @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 {
         String oldValue = fixity.getValue();
@@ -155,6 +156,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler {
         fixity.setResult((oldValue == null) || (oldValue.equalsIgnoreCase(fixity.getValue())));
         boolean result = fixity.getResult();
         log.info("SLUBStoragePlugin.checkFixity() result=" + result);
+        is.close();
         return result;
     }
 
@@ -164,44 +166,27 @@ public class SLUBStoragePlugin extends AbstractStorageHandler {
      * @param storedEntityIdentifier path to file which should be checked
      * @param isRelativePath indicates if path is relative
      * @return result
-     * @throws Exception
+     * @throws Exception to inform rosetta
      */
     private boolean checkFixityByPlugin(Fixity fixity, String storedEntityIdentifier, boolean isRelativePath) throws Exception {
         log.info("SLUBStoragePlugin.checkFixityByPlugin() another fixity");
-        String pluginname = "";
-        try {
-            pluginname = fixity.getPluginName();
-            log.info("SLUBStoragePlugin.checkFixityByPlugin() pluginname=" + pluginname);
-        }
-        catch (Exception e) {
-            log.warn("SLUBStoragePlugin.checkFixityByPlugin() hard error getting plugin name, " + e.getMessage());
-            pluginname = "";
-        }
-        finally {
-            // HINT: Workaround for Rosetta case 06472860 - Storage Plugin: Empty custom fixity plugin name breaks file updates [Rosetta 7.3.0.0]
-            //       -> use fallback names in case fixity plugin name is missing
-            if (pluginname == null || pluginname.length() == 0) {
-                log.warn("SLUBStoragePlugin failed to get pluginname, because it is empty. Possibly, there is no valid fixity type used or a CustomFixityPlugin missed");
-                log.warn("SLUBStoragePlugin.checkFixityByPlugin() trying to use fallback table to determine plugin name");
-                String algorithm = fixity.getAlgorithm();
-                if (algorithm.equals("SHA512")) {
-                    pluginname = "CustomFixitySHA512Plugin";
-                    log.info("SLUBStoragePlugin.checkFixityByPlugin() pluginname=" + pluginname);
-                } else {
-                    log.error("SLUBStoragePlugin.checkFixityByPlugin() fallback table has no plugin name entry for algorithm '" + algorithm + "'");
-                }
+        String pluginname = fixity.getPluginName();
+        log.info("SLUBStoragePlugin.checkFixityByPlugin() pluginname=" + pluginname);
+        // HINT: Workaround for Rosetta case 06472860 - Storage Plugin: Empty custom fixity plugin name breaks file updates [Rosetta 7.3.0.0]
+        //       -> use fallback names in case fixity plugin name is missing
+        if (pluginname == null || pluginname.length() == 0) {
+            log.warn("SLUBStoragePlugin failed to get pluginname, because it is empty. Possibly, there is no valid fixity type used or a CustomFixityPlugin missed");
+            log.warn("SLUBStoragePlugin.checkFixityByPlugin() trying to use fallback table to determine plugin name");
+            String algorithm = fixity.getAlgorithm();
+            if (algorithm.equals("SHA512")) {
+                pluginname = "CustomFixitySHA512Plugin";
+                log.info("SLUBStoragePlugin.checkFixityByPlugin() pluginname=" + pluginname);
+            } else {
+                log.error("SLUBStoragePlugin.checkFixityByPlugin() fallback table has no plugin name entry for algorithm '" + algorithm + "'");
             }
         }
-
-        String oldValue = "";
-        try {
-            oldValue = fixity.getValue();
-            log.info("SLUBStoragePlugin.checkFixityByPlugin() oldvalue=" + oldValue);
-        }
-        catch (Exception e) {
-            log.error("SLUBStoragePlugin.checkFixityByPlugin() hard error getting previous fixity value, ", e.getMessage());
-            throw e;  // let Rosetta know something broke, creates technical issue in workbench
-        }
+        String oldValue = fixity.getValue();
+        log.info("SLUBStoragePlugin.checkFixityByPlugin() oldvalue=" + oldValue);
         fixity.setValue(getChecksumUsingPlugin(isRelativePath ? getLocalFilePath(storedEntityIdentifier) : 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)
@@ -257,14 +242,13 @@ 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 = null;
-        try
+        /* try-with-ressource block, no close if failed needed */
+        try (RandomAccessFile 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)];
+            byte[] bytes = new byte[(int)(end - start + 1L)];
             file.readFully(bytes, 0, (int)(end - start + 1L));
-            file.close();
+            return bytes;
         }
         catch (FileNotFoundException e) {
             log.error("SLUBStoragePlugin.retrieveEntitybyRange(), file not found, " + e.getMessage());
@@ -278,8 +262,6 @@ public class SLUBStoragePlugin extends AbstractStorageHandler {
             log.error("SLUBStoragePlugin.retrieveEntityByRange(), unknown problem, " + e.getMessage());
             throw e;
         }
-
-        return bytes;
     }
 
     public String storeEntity(InputStream is, StoredEntityMetaData storedEntityMetadata)
@@ -321,17 +303,11 @@ public class SLUBStoragePlugin extends AbstractStorageHandler {
                     log.warn("SLUBStoragePlugin.storeEntity() InputStream is null");
                     return null;
                 }
-                OutputStream output = null;
-                try
+                try (OutputStream output = java.nio.file.Files.newOutputStream( Paths.get( destFilePath)))
                 {
-                    output = java.nio.file.Files.newOutputStream( Paths.get( destFilePath));
                     IOUtil.copy(is, output);
                     log.debug("SLUBStoragePlugin.storeEntity() try copy was successfull");
                 }
-                finally
-                {
-                    IOUtil.closeQuietly(output);
-                }
             }
             if (!checkFixity(storedEntityMetadata.getFixities(), storedEntityIdentifier)) {
                 log.info("SLUBStoragePlugin.storeEntity() checkFixity failed");
@@ -512,7 +488,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler {
         }
         // raise Exception if IEPID is null
         if (null == iepid) {
-            log.error ("SLUBStoragePlugin.getStreamRelativePath iesec="+iesec() );
+            log.error ("SLUBStoragePlugin.getStreamRelativePath iesec="+iesec );
             throw new Exception("error, could not get IEPID for storedEntityMetaData:"+storedEntityMetaData +" of type " + entitytype);
         }
         log.debug("SLUBStoragePlugin.getStreamRelativePath iepid=" + iepid + " (entitytype=" + entitytype + ")");
@@ -546,7 +522,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler {
         log.info("SLUBStoragePlugin.hardLink srcPath='" + srcPath + "' destPath='" + destPath + "'");
         String command = "ln";
         ExecExternalProcess proc = new ExecExternalProcess();
-        List<String> args = new LinkedList<>();
+        List<String> args = new ArrayList<>();
         args.add(srcPath);
         args.add(destPath);
         int retValue = proc.execExternalProcess(command, args);
-- 
GitLab