From 1f83cff5484e409ffaed188eba61e62fa89a94a9 Mon Sep 17 00:00:00 2001 From: "Matteo V." Date: Fri, 6 Sep 2024 10:09:57 +0200 Subject: [PATCH] Fix #10530 fix catalog editor for array scenario (#10531) --- .../components/catalog/editor/MainForm.jsx | 14 ++++---- .../catalog/editor/MainFormUtils.js | 19 +++++++---- .../editor/__tests__/MainFormUtils-test.js | 34 ++++++++++--------- web/client/translations/data.de-DE.json | 5 +-- web/client/translations/data.en-US.json | 1 + web/client/translations/data.es-ES.json | 1 + web/client/translations/data.fr-FR.json | 1 + web/client/translations/data.it-IT.json | 1 + 8 files changed, 44 insertions(+), 32 deletions(-) diff --git a/web/client/components/catalog/editor/MainForm.jsx b/web/client/components/catalog/editor/MainForm.jsx index 140063a504..3ec7ff0e83 100644 --- a/web/client/components/catalog/editor/MainForm.jsx +++ b/web/client/components/catalog/editor/MainForm.jsx @@ -16,7 +16,7 @@ import InfoPopover from '../../widgets/widget/InfoPopover'; import { FormControl as FC, Form, Col, FormGroup, ControlLabel, Alert } from "react-bootstrap"; import localizedProps from '../../misc/enhancers/localizedProps'; -import {defaultPlaceholder, isValidURL} from "./MainFormUtils"; +import {defaultPlaceholder, checkUrl} from "./MainFormUtils"; const FormControl = localizedProps('placeholder')(FC); @@ -153,13 +153,13 @@ export default ({ onChangeType, setValid = () => {} }) => { - const [invalidProtocol, setInvalidProtocol] = useState(false); + const [error, setError] = useState(null); function handleProtocolValidity(url) { onChangeUrl(url); if (url) { - const isInvalidProtocol = !isValidURL(url, null, service?.allowUnsecureLayers); - setInvalidProtocol(isInvalidProtocol); - setValid(!isInvalidProtocol); + const {valid, errorMsgId} = checkUrl(url, null, service?.allowUnsecureLayers); + setError(valid ? null : errorMsgId); + setValid(valid); } } useEffect(() => { @@ -192,8 +192,8 @@ export default ({ - {invalidProtocol ? - + {error ? + : null} ); diff --git a/web/client/components/catalog/editor/MainFormUtils.js b/web/client/components/catalog/editor/MainFormUtils.js index b1cd334289..a2d16a13d5 100644 --- a/web/client/components/catalog/editor/MainFormUtils.js +++ b/web/client/components/catalog/editor/MainFormUtils.js @@ -25,13 +25,18 @@ export const defaultPlaceholder = (service) => { * @param {string} catalogUrl The URL of the catalog * @param {string} currentLocation The current location, by default `window.location.href` * @param {boolean} allowUnsecureLayers flag to allow unsecure url - * @returns {boolean} true if the URL is valid + * @returns {object} {valid: boolean, errorMsgId: string} */ -export const isValidURL = (catalogUrl = '', currentLocation, allowUnsecureLayers) => { - const { protocol: mapStoreProtocol } = url.parse(currentLocation ?? window.location.href); - const { protocol: catalogProtocol } = url.parse(catalogUrl); - if (mapStoreProtocol === 'https:' && !!catalogProtocol) { - return (mapStoreProtocol === catalogProtocol || allowUnsecureLayers); +export const checkUrl = (catalogUrl = '', currentLocation, allowUnsecureLayers) => { + try { + const { protocol: mapStoreProtocol } = url.parse(currentLocation ?? window.location.href); + const { protocol: catalogProtocol } = url.parse(catalogUrl); + if (mapStoreProtocol === 'https:' && !!catalogProtocol) { + const isProtocolValid = (mapStoreProtocol === catalogProtocol || allowUnsecureLayers); + return isProtocolValid ? {valid: true} : {valid: false, errorMsgId: "catalog.invalidUrlHttpProtocol"}; + } + return {valid: true}; + } catch (e) { + return {valid: false, errorMsgId: "catalog.invalidArrayUsageForUrl"}; } - return true; }; diff --git a/web/client/components/catalog/editor/__tests__/MainFormUtils-test.js b/web/client/components/catalog/editor/__tests__/MainFormUtils-test.js index 2c82408cb3..dd67581e79 100644 --- a/web/client/components/catalog/editor/__tests__/MainFormUtils-test.js +++ b/web/client/components/catalog/editor/__tests__/MainFormUtils-test.js @@ -1,12 +1,12 @@ import expect from "expect"; -import { isValidURL } from '../MainFormUtils'; +import { checkUrl } from '../MainFormUtils'; describe('Catalog Main Form Editor Utils', () => { - it('isValidURL', () => { + it('checkUrl', () => { const URLS = [ // http - ['http://myDomain.com/geoserver/wms', 'https://myMapStore.com/geoserver/wms', false], + ['http://myDomain.com/geoserver/wms', 'https://myMapStore.com/geoserver/wms', false, "catalog.invalidUrlHttpProtocol"], ['http://myDomain.com/geoserver/wms', 'http://myMapStore.com/geoserver/wms', true], // https ['https://myDomain.com/geoserver/wms', 'http://myMapStore.com/geoserver/wms', true], @@ -19,16 +19,17 @@ describe('Catalog Main Form Editor Utils', () => { ['/geoserver/wms', 'https://myMapStore.com/geoserver/wms', true], // relative path ["geoserver/wms", "http://myMapStore.com/geoserver/wms", true], - ["geoserver/wms", "https://myMapStore.com/geoserver/wms", true] - - + ["geoserver/wms", "https://myMapStore.com/geoserver/wms", true], + [["geoserver/wms", "geoserver/wms"], "https://myMapStore.com/geoserver/wms", false, "catalog.invalidArrayUsageForUrl"], // array + ["http://com/geoserver/wms", "https://myMapStore.com/geoserver/wms", false, "catalog.invalidUrlHttpProtocol"] ]; - URLS.forEach(([catalogURL, locationURL, valid]) => { - const result = isValidURL(catalogURL, locationURL); - expect(!!result).toEqual(!!valid, `${catalogURL} - added when location is ${locationURL} should be ${valid}, but it is ${result}`); + URLS.forEach(([catalogURL, locationURL, valid, messageId]) => { + const {valid: isValid, errorMsgId} = checkUrl(catalogURL, locationURL); + expect(!!isValid).toEqual(!!valid, `${catalogURL} - added when location is ${locationURL} should be ${valid}, but it is ${isValid}`); + expect(messageId).toEqual(errorMsgId); }); }); - it('isValidURL with allowUnsecureLayers', () => { + it('checkUrl with allowUnsecureLayers', () => { const URLS = [ // http ['http://myDomain.com/geoserver/wms', 'https://myMapStore.com/geoserver/wms', true, true], @@ -44,13 +45,14 @@ describe('Catalog Main Form Editor Utils', () => { ['/geoserver/wms', 'https://myMapStore.com/geoserver/wms', true, false], // relative path ["geoserver/wms", "http://myMapStore.com/geoserver/wms", true, false], - ["geoserver/wms", "https://myMapStore.com/geoserver/wms", true, true] - - + ["geoserver/wms", "https://myMapStore.com/geoserver/wms", true, true], + [["geoserver/wms", "geoserver/wms"], "https://myMapStore.com/geoserver/wms", false, false, "catalog.invalidArrayUsageForUrl"] // array ]; - URLS.forEach(([catalogURL, locationURL, valid, allowUnsecureLayers]) => { - const result = isValidURL(catalogURL, locationURL, allowUnsecureLayers); - expect(!!result).toEqual(!!valid, `${catalogURL} - added when location is ${locationURL} should be ${valid}, but it is ${result}`); + URLS.forEach(([catalogURL, locationURL, valid, allowUnsecureLayers, messageId]) => { + const {valid: isValid, errorMsgId} = checkUrl(catalogURL, locationURL, allowUnsecureLayers); + expect(!!isValid).toEqual(!!valid, `${catalogURL} - added when location is ${locationURL} should be ${valid}, but it is ${isValid}`); + expect(messageId).toEqual(errorMsgId); + expect(!!isValid).toEqual(!!valid, `${catalogURL} - added when location is ${locationURL} should be ${valid}, but it is ${isValid}`); }); }); }); diff --git a/web/client/translations/data.de-DE.json b/web/client/translations/data.de-DE.json index 82a9ebd075..73ddc2cc2c 100644 --- a/web/client/translations/data.de-DE.json +++ b/web/client/translations/data.de-DE.json @@ -1669,6 +1669,7 @@ "advancedSettings": "Erweiterte Einstellungen", "templateMetadataAvailable": "Metadaten im Dublin Core-Format verfügbar: abstract, boundingBox, contributor, creator, description, format, identifier, references, rights, source, subject, temporal, title, type, uri", "invalidUrlHttpProtocol": "Dieser Katalog kann nicht zu den verfügbaren hinzugefügt werden, da er ein http-Protokoll verwendet. Bitte geben Sie eine Katalog-URL an, die das https-Protokoll verwendet.", + "invalidArrayUsageForUrl": "Die Katalog-URL konnte nicht korrekt gelesen werden", "notification": { "errorTitle": "Error", "errorSearchingRecords": "Einige Datensätze wurden nicht gefunden: {records} . Bitte überprüfen Sie die URL des Abfrageparameters", @@ -2730,13 +2731,13 @@ "msExtrudedHeight": "Extrudierte Höhe", "msExtrusionColor": "Extrusionsfarbe", "msExtrusionType": "Extrusionstyp", - "wall": "Wand", + "wall": "Wand", "enableBanding": "Band styling", "selectChannel": "Wählen Sie eine Band aus", "minLabel": "Min", "maxLabel": "Max", "minSourceValue": "Mindestwert der Quelldaten", - "maxSourceValue": "Maximaler Quelldatenwert", + "maxSourceValue": "Maximaler Quelldatenwert", "customParams": "Benutzerdefinierte Parameter", "wrongFormatMsg": "Die eingegebene Konfiguration ist in falschem Format !!" }, diff --git a/web/client/translations/data.en-US.json b/web/client/translations/data.en-US.json index bd8aca9cd4..353b4a7ac1 100644 --- a/web/client/translations/data.en-US.json +++ b/web/client/translations/data.en-US.json @@ -1630,6 +1630,7 @@ "advancedSettings": "Advanced settings", "templateMetadataAvailable": "Metadata available from Dublin Core format: abstract, boundingBox, contributor, creator, description, format, identifier, references, rights, source, subject, temporal, title, type, uri", "invalidUrlHttpProtocol": "This catalog cannot be added to the available ones because it uses an http protocol. Please provide a catalog url that uses https protocol", + "invalidArrayUsageForUrl": "The catalog URL could not be parsed correctly", "notification": { "errorTitle": "Error", "errorSearchingRecords": "Some records have not been found: {records} Please check the query URL and its parameters.", diff --git a/web/client/translations/data.es-ES.json b/web/client/translations/data.es-ES.json index 6735ca97ea..b2a79257bf 100644 --- a/web/client/translations/data.es-ES.json +++ b/web/client/translations/data.es-ES.json @@ -1631,6 +1631,7 @@ "advancedSettings": "Ajustes avanzados", "templateMetadataAvailable": "Metadatos disponibles en formato Dublin Core: abstract, boundingBox, contributor, creator, description, format, identifier, references, rights, source, subject, temporal, title, type, uri", "invalidUrlHttpProtocol": "Este catálogo no se puede agregar a los disponibles porque usa un protocolo http. Proporcione una URL del catálogo que utilice el protocolo https", + "invalidArrayUsageForUrl": "La URL del catálogo no se pudo leer correctamente.", "notification": { "errorTitle": "Error", "errorSearchingRecords": "No se han encontrado algunos registros: {records} . Por favor revise la consulta param url", diff --git a/web/client/translations/data.fr-FR.json b/web/client/translations/data.fr-FR.json index 24d7bd929b..bc4ae4bd44 100644 --- a/web/client/translations/data.fr-FR.json +++ b/web/client/translations/data.fr-FR.json @@ -1631,6 +1631,7 @@ "advancedSettings": "Réglages avancés", "templateMetadataAvailable": "Métadonnées disponibles pour le format Dublin Core : abstract, boundingBox, contributor, creator, description, format, identifier, references, rights, source, subject, temporal, title, type, uri", "invalidUrlHttpProtocol": "Ce catalogue ne peut pas être ajouté à ceux disponibles car il utilise un protocole http. Veuillez fournir une URL de catalogue qui utilise le protocole https.", + "invalidArrayUsageForUrl": "L'URL du catalogue n'a pas pu être lue correctement", "notification": { "errorTitle": "Erreur", "errorSearchingRecords": "Certains enregistrements n'ont pas été trouvés: {records} . Veuillez vérifier l'URL de la requête et ses paramètres.", diff --git a/web/client/translations/data.it-IT.json b/web/client/translations/data.it-IT.json index ff53cd37fc..38c108ac8a 100644 --- a/web/client/translations/data.it-IT.json +++ b/web/client/translations/data.it-IT.json @@ -1630,6 +1630,7 @@ "advancedSettings": "Impostazioni avanzate", "templateMetadataAvailable": "Metadati disponibili del formato Dublin Core: abstract, boundingBox, contributor, creator, description, format, identifier, references, rights, source, subject, temporal, title, type, uri", "invalidUrlHttpProtocol": "Questo catalogo non può essere aggiunto a quelli disponibili perché utilizza un protocollo http. Fornisci un URL di catalogo che utilizzi il protocollo https", + "invalidArrayUsageForUrl": "Non è stato possibile leggere correttamente l'URL del catalogo", "notification": { "errorTitle": "Errore", "errorSearchingRecords": "Alcuni record non sono stati trovati: {records} . Controlla la lista dei parametri nella barra degli indirizzi",