use of org.jboss.resteasy.spi.AsyncWriterInterceptorContext in project resteasy by resteasy.
the class Jackson2JsonpInterceptor method asyncAroundWriteTo.
@Override
public CompletionStage<Void> asyncAroundWriteTo(AsyncWriterInterceptorContext context) {
LogMessages.LOGGER.debugf("Interceptor : %s, Method : aroundWriteTo", getClass().getName());
String function = uri.getQueryParameters().getFirst(callbackQueryParameter);
if (enabled && function != null && !function.trim().isEmpty() && !jsonpCompatibleMediaTypes.getPossible(context.getMediaType()).isEmpty()) {
AsyncOutputWriter writer = new AsyncOutputWriter(context.getAsyncOutputStream());
CompletionStage<Void> ret = CompletableFuture.completedFuture(null);
if (wrapInTryCatch) {
ret = ret.thenCompose(v -> writer.asyncWrite("try{"));
}
ret = ret.thenCompose(v -> writer.asyncWrite(function + "(")).thenCompose(v -> writer.asyncFlush()).thenCompose(v -> context.asyncProceed()).thenCompose(v -> writer.asyncFlush()).thenCompose(v -> writer.asyncWrite(")"));
if (wrapInTryCatch) {
ret = ret.thenCompose(v -> writer.asyncWrite("}catch(e){}"));
}
return ret.thenCompose(v -> writer.asyncFlush());
} else {
return context.asyncProceed();
}
}
use of org.jboss.resteasy.spi.AsyncWriterInterceptorContext in project resteasy by resteasy.
the class DigitalSigningInterceptor method asyncAroundWriteTo.
@Override
public CompletionStage<Void> asyncAroundWriteTo(AsyncWriterInterceptorContext context) {
LogMessages.LOGGER.debugf("Interceptor : %s, Method : aroundWriteTo", getClass().getName());
MultivaluedMap<String, Object> headers = context.getHeaders();
List<DKIMSignature> list = getHeaders(headers);
if (list.isEmpty()) {
return context.asyncProceed();
}
// System.out.println("TRACE: Found ContentSignatures");
AsyncOutputStream old = context.getAsyncOutputStream();
// store body in a byte array so we can use it to calculate signature
ByteArrayOutputStream baos = new ByteArrayOutputStream();
context.setAsyncOutputStream(new BlockingAsyncOutputStream(baos));
return context.asyncProceed().thenCompose(v -> {
byte[] body = baos.toByteArray();
try {
for (DKIMSignature dosetaSignature : list) {
KeyRepository repository = (KeyRepository) context.getProperty(KeyRepository.class.getName());
sign(repository, headers, body, dosetaSignature);
}
} catch (InvalidKeyException | NoSuchAlgorithmException | SignatureException | UnsupportedEncodingException e) {
CompletableFuture<Void> ret = new CompletableFuture<>();
ret.completeExceptionally(e);
return ret;
}
return old.asyncWrite(body);
}).whenComplete((v, t) -> {
context.setAsyncOutputStream(old);
if (t != null)
throw new RuntimeException(Messages.MESSAGES.failedToSign(), t);
});
}
Aggregations