Skip to content
Snippets Groups Projects

chore[aou-1869]: refactor export bibliograpy endpoint

Merged Homada.Boumedane requested to merge aou-1869_update_export_bibliograpy_controller into master
1 unresolved thread
@@ -23,7 +23,10 @@
package ch.unige.aou.controller.admin;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -40,6 +43,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
import ch.unige.solidify.exception.SolidifyHttpErrorException;
import ch.unige.solidify.rest.FacetPage;
@@ -82,7 +86,7 @@ public class ExportMetadataController {
}
@GetMapping(value = "/" + AouActionName.EXPORT)
public HttpEntity<String> getExportBibliography(
public HttpEntity<StreamingResponseBody> getExportBibliography(
@RequestParam(name = "searches", required = false) String commaSeparatedSearchIds,
@RequestParam(name = "publicationIds", required = false) String commaSeparatedPublicationIds,
@RequestParam(name = "days", required = false) String searchFrom,
@@ -114,20 +118,42 @@ public class ExportMetadataController {
if (!results.getContent().isEmpty()) {
result = this.exportMetadataService.exportMetadata(exportFormat, results.getContent());
}
HttpHeaders headers = this.getHeaders(exportFormat);
return new ResponseEntity<>(result, headers, HttpStatus.OK);
// convert the string result to Inputstream
InputStream is = new BufferedInputStream(new ByteArrayInputStream(result.getBytes(StandardCharsets.UTF_8)));
// build the responseBody as stream
StreamingResponseBody responseBody = outputStream -> {
Please register or sign in to reply
int numberOfBytesToWrite;
byte[] data = new byte[1024];
while ((numberOfBytesToWrite = is.read(data, 0, data.length)) != -1) {
outputStream.write(data, 0, numberOfBytesToWrite);
}
is.close();
};
return new ResponseEntity(responseBody, headers, HttpStatus.OK);
}
public HttpHeaders getHeaders(ExportFormat format) {
private HttpHeaders getHeaders(ExportFormat format) {
HttpHeaders headers = new HttpHeaders();
switch (format) {
case XML: headers.setContentType(MediaType.APPLICATION_XML);
break;
case JSON: headers.setContentType(MediaType.APPLICATION_JSON);
break;
case CSV: headers.setContentType(MediaType.TEXT_PLAIN);
break;
case XML -> {
headers.setContentType(MediaType.APPLICATION_XML);
headers.setContentDispositionFormData("attachment", "bibliography.xml");
}
case JSON -> {
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setContentDispositionFormData("attachment", "bibliography.json");
}
case CSV -> {
headers.setContentType(MediaType.TEXT_PLAIN);
headers.setContentDispositionFormData("attachment", "bibliography.txt");
}
}
return headers;
}
Loading