Search in sources :

Example 1 with CloseableContext

use of org.jboss.resteasy.core.ResteasyContext.CloseableContext in project resteasy by resteasy.

the class AsyncResponseConsumer method internalResume.

protected void internalResume(Throwable t, Consumer<Throwable> onComplete) {
    try (CloseableContext c = ResteasyContext.addCloseableContextDataLevel(contextDataMap)) {
        HttpRequest httpRequest = (HttpRequest) contextDataMap.get(HttpRequest.class);
        HttpResponse httpResponse = (HttpResponse) contextDataMap.get(HttpResponse.class);
        try {
            dispatcher.writeException(httpRequest, httpResponse, t, onComplete);
        } catch (Throwable t2) {
            // ignore t2 and report the original exception without going through filters
            dispatcher.unhandledAsynchronousException(httpResponse, t);
            onComplete.accept(t);
        }
    }
}
Also used : HttpRequest(org.jboss.resteasy.spi.HttpRequest) CloseableContext(org.jboss.resteasy.core.ResteasyContext.CloseableContext) HttpResponse(org.jboss.resteasy.spi.HttpResponse)

Example 2 with CloseableContext

use of org.jboss.resteasy.core.ResteasyContext.CloseableContext in project resteasy by resteasy.

the class AsyncResponseConsumer method internalResume.

protected void internalResume(Object entity, Consumer<Throwable> onComplete) {
    try (CloseableContext c = ResteasyContext.addCloseableContextDataLevel(contextDataMap)) {
        HttpRequest httpRequest = (HttpRequest) contextDataMap.get(HttpRequest.class);
        HttpResponse httpResponse = (HttpResponse) contextDataMap.get(HttpResponse.class);
        BuiltResponse builtResponse = createResponse(entity, httpRequest);
        try {
            sendBuiltResponse(builtResponse, httpRequest, httpResponse, e -> {
                if (e != null) {
                    exceptionWhileResuming(e);
                }
                onComplete.accept(e);
            });
        } catch (Throwable e) {
            exceptionWhileResuming(e);
            onComplete.accept(e);
        }
    }
}
Also used : HttpRequest(org.jboss.resteasy.spi.HttpRequest) CloseableContext(org.jboss.resteasy.core.ResteasyContext.CloseableContext) HttpResponse(org.jboss.resteasy.spi.HttpResponse) BuiltResponse(org.jboss.resteasy.specimpl.BuiltResponse)

Example 3 with CloseableContext

use of org.jboss.resteasy.core.ResteasyContext.CloseableContext in project resteasy by resteasy.

the class AbstractAsynchronousResponse method internalResume.

protected boolean internalResume(Throwable exc, Consumer<Throwable> onComplete) {
    try (CloseableContext c = ResteasyContext.addCloseableContextDataLevel(contextDataMap)) {
        dispatcher.asynchronousExceptionDelivery(request, response, exc, t -> {
            onComplete.accept(t);
            completionCallbacks(exc);
        });
    }
    return true;
}
Also used : CloseableContext(org.jboss.resteasy.core.ResteasyContext.CloseableContext)

Example 4 with CloseableContext

use of org.jboss.resteasy.core.ResteasyContext.CloseableContext in project resteasy by resteasy.

the class AbstractAsynchronousResponse method internalResume.

