Search in sources :

Example 6 with ReaderInterceptor

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

the class ResponseImpl method doReadEntity.

public <T> T doReadEntity(Class<T> cls, Type t, Annotation[] anns) throws ProcessingException, IllegalStateException {
    checkEntityIsClosed();
    // according to javadoc, should close when is not buffered.
    boolean shouldClose = !entityBufferred && !JAXRSUtils.isStreamingOutType(cls);
    if (lastEntity != null && cls.isAssignableFrom(lastEntity.getClass()) && !(lastEntity instanceof InputStream)) {
        return cls.cast(lastEntity);
    }
    MediaType mediaType = getMediaType();
    if (mediaType == null) {
        mediaType = MediaType.WILDCARD_TYPE;
    }
    // the stream is available if entity is IS or
    // message contains XMLStreamReader or Reader
    boolean entityStreamAvailable = entityStreamAvailable();
    InputStream entityStream = null;
    if (!entityStreamAvailable) {
        // try create a stream if the entity is String or Number
        entityStream = convertEntityToStreamIfPossible();
        entityStreamAvailable = entityStream != null;
    } else if (entity instanceof InputStream) {
        entityStream = InputStream.class.cast(entity);
    } else {
        Message inMessage = getResponseMessage();
        Reader reader = inMessage.getContent(Reader.class);
        if (reader != null) {
            entityStream = InputStream.class.cast(new ReaderInputStream(reader));
        }
    }
    // we need to check for readers even if no IS is set - the readers may still do it
    List<ReaderInterceptor> readers = outMessage == null ? null : ProviderFactory.getInstance(outMessage).createMessageBodyReaderInterceptor(cls, t, anns, mediaType, outMessage, entityStreamAvailable, null);
    if (readers != null) {
        try {
            if (entityBufferred) {
                InputStream.class.cast(entity).reset();
            }
            Message responseMessage = getResponseMessage();
            responseMessage.put(Message.PROTOCOL_HEADERS, getHeaders());
            lastEntity = JAXRSUtils.readFromMessageBodyReader(readers, cls, t, anns, entityStream, mediaType, responseMessage);
            // close the entity after readEntity is called.
            T tCastLastEntity = castLastEntity();
            shouldClose = shouldClose && !(tCastLastEntity instanceof AutoCloseable) && !(tCastLastEntity instanceof Source);
            if (shouldClose) {
                close();
            }
            return tCastLastEntity;
        } catch (NoContentException ex) {
            // check if basic type. Basic type throw exception, else return null.
            if (isBasicType(cls)) {
                autoClose(cls, true);
                reportMessageHandlerProblem("MSG_READER_PROBLEM", cls, mediaType, ex);
            } else {
                if (shouldClose) {
                    close();
                }
                return null;
            }
        } catch (Exception ex) {
            autoClose(cls, true);
            reportMessageHandlerProblem("MSG_READER_PROBLEM", cls, mediaType, ex);
        } finally {
            ProviderFactory pf = ProviderFactory.getInstance(outMessage);
            if (pf != null) {
                pf.clearThreadLocalProxies();
            }
        }
    } else if (entity != null && cls.isAssignableFrom(entity.getClass())) {
        lastEntity = entity;
        return castLastEntity();
    } else if (entityStreamAvailable) {
        reportMessageHandlerProblem("NO_MSG_READER", cls, mediaType, null);
    }
    throw new IllegalStateException("The entity is not backed by an input stream, entity class is : " + (entity != null ? entity.getClass().getName() : cls.getName()));
}
Also used : ReaderInterceptor(jakarta.ws.rs.ext.ReaderInterceptor) Message(org.apache.cxf.message.Message) PushbackInputStream(java.io.PushbackInputStream) ReaderInputStream(org.apache.cxf.io.ReaderInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) XMLStreamReader(javax.xml.stream.XMLStreamReader) Reader(java.io.Reader) NoContentException(jakarta.ws.rs.core.NoContentException) Source(javax.xml.transform.Source) ResponseProcessingException(jakarta.ws.rs.client.ResponseProcessingException) ProcessingException(jakarta.ws.rs.ProcessingException) IOException(java.io.IOException) NoContentException(jakarta.ws.rs.core.NoContentException) ReaderInputStream(org.apache.cxf.io.ReaderInputStream) ProviderFactory(org.apache.cxf.jaxrs.provider.ProviderFactory) MediaType(jakarta.ws.rs.core.MediaType)

Example 7 with ReaderInterceptor

use of jakarta.ws.rs.ext.ReaderInterceptor 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)

Example 8 with ReaderInterceptor

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

the class ConfigurationInheritanceTest method testReaderInterceptorInheritance.

