Search in sources :

Example 1 with CompletionCallback

use of jakarta.ws.rs.container.CompletionCallback in project resteasy by resteasy.

the class AsyncRequestFilter method filter.

@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
    requestContext.getHeaders().add("RequestFilterCallback" + name, String.valueOf(callbackException));
    callbackException = null;
    SuspendableContainerRequestContext ctx = (SuspendableContainerRequestContext) requestContext;
    String action = ctx.getHeaderString(name);
    LOG.error("Filter request for " + name + " with action: " + action);
    if ("sync-pass".equals(action)) {
    // do nothing
    } else if ("sync-fail".equals(action)) {
        ctx.abortWith(Response.ok(name).build());
    } else if ("async-pass".equals(action)) {
        ctx.suspend();
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.submit(() -> ctx.resume());
    } else if ("async-pass-instant".equals(action)) {
        ctx.suspend();
        ctx.resume();
    } else if ("async-fail".equals(action)) {
        ctx.suspend();
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.submit(() -> ctx.abortWith(Response.ok(name).build()));
    } else if ("async-fail-instant".equals(action)) {
        ctx.suspend();
        ctx.abortWith(Response.ok(name).build());
    } else if ("async-throw-late".equals(action)) {
        ctx.suspend();
        HttpRequest req = ResteasyContext.getContextData(HttpRequest.class);
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.submit(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                LOG.error("Error:", e);
            }
            AsyncResponse resp = req.getAsyncContext().getAsyncResponse();
            resp.register((CompletionCallback) (t) -> {
                if (callbackException != null)
                    throw new RuntimeException("Callback called twice");
                callbackException = Objects.toString(t);
            });
            if ("true".equals(req.getHttpHeaders().getHeaderString("UseExceptionMapper")))
                ctx.resume(new AsyncFilterException("ouch"));
            else
                ctx.resume(new Throwable("ouch"));
        });
    }
    LOG.error("Filter request for " + name + " with action: " + action + " done");
}
Also used : HttpRequest(org.jboss.resteasy.spi.HttpRequest) CompletionCallback(jakarta.ws.rs.container.CompletionCallback) Logger(org.jboss.logging.Logger) AsyncResponse(jakarta.ws.rs.container.AsyncResponse) IOException(java.io.IOException) HttpRequest(org.jboss.resteasy.spi.HttpRequest) Executors(java.util.concurrent.Executors) Objects(java.util.Objects) Response(jakarta.ws.rs.core.Response) ResteasyContext(org.jboss.resteasy.core.ResteasyContext) ContainerRequestContext(jakarta.ws.rs.container.ContainerRequestContext) ContainerRequestFilter(jakarta.ws.rs.container.ContainerRequestFilter) ExecutorService(java.util.concurrent.ExecutorService) SuspendableContainerRequestContext(org.jboss.resteasy.core.interception.jaxrs.SuspendableContainerRequestContext) ExecutorService(java.util.concurrent.ExecutorService) SuspendableContainerRequestContext(org.jboss.resteasy.core.interception.jaxrs.SuspendableContainerRequestContext) AsyncResponse(jakarta.ws.rs.container.AsyncResponse)

Example 2 with CompletionCallback

use of jakarta.ws.rs.container.CompletionCallback in project helidon by oracle.

the class HelloWorldResource method slowFailNoCounter.

@GET
@Path("/slowFailNoCounter")
@Produces(MediaType.TEXT_PLAIN)
public void slowFailNoCounter(@Suspended AsyncResponse ar) {
    ar.register(new CompletionCallback() {

        @Override
        public void onComplete(Throwable throwable) {
            if (throwable == null) {
                System.out.println("OK");
            } else {
                System.out.println("No good");
            }
        }
    });
    executorService.execute(() -> {
        try {
            TimeUnit.MILLISECONDS.sleep(NOT_SO_SLOW_DELAY_MS);
        } catch (InterruptedException e) {
        // Don't care for testing.
        }
        ar.resume(new IllegalStateException("Fail on request"));
    });
}
Also used : CompletionCallback(jakarta.ws.rs.container.CompletionCallback) Path(jakarta.ws.rs.Path) Produces(jakarta.ws.rs.Produces) GET(jakarta.ws.rs.GET)

