From f158a1c25fd11148c9367913f5f71147829516e1 Mon Sep 17 00:00:00 2001 From: Andreas Romeyke <andreas.romeyke@slub-dresden.de> Date: Mon, 3 Apr 2023 12:49:33 +0200 Subject: [PATCH] - specialized Exceptions - simplified storeEntity() - fixed string warning using backslash as path separator --- .../plugin/storage/nfs/SLUBStoragePlugin.java | 51 ++++++++++--------- 1 file changed, 26 insertions(+), 25 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 889244c..e4666a0 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 @@ -47,6 +47,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.security.NoSuchAlgorithmException; +import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; @@ -148,7 +149,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { * @return true if valid, otherwise false * @throws Exception */ - public boolean checkFixity(List<Fixity> fixities, String storedEntityIdentifier) throws Exception { + public boolean checkFixity(List<Fixity> fixities, String storedEntityIdentifier) throws IOException{ //log.debug("SLUBStoragePlugin.checkFixity() storedEntityIdentifier='" + storedEntityIdentifier + "'"); String absolute_storedEntityIdentifier = getFullFilePath(storedEntityIdentifier); contractAssertIsAbsolutePath(absolute_storedEntityIdentifier); @@ -178,7 +179,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { * @return true if valid otherwise false * @throws Exception */ - private boolean checkSpecificFixity(String absolute_storedEntityIdentifier, Fixity fixity) throws Exception { + private boolean checkSpecificFixity(String absolute_storedEntityIdentifier, Fixity fixity) throws IOException, NoSuchAlgorithmException { contractAssertIsAbsolutePath(absolute_storedEntityIdentifier); String algorithm = fixity.getAlgorithm(); /* HINT: in past versions of Rosetta was a workaround for fixity.getAlgorithm() required, @@ -207,10 +208,11 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { } catch (NoSuchAlgorithmException e) { log.error("SLUBStoragePlugin.checkSpecificFixity(), missed Algorithm, " + e.getMessage()); throw e; // let Rosetta know something broke, creates technical issue in workbench - } catch (Exception e) { + } /*catch (Exception e) { log.error("SLUBStoragePlugin.checkSpecificFixity(), unknown problem, " + e.getMessage()); throw e; // let Rosetta know something broke, creates technical issue in workbench } + */ return false; } @@ -269,7 +271,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { * @return true if valid, otherwise false * @throws Exception to inform rosetta */ - private boolean checkFixityByPlugin(Fixity fixity, String absolute_storedEntityIdentifier) throws Exception { + private boolean checkFixityByPlugin(Fixity fixity, String absolute_storedEntityIdentifier) throws IOException { //log.debug("SLUBStoragePlugin.checkFixityByPlugin() another fixity, root=" + getDirRoot() + " absolute_storedEntityIdentifier=" + absolute_storedEntityIdentifier); contractAssertIsAbsolutePath(absolute_storedEntityIdentifier); String pluginname = fixity.getPluginName(); @@ -430,23 +432,18 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { * @throws Exception */ public String storeEntity(InputStream is, StoredEntityMetaData storedEntityMetadata) - throws Exception + throws IOException { log.info("SLUBStoragePlugin.storeEntity()"); - String existsDescPath = getFilePathInDescIfExists(storedEntityMetadata); + String nullOrAbsoluteDestFilePath = getAbsolutePathInDescIfExists(storedEntityMetadata); //log.debug("SLUBStoragePlugin.storeEntity() existsDescPath='" + existsDescPath + "'"); - String destFilePath = null; - boolean isCopyFileNeeded = true; - if (existsDescPath != null) + if (nullOrAbsoluteDestFilePath != null) { - destFilePath = existsDescPath; - /* destFilePath is relative, checkfixity expects absolute */ - //log.debug("SLUBStoragePlugin.storeEntity() calling checkFixity(storedEntityMetadata.getFixities(), '"+destFilePath+");"); - isCopyFileNeeded = !checkFixity(storedEntityMetadata.getFixities(), destFilePath); + isCopyFileNeeded = !checkFixity(storedEntityMetadata.getFixities(), nullOrAbsoluteDestFilePath); } //log.debug("SLUBStoragePlugin.storeEntity() destFilePath='" + destFilePath + "'"); - Map<String, String> paths = getStoreEntityIdentifier(storedEntityMetadata, destFilePath); + Map<String, String> paths = getStoreEntityIdentifier(storedEntityMetadata, nullOrAbsoluteDestFilePath); String storedEntityIdentifier = paths.get("relativeDirectoryPath"); contractAssertIsRelativePath(storedEntityIdentifier); //log.debug("SLUBStoragePlugin.storeEntity() storedEntityIdentifier='" + storedEntityIdentifier + "'"); @@ -556,14 +553,14 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { throw new IOException(msg); } } - protected String getFilePathInDescIfExists(StoredEntityMetaData storedEntityMetadata) throws IOException { + protected String getAbsolutePathInDescIfExists(StoredEntityMetaData storedEntityMetadata) throws IOException { log.info("SLUBStoragePlugin.getFilePathInDescIfExists()"); if (storedEntityMetadata.getIePid() == null) { return null; } - String existsDescPath = loadAbsoluteDestPathsTmpFile(storedEntityMetadata); + String existsAbsoluteDescPath = loadAbsoluteDestPathsTmpFile(storedEntityMetadata); //log.debug("SLUBStoragePlugin.getFilePathInDescIfExists() existsDescPath='" + existsDescPath + "'"); - return existsDescPath; + return existsAbsoluteDescPath; } @@ -603,7 +600,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { private File getCanonicalFile(String srcPath) { //log.debug("SLUBStoragePlugin.getCanonicalFile() srcPath='"+ srcPath + "'"); - String backslash = "\\"; + String backslash = String.valueOf('\\'); String fileName = srcPath.split(backslash + File.separator)[(srcPath.split(backslash + File.separator).length - 1)]; try { @@ -617,8 +614,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { } /** copied from NFS Storage Plugin, enhanced with debugging info, {@inheritDoc} */ - private Map<String, String> getStoreEntityIdentifier(StoredEntityMetaData storedEntityMetadata, String absoluteDestFilePath) throws Exception - { + private Map<String, String> getStoreEntityIdentifier(StoredEntityMetaData storedEntityMetadata, String absoluteDestFilePath) throws IOException { log.info("SLUBStoragePlugin.getStoreEntityIdentifier() destFilePath='" + absoluteDestFilePath +"'"); Map<String, String> paths = new HashMap<>(); //log.debug("(1) storedEntityMetadata is null?" + (null == storedEntityMetadata)); @@ -667,7 +663,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { * returns the path as string */ - String getStreamRelativePath(StoredEntityMetaData storedEntityMetaData) throws Exception { + String getStreamRelativePath(StoredEntityMetaData storedEntityMetaData) throws IOException { log.info("SLUBStoragePlugin.getStreamRelativePath()"); if (null == storedEntityMetaData) { log.fatal("SLUBStoragePlugin.getStreamRelativePath() with empty storedEntityMetaData called!"); @@ -680,7 +676,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { //log.debug("SLUBStoragePlugin.getStreamRelativePath() getIeDnx fine"); if (null == iedoc) { log.error ("SLUBStoragePlugin.getStreamRelativePath no iedoc found, do you use plugin for others than permanent data (IE and filestream)? You should not! Also do not use it for SIPstoragePermanentGroup!"); - throw new Exception("error, no iedoc found, do you use plugin for others than permanent data (IE and filestream)? You should not! Also do not use it for SIPstoragePermanentGroup!"); + throw new IOException("error, no iedoc found, do you use plugin for others than permanent data (IE and filestream)? You should not! Also do not use it for SIPstoragePermanentGroup!"); } StoredEntityMetaData.EntityType entityType = storedEntityMetaData.getEntityType(); //log.debug("SLUBStoragePlugin.getStreamRelativePath() getEntityType fine"); @@ -689,7 +685,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { DnxSection iesec = iedoc.getSectionById("internalIdentifier"); if (null == iesec) { log.error ("SLUBStoragePlugin.getStreamRelativePath no section in entity of type "+entitytype +" with 'internalIdentfier' in associated iedoc found, do you use plugin for others than permanent data (IE and filestream)? You should not! Also do not use it for SIPstoragePermanentGroup!"); - throw new Exception("error, no section in entity of type "+entitytype +" with 'internalIdentfier' in associated iedoc found, do you use plugin for others than permanent data (IE and filestream)? You should not! Also do not use it for SIPstoragePermanentGroup!"); + throw new IOException("error, no section in entity of type "+entitytype +" with 'internalIdentfier' in associated iedoc found, do you use plugin for others than permanent data (IE and filestream)? You should not! Also do not use it for SIPstoragePermanentGroup!"); } //log.debug("SLUBStoragePlugin.getStreamRelativePath iesec=" + iesec); @@ -700,7 +696,7 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { if (match.isEmpty()) { // raise Exception if IEPID is null log.error ("SLUBStoragePlugin.getStreamRelativePath iesec="+iesec ); - throw new Exception("error, could not get IEPID for storedEntityMetaData:"+storedEntityMetaData +" of type " + entitytype); + throw new IOException("error, could not get IEPID for storedEntityMetaData:"+storedEntityMetaData +" of type " + entitytype); } String iepid = match.get().getKeyById("internalIdentifierValue").getValue(); // found IEPID //log.debug("SLUBStoragePlugin.getStreamRelativePath iepid=" + iepid + " (entitytype=" + entitytype + ")"); @@ -711,7 +707,12 @@ public class SLUBStoragePlugin extends AbstractStorageHandler { SimpleDateFormat sdf; sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); sdf.setLenient(false); /* if parse errors, do not guess about */ - Date d = sdf.parse(datestring); + Date d = null; + try { + d = sdf.parse(datestring); + } catch (ParseException e) { + throw new IOException("Could not parse datestring from creation date=" + datestring + ", which is base for relative IE path, " + e); + } date.setTime(d); //log.debug("SLUBStoragePlugin.getStreamRelativePath creation Date read=" + datestring + " parsed=" + date); // now create path in format /yyyy/MM/dd/IEPID/ -- GitLab