From 6fdb1758027a5ad94f302d119b45a7da90bd2107 Mon Sep 17 00:00:00 2001 From: Florent gravin Date: Wed, 15 Sep 2021 12:28:13 +0200 Subject: [PATCH] jsonHarvester: add api& nodeUrl in XML for transfo used by ODS to compute exports links --- .../harvester/simpleUrl/Harvester.java | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/harvesters/src/main/java/org/fao/geonet/kernel/harvest/harvester/simpleUrl/Harvester.java b/harvesters/src/main/java/org/fao/geonet/kernel/harvest/harvester/simpleUrl/Harvester.java index 088611c999b..6621e5f634f 100644 --- a/harvesters/src/main/java/org/fao/geonet/kernel/harvest/harvester/simpleUrl/Harvester.java +++ b/harvesters/src/main/java/org/fao/geonet/kernel/harvest/harvester/simpleUrl/Harvester.java @@ -52,8 +52,10 @@ import java.io.IOException; import java.io.InputStreamReader; +import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; +import java.net.URL; import java.nio.file.Path; import java.util.ArrayList; import java.util.HashMap; @@ -137,8 +139,16 @@ public HarvestResult harvest(Logger log) throws Exception { nodes.forEach(record -> { String uuid = this.extractUuidFromIdentifier(record.get(params.recordIdPath).asText()); - Element xml = convertRecordToXml(record, uuid); - uuids.put(uuid, xml); + String apiUrl = params.url.split("\\?")[0]; + URL url = null; + try { + url = new URL(apiUrl); + String nodeUrl = new StringBuilder(url.getProtocol()).append("://").append(url.getAuthority()).toString(); + Element xml = convertRecordToXml(record, uuid, apiUrl, nodeUrl); + uuids.put(uuid, xml); + } catch (MalformedURLException e) { + log.warning("Failed to parse Node URL"); + } }); aligner.align(uuids, errors); allUuids.putAll(uuids); @@ -191,8 +201,8 @@ protected List buildListOfUrl(SimpleUrlParams params, int numberOfRecord numberOfRecordsPerPage = Integer.parseInt(pageSizeParamValue); } else { log.warning(String.format( - "Page size param '%s' not found or is not a numeric in URL '%s'. Can't build a list of pages.", - params.pageSizeParam, params.url)); + "Page size param '%s' not found or is not a numeric in URL '%s'. Can't build a list of pages.", + params.pageSizeParam, params.url)); urlList.add(params.url); return urlList; } @@ -203,8 +213,8 @@ protected List buildListOfUrl(SimpleUrlParams params, int numberOfRecord startAtZero = Integer.parseInt(pageFromParamValue) == 0; } else { log.warning(String.format( - "Page from param '%s' not found or is not a numeric in URL '%s'. Can't build a list of pages.", - params.pageFromParam, params.url)); + "Page from param '%s' not found or is not a numeric in URL '%s'. Can't build a list of pages.", + params.pageFromParam, params.url)); urlList.add(params.url); return urlList; } @@ -215,26 +225,28 @@ protected List buildListOfUrl(SimpleUrlParams params, int numberOfRecord for (int i = 0; i < numberOfPages; i++) { int from = i * numberOfRecordsPerPage + (startAtZero ? 0 : 1); int size = i == numberOfPages - 1 ? // Last page - numberOfRecordsToHarvest - from + (startAtZero ? 0 : 1) : - numberOfRecordsPerPage; + numberOfRecordsToHarvest - from + (startAtZero ? 0 : 1) : + numberOfRecordsPerPage; String url = params.url - .replaceAll(params.pageFromParam + "=[0-9]+", params.pageFromParam + "=" + from) - .replaceAll(params.pageSizeParam + "=[0-9]+", params.pageSizeParam + "=" + size); + .replaceAll(params.pageFromParam + "=[0-9]+", params.pageFromParam + "=" + from) + .replaceAll(params.pageSizeParam + "=[0-9]+", params.pageSizeParam + "=" + size); urlList.add(url); } return urlList; } - private Element convertRecordToXml(JsonNode record, String uuid) { + private Element convertRecordToXml(JsonNode record, String uuid, String apiUrl, String nodeUrl) { ObjectMapper objectMapper = new ObjectMapper(); try { String recordAsXml = XML.toString( - new JSONObject( - objectMapper.writeValueAsString(record)), "record"); + new JSONObject( + objectMapper.writeValueAsString(record)), "record"); recordAsXml = Xml.stripNonValidXMLCharacters(recordAsXml).replace("<@", "<").replace("