Example 3 with CompletionCallback

use of jakarta.ws.rs.container.CompletionCallback in project resteasy by resteasy.

the class AsyncResponseFilter method filter.

@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
    // copy request filter callback values
    for (Entry<String, List<String>> entry : requestContext.getHeaders().entrySet()) {
        if (entry.getKey().startsWith("RequestFilterCallback"))
            addValuesToContext(responseContext, entry);
    }
    responseContext.getHeaders().add("ResponseFilterCallback" + name, String.valueOf(callbackException));
    callbackException = null;
    SuspendableContainerResponseContext ctx = (SuspendableContainerResponseContext) responseContext;
    String action = requestContext.getHeaderString(name);
    LOG.error("Filter response for " + name + " with action: " + action);
    if ("sync-pass".equals(action)) {
    // do nothing
    } else if ("sync-fail".equals(action)) {
        ctx.setEntity(name);
    } else if ("async-pass".equals(action)) {
        ctx.suspend();
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.submit(() -> ctx.resume());
    } else if ("async-pass-instant".equals(action)) {
        ctx.suspend();
        ctx.resume();
    } else if ("async-fail".equals(action)) {
        ctx.suspend();
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.submit(() -> {
            ctx.setEntity(name);
            ctx.resume();
        });
    } else if ("async-fail-late".equals(action)) {
        ctx.suspend();
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.submit(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                LOG.error("Error:", e);
            }
            ctx.setEntity(name);
            ctx.resume();
        });
    } else if ("async-fail-instant".equals(action)) {
        ctx.suspend();
        ctx.setEntity(name);
        ctx.resume();
    } else if ("sync-throw".equals(action)) {
        throw new AsyncFilterException("ouch");
    } else if ("async-throw-late".equals(action)) {
        ctx.suspend();
        HttpRequest req = ResteasyContext.getContextData(HttpRequest.class);
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.submit(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                LOG.error("Error:", e);
            }
            ctx.setEntity(name);
            ResteasyAsynchronousResponse resp = req.getAsyncContext().getAsyncResponse();
            resp.register((CompletionCallback) (t) -> {
                if (callbackException != null)
                    throw new RuntimeException("Callback called twice");
                callbackException = Objects.toString(t);
            });
            if ("true".equals(req.getHttpHeaders().getHeaderString("UseExceptionMapper")))
                ctx.resume(new AsyncFilterException("ouch"));
            else
                ctx.resume(new Throwable("ouch"));
        });
    }
    LOG.error("Filter response for " + name + " with action: " + action + " done");
}
Also used : HttpRequest(org.jboss.resteasy.spi.HttpRequest) SuspendableContainerResponseContext(org.jboss.resteasy.core.interception.jaxrs.SuspendableContainerResponseContext) CompletionCallback(jakarta.ws.rs.container.CompletionCallback) Logger(org.jboss.logging.Logger) IOException(java.io.IOException) HttpRequest(org.jboss.resteasy.spi.HttpRequest) Executors(java.util.concurrent.Executors) Objects(java.util.Objects) List(java.util.List) ResteasyContext(org.jboss.resteasy.core.ResteasyContext) ContainerRequestContext(jakarta.ws.rs.container.ContainerRequestContext) Entry(java.util.Map.Entry) ContainerResponseContext(jakarta.ws.rs.container.ContainerResponseContext) ContainerResponseFilter(jakarta.ws.rs.container.ContainerResponseFilter) ExecutorService(java.util.concurrent.ExecutorService) ResteasyAsynchronousResponse(org.jboss.resteasy.spi.ResteasyAsynchronousResponse) ResteasyAsynchronousResponse(org.jboss.resteasy.spi.ResteasyAsynchronousResponse) ExecutorService(java.util.concurrent.ExecutorService) List(java.util.List) SuspendableContainerResponseContext(org.jboss.resteasy.core.interception.jaxrs.SuspendableContainerResponseContext)

Example 4 with CompletionCallback

use of jakarta.ws.rs.container.CompletionCallback in project resteasy by resteasy.

the class AsyncResponseFilter method filter.

