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

- refactoring, extracted methods

parent 151639a1
No related branches found
No related tags found
No related merge requests found
...@@ -35,6 +35,7 @@ import java.io.InputStreamReader; ...@@ -35,6 +35,7 @@ import java.io.InputStreamReader;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -53,11 +54,11 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract ...@@ -53,11 +54,11 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract
private String mediainfo_binary_path; private String mediainfo_binary_path;
private final static String MEDIAINFO_XSD = "/transformer.xsl"; private final static String MEDIAINFO_XSD = "/transformer.xsl";
private List<String> extractionErrors = new ArrayList<String>(); private List<String> extractionErrors = new ArrayList<>();
private List<String> validationLog = new ArrayList<String>(); private List<String> validationLog = new ArrayList<>();
private boolean isvalid = false; private boolean isvalid = false;
private boolean iswellformed = false; private boolean iswellformed = false;
private Map<String,String> attributes = new HashMap<String, String>(); private Map<String,String> attributes = new HashMap<>();
//static final ExLogger log = ExLogger.getExLogger(SLUBTechnicalMetadataExtractorMediaConchPlugin.class, ExLogger.VALIDATIONSTACK); //static final ExLogger log = ExLogger.getExLogger(SLUBTechnicalMetadataExtractorMediaConchPlugin.class, ExLogger.VALIDATIONSTACK);
/** constructor */ /** constructor */
...@@ -83,59 +84,34 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract ...@@ -83,59 +84,34 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract
@Override @Override
public void extract(String filePath) throws Exception { public void extract(String filePath) throws Exception {
if (StringUtils.isEmptyString(mediaconch_binary_path)) { if (StringUtils.isEmptyString(mediaconch_binary_path)) {
//log.error("No mediaconch_binary_path defined. Please set the plugin parameter to hold your mediaconch_binary_path.");
throw new Exception("mediaconch_binary_path not found"); throw new Exception("mediaconch_binary_path not found");
} }
if (StringUtils.isEmptyString(mediaconch_profile_path)) { if (StringUtils.isEmptyString(mediaconch_profile_path)) {
//log.error("No mediaconch_config_path defined. Please set the plugin parameter to hold your mediaconch_config_path.");
throw new Exception("mediaconch_profile_path not found"); throw new Exception("mediaconch_profile_path not found");
} }
if (StringUtils.isEmptyString(mediainfo_binary_path)) { if (StringUtils.isEmptyString(mediainfo_binary_path)) {
throw new Exception("mediainfo_binary_path not found"); throw new Exception("mediainfo_binary_path not found");
} }
// mediaconch validation // mediaconch validation
try { call_mediaconch(filePath);
String execstring = this.mediaconch_binary_path + " " + filePath + " " + this.mediaconch_profile_path; // mediainfo metadata extraction
System.out.println("executing: " + execstring); call_mediainfo_and_result_processing(filePath);
Process p = Runtime.getRuntime().exec(execstring);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = reader.readLine();
while (line != null) {
System.out.println(line);
validationLog.add(line);
line = reader.readLine();
}
if (p.exitValue() == 0) {
isvalid = true;
iswellformed=true;
} else { // something wrong
isvalid = false;
iswellformed = false;
extractionErrors = validationLog;
}
} catch (IOException e) {
//log.error("exception creation socket, clamd not available at host=" + host + "port=" + port, e);
System.out.println("ERROR: (actual) mediaconch not available, path=" + this.mediaconch_binary_path + ", " + e.getMessage());
throw new Exception("ERROR: (actual) mediaconch not available, path=" + this.mediaconch_binary_path + ", " + e.getMessage());
} }
private void call_mediainfo_and_result_processing(String filePath) throws Exception {
/* ffprobe output of metadata
supports different outputs. we are using the flat-model, see WRITERS section in ffprobe manual
the streams will be mapped as: streams.stream.0.$property
the separator is "="
*/
try {
String execstring = this.mediainfo_binary_path + " -f --Output=XML " + filePath; String execstring = this.mediainfo_binary_path + " -f --Output=XML " + filePath;
System.out.println("executing: " + execstring); System.out.println("executing: " + execstring);
InputStreamReader process_out;
try {
Process p = Runtime.getRuntime().exec(execstring); Process p = Runtime.getRuntime().exec(execstring);
p.waitFor(); p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); process_out = new InputStreamReader(p.getInputStream());
} catch (IOException e) {
//log.error("exception creation socket, clamd not available at host=" + host + "port=" + port, e);
System.out.println("ERROR: (actual) mediainfo not available, path=" + this.mediainfo_binary_path + ", " + e.getMessage());
throw new Exception("ERROR: (actual) mediaconch not available, path=" + this.mediainfo_binary_path + ", " + e.getMessage());
}
BufferedReader reader = new BufferedReader(process_out);
String line = reader.readLine(); String line = reader.readLine();
StringBuilder mediainfo_output = new StringBuilder(); StringBuilder mediainfo_output = new StringBuilder();
while (line != null) { while (line != null) {
...@@ -148,8 +124,13 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract ...@@ -148,8 +124,13 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract
//mediainfo_output.append(line); //mediainfo_output.append(line);
line = reader.readLine(); line = reader.readLine();
} }
File temp_media_output_file = File.createTempFile("mediainfo_", ".xml");
OutputStream temp_media_outputstream = new FileOutputStream( temp_media_output_file); File temp_media_outputfile = File.createTempFile("mediainfo_outp", ".xml");
temp_media_outputfile.deleteOnExit();
File temp_media_transformed_outputfile = File.createTempFile("mediainfo_transf_", ".xml");
temp_media_transformed_outputfile.deleteOnExit();
OutputStream temp_media_transformed_outputstream = new FileOutputStream(temp_media_transformed_outputfile);
OutputStream temp_media_outputstream = new FileOutputStream(temp_media_outputfile);
OutputStreamWriter temp_media_streamwriter = new OutputStreamWriter(temp_media_outputstream); OutputStreamWriter temp_media_streamwriter = new OutputStreamWriter(temp_media_outputstream);
temp_media_streamwriter.append(mediainfo_output); temp_media_streamwriter.append(mediainfo_output);
temp_media_streamwriter.close(); temp_media_streamwriter.close();
...@@ -160,38 +141,59 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract ...@@ -160,38 +141,59 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract
); );
System.out.println("Resource=" + tmp.getPath()); System.out.println("Resource=" + tmp.getPath());
StreamSource stylesource = new StreamSource(stylestream); StreamSource stylesource = new StreamSource(stylestream);
//stylesource.setPublicId("https://mediaarea.net/mediainfo");
//stylesource.setSystemId("https://mediaarea.net/mediainfo");
stylesource.setSystemId("./resources" + MEDIAINFO_XSD); stylesource.setSystemId("./resources" + MEDIAINFO_XSD);
System.out.println("stylesource, systemID=" + stylesource.getSystemId()); System.out.println("stylesource, systemID=" + stylesource.getSystemId());
System.out.println("stylesource, publicID=" + stylesource.getPublicId()); System.out.println("stylesource, publicID=" + stylesource.getPublicId());
/* media info xml */
// Use a Transformer for output InputStream mediastream = new FileInputStream(temp_media_outputfile);
InputStream mediastream = new FileInputStream( temp_media_output_file );
StreamSource mediainfo_source = new StreamSource(mediastream); StreamSource mediainfo_source = new StreamSource(mediastream);
//stylesource.setPublicId("https://mediaarea.net/mediainfo"); mediainfo_source.setSystemId(temp_media_outputfile);
mediainfo_source.setSystemId( temp_media_output_file);
System.out.println("mediainfo_source, systemID=" + mediainfo_source.getSystemId()); System.out.println("mediainfo_source, systemID=" + mediainfo_source.getSystemId());
System.out.println("mediainfo_source, publicID=" + mediainfo_source.getPublicId()); System.out.println("mediainfo_source, publicID=" + mediainfo_source.getPublicId());
// Use a Transformer for output
TransformerFactory tFactory = TransformerFactory.newInstance(); TransformerFactory tFactory = TransformerFactory.newInstance();
tFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); tFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
//tFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
tFactory.setFeature(XMLConstants.USE_CATALOG, false); tFactory.setFeature(XMLConstants.USE_CATALOG, false);
Transformer transformer = tFactory.newTransformer(stylesource); Transformer transformer = tFactory.newTransformer(stylesource);
/* ok, mediainfo is loaded correctly, and xslt loaded too */ /* ok, mediainfo is loaded correctly, and xslt loaded too */
OutputStream debugFile = new FileOutputStream("DEBUG.xml"); /* debug output: */
StreamResult result = new StreamResult( debugFile); StreamResult result = new StreamResult(temp_media_transformed_outputstream);
// StreamResult result = new StreamResult(System.out); /* FIXME , use StringOutputStream */
transformer.transform(mediainfo_source, result); transformer.transform(mediainfo_source, result);
temp_media_output_file.deleteOnExit(); /* TODO: read debugFile and return attributes */
attributes.put("key", "value"); attributes.put("key", "value");
} catch (InterruptedException e) { /* */
Thread.currentThread().interrupt(); temp_media_transformed_outputstream.close();
e.printStackTrace(); mediastream.close();
}
private void call_mediaconch(String filePath) throws Exception {
String execstring = this.mediaconch_binary_path + " " + filePath + " " + this.mediaconch_profile_path;
System.out.println("executing: " + execstring);
InputStreamReader process_out;
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;
} }
} catch (IOException e) {
//log.error("exception creation socket, clamd not available at host=" + host + "port=" + port, e);
System.out.println("ERROR: (actual) mediaconch not available, path=" + this.mediaconch_binary_path + ", " + e.getMessage());
throw new Exception("ERROR: (actual) mediaconch not available, path=" + this.mediaconch_binary_path + ", " + e.getMessage());
}
BufferedReader reader = new BufferedReader(process_out);
String line = reader.readLine();
while (line != null) {
System.out.println(line);
validationLog.add(line);
line = reader.readLine();
}
extractionErrors = validationLog;
} }
public String getAgentName() public String getAgentName()
...@@ -206,29 +208,42 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract ...@@ -206,29 +208,42 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract
public String getAgent() { public String getAgent() {
StringBuilder response = new StringBuilder(); StringBuilder response = new StringBuilder();
response.append("mediaconch:\n"); response.append("mediaconch:\n");
try {
String[] executables = { String[] executables = {
this.mediaconch_binary_path, this.mediaconch_binary_path,
this.mediainfo_binary_path this.mediainfo_binary_path
}; };
for (String executable : executables) { for (String executable : executables) {
String execstring = executable + " --Version"; String execstring = executable + " --Version";
InputStreamReader process_out = null;
try {
Process p = Runtime.getRuntime().exec(execstring); Process p = Runtime.getRuntime().exec(execstring);
p.waitFor(); p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); process_out = new InputStreamReader(p.getInputStream());
String line = reader.readLine(); } catch (IOException e) {
//log.error("exception creation socket, clamd not available at host=" + host + "port=" + port, e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
e.printStackTrace();
}
if (process_out != null) {
BufferedReader reader = new BufferedReader(process_out);
String line = null;
try {
line = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
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) { } catch (IOException e) {
//log.error("exception creation socket, clamd not available at host=" + host + "port=" + port, e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
e.printStackTrace(); e.printStackTrace();
} }
}
}
}
return response.toString().trim(); return response.toString().trim();
} }
...@@ -243,7 +258,7 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract ...@@ -243,7 +258,7 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract
@Override @Override
public List<String> getExtractionErrors() { public List<String> getExtractionErrors() {
List<String> extractionErrors = this.extractionErrors; List<String> extractionErrors = this.extractionErrors;
return extractionErrors; return Collections.unmodifiableList(extractionErrors);
} }
/* following list is build using: /* following list is build using:
...@@ -254,7 +269,7 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract ...@@ -254,7 +269,7 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract
@Override @Override
public List<String> getSupportedAttributeNames() { public List<String> getSupportedAttributeNames() {
//return new ArrayList<String>(attributes.keySet()); //return new ArrayList<String>(attributes.keySet());
List<String> available = new ArrayList<String>(); List<String> available = new ArrayList<>();
available.add("mediainfo.track.Audio.BitDepth"); available.add("mediainfo.track.Audio.BitDepth");
available.add("mediainfo.track.Audio.BitRate"); available.add("mediainfo.track.Audio.BitRate");
available.add("mediainfo.track.Audio.BitRate_Mode"); available.add("mediainfo.track.Audio.BitRate_Mode");
...@@ -398,7 +413,7 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract ...@@ -398,7 +413,7 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract
*/ */
public static void main(String[] args) { public static void main(String[] args) {
SLUBTechnicalMetadataExtractorMediaConchPlugin plugin = new SLUBTechnicalMetadataExtractorMediaConchPlugin(); SLUBTechnicalMetadataExtractorMediaConchPlugin plugin = new SLUBTechnicalMetadataExtractorMediaConchPlugin();
Map<String, String> initp = new HashMap<String, String>(); Map<String, String> initp = new HashMap<>();
initp.put( "mediaconch_binary_path", "/usr/bin/mediaconch"); initp.put( "mediaconch_binary_path", "/usr/bin/mediaconch");
initp.put( "mediaconch_profile_path", "/etc/mediaconch/profile.xml"); initp.put( "mediaconch_profile_path", "/etc/mediaconch/profile.xml");
initp.put( "mediainfo_binary_path", "/usr/bin/mediainfo"); initp.put( "mediainfo_binary_path", "/usr/bin/mediainfo");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment