Search in sources :

Example 1 with DATA

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));
    }
}
Also used : AssetStorageService(org.openremote.manager.asset.AssetStorageService) ZipOutputStream(java.util.zip.ZipOutputStream) DatapointInterval(org.openremote.model.datapoint.DatapointInterval) ScheduledFuture(java.util.concurrent.ScheduledFuture) AttributeRef(org.openremote.model.attribute.AttributeRef) AssetDatapointResource(org.openremote.model.datapoint.AssetDatapointResource) LocalDateTime(java.time.LocalDateTime) DATA(org.openremote.model.syslog.SyslogCategory.DATA) JSON(org.openremote.model.util.ValueUtil.JSON) ManagerWebResource(org.openremote.manager.web.ManagerWebResource) Level(java.util.logging.Level) Attribute(org.openremote.model.attribute.Attribute) BadRequestException(javax.ws.rs.BadRequestException) SyslogCategory(org.openremote.model.syslog.SyslogCategory) ZipEntry(java.util.zip.ZipEntry) NotSupportedException(javax.ws.rs.NotSupportedException) ManagerIdentityService(org.openremote.manager.security.ManagerIdentityService) Asset(org.openremote.model.asset.Asset) AsyncResponse(javax.ws.rs.container.AsyncResponse) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Instant(java.time.Instant) Logger(java.util.logging.Logger) BeanParam(javax.ws.rs.BeanParam) ZoneId(java.time.ZoneId) ValueDatapoint(org.openremote.model.datapoint.ValueDatapoint) IOUtils(org.apache.commons.io.IOUtils) HttpHeaders(javax.ws.rs.core.HttpHeaders) Response(javax.ws.rs.core.Response) java.io(java.io) TimerService(org.openremote.container.timer.TimerService) DatapointPeriod(org.openremote.model.datapoint.DatapointPeriod) WebApplicationException(javax.ws.rs.WebApplicationException) ConnectionCallback(javax.ws.rs.container.ConnectionCallback) RequestParams(org.openremote.model.http.RequestParams) AttributeRef(org.openremote.model.attribute.AttributeRef) WebApplicationException(javax.ws.rs.WebApplicationException) ZipEntry(java.util.zip.ZipEntry) BadRequestException(javax.ws.rs.BadRequestException) NotSupportedException(javax.ws.rs.NotSupportedException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) WebApplicationException(javax.ws.rs.WebApplicationException) ZipOutputStream(java.util.zip.ZipOutputStream) BadRequestException(javax.ws.rs.BadRequestException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 java.io (java.io)1 Instant (java.time.Instant)1 LocalDateTime (java.time.LocalDateTime)1 ZoneId (java.time.ZoneId)1 ScheduledFuture (java.util.concurrent.ScheduledFuture)1 Level (java.util.logging.Level)1 Logger (java.util.logging.Logger)1 ZipEntry (java.util.zip.ZipEntry)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 BadRequestException (javax.ws.rs.BadRequestException)1 BeanParam (javax.ws.rs.BeanParam)1 NotSupportedException (javax.ws.rs.NotSupportedException)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 AsyncResponse (javax.ws.rs.container.AsyncResponse)1 ConnectionCallback (javax.ws.rs.container.ConnectionCallback)1 HttpHeaders (javax.ws.rs.core.HttpHeaders)1 Response (javax.ws.rs.core.Response)1 IOUtils (org.apache.commons.io.IOUtils)1 TimerService (org.openremote.container.timer.TimerService)1