From c12ff2397819baaff55acc16cba93ae99eedf7ef Mon Sep 17 00:00:00 2001 From: Ruslan Forostianov Date: Sat, 14 Dec 2024 18:15:32 +0100 Subject: [PATCH] Stream study generate zip content --- .../export/ClinicalAttributeDataWriter.java | 2 ++ .../file/export/KeyValueMetadataWriter.java | 1 + .../file/export/MafRecordWriter.java | 1 + .../service/impl/ExportService.java | 7 +++--- .../org/cbioportal/web/ExportController.java | 22 +++++++++---------- 5 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/cbioportal/file/export/ClinicalAttributeDataWriter.java b/src/main/java/org/cbioportal/file/export/ClinicalAttributeDataWriter.java index 84973cb5882..0861b9906a4 100644 --- a/src/main/java/org/cbioportal/file/export/ClinicalAttributeDataWriter.java +++ b/src/main/java/org/cbioportal/file/export/ClinicalAttributeDataWriter.java @@ -41,6 +41,7 @@ public void write(ClinicalAttributeData clinicalAttributeData) { private void writeRow(Iterable row) { try { writer.write(composeRow(row)); + writer.flush(); } catch (IOException e) { throw new RuntimeException(e); } @@ -49,6 +50,7 @@ private void writeRow(Iterable row) { private void writeCommentsRow(Iterable row) { try { writer.write("#" + composeRow(row)); + writer.flush(); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/src/main/java/org/cbioportal/file/export/KeyValueMetadataWriter.java b/src/main/java/org/cbioportal/file/export/KeyValueMetadataWriter.java index 6d751092d86..191f319f97b 100644 --- a/src/main/java/org/cbioportal/file/export/KeyValueMetadataWriter.java +++ b/src/main/java/org/cbioportal/file/export/KeyValueMetadataWriter.java @@ -71,6 +71,7 @@ private void write(LinkedHashMap metadata) { metadata.forEach((key, value) -> { try { writer.write(composeKeyValueLine(key, value)); + writer.flush(); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/src/main/java/org/cbioportal/file/export/MafRecordWriter.java b/src/main/java/org/cbioportal/file/export/MafRecordWriter.java index f9feb2f1766..2cd27bf3a0c 100644 --- a/src/main/java/org/cbioportal/file/export/MafRecordWriter.java +++ b/src/main/java/org/cbioportal/file/export/MafRecordWriter.java @@ -69,6 +69,7 @@ public void write(Iterator maf) { private void writeRow(Iterable row) { try { writer.write(composeRow(row)); + writer.flush(); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/src/main/java/org/cbioportal/service/impl/ExportService.java b/src/main/java/org/cbioportal/service/impl/ExportService.java index c1cac6120d2..23e7b428249 100644 --- a/src/main/java/org/cbioportal/service/impl/ExportService.java +++ b/src/main/java/org/cbioportal/service/impl/ExportService.java @@ -12,6 +12,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.OutputStream; import java.io.StringWriter; import java.util.HashMap; import java.util.List; @@ -39,7 +40,7 @@ public ExportService(StudyService studyService, this.mafRecordFetcher = mafRecordFetcher; } - public ByteArrayOutputStream exportStudyDataToZip(String studyId) throws IOException { + public void exportStudyDataToZip(OutputStream outputStream, String studyId) throws IOException { List studies = studyService.fetchStudies(List.of(studyId), "DETAILED"); Map> studyToSampleMap = new HashMap<>(); if (studies.isEmpty()) { @@ -50,8 +51,7 @@ public ByteArrayOutputStream exportStudyDataToZip(String studyId) throws IOExcep List samples = sampleService.getAllSamplesInStudies(List.of(studyId), "ID", null, null, null, null); studyToSampleMap.put(studyId, samples.stream().map(Sample::getStableId).collect(Collectors.toSet())); } - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - try (ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream)) { + try (ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream)) { // Add files to the ZIP StringWriter mafRecordsStringWriter = new StringWriter(); MafRecordWriter mafRecordWriter = new MafRecordWriter(mafRecordsStringWriter); @@ -59,7 +59,6 @@ public ByteArrayOutputStream exportStudyDataToZip(String studyId) throws IOExcep mafRecordWriter.write(mafRecordFetcher.fetch(studyToSampleMap)); addFileToZip(zipOutputStream, "data_mutation.txt", mafRecordsStringWriter.toString().getBytes()); } - return byteArrayOutputStream; } private void addFileToZip(ZipOutputStream zipOutputStream, String fileName, byte[] fileContent) throws IOException { diff --git a/src/main/java/org/cbioportal/web/ExportController.java b/src/main/java/org/cbioportal/web/ExportController.java index 73735b5e2cd..6282908d2cf 100644 --- a/src/main/java/org/cbioportal/web/ExportController.java +++ b/src/main/java/org/cbioportal/web/ExportController.java @@ -1,5 +1,6 @@ package org.cbioportal.web; +import jakarta.servlet.http.HttpServletResponse; import org.cbioportal.service.impl.ExportService; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; @@ -11,6 +12,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.OutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -26,17 +28,13 @@ public ExportController(ExportService exportService) { //TODO make it work for virtual studies as well //@PreAuthorize("hasPermission(#studyId, 'CancerStudyId', T(org.cbioportal.utils.security.AccessLevel).READ)") @GetMapping("/export/study/{studyId}.zip") - public ResponseEntity downloadStudyData(@PathVariable String studyId) throws IOException { - ByteArrayOutputStream byteArrayOutputStream = exportService.exportStudyDataToZip(studyId); - - // Build the response - byte[] zipBytes = byteArrayOutputStream.toByteArray(); - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(new MediaType("application", "zip")); - headers.setContentDispositionFormData("attachment", studyId + ".zip"); - - return ResponseEntity.ok() - .headers(headers) - .body(zipBytes); + public void downloadStudyData(HttpServletResponse response, @PathVariable String studyId) throws IOException { + + response.setContentType(("application/zip")); + response.setHeader("Content-Disposition", "attachment; filename=\""+ studyId +".zip\""); + + try (OutputStream out = response.getOutputStream()) { + exportService.exportStudyDataToZip(out, studyId); + } } }