Search in sources :

Example 1 with ReaderInterceptorContext

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

the class PriorityTest method testReaderInterceptorPriorityOverride.

@Test
public void testReaderInterceptorPriorityOverride() {
    Client client = ClientBuilder.newClient();
    try {
        fakeHttpServer.start();
        WebTarget webTarget = client.target("http://" + fakeHttpServer.getHostAndPort());
        webTarget.register((ClientResponseFilter) (containerRequestContext, containerResponseContext) -> {
            containerResponseContext.getHeaders().putSingle(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN);
            containerResponseContext.setEntityStream(new ByteArrayInputStream("hello".getBytes()));
        });
        StringBuilder result = new StringBuilder();
        webTarget.register(new ReaderInterceptor() {

            @Override
            public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException {
                result.append("K");
                return context.proceed();
            }
        }, 1);
        webTarget.register(new ReaderInterceptor() {

            @Override
            public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException {
                result.append("O");
                return context.proceed();
            }
        }, 0);
        webTarget.request().get().readEntity(String.class);
        Assert.assertEquals("OK", result.toString());
    } finally {
        client.close();
    }
}
Also used : Context(jakarta.ws.rs.core.Context) ReaderInterceptorContext(jakarta.ws.rs.ext.ReaderInterceptorContext) WriterInterceptor(jakarta.ws.rs.ext.WriterInterceptor) FakeHttpServer(org.jboss.resteasy.test.common.FakeHttpServer) ClientResponseFilterRegistryImpl(org.jboss.resteasy.core.interception.jaxrs.ClientResponseFilterRegistryImpl) ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) WebApplicationException(jakarta.ws.rs.WebApplicationException) PriorityContainerResponseFilter3(org.jboss.resteasy.test.interception.resource.PriorityContainerResponseFilter3) PriorityContainerResponseFilter1(org.jboss.resteasy.test.interception.resource.PriorityContainerResponseFilter1) MessageBodyWriter(jakarta.ws.rs.ext.MessageBodyWriter) ContainerResponseFilterRegistryImpl(org.jboss.resteasy.core.interception.jaxrs.ContainerResponseFilterRegistryImpl) PriorityContainerResponseFilter2(org.jboss.resteasy.test.interception.resource.PriorityContainerResponseFilter2) ResteasyProviderFactory(org.jboss.resteasy.spi.ResteasyProviderFactory) ClientResponseFilter(jakarta.ws.rs.client.ClientResponseFilter) ByteArrayInputStream(java.io.ByteArrayInputStream) MultivaluedMap(jakarta.ws.rs.core.MultivaluedMap) ContainerResponseFilter(jakarta.ws.rs.container.ContainerResponseFilter) JaxrsInterceptorRegistry(org.jboss.resteasy.spi.interception.JaxrsInterceptorRegistry) OutputStream(java.io.OutputStream) PriorityClientRequestFilter3(org.jboss.resteasy.test.interception.resource.PriorityClientRequestFilter3) WriterInterceptorContext(jakarta.ws.rs.ext.WriterInterceptorContext) Client(jakarta.ws.rs.client.Client) ClientRequestFilter(jakarta.ws.rs.client.ClientRequestFilter) ReaderInterceptor(jakarta.ws.rs.ext.ReaderInterceptor) Assert.assertTrue(org.junit.Assert.assertTrue) WebTarget(jakarta.ws.rs.client.WebTarget) IOException(java.io.IOException) Test(org.junit.Test) JaxrsInterceptorRegistryImpl(org.jboss.resteasy.core.interception.jaxrs.JaxrsInterceptorRegistryImpl) PriorityClientResponseFilter1(org.jboss.resteasy.test.interception.resource.PriorityClientResponseFilter1) PriorityClientResponseFilter2(org.jboss.resteasy.test.interception.resource.PriorityClientResponseFilter2) ClientResponseContext(jakarta.ws.rs.client.ClientResponseContext) PriorityClientRequestFilter1(org.jboss.resteasy.test.interception.resource.PriorityClientRequestFilter1) PriorityClientResponseFilter3(org.jboss.resteasy.test.interception.resource.PriorityClientResponseFilter3) Entity(jakarta.ws.rs.client.Entity) PriorityClientRequestFilter2(org.jboss.resteasy.test.interception.resource.PriorityClientRequestFilter2) Rule(org.junit.Rule) MediaType(jakarta.ws.rs.core.MediaType) Type(java.lang.reflect.Type) HttpHeaders(jakarta.ws.rs.core.HttpHeaders) ContextResolver(jakarta.ws.rs.ext.ContextResolver) Annotation(java.lang.annotation.Annotation) Providers(jakarta.ws.rs.ext.Providers) MessageBodyReader(jakarta.ws.rs.ext.MessageBodyReader) Assert(org.junit.Assert) InputStream(java.io.InputStream) ClientBuilder(jakarta.ws.rs.client.ClientBuilder) ReaderInterceptor(jakarta.ws.rs.ext.ReaderInterceptor) WebApplicationException(jakarta.ws.rs.WebApplicationException) ByteArrayInputStream(java.io.ByteArrayInputStream) WebTarget(jakarta.ws.rs.client.WebTarget) IOException(java.io.IOException) Client(jakarta.ws.rs.client.Client) ReaderInterceptorContext(jakarta.ws.rs.ext.ReaderInterceptorContext) Test(org.junit.Test)

Example 2 with ReaderInterceptorContext

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

the class JAXRSUtils method readFromMessageBodyReader.

@SuppressWarnings("unchecked")
public static Object readFromMessageBodyReader(List<ReaderInterceptor> readers, Class<?> targetTypeClass, Type parameterType, Annotation[] parameterAnnotations, InputStream is, MediaType mediaType, Message m) throws IOException, WebApplicationException {
    // Verbose but avoids an extra context instantiation for the typical path
    if (readers.size() > 1) {
        ReaderInterceptor first = readers.remove(0);
        ReaderInterceptorContext context = new ReaderInterceptorContextImpl(targetTypeClass, parameterType, parameterAnnotations, is, m, readers);
        return first.aroundReadFrom(context);
    }
    MessageBodyReader<?> provider = ((ReaderInterceptorMBR) readers.get(0)).getMBR();
    @SuppressWarnings("rawtypes") Class cls = targetTypeClass;
    return provider.readFrom(cls, parameterType, parameterAnnotations, mediaType, new HttpHeadersImpl(m).getRequestHeaders(), is);
}
Also used : ReaderInterceptor(jakarta.ws.rs.ext.ReaderInterceptor) ReaderInterceptorContextImpl(org.apache.cxf.jaxrs.impl.ReaderInterceptorContextImpl) ReaderInterceptorMBR(org.apache.cxf.jaxrs.impl.ReaderInterceptorMBR) ReaderInterceptorContext(jakarta.ws.rs.ext.ReaderInterceptorContext) HttpHeadersImpl(org.apache.cxf.jaxrs.impl.HttpHeadersImpl)

Aggregations

ReaderInterceptor (jakarta.ws.rs.ext.ReaderInterceptor)2 ReaderInterceptorContext (jakarta.ws.rs.ext.ReaderInterceptorContext)2 WebApplicationException (jakarta.ws.rs.WebApplicationException)1 Client (jakarta.ws.rs.client.Client)1 ClientBuilder (jakarta.ws.rs.client.ClientBuilder)1 ClientRequestContext (jakarta.ws.rs.client.ClientRequestContext)1 ClientRequestFilter (jakarta.ws.rs.client.ClientRequestFilter)1 ClientResponseContext (jakarta.ws.rs.client.ClientResponseContext)1 ClientResponseFilter (jakarta.ws.rs.client.ClientResponseFilter)1 Entity (jakarta.ws.rs.client.Entity)1 WebTarget (jakarta.ws.rs.client.WebTarget)1 ContainerResponseFilter (jakarta.ws.rs.container.ContainerResponseFilter)1 Context (jakarta.ws.rs.core.Context)1 HttpHeaders (jakarta.ws.rs.core.HttpHeaders)1 MediaType (jakarta.ws.rs.core.MediaType)1 MultivaluedMap (jakarta.ws.rs.core.MultivaluedMap)1 ContextResolver (jakarta.ws.rs.ext.ContextResolver)1 MessageBodyReader (jakarta.ws.rs.ext.MessageBodyReader)1 MessageBodyWriter (jakarta.ws.rs.ext.MessageBodyWriter)1 Providers (jakarta.ws.rs.ext.Providers)1