@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
    // copy request filter callback values
    for (Entry<String, List<String>> entry : requestContext.getHeaders().entrySet()) {
        if (entry.getKey().startsWith("RequestFilterCallback"))
            addValuesToContext(responseContext, entry);
    }
    responseContext.getHeaders().add("ResponseFilterCallback" + name, String.valueOf(callbackException));
    callbackException = null;
    SuspendableContainerResponseContext ctx = (SuspendableContainerResponseContext) responseContext;
    String action = requestContext.getHeaderString(name);
    LOG.error("Filter response for " + name + " with action: " + action);
    if ("sync-pass".equals(action)) {
    // do nothing
    } else if ("sync-fail".equals(action)) {
        ctx.setEntity(name);
    } else if ("async-pass".equals(action)) {
        ctx.suspend();
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.submit(() -> ctx.resume());
    } else if ("async-pass-instant".equals(action)) {
        ctx.suspend();
        ctx.resume();
    } else if ("async-fail".equals(action)) {
        ctx.suspend();
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.submit(() -> {
            ctx.setEntity(name);
            ctx.resume();
        });
    } else if ("async-fail-late".equals(action)) {
        ctx.suspend();
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.submit(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                LOG.error("Error:", e);
            }
            ctx.setEntity(name);
            ctx.resume();
        });
    } else if ("async-fail-instant".equals(action)) {
        ctx.suspend();
        ctx.setEntity(name);
        ctx.resume();
    } else if ("sync-throw".equals(action)) {
        throw new AsyncFilterException("ouch");
    } else if ("async-throw-late".equals(action)) {
        ctx.suspend();
        HttpRequest req = ResteasyContext.getContextData(HttpRequest.class);
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.submit(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                LOG.error("Error:", e);
            }
            ctx.setEntity(name);
            ResteasyAsynchronousResponse resp = req.getAsyncContext().getAsyncResponse();
            resp.register((CompletionCallback) (t) -> {
                if (callbackException != null)
                    throw new RuntimeException("Callback called twice");
                callbackException = Objects.toString(t);
            });
            if ("true".equals(req.getHttpHeaders().getHeaderString("UseExceptionMapper")))
                ctx.resume(new AsyncFilterException("ouch"));
            else
                ctx.resume(new Throwable("ouch"));
        });
    }
    LOG.error("Filter response for " + name + " with action: " + action + " done");
}
Also used : HttpRequest(org.jboss.resteasy.spi.HttpRequest) SuspendableContainerResponseContext(org.jboss.resteasy.core.interception.jaxrs.SuspendableContainerResponseContext) CompletionCallback(jakarta.ws.rs.container.CompletionCallback) Logger(org.jboss.logging.Logger) HttpRequest(org.jboss.resteasy.spi.HttpRequest) IOException(java.io.IOException) Executors(java.util.concurrent.Executors) Objects(java.util.Objects) ResteasyContext(org.jboss.resteasy.core.ResteasyContext) List(java.util.List) ContainerRequestContext(jakarta.ws.rs.container.ContainerRequestContext) Entry(java.util.Map.Entry) ContainerResponseContext(jakarta.ws.rs.container.ContainerResponseContext) ContainerResponseFilter(jakarta.ws.rs.container.ContainerResponseFilter) ResteasyAsynchronousResponse(org.jboss.resteasy.spi.ResteasyAsynchronousResponse) ExecutorService(java.util.concurrent.ExecutorService) ResteasyAsynchronousResponse(org.jboss.resteasy.spi.ResteasyAsynchronousResponse) ExecutorService(java.util.concurrent.ExecutorService) List(java.util.List) SuspendableContainerResponseContext(org.jboss.resteasy.core.interception.jaxrs.SuspendableContainerResponseContext)

Example 5 with CompletionCallback

use of jakarta.ws.rs.container.CompletionCallback in project resteasy by resteasy.

the class AsyncRequestFilter method filter.