protected boolean internalResume(Object entity, Consumer<Throwable> onComplete) {
    try (CloseableContext c = ResteasyContext.addCloseableContextDataLevel(contextDataMap)) {
        Response response = null;
        if (entity == null) {
            response = Response.noContent().build();
        } else if (entity instanceof Response) {
            response = (Response) entity;
        } else {
            if (method == null)
                throw new IllegalStateException(Messages.MESSAGES.unknownMediaTypeResponseEntity());
            MediaType type = method.resolveContentType(request, entity);
            BuiltResponse jaxrsResponse = (BuiltResponse) Response.ok(entity, type).build();
            if (!(entity instanceof GenericEntity)) {
                jaxrsResponse.setGenericType(method.getGenericReturnType());
            }
            jaxrsResponse.addMethodAnnotations(method.getMethodAnnotations());
            response = jaxrsResponse;
        }
        try {
            dispatcher.asynchronousDelivery(this.request, this.response, response, t -> {
                if (t != null) {
                    internalResume(t, t2 -> {
                        onComplete.accept(t);
                    // callbacks done by internalResume
                    });
                } else {
                    onComplete.accept(null);
                    completionCallbacks(null);
                }
            });
        } catch (Throwable e) {
            return internalResume(e, t -> {
                onComplete.accept(e);
            // callbacks done by internalResume
            });
        }
    }
    return true;
}
Also used : HttpResponse(org.jboss.resteasy.spi.HttpResponse) Response(jakarta.ws.rs.core.Response) BuiltResponse(org.jboss.resteasy.specimpl.BuiltResponse) ResteasyAsynchronousResponse(org.jboss.resteasy.spi.ResteasyAsynchronousResponse) CompletionCallback(jakarta.ws.rs.container.CompletionCallback) WriterInterceptor(jakarta.ws.rs.ext.WriterInterceptor) Collection(java.util.Collection) GenericEntity(jakarta.ws.rs.core.GenericEntity) HttpRequest(org.jboss.resteasy.spi.HttpRequest) HashMap(java.util.HashMap) HttpResponse(org.jboss.resteasy.spi.HttpResponse) CloseableContext(org.jboss.resteasy.core.ResteasyContext.CloseableContext) ArrayList(java.util.ArrayList) Consumer(java.util.function.Consumer) Messages(org.jboss.resteasy.resteasy_jaxrs.i18n.Messages) Response(jakarta.ws.rs.core.Response) List(java.util.List) BuiltResponse(org.jboss.resteasy.specimpl.BuiltResponse) MediaType(jakarta.ws.rs.core.MediaType) Map(java.util.Map) Annotation(java.lang.annotation.Annotation) ContainerResponseFilter(jakarta.ws.rs.container.ContainerResponseFilter) AsyncWriterInterceptor(org.jboss.resteasy.spi.AsyncWriterInterceptor) ResteasyAsynchronousResponse(org.jboss.resteasy.spi.ResteasyAsynchronousResponse) TimeoutHandler(jakarta.ws.rs.container.TimeoutHandler) GenericEntity(jakarta.ws.rs.core.GenericEntity) CloseableContext(org.jboss.resteasy.core.ResteasyContext.CloseableContext) MediaType(jakarta.ws.rs.core.MediaType) BuiltResponse(org.jboss.resteasy.specimpl.BuiltResponse)

Example 5 with CloseableContext

use of org.jboss.resteasy.core.ResteasyContext.CloseableContext in project resteasy by resteasy.

the class SseEventOutputImpl method close.

private void close(final boolean flushBeforeClose, final Throwable error) {
    // avoid even attempting to get a lock if someone else has closed it or is closing it
    if (state.getAndSet(CLOSED) != CLOSED) {
        if (error != null) {
            drainQueue(error);
        } else {
            synchronized (lock) {
                events.clear();
            }
        }
        if (flushBeforeClose && responseFlushed) {
            try (CloseableContext c = ResteasyContext.addCloseableContextDataLevel(contextDataMap)) {
                // make sure we flush to await for any queued data being sent
                AsyncOutputStream aos = response.getAsyncOutputStream();
                aos.asyncFlush().toCompletableFuture().get();
            } catch (IOException | InterruptedException | ExecutionException x) {
            // ignore it and let's just close
            }
        }
        if (asyncContext.isSuspended()) {
            ResteasyAsynchronousResponse asyncResponse = asyncContext.getAsyncResponse();
            if (asyncResponse != null) {
                try {
                    asyncResponse.complete();
                } catch (RuntimeException x) {
                    Throwable cause = x;
                    while (cause.getCause() != null && cause.getCause() != cause) cause = cause.getCause();
                    if (cause instanceof IOException) {
                    // ignore it, we're closed now
                    } else {
                        LOG.debug(cause.getMessage());
                        return;
                    }
                }
            }
        }
        clearContextData();
    }
}
Also used : ResteasyAsynchronousResponse(org.jboss.resteasy.spi.ResteasyAsynchronousResponse) AsyncOutputStream(org.jboss.resteasy.spi.AsyncOutputStream) CloseableContext(org.jboss.resteasy.core.ResteasyContext.CloseableContext) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

CloseableContext (org.jboss.resteasy.core.ResteasyContext.CloseableContext)12 HttpRequest (org.jboss.resteasy.spi.HttpRequest)4 HttpResponse (org.jboss.resteasy.spi.HttpResponse)4 IOException (java.io.IOException)3 ExecutionException (java.util.concurrent.ExecutionException)3 BuiltResponse (org.jboss.resteasy.specimpl.BuiltResponse)3 ResteasyAsynchronousResponse (org.jboss.resteasy.spi.ResteasyAsynchronousResponse)3 ProcessingException (jakarta.ws.rs.ProcessingException)2 ResponseProcessingException (jakarta.ws.rs.client.ResponseProcessingException)2 MediaType (jakarta.ws.rs.core.MediaType)2 Response (jakarta.ws.rs.core.Response)2 UncheckedIOException (java.io.UncheckedIOException)2 Annotation (java.lang.annotation.Annotation)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Messages (org.jboss.resteasy.resteasy_jaxrs.i18n.Messages)2 AsyncOutputStream (org.jboss.resteasy.spi.AsyncOutputStream)2 BadRequestException (jakarta.ws.rs.BadRequestException)1 ClientErrorException (jakarta.ws.rs.ClientErrorException)1 ForbiddenException (jakarta.ws.rs.ForbiddenException)1