Search in sources :

Example 26 with AsyncResponse

use of javax.ws.rs.container.AsyncResponse 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 27 with AsyncResponse

use of javax.ws.rs.container.AsyncResponse in project jersey by jersey.

the class Jersey2812ITCase method asyncSuspendedResourceDoesNotGetStuck.

/**
     * Tests whether the server-side thread that is processing a http request to the servlet-filter-based Jersey setup ends up
     * stuck or returned back to the pool of available threads.
     * <p/>
     * This test prevents a regression reported in JERSEY-2812.
     * <p/>
     * When the {@link javax.ws.rs.container.AsyncResponse} was left intact in the RESTful resource (as implemented in {@link
     * TestWaitResource#waitForEvent(AsyncResponse, HttpServletRequest, String)}), the server-side Jersey thread ended up in
     * {@link org.glassfish.jersey.servlet.internal.ResponseWriter#getResponseContext()} blocked because of the resolution of http
     * response status from {@link org.glassfish.jersey.servlet.ServletContainer#doFilter(HttpServletRequest, HttpServletResponse,
     * FilterChain, String, String, String)}
     * <p/>
     * This test uses a separate thread to call {@code /async/wait/{uuid}} resource which blocks until the {@code
     * /async/release/{uuid}} is called. In the meantime the JUnit thread calls {@code /async/await/{uuid}} to discover whether
     * the server-side thread processing the request to {@code /async/await/{uuid}/started} did start processing of the request.
     * Consecutively, the JUnit thread calls {@code /async/await/{uuid}/finished} with a timeout {@code #WAIT_TIMEOUT} to discover
     * whether the server-side thread got stuck (which is what JERSEY-2812 reported) or not.
     *
     * @throws Exception
     */
