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);
}
}
}
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);
}
}
}
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;
}
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;
}
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();
}
}
Aggregations