use of org.jboss.resteasy.spi.BlockingAsyncOutputStream 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