From 6ee94745666f87ce23243de96c15100d86b34c52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20Garc=C3=ADa?= Date: Thu, 30 Nov 2023 14:45:12 +0100 Subject: [PATCH] Use Apache Commons Text library to escape JSON content in xslt processing --- .../java/org/fao/geonet/util/XslUtil.java | 9 ++- .../plugin/dublin-core/index-fields/index.xsl | 22 ++--- .../plugin/iso19110/index-fields/index.xsl | 32 ++++---- .../jsonld/iso19115-3.2018-to-jsonld.xsl | 10 +-- .../index-fields/index-subtemplate.xsl | 22 ++--- .../iso19115-3.2018/index-fields/index.xsl | 80 +++++++++---------- .../iso19115-3.2018/layout/utility-tpl.xsl | 5 +- .../formatter/jsonld/iso19139-to-jsonld.xsl | 10 +-- .../index-fields/index-subtemplate.xsl | 16 ++-- .../plugin/iso19139/index-fields/index.xsl | 60 +++++++------- .../plugin/iso19139/layout/utility-tpl.xsl | 5 +- .../org/fao/geonet/api/es/EsHTTPProxy.java | 2 +- .../main/webapp/xslt/common/index-utils.xsl | 63 ++++++--------- 13 files changed, 164 insertions(+), 172 deletions(-) diff --git a/core/src/main/java/org/fao/geonet/util/XslUtil.java b/core/src/main/java/org/fao/geonet/util/XslUtil.java index dcf3507691fd..344ce2b2d772 100644 --- a/core/src/main/java/org/fao/geonet/util/XslUtil.java +++ b/core/src/main/java/org/fao/geonet/util/XslUtil.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2001-2020 Food and Agriculture Organization of the + * Copyright (C) 2001-2023 Food and Agriculture Organization of the * United Nations (FAO-UN), United Nations World Food Programme (WFP) * and United Nations Environment Programme (UNEP) * @@ -39,6 +39,7 @@ import org.apache.commons.io.IOUtils; import org.apache.commons.lang.NotImplementedException; import org.apache.commons.lang.StringUtils; +import org.apache.commons.text.StringEscapeUtils; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; @@ -469,7 +470,7 @@ public static String getNodeId() { return ApplicationContextHolder.get().getBean(org.fao.geonet.NodeInfo.class).getId(); } - + public static String getNodeLogo(String key) { Optional source = getSource(key); return source.isPresent() ? source.get().getLogo() : ""; @@ -1706,4 +1707,8 @@ private static List buildRecordLink(SearchHits hits, String type) { }); return listOfLinks; } + + public static String escapeForJson(String value) { + return StringEscapeUtils.escapeJson(value); + } } diff --git a/schemas/dublin-core/src/main/plugin/dublin-core/index-fields/index.xsl b/schemas/dublin-core/src/main/plugin/dublin-core/index-fields/index.xsl index 2f87019176e4..ad96831b557e 100644 --- a/schemas/dublin-core/src/main/plugin/dublin-core/index-fields/index.xsl +++ b/schemas/dublin-core/src/main/plugin/dublin-core/index-fields/index.xsl @@ -71,7 +71,7 @@ - + - + - + - + - + - + @@ -187,7 +187,7 @@ - "default": + "default": @@ -206,7 +206,7 @@ - "default": + "default": @@ -227,8 +227,8 @@ { "protocol":"", - "urlObject":{"default": ""}, - "nameObject":{"default": ""}, + "urlObject":{"default": ""}, + "nameObject":{"default": ""}, "descriptionObject":{"default": ""}, "function": "" } diff --git a/schemas/iso19110/src/main/plugin/iso19110/index-fields/index.xsl b/schemas/iso19110/src/main/plugin/iso19110/index-fields/index.xsl index 1706f2d20bc2..c2d2e3913d38 100644 --- a/schemas/iso19110/src/main/plugin/iso19110/index-fields/index.xsl +++ b/schemas/iso19110/src/main/plugin/iso19110/index-fields/index.xsl @@ -50,7 +50,7 @@ [ { - "typeName" : "", - "definition" :"", - "code" :"", + "typeName" : "", + "definition" :"", + "code" :"", "isAbstract" :"", - "aliases" : "" + "aliases" : "" diff --git a/schemas/iso19115-3.2018/src/main/plugin/iso19115-3.2018/index-fields/index-subtemplate.xsl b/schemas/iso19115-3.2018/src/main/plugin/iso19115-3.2018/index-fields/index-subtemplate.xsl index f27d590d5bb1..15ccf3649267 100644 --- a/schemas/iso19115-3.2018/src/main/plugin/iso19115-3.2018/index-fields/index-subtemplate.xsl +++ b/schemas/iso19115-3.2018/src/main/plugin/iso19115-3.2018/index-fields/index-subtemplate.xsl @@ -113,13 +113,13 @@ then concat(' (', $contactInfo, ')') else ''"/> { - "default": "" ,"lang": "": "" } @@ -128,7 +128,7 @@ {"common": ""} + select="util:escapeForJson(normalize-space(.))"/>"} { - "default": "" + "default": "" ,"lang": "" + ]/@value"/>": "" } @@ -168,7 +168,7 @@ { - "default": "" } @@ -191,9 +191,9 @@ { "default": "" } @@ -217,7 +217,7 @@ select="string-join(mco:useLimitations/*/text(), ', ')"/> { - "default": " { - "default": "" @@ -250,7 +250,7 @@ {"common": ""} + select="util:escapeForJson(normalize-space(.))"/>"} diff --git a/schemas/iso19115-3.2018/src/main/plugin/iso19115-3.2018/index-fields/index.xsl b/schemas/iso19115-3.2018/src/main/plugin/iso19115-3.2018/index-fields/index.xsl index 97c572148ebd..b4959abe64a1 100644 --- a/schemas/iso19115-3.2018/src/main/plugin/iso19115-3.2018/index-fields/index.xsl +++ b/schemas/iso19115-3.2018/src/main/plugin/iso19115-3.2018/index-fields/index.xsl @@ -920,10 +920,10 @@ { - "code": "", - "codeSpace": "", - "name": "", - "url": "" + "code": "", + "codeSpace": "", + "name": "", + "url": "" } @@ -954,7 +954,7 @@ { - "title": "", + "title": "", "date": "", @@ -962,7 +962,7 @@ "link": "", - "explanation": "", + "explanation": "", "pass": "" } @@ -1002,11 +1002,11 @@ [ { - "typeName" : "", - "definition" :"", - "code" :"", + "typeName" : "", + "definition" :"", + "code" :"", "isAbstract" :"", - "aliases" : "" + "aliases" : "" - {"name": "", - "definition": "", - "code": "", + {"name": "", + "definition": "", + "code": "", "link": "", "type": "" - ,"cardinality": "" + ,"cardinality": "" ,"values": [ { - "label": "", - "code": "", - "definition": ""} + "label": "", + "code": "", + "definition": ""} , ] @@ -1069,9 +1069,9 @@ { - "protocol": "", - "function": "", "urlObject": "nilReason": "", - "applicationProfile": "" } @@ -1147,7 +1147,7 @@ 'description', .//cit:CI_Organisation/cit:name, $allLanguages, true())"/> - ,"individual":"" + ,"individual":"" } , @@ -1187,17 +1187,17 @@ select="mdq:dateTime/gco:DateTime"/> { - "name": "", + "name": "", - "description": "", + "description": "", - "date": "", + "date": "", - "value": "", + "value": "", - "unit": "", + "unit": "", "type": "" } @@ -1249,11 +1249,11 @@ { - "protocol":"", + "protocol":"", "mimeType":"", "urlObject": ", "function":"", - "applicationProfile":"", + "applicationProfile":"", "group": } @@ -1304,7 +1304,7 @@ + {"name": "children", "parent": ""}--> @@ -1430,13 +1430,13 @@ 'organisation', $organisationName, $languages, true())"/>, "role":"", - "email":"", - "website":"", - "logo":"", - "individual":"", - "position":"", - "phone":"", - "address":"" + "email":"", + "website":"", + "logo":"", + "individual":"", + "position":"", + "phone":"", + "address":"" ,"nilReason": "" @@ -1444,7 +1444,7 @@ ,"identifiers":[ { - "code": "", + "code": "", "codeSpace": "", "link": "" } @@ -1549,7 +1549,7 @@ - + diff --git a/schemas/iso19115-3.2018/src/main/plugin/iso19115-3.2018/layout/utility-tpl.xsl b/schemas/iso19115-3.2018/src/main/plugin/iso19115-3.2018/layout/utility-tpl.xsl index 59a5bfde4796..2022184aca4d 100644 --- a/schemas/iso19115-3.2018/src/main/plugin/iso19115-3.2018/layout/utility-tpl.xsl +++ b/schemas/iso19115-3.2018/src/main/plugin/iso19115-3.2018/layout/utility-tpl.xsl @@ -19,6 +19,7 @@ xmlns:gco="http://standards.iso.org/iso/19115/-3/gco/1.0" xmlns:gfc="http://standards.iso.org/iso/19110/gfc/1.1" xmlns:gn-fn-index="http://geonetwork-opensource.org/xsl/functions/index" + xmlns:util="java:org.fao.geonet.util.XslUtil" xmlns:gn="http://www.fao.org/geonetwork" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="#all"> @@ -40,8 +41,8 @@ [ { - "value": "WWW:DOWNLOAD:", - "label": ""} + "value": "WWW:DOWNLOAD:", + "label": ""} , ] diff --git a/schemas/iso19139/src/main/plugin/iso19139/formatter/jsonld/iso19139-to-jsonld.xsl b/schemas/iso19139/src/main/plugin/iso19139/formatter/jsonld/iso19139-to-jsonld.xsl index 310d01c6bbf9..20e654c1f72a 100644 --- a/schemas/iso19139/src/main/plugin/iso19139/formatter/jsonld/iso19139-to-jsonld.xsl +++ b/schemas/iso19139/src/main/plugin/iso19139/formatter/jsonld/iso19139-to-jsonld.xsl @@ -248,9 +248,9 @@ { "@type":"DataDownload", - "contentUrl":"" + "contentUrl":"" , - "encodingFormat":"" + "encodingFormat":"" , "name": { , diff --git a/schemas/iso19139/src/main/plugin/iso19139/index-fields/index-subtemplate.xsl b/schemas/iso19139/src/main/plugin/iso19139/index-fields/index-subtemplate.xsl index d63dd1ce1a29..7c27ca271ed3 100644 --- a/schemas/iso19139/src/main/plugin/iso19139/index-fields/index-subtemplate.xsl +++ b/schemas/iso19139/src/main/plugin/iso19139/index-fields/index-subtemplate.xsl @@ -90,12 +90,12 @@ then concat(' (', $contactInfo, ')') else ''"/> { - "default": "" ,"lang": "": "" } @@ -122,11 +122,11 @@ |gmd:description/gmd:PT_FreeText/*/gmd:LocalisedCharacterString[. != ''] )[1])"/> { - "default": "" + "default": "" ,"lang": "" + ]/@value"/>": "" } @@ -136,7 +136,7 @@ select="concat('S:', .//gmd:southBoundLatitude/*/text(), ', W:', .//gmd:westBoundLongitude/*/text(), ', N:', .//gmd:northBoundLatitude/*/text(), ', E:',.//gmd:eastBoundLongitude/*/text())"/> { - "default": "" + "default": "" } @@ -153,7 +153,7 @@ then gmd:name/gco:CharacterString else concat(gmd:name/gco:CharacterString, ' ', gmd:version/gco:CharacterString)"/> { - "default": "" + "default": "" } @@ -168,7 +168,7 @@ string-join(gmd:MD_LegalConstraints/gmd:otherConstraints/*/text(), ', '))"/> { - "default": "" + "default": "" } @@ -181,7 +181,7 @@ { - "default": "" + "default": "" } diff --git a/schemas/iso19139/src/main/plugin/iso19139/index-fields/index.xsl b/schemas/iso19139/src/main/plugin/iso19139/index-fields/index.xsl index a2b75ada3223..ab8df8449fa5 100644 --- a/schemas/iso19139/src/main/plugin/iso19139/index-fields/index.xsl +++ b/schemas/iso19139/src/main/plugin/iso19139/index-fields/index.xsl @@ -335,7 +335,7 @@ { - "code": "", + "code": "", "codeSpace": "", "link": "" } @@ -866,10 +866,10 @@ { - "code": "", - "codeSpace": "", - "name": "", - "url": "" + "code": "", + "codeSpace": "", + "name": "", + "url": "" } @@ -899,7 +899,7 @@ { - "title": "", + "title": "", "date": "", @@ -907,7 +907,7 @@ "link": "", - "explanation": "", + "explanation": "", "pass": "" } @@ -934,7 +934,7 @@ then 'remote' else 'catalog'"/>", "to": "", - "title": "", + "title": "", "url": "" } @@ -957,7 +957,7 @@ then 'remote' else 'catalog'"/>", "to": "", - "title": "", + "title": "", "url": "" } @@ -979,17 +979,17 @@ { - "name": "", + "name": "", - "description": "", + "description": "", - "date": "", + "date": "", - "value": "", + "value": "", - "unit": "", + "unit": "", "type": "" } @@ -1035,7 +1035,7 @@ select="gn-fn-index:add-multilingual-field( 'description', gmd:organisationName, $allLanguages, true())"/> - ,"individual":"" + ,"individual":"" } , @@ -1088,7 +1088,7 @@ + select="util:escapeForJson((gmd:name/*/text())[1])"/> @@ -1105,13 +1105,13 @@ { - "protocol":"", + "protocol":"", "mimeType":"", - "urlObject":{"default": ""}, + "urlObject":{"default": ""}, "nameObject": , @@ -1124,7 +1124,7 @@ "nilReason": "", "function":"", - "applicationProfile":"", + "applicationProfile":"", "group": } - + @@ -1278,13 +1278,13 @@ 'organisation', $organisationName, $languages, true())"/>, "role":"", - "email":"", - "website":"", - "logo":"", - "individual":"", - "position":"", - "phone":"", - "address":"" + "email":"", + "website":"", + "logo":"", + "individual":"", + "position":"", + "phone":"", + "address":"" ,"nilReason": "" @@ -1384,7 +1384,7 @@ - + diff --git a/schemas/iso19139/src/main/plugin/iso19139/layout/utility-tpl.xsl b/schemas/iso19139/src/main/plugin/iso19139/layout/utility-tpl.xsl index 02b3303886d8..6eac79b2bf88 100644 --- a/schemas/iso19139/src/main/plugin/iso19139/layout/utility-tpl.xsl +++ b/schemas/iso19139/src/main/plugin/iso19139/layout/utility-tpl.xsl @@ -29,6 +29,7 @@ xmlns:gmx="http://www.isotc211.org/2005/gmx" xmlns:gn-fn-index="http://geonetwork-opensource.org/xsl/functions/index" xmlns:gn="http://www.fao.org/geonetwork" + xmlns:util="java:org.fao.geonet.util.XslUtil" version="2.0" exclude-result-prefixes="#all"> @@ -47,8 +48,8 @@ [ { - "value": "WWW:DOWNLOAD:", - "label": ""} + "value": "WWW:DOWNLOAD:", + "label": ""} , ] diff --git a/services/src/main/java/org/fao/geonet/api/es/EsHTTPProxy.java b/services/src/main/java/org/fao/geonet/api/es/EsHTTPProxy.java index b63d2524aade..7766e2053c50 100644 --- a/services/src/main/java/org/fao/geonet/api/es/EsHTTPProxy.java +++ b/services/src/main/java/org/fao/geonet/api/es/EsHTTPProxy.java @@ -815,7 +815,7 @@ protected boolean isContentTypeValid(final String contentType) { * * { * ... - * "address":"" + * "address":"" * * ,"nilReason": "withheld" * diff --git a/web/src/main/webapp/xslt/common/index-utils.xsl b/web/src/main/webapp/xslt/common/index-utils.xsl index e6e25d9c4f9d..9bd44053d9ff 100644 --- a/web/src/main/webapp/xslt/common/index-utils.xsl +++ b/web/src/main/webapp/xslt/common/index-utils.xsl @@ -150,7 +150,7 @@ "to": "", "url": "", - "title": "", + "title": "", "origin": "" } @@ -245,10 +245,10 @@ + $doubleQuote, util:escapeForJson(.), $doubleQuote)"/> + $doubleQuote, util:escapeForJson(.), $doubleQuote)"/> @@ -256,9 +256,9 @@ + $doubleQuote, util:escapeForJson(.), $doubleQuote)"/> + $doubleQuote, util:escapeForJson(.), $doubleQuote)"/> @@ -287,7 +287,7 @@ select="concat('lang', $elementLanguage3LetterCode)"/> + $doubleQuote, util:escapeForJson(.), $doubleQuote)"/> @@ -296,16 +296,16 @@ + $doubleQuote, util:escapeForJson(.), $doubleQuote)"/> + $doubleQuote, util:escapeForJson(.), $doubleQuote)"/> + $doubleQuote, util:escapeForJson(.), $doubleQuote)"/> @@ -434,12 +434,12 @@ "": { - "id": "", + "id": "", - "title": "", - "theme": "", + "title": "", + "theme": "", - "link": "", + "link": "", "keywords": [ @@ -467,14 +467,14 @@ "default": [ - , + , ], "key": [ - , + , ] @@ -528,16 +528,16 @@ + $doubleQuote, util:escapeForJson($value/@codeListValue), $doubleQuote)"/> + $doubleQuote, util:escapeForJson($translation), $doubleQuote)"/> + $doubleQuote, util:escapeForJson($translation), $doubleQuote)"/> @@ -545,16 +545,16 @@ select="util:getCodelistTranslation( string($codelistType), string($value/@codeListValue), string(.))"/> + $doubleQuote, util:escapeForJson($translation), $doubleQuote)"/> + $doubleQuote, util:escapeForJson(.), $doubleQuote)"/> + $doubleQuote, util:escapeForJson(.), $doubleQuote)"/> @@ -612,7 +612,7 @@ - + @@ -623,7 +623,7 @@ - + @@ -682,19 +682,4 @@ else $fieldName"/> - - - - - - - - - -