Search in sources :

Example 1 with DataExplorerDownloadHandler

use of org.molgenis.dataexplorer.download.DataExplorerDownloadHandler in project molgenis by molgenis.

the class DataExplorerController method download.

@PostMapping("/download")
public void download(@RequestParam("dataRequest") String dataRequestStr, HttpServletResponse response) throws IOException {
    DataExplorerDownloadHandler download = new DataExplorerDownloadHandler(dataService, attrMetaFactory);
    // Workaround because binding with @RequestBody is not possible:
    // http://stackoverflow.com/a/9970672
    dataRequestStr = URLDecoder.decode(dataRequestStr, "UTF-8");
    LOG.info("Download request: [" + dataRequestStr + "]");
    DataRequest dataRequest = gson.fromJson(dataRequestStr, DataRequest.class);
    final String fileName = getDownloadFilename(dataRequest.getEntityName(), LocalDateTime.now(), dataRequest.getDownloadType());
    ServletOutputStream outputStream;
    switch(dataRequest.getDownloadType()) {
        case DOWNLOAD_TYPE_CSV:
            response.setContentType("text/csv");
            response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
            outputStream = response.getOutputStream();
            download.writeToCsv(dataRequest, outputStream, ',');
            break;
        case DOWNLOAD_TYPE_XLSX:
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
            outputStream = response.getOutputStream();
            download.writeToExcel(dataRequest, outputStream);
            break;
        default:
            throw new UnexpectedEnumException(dataRequest.getDownloadType());
    }
}
Also used : UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) ServletOutputStream(javax.servlet.ServletOutputStream) DataExplorerDownloadHandler(org.molgenis.dataexplorer.download.DataExplorerDownloadHandler)

Aggregations

ServletOutputStream (javax.servlet.ServletOutputStream)1 DataExplorerDownloadHandler (org.molgenis.dataexplorer.download.DataExplorerDownloadHandler)1 UnexpectedEnumException (org.molgenis.util.UnexpectedEnumException)1