diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/concepts/ConceptsCollectionServiceImpl.java b/src/main/java/fr/insee/rmes/bauhaus_services/concepts/ConceptsCollectionServiceImpl.java index 5d368bd76..6ccb4e9e5 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/concepts/ConceptsCollectionServiceImpl.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/concepts/ConceptsCollectionServiceImpl.java @@ -167,7 +167,7 @@ public void exportZipCollection(String ids, String acceptHeader, HttpServletResp private String getFileNameForExport(CollectionForExport collection, ConceptsCollectionsResources.Language lg){ String label = (lg == ConceptsCollectionsResources.Language.lg2 && collection.getPrefLabelLg2() != null) ? collection.getPrefLabelLg2() : collection.getPrefLabelLg1(); - return FilesUtils.reduceFileNameSize(CaseUtils.toCamelCase(label, false) + "-" + collection.getId(), maxLength); + return FilesUtils.generateFinalFileNameWithoutExtension(collection.getId() + "-" + label, maxLength); } } \ No newline at end of file diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/operations/OperationsDocumentationsImpl.java b/src/main/java/fr/insee/rmes/bauhaus_services/operations/OperationsDocumentationsImpl.java index 9a80b42ff..e929b9b73 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/operations/OperationsDocumentationsImpl.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/operations/OperationsDocumentationsImpl.java @@ -36,6 +36,8 @@ public class OperationsDocumentationsImpl extends RdfService implements Operati @Value("classpath:bauhaus-sims.json") org.springframework.core.io.Resource simsDefaultValue; + @Value("${fr.insee.rmes.bauhaus.filenames.maxlength}") int maxLength; + @Autowired DocumentationsUtils documentationsUtils; @@ -147,13 +149,13 @@ public ResponseEntity exportMetadataReport(String id, boolean includeE ErrorCodes.SIMS_EXPORT_WITHOUT_LANGUAGE, "at least one language must be selected for export", "in export of sims: " + id); - return documentationsExport.exportMetadataReport(id,includeEmptyMas, lg1, lg2, document, Constants.GOAL_RMES); + return documentationsExport.exportMetadataReport(id,includeEmptyMas, lg1, lg2, document, Constants.GOAL_RMES, maxLength); } @Override public ResponseEntity exportMetadataReportForLabel(String id) throws RmesException { - return documentationsExport.exportMetadataReport(id,true, true, false, false, Constants.GOAL_COMITE_LABEL); + return documentationsExport.exportMetadataReport(id,true, true, false, false, Constants.GOAL_COMITE_LABEL, maxLength); } @Override diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/DocumentationExport.java b/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/DocumentationExport.java index fcf3ee697..46eef73dd 100644 --- a/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/DocumentationExport.java +++ b/src/main/java/fr/insee/rmes/bauhaus_services/operations/documentations/DocumentationExport.java @@ -89,7 +89,7 @@ public DocumentationExport(@Value("${fr.insee.rmes.bauhaus.filenames.maxlength}" * the export will be a .ODT file. */ public ResponseEntity exportAsResponse(String id, Map xmlContent, String targetType, boolean includeEmptyFields, boolean lg1, - boolean lg2, boolean documents, String goal) throws RmesException { + boolean lg2, boolean documents, String goal, int maxLength) throws RmesException { PatternAndZip patternAndZip = PatternAndZip.of(goal); String parametersXML = XsltUtils.buildParams(lg1, lg2, includeEmptyFields, targetType); @@ -99,18 +99,18 @@ public ResponseEntity exportAsResponse(String id, Map JSONObject sims = this.documentationsUtils.getDocumentationByIdSims(id); if (documents) { - exporter = (xml, xsl, xmlPattern, zip, documentation) -> exportAsZip(sims, xml, xsl, xmlPattern, zip, documentation ); + exporter = (xml, xsl, xmlPattern, zip, documentation) -> exportAsZip(sims, xml, xsl, xmlPattern, zip, documentation, maxLength); } else{ - String fileName = sims.getString(Constants.LABEL_LG1); + String fileName = FilesUtils.generateFinalFileNameWithoutExtension(sims.getString(Constants.LABEL_LG1), maxLength); exporter = (xml, xsl, xmlPattern, zip, documentation) -> exportUtils.exportAsODT(fileName, xml, xsl, xmlPattern, zip, documentation ); } return export(exporter, xmlContent, patternAndZip); } - public ResponseEntity exportAsZip(JSONObject sims, Map xmlContent, String xslFile, String xmlPattern, String zip, String objectType) throws RmesException { + public ResponseEntity exportAsZip(JSONObject sims, Map xmlContent, String xslFile, String xmlPattern, String zip, String objectType, int maxLength) throws RmesException { String simsId = sims.getString("id"); logger.debug("Begin to download the SIMS {} with its documents", simsId); - String fileName = FilesUtils.removeAsciiCharacters(sims.getString(Constants.LABEL_LG1)); + String fileName = FilesUtils.generateFinalFileNameWithoutExtension(sims.getString(Constants.LABEL_LG1), maxLength); try { @@ -141,7 +141,7 @@ public ResponseEntity exportAsZip(JSONObject sims, Map FilesUtils.zipDirectory(simsDirectory.toFile()); logger.debug("Zip created for the SIMS {}", simsId); - HttpHeaders responseHeaders = HttpUtils.generateHttpHeaders(sims.getString(Constants.LABEL_LG1), FilesUtils.ZIP_EXTENSION, this.maxLength); + HttpHeaders responseHeaders = HttpUtils.generateHttpHeaders(fileName, FilesUtils.ZIP_EXTENSION); responseHeaders.set("X-Missing-Documents", String.join(",", missingDocuments)); Resource resource = new UrlResource(Paths.get(simsDirectory.toString(), simsDirectory.getFileName() + FilesUtils.ZIP_EXTENSION).toUri()); return ResponseEntity.ok() @@ -171,7 +171,7 @@ private Set exportRubricsDocuments(JSONObject sims, Path directory) thro if(!Files.exists(documentPath)){ missingDocuments.add(document.getString("id")); } else { - String documentFileName = FilesUtils.reduceFileNameSize(FilesUtils.removeAsciiCharacters(UriUtils.getLastPartFromUri(url)), maxLength); + String documentFileName = FilesUtils.generateFinalFileNameWithExtension(UriUtils.getLastPartFromUri(url), maxLength); try (InputStream inputStream = Files.newInputStream(documentPath)){ Path documentDirectory = Path.of(directory.toString(), "documents"); if (!Files.exists(documentDirectory)) { @@ -205,12 +205,12 @@ public ResponseEntity exportXmlFiles(Map xmlContent, Str } - public ResponseEntity exportMetadataReport(String id, Boolean includeEmptyMas, Boolean lg1, Boolean lg2, Boolean document, String goal) throws RmesException { + public ResponseEntity exportMetadataReport(String id, Boolean includeEmptyMas, Boolean lg1, Boolean lg2, Boolean document, String goal, int maxLength) throws RmesException { Map xmlContent = new HashMap<>(); String targetType = getXmlContent(id, xmlContent); String msdXML = buildShellSims(); xmlContent.put("msdFile", msdXML); - return exportAsResponse(id, xmlContent,targetType,includeEmptyMas,lg1,lg2, document, goal); + return exportAsResponse(id, xmlContent,targetType,includeEmptyMas,lg1,lg2, document, goal, maxLength); } diff --git a/src/main/java/fr/insee/rmes/utils/ExportUtils.java b/src/main/java/fr/insee/rmes/utils/ExportUtils.java index 8a373c6b8..511808879 100644 --- a/src/main/java/fr/insee/rmes/utils/ExportUtils.java +++ b/src/main/java/fr/insee/rmes/utils/ExportUtils.java @@ -51,7 +51,7 @@ public ResponseEntity exportAsODS(String fileName, Map private ResponseEntity exportAsFileByExtension(String fileName, Map xmlContent, String xslFile, String xmlPattern, String zip, String objectType, String extension) throws RmesException { logger.debug("Begin To export {} as Response", objectType); - fileName = fileName.replace(extension, ""); + fileName = FilesUtils.generateFinalFileNameWithoutExtension(fileName.replace(extension, ""), maxLength); InputStream input = exportAsInputStream(fileName, xmlContent, xslFile, xmlPattern, zip, objectType, extension); if (input == null) @@ -67,7 +67,7 @@ private ResponseEntity exportAsFileByExtension(String fileName, Map exportFilesAsResponse(Map xmlConte logger.debug("End To export temp files as Response"); - HttpHeaders responseHeaders = HttpUtils.generateHttpHeaders("xmlFiles", FilesUtils.ZIP_EXTENSION, this.maxLength); + HttpHeaders responseHeaders = HttpUtils.generateHttpHeaders("xmlFiles", FilesUtils.ZIP_EXTENSION); Resource resource = new UrlResource(Paths.get(tempDir.toString(), tempDir.getFileName() + FilesUtils.ZIP_EXTENSION).toUri()); return ResponseEntity.ok() .headers(responseHeaders) diff --git a/src/test/java/fr/insee/rmes/bauhaus_services/operations/OperationsDocumentationsImplTest.java b/src/test/java/fr/insee/rmes/bauhaus_services/operations/OperationsDocumentationsImplTest.java index 463edc227..07627adef 100644 --- a/src/test/java/fr/insee/rmes/bauhaus_services/operations/OperationsDocumentationsImplTest.java +++ b/src/test/java/fr/insee/rmes/bauhaus_services/operations/OperationsDocumentationsImplTest.java @@ -42,7 +42,7 @@ public void testExportMetadataReport_Success() throws RmesException { boolean document = true; Resource resource = new ByteArrayResource("Mocked Document Content".getBytes()); - when(documentationsExport.exportMetadataReport(id, includeEmptyMas, lg1, lg2, document, Constants.GOAL_RMES)) + when(documentationsExport.exportMetadataReport(id, includeEmptyMas, lg1, lg2, document, Constants.GOAL_RMES, 0)) .thenReturn(ResponseEntity.ok().body(resource)); ResponseEntity response = metadataReportService.exportMetadataReport(id, includeEmptyMas, lg1, lg2, document); diff --git a/src/test/java/fr/insee/rmes/bauhaus_services/operations/documentations/DocumentationExportTest.java b/src/test/java/fr/insee/rmes/bauhaus_services/operations/documentations/DocumentationExportTest.java index c3e1f10ee..5c6456e73 100644 --- a/src/test/java/fr/insee/rmes/bauhaus_services/operations/documentations/DocumentationExportTest.java +++ b/src/test/java/fr/insee/rmes/bauhaus_services/operations/documentations/DocumentationExportTest.java @@ -85,11 +85,11 @@ public void testExportAsZip_success() throws Exception { InputStream inputStreamMock = mock(InputStream.class); - when(exportUtils.exportAsInputStream(eq("simsLabel"), eq(xmlContent), eq(xslFile), eq(xmlPattern), eq(zip), eq(objectType), eq(FilesUtils.ODT_EXTENSION))) + when(exportUtils.exportAsInputStream(eq("simslabel"), eq(xmlContent), eq(xslFile), eq(xmlPattern), eq(zip), eq(objectType), eq(FilesUtils.ODT_EXTENSION))) .thenReturn(inputStreamMock); when(inputStreamMock.readAllBytes()).thenReturn(new byte[0]); - ResponseEntity response = documentationExport.exportAsZip(sims, xmlContent, xslFile, xmlPattern, zip, objectType); + ResponseEntity response = documentationExport.exportAsZip(sims, xmlContent, xslFile, xmlPattern, zip, objectType, 50); assertEquals(HttpStatus.OK, response.getStatusCode()); assertNotNull(response.getBody()); @@ -116,7 +116,7 @@ public void testExportMetadataReport_Success_WithoutDocuments_Label() throws Rm when(documentationsUtils.getFullSimsForXml(id)).thenReturn(new Documentation()); when(exportUtils.exportAsODT(any(), any(), any(), any(), any(), any())).thenReturn(ResponseEntity.ok().body(resource)); - ResponseEntity response = documentationExport.exportMetadataReport(id, includeEmptyMas, lg1, lg2, document, goal); + ResponseEntity response = documentationExport.exportMetadataReport(id, includeEmptyMas, lg1, lg2, document, goal, 100); assertEquals(ResponseEntity.ok().body(resource), response); } @@ -135,7 +135,7 @@ public void testExportMetadataReport_Failure_UnknownGoal() throws RmesException when(documentationsUtils.getFullSimsForXml(id)).thenReturn(new Documentation()); RmesBadRequestException exception = assertThrows(RmesBadRequestException.class, () -> { - documentationExport.exportMetadataReport(id, includeEmptyMas, lg1, lg2, document, goal); + documentationExport.exportMetadataReport(id, includeEmptyMas, lg1, lg2, document, goal, 100); }); assertEquals("{\"message\":\"The goal is unknown\"}", exception.getDetails()); diff --git a/src/test/java/fr/insee/rmes/utils/ExportUtilsTest.java b/src/test/java/fr/insee/rmes/utils/ExportUtilsTest.java deleted file mode 100644 index 10bce820a..000000000 --- a/src/test/java/fr/insee/rmes/utils/ExportUtilsTest.java +++ /dev/null @@ -1,7 +0,0 @@ -package fr.insee.rmes.utils; - -import static org.junit.jupiter.api.Assertions.*; - -class ExportUtilsTest { - -} \ No newline at end of file