diff --git a/java/org/slub/rosetta/dps/repository/plugin/SLUBMatroskaFFV1FormatValidationPlugin.java b/java/org/slub/rosetta/dps/repository/plugin/SLUBMatroskaFFV1FormatValidationPlugin.java index 41ef3f928339bd9bdccee608387e294378af5cb5..52ab56fd4a633d94b6a5f7558943f3333740717f 100644 --- a/java/org/slub/rosetta/dps/repository/plugin/SLUBMatroskaFFV1FormatValidationPlugin.java +++ b/java/org/slub/rosetta/dps/repository/plugin/SLUBMatroskaFFV1FormatValidationPlugin.java @@ -48,7 +48,6 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP private boolean iswellformed = false; private String md5CurrentProfile; private String md5UpcomingProfile; - //static final ExLogger log = ExLogger.getExLogger(SLUBTechnicalMetadataExtractorMediaConchPlugin.class, ExLogger.VALIDATIONSTACK); private boolean isDifferentProfile = true; /** constructor */ @@ -76,8 +75,7 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP + " mediaconch_upcoming_profile_path=" + mediaconch_upcoming_profile_path + (isDifferentProfile ? "with different profiles" : "with same profile") ); - } catch (Exception e) { - e.printStackTrace(); + } catch (Exception ignored) { } log.info( "SLUBMatroskaFFV1FormatValidationPlugin instantiated with" + " mediaconch binary='" + mediaconch_binary_path @@ -107,46 +105,62 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP private void callMediaconch(String filePath, String profilePath) { String execstring = this.mediaconch_binary_path + " " + filePath + " -p " + profilePath; log.info("executing: " + execstring); - InputStreamReader process_out; + System.out.println("executing: " + execstring); try { Process p = Runtime.getRuntime().exec(execstring); p.waitFor(); - process_out = new InputStreamReader(p.getInputStream()); - if (p.exitValue() == 0) { - isvalid = true; - iswellformed = true; - } else { // something wrong - isvalid = false; - iswellformed = false; - } - BufferedReader reader = new BufferedReader(process_out); - String line = reader.readLine(); - while (line != null) { - if (line.contains("pass!")) { - break; + try (InputStreamReader process_out = new InputStreamReader(p.getInputStream())) { + // something wrong + setValid(p.exitValue() == 0); + try (BufferedReader reader = new BufferedReader(process_out)) { + String line = reader.readLine(); + if (line == null) { + log.error("(actual) mediaconch has no result, path=" + this.mediaconch_binary_path); + System.out.println("ERROR: (actual) mediaconch has no result, path=" + this.mediaconch_binary_path); + } else { + if (line.startsWith("pass!")) { + setValid(true); + } else { + if (!isvalid || line.startsWith("fail!")) { + setValid(false); + extractErrorsFromReader(reader); + } + } + } } - System.out.println("MEDIACONCH line: " + line); - validationLog.add(line); - line = reader.readLine(); - log.info( line ); } - reader.close(); } catch (IOException e) { log.error("(actual) mediaconch not available, path=" + this.mediaconch_binary_path + ", " , e.getMessage()); System.out.println("ERROR: (actual) mediaconch not available, path=" + this.mediaconch_binary_path + ", " + e.getMessage()); } catch (InterruptedException e) { + Thread.currentThread().interrupt(); log.error("ERROR: call of mediaconch interrupted, path=" + this.mediaconch_binary_path + ", " + e.getMessage()); } } + private void setValid(boolean value) { + isvalid = value; + iswellformed = value; + } + + private void extractErrorsFromReader(BufferedReader reader) throws IOException { + while (true) { + String line = reader.readLine(); + if (line == null) break; + System.out.println("MEDIACONCH line: " + line); + validationLog.add(line); + log.info(line); + } + } + @Override public boolean validateFormat(String filePath) { + setValid(false); // mediaconch validation, first using upcoming profile, if invalid then retry with current profile callMediaconch(filePath, this.mediaconch_upcoming_profile_path); if (this.isDifferentProfile && !isvalid) { callMediaconch(filePath, this.mediaconch_current_profile_path); } - return isvalid; } @@ -154,40 +168,27 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP * * @return string with version and signature version */ + @Override public final String getAgent() { StringBuilder response = new StringBuilder(); response.append("mediaconch:\n"); - InputStreamReader process_out = null; try { String execstring = this.mediaconch_binary_path + " --Version"; checkFileExists(this.mediaconch_binary_path); Process p = Runtime.getRuntime().exec(execstring); p.waitFor(); - process_out = new InputStreamReader(p.getInputStream()); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - e.printStackTrace(); - } catch (Exception e) { - e.printStackTrace(); - } - if (process_out != null) { + InputStreamReader process_out = new InputStreamReader(p.getInputStream()); BufferedReader reader = new BufferedReader(process_out); String line; - try { + line = reader.readLine(); + while (line != null) { + System.out.println(line); + response.append(line); line = reader.readLine(); - while (line != null) { - System.out.println(line); - response.append(line); - try { - line = reader.readLine(); - } catch (IOException e) { - e.printStackTrace(); - } - } - reader.close(); - } catch (IOException e) { - e.printStackTrace(); } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } catch (Exception ignored) { } return response.toString().trim(); } @@ -199,10 +200,10 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP @Override public final List<String> getErrors() { - List<String> truncated = new ArrayList<String>(); + List<String> truncated = new ArrayList<>(); int chars=0; /* workaround for Rosetta Issue https://support.proquest.com/500Do0000083LJkIAM - * truncate to ensure not more than 2000 chars used, to + * truncate to ensure not more than 2000 chars used */ for (String e: validationLog) { if ( (chars+ e.length()) < 1900 ) { @@ -237,8 +238,7 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP byte[] b = Files.readAllBytes(Paths.get(filename)); byte[] digest = md.digest(b); return new BigInteger(1, digest).toString(16); - } catch (Exception e) { - e.printStackTrace(); + } catch (Exception ignored) { } return ""; } @@ -246,8 +246,7 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP private String modificationDateOfFile(String filename ) { try { return Files.getLastModifiedTime(Paths.get(filename)).toString(); - } catch (IOException e) { - e.printStackTrace(); + } catch (IOException ignored) { } return ""; } @@ -266,7 +265,9 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP System.out.println("Agent: '" + plugin.getAgent() + "'"); System.out.println(); for (String file : args) { + plugin.validateFormat( file ); System.out.println("Validation RESULT: " + plugin.isValid()); + System.out.println("Errors:" + plugin.validationLog); } System.out.println("----------------------------------"); System.out.println("getAgent:");