Search in sources :

Example 1 with AsyncWriterInterceptorContext

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();
    }
}
Also used : Context(jakarta.ws.rs.core.Context) CompletableFuture(java.util.concurrent.CompletableFuture) WebApplicationException(jakarta.ws.rs.WebApplicationException) ResteasyConfiguration(org.jboss.resteasy.spi.ResteasyConfiguration) BufferedOutputStream(java.io.BufferedOutputStream) ResteasyContext(org.jboss.resteasy.core.ResteasyContext) LogMessages(org.jboss.resteasy.resteasy_jaxrs.i18n.LogMessages) CommitHeaderOutputStream(org.jboss.resteasy.util.CommitHeaderOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) OutputStream(java.io.OutputStream) WriterInterceptorContext(jakarta.ws.rs.ext.WriterInterceptorContext) AsyncOutputWriter(org.jboss.resteasy.spi.AsyncOutputWriter) RuntimeType(jakarta.ws.rs.RuntimeType) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) UriInfo(jakarta.ws.rs.core.UriInfo) Provider(jakarta.ws.rs.ext.Provider) MediaTypeMap(org.jboss.resteasy.core.MediaTypeMap) CompletionStage(java.util.concurrent.CompletionStage) MediaType(jakarta.ws.rs.core.MediaType) ContextResolver(jakarta.ws.rs.ext.ContextResolver) ConstrainedTo(jakarta.ws.rs.ConstrainedTo) Providers(jakarta.ws.rs.ext.Providers) AsyncWriterInterceptor(org.jboss.resteasy.spi.AsyncWriterInterceptor) AsyncWriterInterceptorContext(org.jboss.resteasy.spi.AsyncWriterInterceptorContext) AsyncOutputWriter(org.jboss.resteasy.spi.AsyncOutputWriter)

Example 2 with AsyncWriterInterceptorContext

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

WebApplicationException (jakarta.ws.rs.WebApplicationException)2 Provider (jakarta.ws.rs.ext.Provider)2 WriterInterceptorContext (jakarta.ws.rs.ext.WriterInterceptorContext)2 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CompletionStage (java.util.concurrent.CompletionStage)2 ResteasyContext (org.jboss.resteasy.core.ResteasyContext)2 AsyncWriterInterceptor (org.jboss.resteasy.spi.AsyncWriterInterceptor)2 AsyncWriterInterceptorContext (org.jboss.resteasy.spi.AsyncWriterInterceptorContext)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Priority (jakarta.annotation.Priority)1 ConstrainedTo (jakarta.ws.rs.ConstrainedTo)1 Priorities (jakarta.ws.rs.Priorities)1 ProcessingException (jakarta.ws.rs.ProcessingException)1 RuntimeType (jakarta.ws.rs.RuntimeType)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