Search in sources :

Example 1 with ConnectionCallback

use of javax.ws.rs.container.ConnectionCallback 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)

Example 2 with ConnectionCallback

use of javax.ws.rs.container.ConnectionCallback in project cxf by apache.

the class BookContinuationStore method handleClientDisconnects.

@GET
@Path("/disconnect")
public void handleClientDisconnects(@Suspended AsyncResponse response) {
    response.setTimeout(0, TimeUnit.SECONDS);
    response.register(new ConnectionCallback() {

        @Override
        public void onDisconnect(AsyncResponse disconnected) {
            System.out.println("ConnectionCallback: onDisconnect, client disconnects");
        }
    });
    try {
        Thread.sleep(3000);
    } catch (InterruptedException ex) {
    // ignore
    }
    response.resume(books.values().toString());
}
Also used : ConnectionCallback(javax.ws.rs.container.ConnectionCallback) AsyncResponse(javax.ws.rs.container.AsyncResponse) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

AsyncResponse (javax.ws.rs.container.AsyncResponse)2 ConnectionCallback (javax.ws.rs.container.ConnectionCallback)2 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 GET (javax.ws.rs.GET)1 NotSupportedException (javax.ws.rs.NotSupportedException)1 Path (javax.ws.rs.Path)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 HttpHeaders (javax.ws.rs.core.HttpHeaders)1 Response (javax.ws.rs.core.Response)1