Skip to content
Snippets Groups Projects
Commit 1afc875a authored by Andreas Romeyke's avatar Andreas Romeyke
Browse files

- refactoring, extracted setValid()

- refactoring, extracted extractErrorsFromReader()
- simplified handling external command calls
parent d6334bde
No related branches found
No related tags found
No related merge requests found
Pipeline #8993 passed
...@@ -48,7 +48,6 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP ...@@ -48,7 +48,6 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP
private boolean iswellformed = false; private boolean iswellformed = false;
private String md5CurrentProfile; private String md5CurrentProfile;
private String md5UpcomingProfile; private String md5UpcomingProfile;
//static final ExLogger log = ExLogger.getExLogger(SLUBTechnicalMetadataExtractorMediaConchPlugin.class, ExLogger.VALIDATIONSTACK);
private boolean isDifferentProfile = true; private boolean isDifferentProfile = true;
/** constructor */ /** constructor */
...@@ -76,8 +75,7 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP ...@@ -76,8 +75,7 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP
+ " mediaconch_upcoming_profile_path=" + mediaconch_upcoming_profile_path + " mediaconch_upcoming_profile_path=" + mediaconch_upcoming_profile_path
+ (isDifferentProfile ? "with different profiles" : "with same profile") + (isDifferentProfile ? "with different profiles" : "with same profile")
); );
} catch (Exception e) { } catch (Exception ignored) {
e.printStackTrace();
} }
log.info( "SLUBMatroskaFFV1FormatValidationPlugin instantiated with" log.info( "SLUBMatroskaFFV1FormatValidationPlugin instantiated with"
+ " mediaconch binary='" + mediaconch_binary_path + " mediaconch binary='" + mediaconch_binary_path
...@@ -107,46 +105,62 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP ...@@ -107,46 +105,62 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP
private void callMediaconch(String filePath, String profilePath) { private void callMediaconch(String filePath, String profilePath) {
String execstring = this.mediaconch_binary_path + " " + filePath + " -p " + profilePath; String execstring = this.mediaconch_binary_path + " " + filePath + " -p " + profilePath;
log.info("executing: " + execstring); log.info("executing: " + execstring);
InputStreamReader process_out; System.out.println("executing: " + execstring);
try { try {
Process p = Runtime.getRuntime().exec(execstring); Process p = Runtime.getRuntime().exec(execstring);
p.waitFor(); p.waitFor();
process_out = new InputStreamReader(p.getInputStream()); try (InputStreamReader process_out = new InputStreamReader(p.getInputStream())) {
if (p.exitValue() == 0) { // something wrong
isvalid = true; setValid(p.exitValue() == 0);
iswellformed = true; try (BufferedReader reader = new BufferedReader(process_out)) {
} else { // something wrong
isvalid = false;
iswellformed = false;
}
BufferedReader reader = new BufferedReader(process_out);
String line = reader.readLine(); String line = reader.readLine();
while (line != null) { if (line == null) {
if (line.contains("pass!")) { log.error("(actual) mediaconch has no result, path=" + this.mediaconch_binary_path);
break; 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) { } catch (IOException e) {
log.error("(actual) mediaconch not available, path=" + this.mediaconch_binary_path + ", " , e.getMessage()); 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()); System.out.println("ERROR: (actual) mediaconch not available, path=" + this.mediaconch_binary_path + ", " + e.getMessage());
} catch (InterruptedException e) { } catch (InterruptedException e) {
Thread.currentThread().interrupt();
log.error("ERROR: call of mediaconch interrupted, path=" + this.mediaconch_binary_path + ", " + e.getMessage()); 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 @Override
public boolean validateFormat(String filePath) { public boolean validateFormat(String filePath) {
setValid(false);
// mediaconch validation, first using upcoming profile, if invalid then retry with current profile // mediaconch validation, first using upcoming profile, if invalid then retry with current profile
callMediaconch(filePath, this.mediaconch_upcoming_profile_path); callMediaconch(filePath, this.mediaconch_upcoming_profile_path);
if (this.isDifferentProfile && !isvalid) { if (this.isDifferentProfile && !isvalid) {
callMediaconch(filePath, this.mediaconch_current_profile_path); callMediaconch(filePath, this.mediaconch_current_profile_path);
} }
return isvalid; return isvalid;
} }
...@@ -154,40 +168,27 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP ...@@ -154,40 +168,27 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP
* *
* @return string with version and signature version * @return string with version and signature version
*/ */
@Override
public final String getAgent() { public final String getAgent() {
StringBuilder response = new StringBuilder(); StringBuilder response = new StringBuilder();
response.append("mediaconch:\n"); response.append("mediaconch:\n");
InputStreamReader process_out = null;
try { try {
String execstring = this.mediaconch_binary_path + " --Version"; String execstring = this.mediaconch_binary_path + " --Version";
checkFileExists(this.mediaconch_binary_path); checkFileExists(this.mediaconch_binary_path);
Process p = Runtime.getRuntime().exec(execstring); Process p = Runtime.getRuntime().exec(execstring);
p.waitFor(); p.waitFor();
process_out = new InputStreamReader(p.getInputStream()); InputStreamReader process_out = new InputStreamReader(p.getInputStream());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
if (process_out != null) {
BufferedReader reader = new BufferedReader(process_out); BufferedReader reader = new BufferedReader(process_out);
String line; String line;
try {
line = reader.readLine(); line = reader.readLine();
while (line != null) { while (line != null) {
System.out.println(line); System.out.println(line);
response.append(line); response.append(line);
try {
line = reader.readLine(); 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(); return response.toString().trim();
} }
...@@ -199,10 +200,10 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP ...@@ -199,10 +200,10 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP
@Override @Override
public final List<String> getErrors() { public final List<String> getErrors() {
List<String> truncated = new ArrayList<String>(); List<String> truncated = new ArrayList<>();
int chars=0; int chars=0;
/* workaround for Rosetta Issue https://support.proquest.com/500Do0000083LJkIAM /* 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) { for (String e: validationLog) {
if ( (chars+ e.length()) < 1900 ) { if ( (chars+ e.length()) < 1900 ) {
...@@ -237,8 +238,7 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP ...@@ -237,8 +238,7 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP
byte[] b = Files.readAllBytes(Paths.get(filename)); byte[] b = Files.readAllBytes(Paths.get(filename));
byte[] digest = md.digest(b); byte[] digest = md.digest(b);
return new BigInteger(1, digest).toString(16); return new BigInteger(1, digest).toString(16);
} catch (Exception e) { } catch (Exception ignored) {
e.printStackTrace();
} }
return ""; return "";
} }
...@@ -246,8 +246,7 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP ...@@ -246,8 +246,7 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP
private String modificationDateOfFile(String filename ) { private String modificationDateOfFile(String filename ) {
try { try {
return Files.getLastModifiedTime(Paths.get(filename)).toString(); return Files.getLastModifiedTime(Paths.get(filename)).toString();
} catch (IOException e) { } catch (IOException ignored) {
e.printStackTrace();
} }
return ""; return "";
} }
...@@ -266,7 +265,9 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP ...@@ -266,7 +265,9 @@ public class SLUBMatroskaFFV1FormatValidationPlugin implements FormatValidationP
System.out.println("Agent: '" + plugin.getAgent() + "'"); System.out.println("Agent: '" + plugin.getAgent() + "'");
System.out.println(); System.out.println();
for (String file : args) { for (String file : args) {
plugin.validateFormat( file );
System.out.println("Validation RESULT: " + plugin.isValid()); System.out.println("Validation RESULT: " + plugin.isValid());
System.out.println("Errors:" + plugin.validationLog);
} }
System.out.println("----------------------------------"); System.out.println("----------------------------------");
System.out.println("getAgent:"); System.out.println("getAgent:");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment