Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,10 @@
import org.openremote.model.http.RequestParams;
import org.openremote.model.security.ClientRole;
import org.openremote.model.syslog.SyslogCategory;
import org.openremote.model.util.UniqueIdentifierGenerator;
import org.openremote.model.value.MetaItemType;

import java.io.File;
import java.io.FileInputStream;
import java.util.concurrent.ScheduledFuture;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
Expand Down Expand Up @@ -206,47 +205,43 @@ public void getDatapointExport(AsyncResponse asyncResponse, String attributeRefs

DATA_EXPORT_LOG.info("User '" + getUsername() + "' started data export for " + attributeRefsString + " from " + fromTimestamp + " to " + toTimestamp + " in format " + format);

ScheduledFuture<File> exportFuture = assetDatapointService.exportDatapoints(attributeRefs, fromTimestamp, toTimestamp, format);
PipedInputStream pipedInputStream = assetDatapointService.exportDatapoints(attributeRefs, fromTimestamp, toTimestamp, format);

asyncResponse.register((ConnectionCallback) disconnected -> exportFuture.cancel(true));

File exportFile = null;

try {
exportFile = exportFuture.get();

try (FileInputStream fin = new FileInputStream(exportFile);
ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream())) {

ZipEntry zipEntry = new ZipEntry(exportFile.getName());
zipOut.putNextEntry(zipEntry);
IOUtils.copy(fin, zipOut);
zipOut.closeEntry();
asyncResponse.register((ConnectionCallback) disconnected -> {
try {
pipedInputStream.close();
} catch (IOException e) {
DATA_EXPORT_LOG.log(Level.SEVERE, "Could not close input stream: ", e);
throw new RuntimeException(e);
}
});

response.setContentType("application/zip");
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"dataexport.zip\"");
try (InputStream fin = pipedInputStream;
ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream())) {

asyncResponse.resume(
response
);
} catch (Exception ex) {
exportFuture.cancel(true);
String fileName = UniqueIdentifierGenerator.generateId() + ".csv";
ZipEntry zipEntry = new ZipEntry(fileName);
zipOut.putNextEntry(zipEntry);
IOUtils.copy(fin, zipOut);
zipOut.closeEntry();
} catch (IOException ex) {
asyncResponse.resume(new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR));
DATA_EXPORT_LOG.log(Level.SEVERE, "Exception in ScheduledFuture: ", ex);
} finally {
if (exportFile != null && exportFile.exists()) {
try {
exportFile.delete();
} catch (Exception e) {
DATA_EXPORT_LOG.log(Level.SEVERE, "Failed to delete temporary export file: " + exportFile.getPath(), e);
}
}
DATA_EXPORT_LOG.log(Level.SEVERE, "Zip exception: ", ex);
}

response.setContentType("application/zip");
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"dataexport.zip\"");

asyncResponse.resume(
response
);
} catch (JsonProcessingException ex) {
asyncResponse.resume(new BadRequestException(ex));
} catch (DatapointQueryTooLargeException dqex) {
asyncResponse.resume(new WebApplicationException(dqex, Response.Status.REQUEST_ENTITY_TOO_LARGE));
} catch (IOException ex) {
asyncResponse.resume(new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR));
DATA_EXPORT_LOG.log(Level.SEVERE, "Failed to create piped output stream: ", ex);
}
}
}
Loading
Loading