@Test
public void asyncSuspendedResourceDoesNotGetStuck() throws Exception {
    // [1] wait for the /async/wait request to be processed
    final Response startResponse = target("/asyncTest/async/await").path(uuid).path("started").queryParam("millis", WAIT_TIMEOUT).request().get();
    assertTrue("The server-side thread handling the request to /async/wait didn't start in timely fashion. " + "This error indicates this test is not executed / designed properly rather than a regression in " + "JERSEY-2812 fix.", startResponse.readEntity(Boolean.class));
    // [2] wait for the /async/wait request to finish
    final Response finishResponse = target("/asyncTest/async/await").path(uuid).path("finished").queryParam("millis", WAIT_TIMEOUT).request().get();
    assertTrue("The thread processing the /async/wait request did not respond in timely fashion. " + "Memory leak / thread got stuck detected!", finishResponse.readEntity(Boolean.class));
    // [3] release the blocked http call to /async/wait
    final String releaseResponse = target("/asyncTest/async/release").path(uuid).request().post(null, String.class);
    assertEquals("RELEASED", releaseResponse);
    // [4] test whether everything ended as expected
    executorService.shutdown();
    assertTrue("The test thread did not finish in timely fashion!", executorService.awaitTermination(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
    assertEquals("async-OK-" + uuid, asyncResult.get());
}
Also used : AsyncResponse(javax.ws.rs.container.AsyncResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(javax.ws.rs.core.Response) Test(org.junit.Test) AbstractAsyncJerseyTest(org.glassfish.jersey.tests.integration.async.AbstractAsyncJerseyTest)

Example 28 with AsyncResponse

use of javax.ws.rs.container.AsyncResponse in project jersey by jersey.

the class FlowableAgentResource method flowable.

@GET
public void flowable(@Suspended final AsyncResponse async) {
    final long time = System.nanoTime();
    final Queue<String> errors = new ConcurrentLinkedQueue<>();
    Flowable.just(new AgentResponse()).zipWith(visited(errors), (agentResponse, visited) -> {
        agentResponse.setVisited(visited);
        return agentResponse;
    }).zipWith(recommended(errors), (agentResponse, recommendations) -> {
        agentResponse.setRecommended(recommendations);
        return agentResponse;
    }).observeOn(Schedulers.io()).subscribe(agentResponse -> {
        agentResponse.setProcessingTime((System.nanoTime() - time) / 1000000);
        async.resume(agentResponse);
    }, async::resume);
}
Also used : Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) AsyncResponse(javax.ws.rs.container.AsyncResponse) Path(javax.ws.rs.Path) Singleton(javax.inject.Singleton) Suspended(javax.ws.rs.container.Suspended) Calculation(org.glassfish.jersey.examples.rx.domain.Calculation) Uri(org.glassfish.jersey.server.Uri) Destination(org.glassfish.jersey.examples.rx.domain.Destination) GenericType(javax.ws.rs.core.GenericType) Forecast(org.glassfish.jersey.examples.rx.domain.Forecast) List(java.util.List) Flowable(io.reactivex.Flowable) Recommendation(org.glassfish.jersey.examples.rx.domain.Recommendation) Schedulers(io.reactivex.schedulers.Schedulers) Queue(java.util.Queue) WebTarget(javax.ws.rs.client.WebTarget) RxFlowableInvoker(org.glassfish.jersey.client.rx.rxjava2.RxFlowableInvoker) RxFlowableInvokerProvider(org.glassfish.jersey.client.rx.rxjava2.RxFlowableInvokerProvider) Collections(java.util.Collections) AgentResponse(org.glassfish.jersey.examples.rx.domain.AgentResponse) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) AgentResponse(org.glassfish.jersey.examples.rx.domain.AgentResponse) GET(javax.ws.rs.GET)

Example 29 with AsyncResponse

use of javax.ws.rs.container.AsyncResponse in project jersey by jersey.

the class AsyncCancelTimeoutResource method setTimeout.

@POST
@Path("timeout")
public void setTimeout(final String stage) throws Exception {
    final AsyncResponse async = stages[Integer.parseInt(stage)].take();
    async.setTimeoutHandler(new TimeoutHandler() {

        @Override
        public void handleTimeout(final AsyncResponse response) {
            response.cancel();
        }
    });
    async.setTimeout(200L, TimeUnit.MILLISECONDS);
    stages[Integer.parseInt(stage) + 1].add(async);
}
Also used : TimeoutHandler(javax.ws.rs.container.TimeoutHandler) AsyncResponse(javax.ws.rs.container.AsyncResponse) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 30 with AsyncResponse

use of javax.ws.rs.container.AsyncResponse in project jersey by jersey.

the class AsyncResumeTimeoutResource method setTimeOut.

@POST
@Path("timeout")
public void setTimeOut(final Integer millis) throws InterruptedException {
    final AsyncResponse asyncResponse = queue.take();
    final boolean timeout1 = asyncResponse.setTimeout(millis, TimeUnit.MILLISECONDS);
    asyncResponse.setTimeoutHandler(new TimeoutHandler() {

        @Override
        public void handleTimeout(final AsyncResponse asyncResponse) {
            final boolean timeout2 = asyncResponse.setTimeout(millis, TimeUnit.MILLISECONDS);
            asyncResponse.setTimeoutHandler(new TimeoutHandler() {

                @Override
                public void handleTimeout(final AsyncResponse asyncResponse) {
                    asyncResponse.resume("timeout1=" + timeout1 + "_timeout2=" + timeout2 + "_handled");
                }
            });
        }
    });
}
Also used : TimeoutHandler(javax.ws.rs.container.TimeoutHandler) AsyncResponse(javax.ws.rs.container.AsyncResponse) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Aggregations

AsyncResponse (javax.ws.rs.container.AsyncResponse)58 Response (javax.ws.rs.core.Response)22 Test (org.junit.Test)15 CompletableFuture (java.util.concurrent.CompletableFuture)12 Path (javax.ws.rs.Path)11 List (java.util.List)10 GET (javax.ws.rs.GET)10 WebApplicationException (javax.ws.rs.WebApplicationException)10 Suspended (javax.ws.rs.container.Suspended)10 Status (javax.ws.rs.core.Response.Status)9 ArrayList (java.util.ArrayList)8 Context (javax.ws.rs.core.Context)8 MediaType (javax.ws.rs.core.MediaType)8 LoggerFactory (org.slf4j.LoggerFactory)8 AuthException (io.pravega.auth.AuthException)6 READ (io.pravega.auth.AuthHandler.Permissions.READ)6 READ_UPDATE (io.pravega.auth.AuthHandler.Permissions.READ_UPDATE)6 ClientConfig (io.pravega.client.ClientConfig)6 ReaderGroupManager (io.pravega.client.admin.ReaderGroupManager)6 ReaderGroupManagerImpl (io.pravega.client.admin.impl.ReaderGroupManagerImpl)6