Skip to content

Commit

Permalink
Stream study generate zip content
Browse files Browse the repository at this point in the history
  • Loading branch information
forus committed Dec 14, 2024
1 parent 84cb1d4 commit c12ff23
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public void write(ClinicalAttributeData clinicalAttributeData) {
private void writeRow(Iterable<String> row) {
try {
writer.write(composeRow(row));
writer.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand All @@ -49,6 +50,7 @@ private void writeRow(Iterable<String> row) {
private void writeCommentsRow(Iterable<String> row) {
try {
writer.write("#" + composeRow(row));
writer.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ private void write(LinkedHashMap<String, String> metadata) {
metadata.forEach((key, value) -> {
try {
writer.write(composeKeyValueLine(key, value));
writer.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public void write(Iterator<MafRecord> maf) {
private void writeRow(Iterable<String> row) {
try {
writer.write(composeRow(row));
writer.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/org/cbioportal/service/impl/ExportService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<CancerStudy> studies = studyService.fetchStudies(List.of(studyId), "DETAILED");
Map<String, Set<String>> studyToSampleMap = new HashMap<>();
if (studies.isEmpty()) {
Expand All @@ -50,16 +51,14 @@ public ByteArrayOutputStream exportStudyDataToZip(String studyId) throws IOExcep
List<Sample> 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);
//TODO do not produce the file if no data has been retrieved
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 {
Expand Down
22 changes: 10 additions & 12 deletions src/main/java/org/cbioportal/web/ExportController.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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<byte[]> 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);
}
}
}

0 comments on commit c12ff23

Please sign in to comment.