use of org.jboss.resteasy.core.interception.jaxrs.SuspendableContainerResponseContext in project quarkus by quarkusio.
the class TemplateResponseFilter method filter.
@SuppressWarnings("unchecked")
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
Object entity = responseContext.getEntity();
if (entity instanceof TemplateInstance) {
SuspendableContainerResponseContext ctx = (SuspendableContainerResponseContext) responseContext;
ctx.suspend();
MediaType mediaType;
TemplateInstance instance = (TemplateInstance) entity;
Object variantsAttr = instance.getAttribute(TemplateInstance.VARIANTS);
if (variantsAttr != null) {
List<javax.ws.rs.core.Variant> variants = new ArrayList<>();
for (Variant variant : (List<Variant>) variantsAttr) {
variants.add(new javax.ws.rs.core.Variant(MediaType.valueOf(variant.getMediaType()), variant.getLocale(), variant.getEncoding()));
}
javax.ws.rs.core.Variant selected = requestContext.getRequest().selectVariant(variants);
if (selected != null) {
Locale selectedLocale = selected.getLanguage();
if (selectedLocale == null) {
List<Locale> acceptableLocales = requestContext.getAcceptableLanguages();
if (!acceptableLocales.isEmpty()) {
selectedLocale = acceptableLocales.get(0);
}
}
instance.setAttribute(TemplateInstance.SELECTED_VARIANT, new Variant(selectedLocale, selected.getMediaType().toString(), selected.getEncoding()));
mediaType = selected.getMediaType();
} else {
mediaType = responseContext.getMediaType();
}
} else {
mediaType = responseContext.getMediaType();
}
CompletionStage<String> cs = instance.renderAsync();
if (!Arc.container().instance(Engine.class).get().useAsyncTimeout()) {
// Make sure the timeout is always used
long timeout = instance.getTimeout();
cs = cs.toCompletableFuture().orTimeout(timeout, TimeUnit.MILLISECONDS);
}
try {
cs.whenComplete((r, t) -> {
if (t == null) {
// an NPE further down
if (mediaType != null) {
ctx.setEntity(r, null, mediaType);
} else {
ctx.setEntity(r);
}
ctx.resume();
} else {
ctx.resume(t);
}
});
} catch (Throwable t) {
ctx.resume(t);
}
}
}
use of org.jboss.resteasy.core.interception.jaxrs.SuspendableContainerResponseContext 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");
}
use of org.jboss.resteasy.core.interception.jaxrs.SuspendableContainerResponseContext 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");
}
Aggregations