diff --git a/CHANGELOG.md b/CHANGELOG.md index 195f775..d191a63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm # Changelog for org.gcube.data.transfer.sis-geotk-plugin -## [v1.2.0] 2020-09-07 +## [v1.2.0-SNAPSHOT] 2020-09-07 Upgrade to apache-sis 1.0 ## [v1.1.3] 2020-09-07 diff --git a/log.txt b/log.txt new file mode 100644 index 0000000..e69de29 diff --git a/pom.xml b/pom.xml index de72653..874ab95 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ org.gcube.data.transfer sis-geotk-plugin - 1.2.0 + 1.2.0-SNAPSHOT Sis/GeoToolkit plugin Apache Sis/Geotk plugin for data-transfer-service @@ -132,6 +132,20 @@ + + + + com.sun.xml.bind + jaxb-impl + 2.2.11 + + + com.sun.xml.bind + jaxb-core + 2.2.11 + + + diff --git a/src/main/java/org/gcube/data/transfer/plugins/thredds/ISOMetadataMarshalling.java b/src/main/java/org/gcube/data/transfer/plugins/thredds/ISOMetadataMarshalling.java new file mode 100644 index 0000000..614549a --- /dev/null +++ b/src/main/java/org/gcube/data/transfer/plugins/thredds/ISOMetadataMarshalling.java @@ -0,0 +1,74 @@ +package org.gcube.data.transfer.plugins.thredds; + +import static java.util.Collections.singletonMap; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import javax.xml.bind.JAXBException; +import javax.xml.transform.stream.StreamResult; + +import org.apache.sis.xml.MarshallerPool; +import org.apache.sis.xml.XML; +import org.opengis.metadata.Metadata; + +import com.sun.xml.bind.marshaller.NamespacePrefixMapper; + +import lombok.Synchronized; + +public class ISOMetadataMarshalling { + + private static MarshallerPool pool=null; + + + @Synchronized + private static MarshallerPool getPool() throws JAXBException { + if(pool==null) + pool=new MarshallerPool( + singletonMap(XML.METADATA_VERSION,"2007")); + + return pool; + } + + public static final File asXML(Metadata meta) throws JAXBException, IOException { + File tmp=File.createTempFile("tmp_meta_", ".xml"); + try (FileWriter fw=new FileWriter(tmp)){ + + getPool().acquireMarshaller().marshal(meta,new StreamResult(fw)); + +// XML.marshal(meta, new StreamResult(fw), singletonMap(XML.METADATA_VERSION,"2007")); + } + return tmp; + } + + + public static class Mapper extends NamespacePrefixMapper { + + private Map namespaceMap = new HashMap<>(); + + /** + * Create mappings. + */ + public Mapper() { + + + + + namespaceMap.put("http://www.w3.org/2001/XMLSchema-instance", "xsi"); + namespaceMap.put("https://www.intertech.com/software-consulting-services/", "consult"); + namespaceMap.put("http://www.w3.org/2003/05/soap-envelope/", "soap"); + } + + /* (non-Javadoc) + * Returning null when not found based on spec. + * @see com.sun.xml.bind.marshaller.NamespacePrefixMapper#getPreferredPrefix(java.lang.String, java.lang.String, boolean) + */ + @Override + public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) { + return namespaceMap.getOrDefault(namespaceUri, suggestion); + } + } +} diff --git a/src/main/java/org/gcube/data/transfer/plugins/thredds/sis/SisPlugin.java b/src/main/java/org/gcube/data/transfer/plugins/thredds/sis/SisPlugin.java index 332936d..486f184 100644 --- a/src/main/java/org/gcube/data/transfer/plugins/thredds/sis/SisPlugin.java +++ b/src/main/java/org/gcube/data/transfer/plugins/thredds/sis/SisPlugin.java @@ -16,6 +16,7 @@ import org.gcube.data.transfer.model.plugins.thredds.ThreddsCatalog; import org.gcube.data.transfer.plugin.AbstractPlugin; import org.gcube.data.transfer.plugin.fails.PluginCleanupException; import org.gcube.data.transfer.plugin.fails.PluginExecutionException; +import org.gcube.data.transfer.plugins.thredds.ISOMetadataMarshalling; import org.gcube.data.transfer.plugins.thredds.ThreddsInstanceManager; import org.gcube.spatial.data.sdi.model.metadata.MetadataPublishOptions; import org.gcube.spatial.data.sdi.model.metadata.MetadataReport; @@ -69,9 +70,7 @@ public class SisPlugin extends AbstractPlugin { log.debug("Extracting meta from {} ",dataStore.getAbsolutePath()); Metadata meta=getMetaFromFile(dataStore); - tmp=File.createTempFile("tmp_meta_", ".xml"); - - XML.marshal(meta,tmp); + tmp=ISOMetadataMarshalling.asXML(meta); org.gcube.spatial.data.sdi.interfaces.Metadata metadataInterface=SDIAbstractPlugin.metadata().build(); diff --git a/src/test/java/org/gcube/data/transfer/plugins/sis/TestGetMetadata.java b/src/test/java/org/gcube/data/transfer/plugins/sis/TestGetMetadata.java index 547b275..27dbbba 100644 --- a/src/test/java/org/gcube/data/transfer/plugins/sis/TestGetMetadata.java +++ b/src/test/java/org/gcube/data/transfer/plugins/sis/TestGetMetadata.java @@ -12,24 +12,29 @@ import java.nio.file.StandardCopyOption; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import javax.xml.bind.JAXBException; + import org.apache.sis.storage.DataStoreException; import org.apache.sis.storage.UnsupportedStorageException; +import org.apache.sis.xml.XML; +import org.gcube.data.transfer.plugins.thredds.ISOMetadataMarshalling; import org.gcube.data.transfer.plugins.thredds.sis.SisPlugin; +import org.opengis.metadata.Metadata; public class TestGetMetadata { - public static void main(String[] args) throws UnsupportedStorageException, DataStoreException, IOException, NoSuchAlgorithmException { + public static void main(String[] args) throws UnsupportedStorageException, DataStoreException, IOException, NoSuchAlgorithmException, JAXBException { String[] toCheckFiles=new String[] { // ********** HDF - NETCDF 4 - "/Users/FabioISTI/Downloads/NASA_Precipitations_1950_2100_rcp45.nc", + "/Users/FabioISTI/Downloads/NASA_Surface_Air_Temperature_1950_2100_rcp45.nc", - "https://thredds.d4science.org/thredds/fileServer/public/netcdf/ClimateChange/NASA_Precipitations_1950_2100_rcp45.nc", - "https://thredds.d4science.org/thredds/fileServer/public/netcdf/ClimateChange/NASA_Precipitations_1950_2100_rcp85.nc", - "https://thredds.d4science.org/thredds/fileServer/public/netcdf/ClimateChange/NASA_Surface_Air_Temperature_1950_2100_rcp45.nc", - "https://thredds.d4science.org/thredds/fileServer/public/netcdf/ClimateChange/NASA_Surface_Air_Temperature_1950_2100_rcp85.nc", - "https://thredds.d4science.org/thredds/fileServer/public/netcdf/ClimateChange/hcaf_v6_1950_2100.nc", +// "https://thredds.d4science.org/thredds/fileServer/public/netcdf/ClimateChange/NASA_Precipitations_1950_2100_rcp45.nc", +// "https://thredds.d4science.org/thredds/fileServer/public/netcdf/ClimateChange/NASA_Precipitations_1950_2100_rcp85.nc", +// "https://thredds.d4science.org/thredds/fileServer/public/netcdf/ClimateChange/NASA_Surface_Air_Temperature_1950_2100_rcp45.nc", +// "https://thredds.d4science.org/thredds/fileServer/public/netcdf/ClimateChange/NASA_Surface_Air_Temperature_1950_2100_rcp85.nc", +// "https://thredds.d4science.org/thredds/fileServer/public/netcdf/ClimateChange/hcaf_v6_1950_2100.nc", // *********** NETCDF 3 @@ -68,7 +73,7 @@ public class TestGetMetadata { // } - private static void check(boolean checkCopy,String src) throws UnsupportedStorageException, DataStoreException, IOException, NoSuchAlgorithmException { + private static void check(boolean checkCopy,String src) throws UnsupportedStorageException, DataStoreException, IOException, NoSuchAlgorithmException, JAXBException { File original=new File(src); @@ -94,14 +99,18 @@ public class TestGetMetadata { if(dataset.exists()) - System.out.println(SisPlugin.getMetaFromFile(dataset)); + System.out.println( + ISOMetadataMarshalling.asXML(SisPlugin.getMetaFromFile(dataset)).getAbsolutePath()); else { - System.out.println(SisPlugin.getMetaFromFile(new URL(src))); + System.out.println( + ISOMetadataMarshalling.asXML(SisPlugin.getMetaFromFile(new URL(src))).getAbsolutePath()); } } + + private static String getChecksum(File datafile) throws NoSuchAlgorithmException, IOException { MessageDigest md = MessageDigest.getInstance("SHA1");