@Test
public void testReaderInterceptorInheritance() {
    Client client = ClientBuilder.newClient();
    try {
        fakeHttpServer.start();
        WebTarget parentWebTarget = client.target("http://" + fakeHttpServer.getHostAndPort());
        WebTarget childWebTarget = parentWebTarget.path("path");
        childWebTarget.register((ClientResponseFilter) (containerRequestContext, containerResponseContext) -> {
            containerResponseContext.getHeaders().putSingle(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN);
            containerResponseContext.setEntityStream(new ByteArrayInputStream("hello".getBytes()));
        });
        // Registration on parent MUST not affect the child
        AtomicInteger parentReaderInterceptorCounter = new AtomicInteger(0);
        ReaderInterceptor parentReaderInterceptor = (readerInterceptorContext) -> {
            parentReaderInterceptorCounter.incrementAndGet();
            return readerInterceptorContext.proceed();
        };
        parentWebTarget.register(parentReaderInterceptor);
        childWebTarget.request().get().readEntity(String.class);
        Assert.assertEquals(0, parentReaderInterceptorCounter.get());
        // Child MUST only use the snapshot configuration of the parent
        // taken at child creation time.
        AtomicInteger childReaderInterceptorCounter = new AtomicInteger(0);
        ReaderInterceptor childReaderInterceptor = (readerInterceptorContext) -> {
            childReaderInterceptorCounter.incrementAndGet();
            return readerInterceptorContext.proceed();
        };
        childWebTarget.register(childReaderInterceptor);
        childWebTarget.request().get().readEntity(String.class);
        Assert.assertEquals(1, childReaderInterceptorCounter.get());
        Assert.assertEquals(0, parentReaderInterceptorCounter.get());
    } finally {
        client.close();
    }
}
Also used : Context(jakarta.ws.rs.core.Context) WriterInterceptor(jakarta.ws.rs.ext.WriterInterceptor) WebApplicationException(jakarta.ws.rs.WebApplicationException) ByteArrayInputStream(java.io.ByteArrayInputStream) MultivaluedMap(jakarta.ws.rs.core.MultivaluedMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RuntimeType(jakarta.ws.rs.RuntimeType) Set(java.util.Set) WebTarget(jakarta.ws.rs.client.WebTarget) Entity(jakarta.ws.rs.client.Entity) List(java.util.List) MediaType(jakarta.ws.rs.core.MediaType) Type(java.lang.reflect.Type) ContextResolver(jakarta.ws.rs.ext.ContextResolver) Annotation(java.lang.annotation.Annotation) ResteasyProviderFactoryImpl(org.jboss.resteasy.core.providerfactory.ResteasyProviderFactoryImpl) ClientBuilder(jakarta.ws.rs.client.ClientBuilder) FakeHttpServer(org.jboss.resteasy.test.common.FakeHttpServer) ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) Configuration(jakarta.ws.rs.core.Configuration) ResteasyClientBuilder(org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder) ArrayList(java.util.ArrayList) MessageBodyWriter(jakarta.ws.rs.ext.MessageBodyWriter) ClientResponseFilter(jakarta.ws.rs.client.ClientResponseFilter) ConfigurationInheritanceTestFeature6(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestFeature6) ConfigurationInheritanceTestFeature1(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestFeature1) ConfigurationInheritanceTestFeature5(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestFeature5) ConfigurationInheritanceTestFeature4(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestFeature4) ConfigurationInheritanceTestFeature3(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestFeature3) ConfigurationInheritanceTestFeature2(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestFeature2) OutputStream(java.io.OutputStream) ConfigurationInheritanceTestFilter6(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestFilter6) Client(jakarta.ws.rs.client.Client) ConfigurationInheritanceTestFilter4(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestFilter4) ConfigurationInheritanceTestFilter5(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestFilter5) ConfigurationInheritanceTestFilter2(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestFilter2) ConfigurationInheritanceTestMessageBodyReader4(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestMessageBodyReader4) ClientRequestFilter(jakarta.ws.rs.client.ClientRequestFilter) ConfigurationInheritanceTestFilter3(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestFilter3) ConfigurationInheritanceTestMessageBodyReader5(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestMessageBodyReader5) ReaderInterceptor(jakarta.ws.rs.ext.ReaderInterceptor) ConfigurationInheritanceTestMessageBodyReader6(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestMessageBodyReader6) ConfigurationInheritanceTestFilter1(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestFilter1) IOException(java.io.IOException) Test(org.junit.Test) Rule(org.junit.Rule) HttpHeaders(jakarta.ws.rs.core.HttpHeaders) ConfigurationInheritanceTestMessageBodyReader1(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestMessageBodyReader1) Providers(jakarta.ws.rs.ext.Providers) ConfigurationInheritanceTestMessageBodyReader2(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestMessageBodyReader2) MessageBodyReader(jakarta.ws.rs.ext.MessageBodyReader) ConfigurationInheritanceTestMessageBodyReader3(org.jboss.resteasy.test.client.resource.ConfigurationInheritanceTestMessageBodyReader3) Assert(org.junit.Assert) ResteasyClientBuilderImpl(org.jboss.resteasy.client.jaxrs.internal.ResteasyClientBuilderImpl) InputStream(java.io.InputStream) ReaderInterceptor(jakarta.ws.rs.ext.ReaderInterceptor) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WebTarget(jakarta.ws.rs.client.WebTarget) Client(jakarta.ws.rs.client.Client) Test(org.junit.Test)

Aggregations

ReaderInterceptor (jakarta.ws.rs.ext.ReaderInterceptor)8 MediaType (jakarta.ws.rs.core.MediaType)6 IOException (java.io.IOException)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 InputStream (java.io.InputStream)5 Type (java.lang.reflect.Type)4 ProcessingException (jakarta.ws.rs.ProcessingException)3 WebApplicationException (jakarta.ws.rs.WebApplicationException)3 Providers (jakarta.ws.rs.ext.Providers)3 Client (jakarta.ws.rs.client.Client)2 ClientBuilder (jakarta.ws.rs.client.ClientBuilder)2 ClientRequestContext (jakarta.ws.rs.client.ClientRequestContext)2 ClientRequestFilter (jakarta.ws.rs.client.ClientRequestFilter)2 ClientResponseFilter (jakarta.ws.rs.client.ClientResponseFilter)2 Entity (jakarta.ws.rs.client.Entity)2 WebTarget (jakarta.ws.rs.client.WebTarget)2 Context (jakarta.ws.rs.core.Context)2 HttpHeaders (jakarta.ws.rs.core.HttpHeaders)2 MultivaluedMap (jakarta.ws.rs.core.MultivaluedMap)2 NoContentException (jakarta.ws.rs.core.NoContentException)2