From 6917cac5a841e80a6c1c4ae3da06dda2488fd9be Mon Sep 17 00:00:00 2001 From: Daniel Radeau Date: Thu, 10 Sep 2026 10:53:13 +0200 Subject: [PATCH] bug #16840 fix: saved Resip session cannot be reloaded The Work SEDA version was never serialized into work.json (private field without accessor), so getSeda2VersionFromFile always returned null. On load, the version switch was systematically offered, with the current UI version in the message instead of the file's, then a SedaVersionChangedEvent carrying a null was published: TreatmentParameters.toPrefs then threw an NPE, shown as "Erreur de chargement de [...] ->null". - Work: getVersion/setVersion accessors so the SEDA version is actually saved and read back, value taken from SedaContext on save when missing, and fallback to SEDA 2.1 for files created before this field was serialized. - Work: unknown properties are tolerated when reading back. - ResipGraphicApp: the file's version is displayed in the switch prompt, with a guard against a null version. - TreatmentParameters: null version change events are ignored and toPrefs can no longer throw an NPE. - WorkTest: non-regression tests on the SEDA version round trip and on save files without a version. - TreatmentParametersTest: reproduction of the "->null" NPE as it occurred when loading a session. --- .../tools/resip/app/ResipGraphicApp.java | 4 +- .../fr/gouv/vitam/tools/resip/data/Work.java | 33 ++++++- .../resip/parameters/TreatmentParameters.java | 11 ++- .../parameters/TreatmentParametersTest.java | 86 +++++++++++++++++++ .../tools/resip/parameters/WorkTest.java | 73 ++++++++++++++++ 5 files changed, 201 insertions(+), 6 deletions(-) create mode 100644 resip/src/test/java/fr/gouv/vitam/tools/resip/parameters/TreatmentParametersTest.java diff --git a/resip/src/main/java/fr/gouv/vitam/tools/resip/app/ResipGraphicApp.java b/resip/src/main/java/fr/gouv/vitam/tools/resip/app/ResipGraphicApp.java index 562e8677..d7d465d9 100644 --- a/resip/src/main/java/fr/gouv/vitam/tools/resip/app/ResipGraphicApp.java +++ b/resip/src/main/java/fr/gouv/vitam/tools/resip/app/ResipGraphicApp.java @@ -772,12 +772,12 @@ private void loadWork() { filename = fileChooser.getSelectedFile().getCanonicalPath(); final SedaVersion sedaVersionFromWorkspace = Work.getSeda2VersionFromFile(filename); - if (sedaVersionFromWorkspace != sedaVersion) { + if ((sedaVersionFromWorkspace != null) && (sedaVersionFromWorkspace != sedaVersion)) { if ( UserInteractionDialog.getUserAnswer( mainWindow, "Pour charger ce fichier il faut faire passer l'interface en " + - sedaVersion + + sedaVersionFromWorkspace + "\n" + "Voulez-vous continuer?", "Confirmation", diff --git a/resip/src/main/java/fr/gouv/vitam/tools/resip/data/Work.java b/resip/src/main/java/fr/gouv/vitam/tools/resip/data/Work.java index 7fba15ba..538c1f54 100644 --- a/resip/src/main/java/fr/gouv/vitam/tools/resip/data/Work.java +++ b/resip/src/main/java/fr/gouv/vitam/tools/resip/data/Work.java @@ -37,6 +37,7 @@ */ package fr.gouv.vitam.tools.resip.data; +import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.module.SimpleModule; @@ -49,6 +50,7 @@ import fr.gouv.vitam.tools.sedalib.core.*; import fr.gouv.vitam.tools.sedalib.core.json.DataObjectPackageDeserializer; import fr.gouv.vitam.tools.sedalib.core.json.DataObjectPackageSerializer; +import fr.gouv.vitam.tools.sedalib.core.seda.SedaContext; import fr.gouv.vitam.tools.sedalib.core.seda.SedaVersion; import fr.gouv.vitam.tools.sedalib.utils.SEDALibException; import fr.gouv.vitam.tools.sedalib.utils.SEDALibProgressLogger; @@ -82,6 +84,11 @@ public class Work { */ static final String JSON_FILENAME = "work.json"; + /** + * The SEDA version assumed for save files created before the version was serialized. + */ + static final SedaVersion DEFAULT_SEDA_VERSION = SedaVersion.V2_1; + /** * The version of this object used for to distinct serialization in prefs or on * disk. @@ -180,6 +187,7 @@ public static SedaVersion getSeda2VersionFromFile(String file) throws ResipExcep module.addDeserializer(ExportContext.class, new NullDeserializer()); module.addDeserializer(CreationContext.class, new NullDeserializer()); mapper.registerModule(module); + mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); ZipEntry ze = zis.getNextEntry(); if (!ze.getName().equals(JSON_FILENAME)) throw new ResipException( "Resip: Le fichier [" + file + "] n'est pas une sauvegarde de session Resip" @@ -191,7 +199,7 @@ public static SedaVersion getSeda2VersionFromFile(String file) throws ResipExcep e ); } - return ow.version; + return ow.version == null ? DEFAULT_SEDA_VERSION : ow.version; } /** @@ -209,6 +217,7 @@ public static Work createFromFile(String file) throws ResipException { module.addSerializer(DataObjectPackage.class, new DataObjectPackageSerializer()); module.addDeserializer(DataObjectPackage.class, new DataObjectPackageDeserializer()); mapper.registerModule(module); + mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); ZipEntry ze = zis.getNextEntry(); if (!ze.getName().equals(JSON_FILENAME)) throw new ResipException( "Resip: Le fichier [" + file + "] n'est pas une sauvegarde de session Resip" @@ -216,6 +225,9 @@ public static Work createFromFile(String file) throws ResipException { ow = mapper.readValue(zis, Work.class); // some fields need to be computed or defined after the load phase from Json + if (ow.version == null) ow.version = SedaContext.getVersion() == null + ? DEFAULT_SEDA_VERSION + : SedaContext.getVersion(); ow.getDataObjectPackage().getGhostRootAu().setDataObjectPackage(ow.getDataObjectPackage()); for (Map.Entry pair : ow .getDataObjectPackage() @@ -254,6 +266,7 @@ public static Work createFromFile(String file) throws ResipException { */ public void save(String file) { try { + if (version == null) version = SedaContext.getVersion(); ObjectMapper mapper = new ObjectMapper(); SimpleModule module = new SimpleModule(); module.addSerializer(DataObjectPackage.class, new DataObjectPackageSerializer()); @@ -307,6 +320,24 @@ public void setCreationContext(CreationContext creationContext) { this.creationContext = creationContext; } + /** + * Gets the SEDA version of this work. + * + * @return the SEDA version + */ + public SedaVersion getVersion() { + return version; + } + + /** + * Sets the SEDA version of this work. + * + * @param version the new SEDA version + */ + public void setVersion(SedaVersion version) { + this.version = version; + } + /** * Gets the global metadata context. * diff --git a/resip/src/main/java/fr/gouv/vitam/tools/resip/parameters/TreatmentParameters.java b/resip/src/main/java/fr/gouv/vitam/tools/resip/parameters/TreatmentParameters.java index 1bff71d8..06ffb9b4 100644 --- a/resip/src/main/java/fr/gouv/vitam/tools/resip/parameters/TreatmentParameters.java +++ b/resip/src/main/java/fr/gouv/vitam/tools/resip/parameters/TreatmentParameters.java @@ -73,7 +73,7 @@ public class TreatmentParameters { */ public TreatmentParameters() { EventBus.subscribe(SedaVersionChangedEvent.class, event -> { - this.sedaVersion = event.getNewVersion(); + if (event.getNewVersion() != null) this.sedaVersion = event.getNewVersion(); }); } @@ -88,7 +88,7 @@ private String canonizeCategoryName(String category) { */ public TreatmentParameters(Preferences preferences) { EventBus.subscribe(SedaVersionChangedEvent.class, event -> { - this.sedaVersion = event.getNewVersion(); + if (event.getNewVersion() != null) this.sedaVersion = event.getNewVersion(); }); final String categoriesString = preferences @@ -143,7 +143,12 @@ public void toPrefs(Preferences preferences) { ); } preferences.getPrefProperties().setProperty("treatmentParameters.dupMax", Integer.toString(dupMax)); - preferences.getPrefProperties().setProperty("treatmentParameters.seda2Version", sedaVersion.toString()); + preferences + .getPrefProperties() + .setProperty( + "treatmentParameters.seda2Version", + (sedaVersion == null ? SedaVersion.V2_1 : sedaVersion).toString() + ); } /** diff --git a/resip/src/test/java/fr/gouv/vitam/tools/resip/parameters/TreatmentParametersTest.java b/resip/src/test/java/fr/gouv/vitam/tools/resip/parameters/TreatmentParametersTest.java new file mode 100644 index 00000000..394a3ce5 --- /dev/null +++ b/resip/src/test/java/fr/gouv/vitam/tools/resip/parameters/TreatmentParametersTest.java @@ -0,0 +1,86 @@ +/** + * Copyright French Prime minister Office/SGMAP/DINSIC/Vitam Program (2019-2022) + * and the signatories of the "VITAM - Accord du Contributeur" agreement. + * + * contact@programmevitam.fr + * + * This software is a computer program whose purpose is to provide + * tools for construction and manipulation of SIP (Submission + * Information Package) conform to the SEDA (Standard d’Échange + * de données pour l’Archivage) standard. + * + * This software is governed by the CeCILL-C license under French law and + * abiding by the rules of distribution of free software. You can use, + * modify and/ or redistribute the software under the terms of the CeCILL-C + * license as circulated by CEA, CNRS and INRIA at the following URL + * "http://www.cecill.info". + * + * As a counterpart to the access to the source code and rights to copy, + * modify and redistribute granted by the license, users are provided only + * with a limited warranty and the software's author, the holder of the + * economic rights, and the successive licensors have only limited + * liability. + * + * In this respect, the user's attention is drawn to the risks associated + * with loading, using, modifying and/or developing or reproducing the + * software by the user in light of its specific status of free software, + * that may mean that it is complicated to manipulate, and that also + * therefore means that it is reserved for developers and experienced + * professionals having in-depth computer knowledge. Users are therefore + * encouraged to load and test the software's suitability as regards their + * requirements in conditions enabling the security of their systems and/or + * data to be ensured and, more generally, to use and operate it in the + * same conditions as regards security. + * + * The fact that you are presently reading this means that you have had + * knowledge of the CeCILL-C license and that you accept its terms. + */ +package fr.gouv.vitam.tools.resip.parameters; + +import fr.gouv.vitam.tools.resip.UseTestFiles; +import fr.gouv.vitam.tools.resip.event.EventBus; +import fr.gouv.vitam.tools.resip.event.SedaVersionChangedEvent; +import org.junit.jupiter.api.Test; + +import java.util.Properties; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * The type Treatment parameters test. + */ +class TreatmentParametersTest implements UseTestFiles { + + /** + * Test that preferences can be stored even if no SEDA version has been defined, + * which was breaking the load of a save file with a "->null" error message. + */ + @Test + void TestToPrefsWithoutDefinedSedaVersion() { + TreatmentParameters treatmentParameters = new TreatmentParameters(); + treatmentParameters.setDefaultPrefs(); + treatmentParameters.sedaVersion = null; + + Properties properties = Preferences.getInstance().getPrefProperties(); + properties.remove("treatmentParameters.seda2Version"); + + assertDoesNotThrow(() -> treatmentParameters.toPrefs(Preferences.getInstance())); + assertEquals("2.1", properties.getProperty("treatmentParameters.seda2Version")); + } + + /** + * Test that a null SEDA version event, published for example when loading a save + * file with an unknown version, doesn't erase the current SEDA version. + */ + @Test + void TestNullSedaVersionEventDoesntEraseCurrentVersion() { + TreatmentParameters treatmentParameters = new TreatmentParameters(); + treatmentParameters.setDefaultPrefs(); + treatmentParameters.sedaVersion = null; + + EventBus.publish(new SedaVersionChangedEvent(null)); + + assertDoesNotThrow(() -> treatmentParameters.toPrefs(Preferences.getInstance())); + } +} diff --git a/resip/src/test/java/fr/gouv/vitam/tools/resip/parameters/WorkTest.java b/resip/src/test/java/fr/gouv/vitam/tools/resip/parameters/WorkTest.java index d60a404a..75ff865b 100644 --- a/resip/src/test/java/fr/gouv/vitam/tools/resip/parameters/WorkTest.java +++ b/resip/src/test/java/fr/gouv/vitam/tools/resip/parameters/WorkTest.java @@ -37,9 +37,11 @@ */ package fr.gouv.vitam.tools.resip.parameters; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.module.SimpleModule; +import com.fasterxml.jackson.databind.node.ObjectNode; import fr.gouv.vitam.tools.resip.UseTestFiles; import fr.gouv.vitam.tools.resip.data.Work; import fr.gouv.vitam.tools.resip.utils.ResipException; @@ -48,17 +50,23 @@ import fr.gouv.vitam.tools.sedalib.core.DataObjectPackage; import fr.gouv.vitam.tools.sedalib.core.json.DataObjectPackageDeserializer; import fr.gouv.vitam.tools.sedalib.core.json.DataObjectPackageSerializer; +import fr.gouv.vitam.tools.sedalib.core.seda.SedaContext; +import fr.gouv.vitam.tools.sedalib.core.seda.SedaVersion; import fr.gouv.vitam.tools.sedalib.inout.importer.DiskToArchiveTransferImporter; import fr.gouv.vitam.tools.sedalib.utils.SEDALibException; import fr.gouv.vitam.tools.sedalib.utils.SEDALibProgressLogger; import org.junit.jupiter.api.Test; import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; +import java.util.zip.ZipOutputStream; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -141,4 +149,69 @@ void TestResipWorkSerializationDeserialization() mapper.writeValue(new FileOutputStream("./target/tmpJunit/junit_resiptWork_after.json"), dssip); assertEquals(ssip, sdssip); } + + /** + * Test that the SEDA version of a work is saved and read back, so that a save file + * created by Resip can be loaded again without any interface version switch. + * + * @throws ResipException the resip exception + * @throws IOException the io exception + */ + @Test + void TestResipWorkSedaVersionSerializationDeserialization() throws ResipException, IOException { + SedaVersion previousVersion = SedaContext.getVersion(); + try { + SedaContext.setVersion(SedaVersion.V2_2); + String file = "./target/tmpJunit/junit_resipWork_seda22.rsp"; + new File("./target/tmpJunit").mkdirs(); + + Work ow = new Work(new DataObjectPackage(), null, new ExportContext()); + ow.save(file); + + assertEquals(SedaVersion.V2_2, Work.getSeda2VersionFromFile(file)); + assertEquals(SedaVersion.V2_2, Work.createFromFile(file).getVersion()); + } finally { + SedaContext.setVersion(previousVersion); + } + } + + /** + * Test that a save file created before the SEDA version was serialized is still + * readable, and is considered as a SEDA 2.1 one. + * + * @throws ResipException the resip exception + * @throws IOException the io exception + */ + @Test + void TestResipWorkWithoutSedaVersionIsReadAsSeda21() throws ResipException, IOException { + SedaVersion previousVersion = SedaContext.getVersion(); + try { + SedaContext.setVersion(SedaVersion.V2_2); + String file = "./target/tmpJunit/junit_resipWork_seda22.rsp"; + String legacyFile = "./target/tmpJunit/junit_resipWork_legacy.rsp"; + new File("./target/tmpJunit").mkdirs(); + + new Work(new DataObjectPackage(), null, new ExportContext()).save(file); + removeVersionFromSaveFile(file, legacyFile); + + assertEquals(SedaVersion.V2_1, Work.getSeda2VersionFromFile(legacyFile)); + } finally { + SedaContext.setVersion(previousVersion); + } + } + + private void removeVersionFromSaveFile(String source, String destination) throws IOException { + ObjectMapper mapper = new ObjectMapper(); + JsonNode workNode; + try (ZipInputStream zis = new ZipInputStream(new FileInputStream(source))) { + zis.getNextEntry(); + workNode = mapper.readTree(zis); + } + ((ObjectNode) workNode).remove("version"); + try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(destination))) { + zos.putNextEntry(new ZipEntry("work.json")); + zos.write(mapper.writeValueAsBytes(workNode)); + zos.closeEntry(); + } + } }