From c3fe382efb3d7fccd576da319abea09cab535a7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Luis=20Rodr=C3=ADguez=20Ponce?= Date: Tue, 21 Jun 2022 09:54:37 +0200 Subject: [PATCH] Harvesters / GeoNetwork harvester / Re-add exiting files when re-harvesting (#6416) * Harvesters / GeoNetwork harvester / Re-add exiting files when re-harvesting getResourceDescription must return null if the resource doesn't exist. This way the caller Aligner process can save the file again as it checks for null to know if the resource exists or not. * Add test cases --- .../records/attachments/FilesystemStore.java | 40 ++++--- .../attachments/FilesystemStoreTest.java | 102 ++++++++++++++++++ .../records/attachments/existingResource.jpg | Bin 0 -> 6416 bytes 3 files changed, 126 insertions(+), 16 deletions(-) create mode 100644 core/src/test/java/org/fao/geonet/api/records/attachments/FilesystemStoreTest.java create mode 100644 core/src/test/resources/org/fao/geonet/api/records/attachments/existingResource.jpg diff --git a/core/src/main/java/org/fao/geonet/api/records/attachments/FilesystemStore.java b/core/src/main/java/org/fao/geonet/api/records/attachments/FilesystemStore.java index 82da882a2e8..4f2c16ec739 100644 --- a/core/src/main/java/org/fao/geonet/api/records/attachments/FilesystemStore.java +++ b/core/src/main/java/org/fao/geonet/api/records/attachments/FilesystemStore.java @@ -1,6 +1,6 @@ /* * ============================================================================= - * === Copyright (C) 2001-2016 Food and Agriculture Organization of the + * === Copyright (C) 2001-2022 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.fao.geonet.utils.Log; import org.springframework.beans.factory.annotation.Autowired; +import javax.annotation.Nullable; import java.io.IOException; import java.io.InputStream; import java.nio.file.DirectoryStream; @@ -50,7 +51,6 @@ import java.util.Date; import java.util.List; import java.util.concurrent.TimeUnit; -import javax.annotation.Nullable; /** * A FileSystemStore store resources files in the catalog data directory. Each metadata record as a directory in the data directory @@ -148,25 +148,33 @@ public MetadataResource getResourceDescription(final ServiceContext context, Str return getResourceDescription(context, metadataUuid, visibility, path, approved); } + /** + * Get the resource description or null if the file doesn't exist. + * @param context the service context. + * @param metadataUuid the uuid of the owner metadata record. + * @param visibility is the resource is public or not. + * @param filePath the path to the resource. + * @param approved if the metadata draft has been approved or not + * @return the resource description or {@code null} if there is any problem accessing the file. + */ private MetadataResource getResourceDescription(final ServiceContext context, final String metadataUuid, - final MetadataResourceVisibility visibility, final Path filePath, Boolean approved) - throws IOException { - Integer metadataId = null; + final MetadataResourceVisibility visibility, final Path filePath, Boolean approved) { + FilesystemStoreResource result = null; try { - metadataId = getAndCheckMetadataId(metadataUuid, approved); - } catch (Exception e) { - Log.error(Geonet.RESOURCES, e.getMessage(), e); - } - - long fileSize = -1; - try { - fileSize = Files.size(filePath); + int metadataId = getAndCheckMetadataId(metadataUuid, approved); + long fileSize = Files.size(filePath); + result = new FilesystemStoreResource(metadataUuid, metadataId, filePath.getFileName().toString(), + settingManager.getNodeURL() + "api/records/", visibility, fileSize, + new Date(Files.getLastModifiedTime(filePath).toMillis()), approved); } catch (IOException e) { - Log.error(Geonet.RESOURCES, e.getMessage(), e); + Log.error(Geonet.RESOURCES, "Error getting size of file " + filePath + ": " + + e.getMessage(), e); + } catch (Exception e) { + Log.error(Geonet.RESOURCES, "Error in getResourceDescription: " + + e.getMessage(), e); } - return new FilesystemStoreResource(metadataUuid, metadataId, filePath.getFileName().toString(), settingManager.getNodeURL() + "api/records/", - visibility, fileSize, new Date(Files.getLastModifiedTime(filePath).toMillis()), approved); + return result; } @Override diff --git a/core/src/test/java/org/fao/geonet/api/records/attachments/FilesystemStoreTest.java b/core/src/test/java/org/fao/geonet/api/records/attachments/FilesystemStoreTest.java new file mode 100644 index 00000000000..34ea93823c6 --- /dev/null +++ b/core/src/test/java/org/fao/geonet/api/records/attachments/FilesystemStoreTest.java @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2001-2022 Food and Agriculture Organization of the + * United Nations (FAO-UN), United Nations World Food Programme (WFP) + * and United Nations Environment Programme (UNEP) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + * + * Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2, + * Rome - Italy. email: geonetwork@osgeo.org + */ +package org.fao.geonet.api.records.attachments; + +import jeeves.server.context.ServiceContext; +import org.fao.geonet.AbstractCoreIntegrationTest; +import org.fao.geonet.api.exception.ResourceNotFoundException; +import org.fao.geonet.domain.ISODate; +import org.fao.geonet.domain.MetadataResource; +import org.fao.geonet.domain.MetadataResourceVisibility; +import org.fao.geonet.domain.ReservedGroup; +import org.fao.geonet.kernel.datamanager.IMetadataManager; +import org.fao.geonet.kernel.setting.SettingManager; +import org.jdom.Element; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import java.io.InputStream; +import java.util.Date; + +import static org.junit.Assert.*; + +public class FilesystemStoreTest extends AbstractCoreIntegrationTest { + + @Autowired + protected IMetadataManager metadataManager; + @Autowired + protected SettingManager settingManager; + + @Test + public void getResourceDescription() throws Exception { + ServiceContext context = createServiceContext(); + loginAsAdmin(context); + + String mdId = metadataManager.insertMetadata( + context, + "iso19139", + new Element("MD_Metadata"), + "uuid", + context.getUserSession().getUserIdAsInt(), + "" + ReservedGroup.all.getId(), + "sourceid", + "n", + "doctype", + null, + new ISODate().getDateAndTime(), + new ISODate().getDateAndTime(), + false, + false); + + FilesystemStore filesystemStore = new FilesystemStore(); + filesystemStore.settingManager = this.settingManager; + + MetadataResource resource = filesystemStore.getResourceDescription(context, "uuid", MetadataResourceVisibility.PUBLIC, "test.jpg", true); + assertNull("Non exising resource must be null", resource); + + try (InputStream file = this.getClass().getResourceAsStream("existingResource.jpg")) { + filesystemStore.putResource(context, "uuid", "existingResource.jpg", file, new Date(), + MetadataResourceVisibility.PUBLIC, true); + resource = filesystemStore.getResourceDescription(context, "uuid", + MetadataResourceVisibility.PUBLIC, "existingResource.jpg", true); + assertNotNull("Existing resource must not return null", resource); + assertEquals("The file size doesn't match the expected one", 6416, resource.getSize()); + assertEquals("The visibility must be public", MetadataResourceVisibility.PUBLIC, resource.getVisibility()); + assertEquals("Filename is wrong", "existingResource.jpg", resource.getFilename()); + assertEquals("Metadata id is wrong", Integer.parseInt(mdId), resource.getMetadataId()); + } + } + + @Test(expected = ResourceNotFoundException.class) + public void testGetResourceDescriptionNonExistingUuid() throws Exception { + ServiceContext context = createServiceContext(); + loginAsAdmin(context); + FilesystemStore filesystemStore = new FilesystemStore(); + filesystemStore.settingManager = this.settingManager; + + + // context, metadataUuid, visibility, path, approved) + filesystemStore.getResourceDescription(context, "nonExistingUuid", + MetadataResourceVisibility.PUBLIC, "existingResource.jpg", true); + } +} diff --git a/core/src/test/resources/org/fao/geonet/api/records/attachments/existingResource.jpg b/core/src/test/resources/org/fao/geonet/api/records/attachments/existingResource.jpg new file mode 100644 index 0000000000000000000000000000000000000000..353a358d1ad7b7f1f57d2dd9a51b6108286e5cf0 GIT binary patch literal 6416 zcmbVwWmH|wvh9M62ids0y9N#J&W4Q!x8Uv&90DYe0Kp}=Y+Qo`cL?q-!Gi>X>*G7; zyfg0od#l&4?$N8OM$f8QJ)akzHvq7boT3~62L}LfF9Udf0w|@O9bMe4+}*7#smy(; z>|CtgvAbDW{Acoc83+du{%c4`h)75%$S;P1j{1+GXR*h4`{~CSYVHJ}>~_3mvTU++_=A zKm5^Ufr)hD<3j+rZW^?>Uln`NshXDU)!$NV_JqbTJtg1}0X%a-3W4N?Bp52e~?8?c

1G0OC z?gs=dCssAX%3eS9-4?Fg&K?0YSPs+Qeq^STB3aMzwS9r|#zcDh+|h!n*Cq-X<8`-6 zQt!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