use of org.openremote.model.syslog.SyslogCategory.DATA in project openremote by openremote.
the class AssetDatapointResourceImpl method getDatapointExport.
@Override
public void getDatapointExport(AsyncResponse asyncResponse, String attributeRefsString, long fromTimestamp, long toTimestamp) {
try {
AttributeRef[] attributeRefs = JSON.readValue(attributeRefsString, AttributeRef[].class);
for (AttributeRef attributeRef : attributeRefs) {
if (isRestrictedUser() && !assetStorageService.isUserAsset(getUserId(), attributeRef.getId())) {
throw new WebApplicationException(Response.Status.FORBIDDEN);
}
Asset<?> asset = assetStorageService.find(attributeRef.getId(), true);
if (asset == null) {
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
if (!isTenantActiveAndAccessible(asset.getRealm())) {
DATA_EXPORT_LOG.info("Forbidden access for user '" + getUsername() + "': " + asset);
throw new WebApplicationException(Response.Status.FORBIDDEN);
}
asset.getAttribute(attributeRef.getName()).orElseThrow(() -> new WebApplicationException(Response.Status.NOT_FOUND));
}
DATA_EXPORT_LOG.info("User '" + getUsername() + "' started data export for " + attributeRefsString + " from " + fromTimestamp + " to " + toTimestamp);
ScheduledFuture<File> exportFuture = assetDatapointService.exportDatapoints(attributeRefs, fromTimestamp, toTimestamp);
asyncResponse.register((ConnectionCallback) disconnected -> {
exportFuture.cancel(true);
});
File exportFile = null;
try {
exportFile = exportFuture.get();
ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream());
FileInputStream fin = new FileInputStream(exportFile);
ZipEntry zipEntry = new ZipEntry(exportFile.getName());
zipOut.putNextEntry(zipEntry);
IOUtils.copy(fin, zipOut);
zipOut.closeEntry();
zipOut.close();
fin.close();
response.setContentType("application/zip");
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"dataexport.zip\"");
asyncResponse.resume(response);
} catch (Exception ex) {
exportFuture.cancel(true);
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);
}
}
}
} catch (JsonProcessingException ex) {
asyncResponse.resume(new BadRequestException(ex));
}
}
Aggregations