@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
    requestContext.getHeaders().add("RequestFilterCallback" + name, String.valueOf(callbackException));
    callbackException = null;
    SuspendableContainerRequestContext ctx = (SuspendableContainerRequestContext) requestContext;
    String action = ctx.getHeaderString(name);
    LOG.error("Filter request for " + name + " with action: " + action);
    if ("sync-pass".equals(action)) {
    // do nothing
    } else if ("sync-fail".equals(action)) {
        ctx.abortWith(Response.ok(name).build());
    } else if ("async-pass".equals(action)) {
        ctx.suspend();
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.submit(() -> ctx.resume());
    } else if ("async-pass-instant".equals(action)) {
        ctx.suspend();
        ctx.resume();
    } else if ("async-fail".equals(action)) {
        ctx.suspend();
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.submit(() -> ctx.abortWith(Response.ok(name).build()));
    } else if ("async-fail-instant".equals(action)) {
        ctx.suspend();
        ctx.abortWith(Response.ok(name).build());
    } else if ("async-throw-late".equals(action)) {
        ctx.suspend();
        HttpRequest req = ResteasyContext.getContextData(HttpRequest.class);
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.submit(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                LOG.error("Error:", e);
            }
            AsyncResponse resp = req.getAsyncContext().getAsyncResponse();
            resp.register((CompletionCallback) (t) -> {
                if (callbackException != null)
                    throw new RuntimeException("Callback called twice");
                callbackException = Objects.toString(t);
            });
            if ("true".equals(req.getHttpHeaders().getHeaderString("UseExceptionMapper")))
                ctx.resume(new AsyncFilterException("ouch"));
            else
                ctx.resume(new Throwable("ouch"));
        });
    }
    LOG.error("Filter request for " + name + " with action: " + action + " done");
}
Also used : HttpRequest(org.jboss.resteasy.spi.HttpRequest) CompletionCallback(jakarta.ws.rs.container.CompletionCallback) Logger(org.jboss.logging.Logger) AsyncResponse(jakarta.ws.rs.container.AsyncResponse) HttpRequest(org.jboss.resteasy.spi.HttpRequest) IOException(java.io.IOException) Executors(java.util.concurrent.Executors) Objects(java.util.Objects) ResteasyContext(org.jboss.resteasy.core.ResteasyContext) Response(jakarta.ws.rs.core.Response) ContainerRequestContext(jakarta.ws.rs.container.ContainerRequestContext) ContainerRequestFilter(jakarta.ws.rs.container.ContainerRequestFilter) SuspendableContainerRequestContext(org.jboss.resteasy.core.interception.jaxrs.SuspendableContainerRequestContext) ExecutorService(java.util.concurrent.ExecutorService) ExecutorService(java.util.concurrent.ExecutorService) SuspendableContainerRequestContext(org.jboss.resteasy.core.interception.jaxrs.SuspendableContainerRequestContext) AsyncResponse(jakarta.ws.rs.container.AsyncResponse)

Aggregations

CompletionCallback (jakarta.ws.rs.container.CompletionCallback)5 ContainerRequestContext (jakarta.ws.rs.container.ContainerRequestContext)4 IOException (java.io.IOException)4 Objects (java.util.Objects)4 ExecutorService (java.util.concurrent.ExecutorService)4 Executors (java.util.concurrent.Executors)4 Logger (org.jboss.logging.Logger)4 ResteasyContext (org.jboss.resteasy.core.ResteasyContext)4 HttpRequest (org.jboss.resteasy.spi.HttpRequest)4 AsyncResponse (jakarta.ws.rs.container.AsyncResponse)2 ContainerRequestFilter (jakarta.ws.rs.container.ContainerRequestFilter)2 ContainerResponseContext (jakarta.ws.rs.container.ContainerResponseContext)2 ContainerResponseFilter (jakarta.ws.rs.container.ContainerResponseFilter)2 Response (jakarta.ws.rs.core.Response)2 List (java.util.List)2 Entry (java.util.Map.Entry)2 SuspendableContainerRequestContext (org.jboss.resteasy.core.interception.jaxrs.SuspendableContainerRequestContext)2 SuspendableContainerResponseContext (org.jboss.resteasy.core.interception.jaxrs.SuspendableContainerResponseContext)2 ResteasyAsynchronousResponse (org.jboss.resteasy.spi.ResteasyAsynchronousResponse)2 GET (jakarta.ws.rs.GET)1