Search in sources :

Example 1 with BlockingAsyncOutputStream

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);
    });
}
Also used : BlockingAsyncOutputStream(org.jboss.resteasy.spi.BlockingAsyncOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CompletableFuture(java.util.concurrent.CompletableFuture) ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) WebApplicationException(jakarta.ws.rs.WebApplicationException) ArrayList(java.util.ArrayList) ResteasyContext(org.jboss.resteasy.core.ResteasyContext) MultivaluedMap(jakarta.ws.rs.core.MultivaluedMap) ProcessingException(jakarta.ws.rs.ProcessingException) ContainerResponseContext(jakarta.ws.rs.container.ContainerResponseContext) ContainerResponseFilter(jakarta.ws.rs.container.ContainerResponseFilter) Priority(jakarta.annotation.Priority) OutputStream(java.io.OutputStream) WriterInterceptorContext(jakarta.ws.rs.ext.WriterInterceptorContext) AsyncOutputStream(org.jboss.resteasy.spi.AsyncOutputStream) ClientRequestFilter(jakarta.ws.rs.client.ClientRequestFilter) SignatureException(java.security.SignatureException) IOException(java.io.IOException) Messages(org.jboss.resteasy.security.doseta.i18n.Messages) Provider(jakarta.ws.rs.ext.Provider) Priorities(jakarta.ws.rs.Priorities) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) ContainerRequestContext(jakarta.ws.rs.container.ContainerRequestContext) PrivateKey(java.security.PrivateKey) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) AsyncWriterInterceptor(org.jboss.resteasy.spi.AsyncWriterInterceptor) AsyncWriterInterceptorContext(org.jboss.resteasy.spi.AsyncWriterInterceptorContext) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Collections(java.util.Collections) LogMessages(org.jboss.resteasy.security.doseta.i18n.LogMessages) BlockingAsyncOutputStream(org.jboss.resteasy.spi.BlockingAsyncOutputStream) AsyncOutputStream(org.jboss.resteasy.spi.AsyncOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BlockingAsyncOutputStream(org.jboss.resteasy.spi.BlockingAsyncOutputStream) CompletableFuture(java.util.concurrent.CompletableFuture)

Aggregations

Priority (jakarta.annotation.Priority)1 Priorities (jakarta.ws.rs.Priorities)1 ProcessingException (jakarta.ws.rs.ProcessingException)1 WebApplicationException (jakarta.ws.rs.WebApplicationException)1 ClientRequestContext (jakarta.ws.rs.client.ClientRequestContext)1 ClientRequestFilter (jakarta.ws.rs.client.ClientRequestFilter)1 ContainerRequestContext (jakarta.ws.rs.container.ContainerRequestContext)1 ContainerResponseContext (jakarta.ws.rs.container.ContainerResponseContext)1 ContainerResponseFilter (jakarta.ws.rs.container.ContainerResponseFilter)1 MultivaluedMap (jakarta.ws.rs.core.MultivaluedMap)1 Provider (jakarta.ws.rs.ext.Provider)1 WriterInterceptorContext (jakarta.ws.rs.ext.WriterInterceptorContext)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 InvalidKeyException (java.security.InvalidKeyException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 PrivateKey (java.security.PrivateKey)1 SignatureException (java.security.SignatureException)1