From 33948e4a69cd61bf0b475b5c6756db33161198de Mon Sep 17 00:00:00 2001 From: Francesco Mangiacrapa Date: Mon, 17 Oct 2016 10:47:53 +0000 Subject: [PATCH] Added xml encoding read form stream during get response updated pom version at 1.8.1 git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-transfer/uri-resolver@133273 82a268e6-3cf1-43bd-a215-b396298e98cf --- pom.xml | 2 +- .../gis/geonetwork/GeonetworkResolver.java | 3 - .../gis/util/GetResponseRecordFilter.java | 12 +- src/test/java/GeonetworkResolverTest.java | 59 +- test.xml | 514 ++++++++++++++++++ 5 files changed, 576 insertions(+), 14 deletions(-) create mode 100644 test.xml diff --git a/pom.xml b/pom.xml index fe0fcbf..f3f482c 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ org.gcube.data.transfer uri-resolver - 1.8.0-SNAPSHOT + 1.8.1-SNAPSHOT war The URI Resolver is an HTTP URI resolver implemented as an HTTP servlet which gives access trough HTTP to different protocols URIs. diff --git a/src/main/java/org/gcube/datatransfer/resolver/gis/geonetwork/GeonetworkResolver.java b/src/main/java/org/gcube/datatransfer/resolver/gis/geonetwork/GeonetworkResolver.java index 4b24440..d0ae7ff 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/gis/geonetwork/GeonetworkResolver.java +++ b/src/main/java/org/gcube/datatransfer/resolver/gis/geonetwork/GeonetworkResolver.java @@ -280,15 +280,12 @@ public class GeonetworkResolver extends HttpServlet{ noAuthenticationB = true; } - logger.info("SCOPE is: " + scope); logger.info(PARAMETER_FILTER_PUBLIC_IDS +" is "+filterPublicMetadataIDsB); logger.info(PARAMETER_NO_AUTHENTICATION +" is "+noAuthenticationB); try { -// ServerParameters geonetworkParams = getGeonetworkCachedServerParameters(scope); - GeonetworkServiceInterface gntwAccess = new GeonetworkAccessParameter(scope, null); GeonetworkInstance gnInstance = gntwAccess.getGeonetworkInstance(); diff --git a/src/main/java/org/gcube/datatransfer/resolver/gis/util/GetResponseRecordFilter.java b/src/main/java/org/gcube/datatransfer/resolver/gis/util/GetResponseRecordFilter.java index f6a5419..a625c23 100644 --- a/src/main/java/org/gcube/datatransfer/resolver/gis/util/GetResponseRecordFilter.java +++ b/src/main/java/org/gcube/datatransfer/resolver/gis/util/GetResponseRecordFilter.java @@ -36,6 +36,10 @@ import org.w3c.dom.NodeList; */ public class GetResponseRecordFilter { + /** + * + */ + private static final String UTF_8 = "UTF-8"; /** * */ @@ -178,7 +182,13 @@ public class GetResponseRecordFilter { */ private static final InputStream documentToInputStream(Document xml) throws Exception { Transformer tf = TransformerFactory.newInstance().newTransformer(); - tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); + if(xml.getXmlEncoding()!=null && !xml.getXmlEncoding().isEmpty()){ + logger.info("Using encoding: "+xml.getXmlEncoding()); + tf.setOutputProperty(OutputKeys.ENCODING, xml.getXmlEncoding()); + }else{ + logger.info("Using default encoding: "+UTF_8); + tf.setOutputProperty(OutputKeys.ENCODING, UTF_8); + } tf.setOutputProperty(OutputKeys.INDENT, "yes"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); StreamResult outputTarget = new StreamResult(outputStream); diff --git a/src/test/java/GeonetworkResolverTest.java b/src/test/java/GeonetworkResolverTest.java index 60919da..3af3b98 100644 --- a/src/test/java/GeonetworkResolverTest.java +++ b/src/test/java/GeonetworkResolverTest.java @@ -5,11 +5,20 @@ import java.io.UnsupportedEncodingException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.ArrayList; import java.util.HashMap; import java.util.Map; +import org.gcube.common.scope.api.ScopeProvider; import org.gcube.datatransfer.resolver.gis.geonetwork.GeonetworkResolver; import org.gcube.datatransfer.resolver.gis.geonetwork.HTTPCallsUtils; +import org.gcube.datatransfer.resolver.gis.util.GetResponseRecordFilter; +import org.gcube.spatial.data.geonetwork.GeoNetwork; +import org.gcube.spatial.data.geonetwork.GeoNetworkPublisher; +import org.gcube.spatial.data.geonetwork.LoginLevel; +import org.gcube.spatial.data.geonetwork.configuration.Configuration; +import org.gcube.spatial.data.geonetwork.model.Account; +import org.gcube.spatial.data.geonetwork.model.Account.Type; /** @@ -22,6 +31,12 @@ import org.gcube.datatransfer.resolver.gis.geonetwork.HTTPCallsUtils; */ public class GeonetworkResolverTest { + private static String scope = "/d4science.research-infrastructures.eu"; + + private static LoginLevel loginLevel = LoginLevel.SCOPE; + + private static Type accountType = Type.CKAN; + /** * The main method. * @@ -46,14 +61,23 @@ public class GeonetworkResolverTest { String gnGetlURL = newQueryString==null || newQueryString.isEmpty()? baseURL : baseURL+"?"+newQueryString; logger.info("Sending get request to URL: "+gnGetlURL);*/ - HTTPCallsUtils httpUtils = new HTTPCallsUtils(); - String data =""; + try{ + HTTPCallsUtils httpUtils = new HTTPCallsUtils(); + String data =""; - String contentType = "application/x-www-form-urlencoded"; - String uuid = "fao-fsa-map-27.7.j"; - String gnCSWlURL = "http://geoserver-dev2.d4science-ii.research-infrastructures.eu/geonetwork/srv/en//srv/en/mef.export"; + ScopeProvider.instance.set(scope); + GeoNetworkPublisher reader=GeoNetwork.get(); + + + Configuration config = reader.getConfiguration(); + Account account=config.getScopeConfiguration().getAccounts().get(accountType); + reader.login(loginLevel); + + String contentType = "application/xml ; charset=\"UTF-8\""; + String uuid = "c15ae8e5-71c0-4b8b-aa29-304cc4e97238"; + String gnCSWlURL = "http://geonetwork.d4science.org/geonetwork/srv/en/csw"; +// String gnCSWlURL = "http://geoserver-dev2.d4science-ii.research-infrastructures.eu/geonetwork/srv/en//srv/en/mef.export"; - try { /*File file = File.createTempFile(uuid, ".xml"); InputStream response = httpUtils.post(gnCSWlURL, file, contentType);*/ @@ -63,14 +87,31 @@ public class GeonetworkResolverTest { value[0]=uuid; map.put(GeonetworkResolver.UUID, value); // data = ""+uuid+""; - data = "uuid="+uuid; + + data = "" + + ""+ + ""+uuid+"" + + "full" + + ""; + byte[] byteArray = data.getBytes(); InputStream response = httpUtils.post(gnCSWlURL, new ByteArrayInputStream(byteArray), contentType, map); +// String respToString = IOUtils.toString(response); +// System.out.println("Response returned by request: \n"+respToString); +// InputStream responseToIs = IOUtils.toInputStream(respToString); if(response!=null){ try { - final Path destination = Paths.get("test"); - Files.copy(response, destination); + + InputStream re = GetResponseRecordFilter.overrideResponseIdsByListIds(response, new ArrayList()); +// String theString = IOUtils.toString(re); +// System.out.println("Response returned after overriding: \n"+theString); + + final Path destination = Paths.get("test.xml"); + Files.copy(re, destination); } catch (IOException e) { // TODO Auto-generated catch block diff --git a/test.xml b/test.xml new file mode 100644 index 0000000..3a516a6 --- /dev/null +++ b/test.xml @@ -0,0 +1,514 @@ + + + + + c15ae8e5-71c0-4b8b-aa29-304cc4e97238 + + + English + + + UTF-8 + + + Dataset + + + + + fabio.sinibaldi + + + iMarine Consortium + + + Author + + + + + + + iMarine Consortium + + + iMarine.eu + + + + + + + info@i-marine.eu + + + + + + + http://www.i-marine.eu + + + WWW:LINK-1.0-http--link + + + + + + + Distributor + + + + + + + iMarine Consortium Technical Support + + + iMarine.eu + + + + + + + support@i-marine.eu + + + + + + + http://www.i-marine.eu + + + WWW:LINK-1.0-http--link + + + + + + + Resource provider + + + + + 2013-06-19T06:28:12.340+02:00 + + + + + Geometry only + + + + + Surface + + + 130 + + + + + + + + + + + Farfantepenaeus paulensis + + + + + 2012-12-21T19:30:57.139+01:00 + + + Creation + + + + + Map digital + + + + + This Farfantepenaeus paulensis Species Distribution Map has been generated with the AquaMaps methodology by exploiting the technology and the computational resources provided by iMarine. In particular, this map has been produced using the HSPEC 2050 Native Range dataset, generated using AquaMaps NativeRange2050 algorithm. + + + The aim of this Species Distribution map is to provide its users with a model-based map displaying prediction of species distributions based on occurrence records. + + + This layer has been produced by iMarine (www.i-marine.eu). iMarine (283644) is funded by the European Commission under Framework Programme 7 + + + Kaschner, K., J. S. Ready, E. Agbayani, J. Rius, K. Kesner-Reyes, P. D. Eastwood, A. B. South, S. O. Kullander, T. Rees, C. H. Close, R. Watson, D. Pauly, and R. Froese. 2008 AquaMaps: Predicted range maps for aquatic species. World wide web electronic publication, www.aquamaps.org, Version 10/2008. + + + + + As needed + + + + + + + http://node49.p.d4science.research-infrastructures.eu:9003/83/Animalia/Arthropoda/Malacostraca/Decapoda/Penaeidae/ITS-551576/Earth.jpg + + + + + + + São Paulo shrimp + + + Theme + + + + + FISHBASE + + + + + 2013-06-18T18:06:55.662+02:00 + + + Creation + + + + + + + FISHBASE + + + IFM-GEOMAR + + + + + + + http://www.fishbase.org/search.php + + + + + + + Point of contact + + + + + FishBase is a global species database of fish species (specifically finfish). + + + + + + + + + Farfantepenaeus paulensis + + + Theme + + + + + OBIS + + + + + 2013-06-18T18:06:55.662+02:00 + + + Creation + + + + + + + OBIS + + + UNESCO + + + + + + + http://www.iobis.org + + + + + + + Point of contact + + + + + Intergovernmental Oceanographic Commission (IOC) of UNESCO. The Ocean Biogeographic Information System. Web. http://www.iobis.org. (Consulted on DATE) + + + + + + + + + Ecological niche modelling + + + AquaMaps + + + SpeciesDistribution + + + iMarine + + + Theme + + + + + General + + + + + 2013-06-19T06:28:12.340+02:00 + + + Creation + + + + + + + + + + + 0.5 + + + + + English + + + biota + + + + + + + true + + + -180.0 + + + 180.0 + + + -90.0 + + + 90.0 + + + + + + + + + + + + + WMS + + + 1.1.0 + + + + + + + WFS + + + 1.1.0 + + + + + + + WCS + + + 1.0.0 + + + + + + + + + http://geoserver4.d4science.research-infrastructures.eu/geoserver/wms?service=wms&version=1.1.0&request=GetMap&layers=lfarfantepenaeuspaulensis20121221193002924cet&styles=&bbox=-180,-90,180,90&width=676&height=330&srs=EPSG:4326&crs=EPSG:4326&format=application/openlayers + + + OGC:WMS-1.3.0-http-get-map + + + lfarfantepenaeuspaulensis20121221193002924cet + + + + + + + http://geoserver4.d4science.research-infrastructures.eu/geoserver/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=lfarfantepenaeuspaulensis20121221193002924cet&format=json + + + OGC:WFS-1.0.0-http-get-feature + + + lfarfantepenaeuspaulensis20121221193002924cet + + + + + + + http://geoserver4.d4science.research-infrastructures.eu/geoserver/wms?service=wms&version=1.1.0&request=GetMap&layers=lfarfantepenaeuspaulensis20121221193002924cet&styles=&bbox=-180,-90,180,90&width=676&height=330&srs=EPSG:4326&crs=EPSG:4326&format=application/openlayers + + + OGC:WMS-1.3.0-http-get-map + + + lfarfantepenaeuspaulensis20121221193002924cet + + + + + + + + + + + + + Dataset + + + + + + + Kaschner, K., J. S. Ready, E. Agbayani, J. Rius, K. Kesner-Reyes, P. D. Eastwood, A. B. South, S. O. Kullander, T. Rees, C. H. Close, R. Watson, D. Pauly, and R. Froese. 2008 AquaMaps: Predicted range maps for aquatic species. World wide web electronic publication, www.aquamaps.org, Version 10/2008. + + + + + AquaMaps Ecological Niche Modelling + + + + + + + + + HSPEC 2050 Native Range + + + + + 2011-10-05T00:39:17.101+02:00 + + + Creation + + + + + + + hspec2011_10_04_21_00_23_274 + + + + + + + + + + + true + + + -180.0 + + + 180.0 + + + -90.0 + + + 90.0 + + + + + + + + + + + + + + + CC-BY-SA + + + License + + + License + + + + +