Search in sources :

Example 1 with WriterInterceptorContext

use of jakarta.ws.rs.ext.WriterInterceptorContext in project resteasy by resteasy.

the class PriorityTest method testWriterPriorityOverride.

@Test
public void testWriterPriorityOverride() {
    Client client = ClientBuilder.newClient();
    try {
        fakeHttpServer.start();
        WebTarget webTarget = client.target("http://" + fakeHttpServer.getHostAndPort());
        StringBuilder result = new StringBuilder();
        webTarget.register(new WriterInterceptor() {

            @Override
            public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
                result.append("K");
                context.proceed();
            }
        }, 1);
        webTarget.register(new WriterInterceptor() {

            @Override
            public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
                result.append("O");
                context.proceed();
            }
        }, 0);
        webTarget.request().post(Entity.text("Hello")).close();
        Assert.assertEquals("OK", result.toString());
    } finally {
        client.close();
    }
}
Also used : WriterInterceptor(jakarta.ws.rs.ext.WriterInterceptor) WriterInterceptorContext(jakarta.ws.rs.ext.WriterInterceptorContext) WebApplicationException(jakarta.ws.rs.WebApplicationException) WebTarget(jakarta.ws.rs.client.WebTarget) IOException(java.io.IOException) Client(jakarta.ws.rs.client.Client) Test(org.junit.Test)

Example 2 with WriterInterceptorContext

use of jakarta.ws.rs.ext.WriterInterceptorContext in project tomee by apache.

the class JAXRSUtils method writeMessageBody.

// CHECKSTYLE:OFF
public static void writeMessageBody(List<WriterInterceptor> writers, Object entity, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, Message message) throws WebApplicationException, IOException {
    OutputStream entityStream = message.getContent(OutputStream.class);
    if ("org.apache.cxf.jaxrs.reactivestreams.server.StreamingAsyncSubscriber$StreamingResponseImpl".equals(entity.getClass().getName())) {
        // cache the OutputStream when it's reactive response
        entityStream = new CacheAndWriteOutputStream(entityStream);
    }
    if (writers.size() > 1) {
        WriterInterceptor first = writers.remove(0);
        WriterInterceptorContext context = new WriterInterceptorContextImpl(entity, type, genericType, annotations, entityStream, message, writers);
        first.aroundWriteTo(context);
    } else {
        MessageBodyWriter<Object> writer = ((WriterInterceptorMBW) writers.get(0)).getMBW();
        if (type == byte[].class) {
            long size = writer.getSize(entity, type, genericType, annotations, mediaType);
            if (size != -1) {
                httpHeaders.putSingle(HttpHeaders.CONTENT_LENGTH, Long.toString(size));
            }
        }
        HttpUtils.convertHeaderValuesToString(httpHeaders, true);
        writer.writeTo(entity, type, genericType, annotations, mediaType, httpHeaders, entityStream);
    }
}
Also used : WriterInterceptor(jakarta.ws.rs.ext.WriterInterceptor) WriterInterceptorContext(jakarta.ws.rs.ext.WriterInterceptorContext) WriterInterceptorMBW(org.apache.cxf.jaxrs.impl.WriterInterceptorMBW) CacheAndWriteOutputStream(org.apache.cxf.io.CacheAndWriteOutputStream) LoadingByteArrayOutputStream(org.apache.cxf.helpers.LoadingByteArrayOutputStream) OutputStream(java.io.OutputStream) WriterInterceptorContextImpl(org.apache.cxf.jaxrs.impl.WriterInterceptorContextImpl) CacheAndWriteOutputStream(org.apache.cxf.io.CacheAndWriteOutputStream)

Aggregations

WriterInterceptor (jakarta.ws.rs.ext.WriterInterceptor)2 WriterInterceptorContext (jakarta.ws.rs.ext.WriterInterceptorContext)2 WebApplicationException (jakarta.ws.rs.WebApplicationException)1 Client (jakarta.ws.rs.client.Client)1 WebTarget (jakarta.ws.rs.client.WebTarget)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 LoadingByteArrayOutputStream (org.apache.cxf.helpers.LoadingByteArrayOutputStream)1 CacheAndWriteOutputStream (org.apache.cxf.io.CacheAndWriteOutputStream)1 WriterInterceptorContextImpl (org.apache.cxf.jaxrs.impl.WriterInterceptorContextImpl)1 WriterInterceptorMBW (org.apache.cxf.jaxrs.impl.WriterInterceptorMBW)1 Test (org.junit.Test)1