From 99a8b9788e1b1062c8d8a93d4b5d4b65f3d34ce2 Mon Sep 17 00:00:00 2001 From: deiflaender Date: Fri, 22 Jul 2022 13:29:22 +0200 Subject: [PATCH] RED-4686: Stream objects to storage --- redaction-service-v1/pom.xml | 2 +- .../storage/RedactionStorageService.java | 14 ++++++----- .../FileSystemBackedStorageService.java | 24 +++++++++++++++++-- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/redaction-service-v1/pom.xml b/redaction-service-v1/pom.xml index 1e0a49d3..c6e4a697 100644 --- a/redaction-service-v1/pom.xml +++ b/redaction-service-v1/pom.xml @@ -37,7 +37,7 @@ com.iqser.red platform-commons-dependency - 1.14.0 + 1.15.0 import pom diff --git a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/storage/RedactionStorageService.java b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/storage/RedactionStorageService.java index 15664f0b..28dc7554 100644 --- a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/storage/RedactionStorageService.java +++ b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/storage/RedactionStorageService.java @@ -55,24 +55,26 @@ public class RedactionStorageService { @Timed("redactmanager_storeObject") public void storeObject(String dossierId, String fileId, FileType fileType, Object any) { - var bytes = serializeObject(any); - storageService.storeObject(StorageIdUtils.getStorageId(dossierId, fileId, fileType), bytes); + try(var byteArrayOutputStream = serializeObject(any)){ + storageService.storeObject(StorageIdUtils.getStorageId(dossierId, fileId, fileType), byteArrayOutputStream); + } } @SneakyThrows @Timed("redactmanager_serializeObject") - private byte[] serializeObject(Object any) { + private ByteArrayOutputStream serializeObject(Object any) { try { var baos = new ByteArrayOutputStream(); dslJson.serialize(any, baos); - return baos.toByteArray(); + return baos; } catch (com.dslplatform.json.SerializationException e) { // Fails on file 49 Cyprodinil - EU AIR3 - MCA Section 8 Supplement - Ecotoxicological studies on the active substance.pdf - var bytes = objectMapper.writeValueAsBytes(any); + var baos = new ByteArrayOutputStream(); + objectMapper.writeValue(baos, any); dslJson.newWriter(); - return bytes; + return baos; } } diff --git a/redaction-service-v1/redaction-service-server-v1/src/test/java/com/iqser/red/service/redaction/v1/server/FileSystemBackedStorageService.java b/redaction-service-v1/redaction-service-server-v1/src/test/java/com/iqser/red/service/redaction/v1/server/FileSystemBackedStorageService.java index b82da1c5..da9d1b32 100644 --- a/redaction-service-v1/redaction-service-server-v1/src/test/java/com/iqser/red/service/redaction/v1/server/FileSystemBackedStorageService.java +++ b/redaction-service-v1/redaction-service-server-v1/src/test/java/com/iqser/red/service/redaction/v1/server/FileSystemBackedStorageService.java @@ -7,12 +7,14 @@ import lombok.SneakyThrows; import org.apache.commons.io.IOUtils; import org.springframework.core.io.InputStreamResource; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.util.*; import java.util.stream.Collectors; +import java.util.zip.GZIPOutputStream; public class FileSystemBackedStorageService implements StorageService{ @@ -63,7 +65,9 @@ public class FileSystemBackedStorageService implements StorageService{ public void storeObject(String objectId, byte[] data) { File tempFile = File.createTempFile("storage", objectId.replace("/","-")); - IOUtils.write(data, new FileOutputStream(tempFile)); + try(var fileOutputStream = new FileOutputStream(tempFile)) { + IOUtils.write(data, fileOutputStream); + } dataMap.put(objectId, tempFile); } @@ -73,11 +77,27 @@ public class FileSystemBackedStorageService implements StorageService{ public void storeObject(String objectId, InputStream stream) { File tempFile = File.createTempFile("test", ".tmp"); - IOUtils.copy(stream, new FileOutputStream(tempFile)); + try(var fileOutputStream = new FileOutputStream(tempFile)) { + IOUtils.copy(stream, fileOutputStream); + } dataMap.put(objectId, tempFile); } + + @Override + @SneakyThrows + public void storeObject(String objectId, ByteArrayOutputStream stream) { + + File tempFile = File.createTempFile("test", ".tmp"); + try(var fileOutputStream = new FileOutputStream(tempFile)){ + stream.writeTo(fileOutputStream); + } + + dataMap.put(objectId, tempFile); + } + + public void clearStorage() { this.dataMap.forEach((k, v) -> { v.delete();