diff --git a/java/org/slub/rosetta/dps/repository/plugin/SLUBTechnicalMetadataExtractorMediaConchPlugin.java b/java/org/slub/rosetta/dps/repository/plugin/SLUBTechnicalMetadataExtractorMediaConchPlugin.java index 25acf002b563dbcd687c524ac9c41627936924e1..14e8d5afc3ce73936f2b701ed620c247efbfc8fe 100644 --- a/java/org/slub/rosetta/dps/repository/plugin/SLUBTechnicalMetadataExtractorMediaConchPlugin.java +++ b/java/org/slub/rosetta/dps/repository/plugin/SLUBTechnicalMetadataExtractorMediaConchPlugin.java @@ -35,6 +35,7 @@ import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -53,11 +54,11 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract private String mediainfo_binary_path; private final static String MEDIAINFO_XSD = "/transformer.xsl"; - private List<String> extractionErrors = new ArrayList<String>(); - private List<String> validationLog = new ArrayList<String>(); + private List<String> extractionErrors = new ArrayList<>(); + private List<String> validationLog = new ArrayList<>(); private boolean isvalid = 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); /** constructor */ @@ -83,115 +84,116 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract @Override public void extract(String filePath) throws Exception { 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"); } 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"); } if (StringUtils.isEmptyString(mediainfo_binary_path)) { - throw new Exception("mediainfo_binary_path not found"); } // mediaconch validation + call_mediaconch(filePath); + // mediainfo metadata extraction + call_mediainfo_and_result_processing(filePath); + } + + private void call_mediainfo_and_result_processing(String filePath) throws Exception { + String execstring = this.mediainfo_binary_path + " -f --Output=XML " + filePath; + System.out.println("executing: " + execstring); + InputStreamReader process_out; try { - String execstring = this.mediaconch_binary_path + " " + filePath + " " + this.mediaconch_profile_path; - System.out.println("executing: " + execstring); Process p = Runtime.getRuntime().exec(execstring); p.waitFor(); - BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); - String line = reader.readLine(); + 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(); + StringBuilder mediainfo_output = new StringBuilder(); + while (line != null) { + /* we should patched out, because not allowed to download xsd */ + /* String regex = " https://mediaarea\\.net/mediainfo/mediainfo.*\\.xsd"; + */ + String regex = "xsi:schemaLocation.*?>"; + String line_patched = line.replaceAll(regex, ""); + mediainfo_output.append(line_patched); + //mediainfo_output.append(line); + line = reader.readLine(); + } - while (line != null) { - System.out.println(line); - validationLog.add(line); - line = reader.readLine(); - } + 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); + temp_media_streamwriter.append(mediainfo_output); + temp_media_streamwriter.close(); + /* xslt transform */ + InputStream stylestream = getClass().getResourceAsStream(MEDIAINFO_XSD); + File tmp = new File( + String.valueOf(getClass().getResource(MEDIAINFO_XSD)) + ); + System.out.println("Resource=" + tmp.getPath()); + StreamSource stylesource = new StreamSource(stylestream); + stylesource.setSystemId("./resources" + MEDIAINFO_XSD); + System.out.println("stylesource, systemID=" + stylesource.getSystemId()); + System.out.println("stylesource, publicID=" + stylesource.getPublicId()); + /* media info xml */ + InputStream mediastream = new FileInputStream(temp_media_outputfile); + StreamSource mediainfo_source = new StreamSource(mediastream); + mediainfo_source.setSystemId(temp_media_outputfile); + System.out.println("mediainfo_source, systemID=" + mediainfo_source.getSystemId()); + System.out.println("mediainfo_source, publicID=" + mediainfo_source.getPublicId()); + // Use a Transformer for output + TransformerFactory tFactory = TransformerFactory.newInstance(); + tFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); + tFactory.setFeature(XMLConstants.USE_CATALOG, false); + Transformer transformer = tFactory.newTransformer(stylesource); + /* ok, mediainfo is loaded correctly, and xslt loaded too */ + /* debug output: */ + StreamResult result = new StreamResult(temp_media_transformed_outputstream); + transformer.transform(mediainfo_source, result); + /* TODO: read debugFile and return attributes */ + attributes.put("key", "value"); + /* */ + temp_media_transformed_outputstream.close(); + 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; + 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()); } - - - - /* 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; - System.out.println("executing: " + execstring); - Process p = Runtime.getRuntime().exec(execstring); - p.waitFor(); - BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); - String line=reader.readLine(); - StringBuilder mediainfo_output= new StringBuilder(); - while (line != null) { - /* we should patched out, because not allowed to download xsd */ -/* String regex = " https://mediaarea\\.net/mediainfo/mediainfo.*\\.xsd"; -*/ - String regex = "xsi:schemaLocation.*?>"; - String line_patched = line.replaceAll( regex, ""); - mediainfo_output.append(line_patched); - //mediainfo_output.append(line); - line = reader.readLine(); - } - File temp_media_output_file = File.createTempFile("mediainfo_", ".xml"); - OutputStream temp_media_outputstream = new FileOutputStream( temp_media_output_file); - OutputStreamWriter temp_media_streamwriter = new OutputStreamWriter( temp_media_outputstream); - temp_media_streamwriter.append( mediainfo_output); - temp_media_streamwriter.close(); - /* xslt transform */ - InputStream stylestream = getClass().getResourceAsStream(MEDIAINFO_XSD); - File tmp = new File( - String.valueOf(getClass().getResource(MEDIAINFO_XSD)) - ); - System.out.println("Resource="+tmp.getPath()); - StreamSource stylesource = new StreamSource( stylestream); - - //stylesource.setPublicId("https://mediaarea.net/mediainfo"); - //stylesource.setSystemId("https://mediaarea.net/mediainfo"); - stylesource.setSystemId( "./resources" + MEDIAINFO_XSD); - System.out.println("stylesource, systemID=" + stylesource.getSystemId()); - System.out.println("stylesource, publicID=" + stylesource.getPublicId()); - - // Use a Transformer for output - InputStream mediastream = new FileInputStream( temp_media_output_file ); - StreamSource mediainfo_source = new StreamSource( mediastream ); - //stylesource.setPublicId("https://mediaarea.net/mediainfo"); - mediainfo_source.setSystemId( temp_media_output_file); - System.out.println("mediainfo_source, systemID=" + mediainfo_source.getSystemId()); - System.out.println("mediainfo_source, publicID=" + mediainfo_source.getPublicId()); - - TransformerFactory tFactory = TransformerFactory.newInstance(); - tFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); - //tFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, ""); - tFactory.setFeature(XMLConstants.USE_CATALOG, false); - Transformer transformer = tFactory.newTransformer(stylesource); - -/* ok, mediainfo is loaded correctly, and xslt loaded too */ - OutputStream debugFile = new FileOutputStream("DEBUG.xml"); - StreamResult result = new StreamResult( debugFile); -// StreamResult result = new StreamResult(System.out); /* FIXME , use StringOutputStream */ - transformer.transform(mediainfo_source, result); - temp_media_output_file.deleteOnExit(); - attributes.put("key", "value"); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - e.printStackTrace(); + 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() @@ -204,30 +206,43 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract * @return string with clamd version and signature version */ public String getAgent() { - StringBuilder response= new StringBuilder(); + StringBuilder response = new StringBuilder(); response.append("mediaconch:\n"); - try { - String[] executables = { - this.mediaconch_binary_path, - this.mediainfo_binary_path - }; - for(String executable: executables){ - String execstring = executable + " --Version"; + String[] executables = { + this.mediaconch_binary_path, + this.mediainfo_binary_path + }; + for (String executable : executables) { + String execstring = executable + " --Version"; + InputStreamReader process_out = null; + try { Process p = Runtime.getRuntime().exec(execstring); p.waitFor(); - BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); - String line = reader.readLine(); + process_out = new InputStreamReader(p.getInputStream()); + } 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) { System.out.println(line); response.append(line); - line = reader.readLine(); + try { + line = reader.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } } } - } 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(); } return response.toString().trim(); } @@ -243,7 +258,7 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract @Override public List<String> getExtractionErrors() { List<String> extractionErrors = this.extractionErrors; - return extractionErrors; + return Collections.unmodifiableList(extractionErrors); } /* following list is build using: @@ -254,7 +269,7 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract @Override public List<String> getSupportedAttributeNames() { //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.BitRate"); available.add("mediainfo.track.Audio.BitRate_Mode"); @@ -398,7 +413,7 @@ public class SLUBTechnicalMetadataExtractorMediaConchPlugin implements MDExtract */ public static void main(String[] args) { 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_profile_path", "/etc/mediaconch/profile.xml"); initp.put( "mediainfo_binary_path", "/usr/bin/mediainfo");