Search in sources :

Example 1 with ReaderInterceptor

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

the class ClientResponse method readFrom.

// this is synchronized in conjunction with finalize to protect against premature finalize called by the GC
@Override
protected synchronized <T> Object readFrom(Class<T> type, Type genericType, MediaType media, Annotation[] annotations) {
    Type useGeneric = genericType == null ? type : genericType;
    Class<?> useType = type;
    media = media == null ? MediaType.WILDCARD_TYPE : media;
    annotations = annotations == null ? this.annotations : annotations;
    boolean isMarshalledEntity = false;
    if (type.equals(MarshalledEntity.class)) {
        isMarshalledEntity = true;
        ParameterizedType param = (ParameterizedType) useGeneric;
        useGeneric = param.getActualTypeArguments()[0];
        useType = Types.getRawType(useGeneric);
    }
    Providers current = ResteasyContext.getContextData(Providers.class);
    ResteasyContext.pushContext(Providers.class, configuration);
    Object obj = null;
    try {
        InputStream is = getEntityStream();
        if (is == null) {
            throw new IllegalStateException(Messages.MESSAGES.inputStreamWasEmpty());
        }
        if (isMarshalledEntity) {
            is = new InputStreamToByteArray(is);
        }
        ReaderInterceptor[] readerInterceptors = configuration.getReaderInterceptors(null, null);
        final Object finalObj;
        final long timestamp = tracingLogger.timestamp("RI_SUMMARY");
        AbstractReaderInterceptorContext context = new ClientReaderInterceptorContext(readerInterceptors, configuration.getProviderFactory(), useType, useGeneric, annotations, media, getStringHeaders(), is, properties, tracingLogger);
        try {
            finalObj = context.proceed();
            obj = finalObj;
        } finally {
            tracingLogger.logDuration("RI_SUMMARY", timestamp, context.getProcessedInterceptorCount());
        }
        if (isMarshalledEntity) {
            InputStreamToByteArray isba = (InputStreamToByteArray) is;
            final byte[] bytes = isba.toByteArray();
            return new MarshalledEntity<Object>() {

                @Override
                public byte[] getMarshalledBytes() {
                    return bytes;
                }

                @Override
                public Object getEntity() {
                    return finalObj;
                }
            };
        } else {
            return finalObj;
        }
    } catch (ProcessingException pe) {
        throw pe;
    } catch (Exception ex) {
        throw new ProcessingException(ex);
    } finally {
        if (!Publisher.class.isAssignableFrom(type) && !EventInput.class.isAssignableFrom(type)) {
            ResteasyContext.popContextData(Providers.class);
            if (current != null)
                ResteasyContext.pushContext(Providers.class, current);
            if (obj instanceof ProvidersContextRetainer)
                ((ProvidersContextRetainer) obj).setProviders(configuration);
        }
    }
}
Also used : AbstractReaderInterceptorContext(org.jboss.resteasy.core.interception.jaxrs.AbstractReaderInterceptorContext) ReaderInterceptor(jakarta.ws.rs.ext.ReaderInterceptor) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MarshalledEntity(org.jboss.resteasy.spi.MarshalledEntity) ProvidersContextRetainer(org.jboss.resteasy.core.ProvidersContextRetainer) Providers(jakarta.ws.rs.ext.Providers) ClientReaderInterceptorContext(org.jboss.resteasy.core.interception.jaxrs.ClientReaderInterceptorContext) ProcessingException(jakarta.ws.rs.ProcessingException) IOException(java.io.IOException) ParameterizedType(java.lang.reflect.ParameterizedType) MediaType(jakarta.ws.rs.core.MediaType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) InputStreamToByteArray(org.jboss.resteasy.util.InputStreamToByteArray) ProcessingException(jakarta.ws.rs.ProcessingException)

Example 2 with ReaderInterceptor

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

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

the class BuiltResponse method readFrom.

// this is synchronized in conjunction with finalize to protect against premature finalize called by the GC
protected synchronized <T> Object readFrom(Class<T> type, Type genericType, MediaType media, Annotation[] annotations) {
    Type useGeneric = genericType == null ? type : genericType;
    Class<?> useType = type;
    media = media == null ? MediaType.WILDCARD_TYPE : media;
    annotations = annotations == null ? this.annotations : annotations;
    boolean isMarshalledEntity = false;
    if (type.equals(MarshalledEntity.class)) {
        isMarshalledEntity = true;
        ParameterizedType param = (ParameterizedType) useGeneric;
        useGeneric = param.getActualTypeArguments()[0];
        useType = Types.getRawType(useGeneric);
    }
    try {
        InputStream is = getEntityStream();
        if (is == null) {
            throw new IllegalStateException(Messages.MESSAGES.inputStreamWasEmpty());
        }
        if (isMarshalledEntity) {
            is = new InputStreamToByteArray(is);
        }
        ResteasyProviderFactory providerFactory = ResteasyProviderFactory.getInstance();
        ReaderInterceptor[] readerInterceptors = providerFactory.getServerReaderInterceptorRegistry().postMatch(null, null);
        final Object finalObj;
        AbstractReaderInterceptorContext context = new ClientReaderInterceptorContext(readerInterceptors, providerFactory, useType, useGeneric, annotations, media, getStringHeaders(), is, new HashMap<String, Object>(), null);
        finalObj = context.proceed();
        if (isMarshalledEntity) {
            InputStreamToByteArray isba = (InputStreamToByteArray) is;
            final byte[] bytes = isba.toByteArray();
            return new MarshalledEntity<Object>() {

                @Override
                public byte[] getMarshalledBytes() {
                    return bytes;
                }

                @Override
                public Object getEntity() {
                    return finalObj;
                }
            };
        } else {
            return finalObj;
        }
    } catch (ProcessingException pe) {
        throw pe;
    } catch (Exception ex) {
        throw new ProcessingException(ex);
    }
}
Also used : AbstractReaderInterceptorContext(org.jboss.resteasy.core.interception.jaxrs.AbstractReaderInterceptorContext) ReaderInterceptor(jakarta.ws.rs.ext.ReaderInterceptor) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MarshalledEntity(org.jboss.resteasy.spi.MarshalledEntity) ClientReaderInterceptorContext(org.jboss.resteasy.core.interception.jaxrs.ClientReaderInterceptorContext) IOException(java.io.IOException) ProcessingException(jakarta.ws.rs.ProcessingException) ParameterizedType(java.lang.reflect.ParameterizedType) MediaType(jakarta.ws.rs.core.MediaType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) InputStreamToByteArray(org.jboss.resteasy.util.InputStreamToByteArray) ResteasyProviderFactory(org.jboss.resteasy.spi.ResteasyProviderFactory) ProcessingException(jakarta.ws.rs.ProcessingException)

Example 4 with ReaderInterceptor

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

the class ProviderFactory method createMessageBodyReaderInterceptor.

public <T> List<ReaderInterceptor> createMessageBodyReaderInterceptor(Class<T> bodyType, Type parameterType, Annotation[] parameterAnnotations, MediaType mediaType, Message m, boolean checkMbrNow, Set<String> names) {
    MessageBodyReader<T> mr = !checkMbrNow ? null : createMessageBodyReader(bodyType, parameterType, parameterAnnotations, mediaType, m);
    int size = readerInterceptors.size();
    if (mr != null || size > 0) {
        ReaderInterceptor mbrReader = new ReaderInterceptorMBR(mr, getResponseMessage(m));
        final List<ReaderInterceptor> interceptors;
        if (size > 0) {
            interceptors = new ArrayList<>(size + 1);
            List<ProviderInfo<ReaderInterceptor>> readers = getBoundFilters(readerInterceptors, names);
            for (ProviderInfo<ReaderInterceptor> p : readers) {
                injectContextValues(p, m);
                interceptors.add(p.getProvider());
            }
            interceptors.add(mbrReader);
        } else {
            interceptors = Collections.singletonList(mbrReader);
        }
        return interceptors;
    }
    return null;
}
Also used : ReaderInterceptor(jakarta.ws.rs.ext.ReaderInterceptor) FilterProviderInfo(org.apache.cxf.jaxrs.model.FilterProviderInfo) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) ReaderInterceptorMBR(org.apache.cxf.jaxrs.impl.ReaderInterceptorMBR) Endpoint(org.apache.cxf.endpoint.Endpoint)

Example 5 with ReaderInterceptor

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

the class JAXRSUtils method readFromMessageBody.

private static Object readFromMessageBody(Class<?> targetTypeClass, Type parameterType, Annotation[] parameterAnnotations, InputStream is, MediaType contentType, OperationResourceInfo ori, Message m) throws IOException, WebApplicationException {
    List<MediaType> types = JAXRSUtils.intersectMimeTypes(ori.getConsumeTypes(), contentType);
    final ProviderFactory pf = ServerProviderFactory.getInstance(m);
    for (MediaType type : types) {
        List<ReaderInterceptor> readers = pf.createMessageBodyReaderInterceptor(targetTypeClass, parameterType, parameterAnnotations, type, m, true, ori.getNameBindings());
        if (readers != null) {
            try {
                return readFromMessageBodyReader(readers, targetTypeClass, parameterType, parameterAnnotations, is, type, m);
            } catch (IOException e) {
                if (e.getClass().getName().equals(NO_CONTENT_EXCEPTION)) {
                    throw ExceptionUtils.toBadRequestException(e, null);
                }
                throw e;
            } catch (WebApplicationException ex) {
                throw ex;
            } catch (Exception ex) {
                throw new Fault(ex);
            }
        }
    }
    logMessageHandlerProblem("NO_MSG_READER", targetTypeClass, contentType);
    throw new WebApplicationException(Response.Status.UNSUPPORTED_MEDIA_TYPE);
}
Also used : ReaderInterceptor(jakarta.ws.rs.ext.ReaderInterceptor) WebApplicationException(jakarta.ws.rs.WebApplicationException) ProviderFactory(org.apache.cxf.jaxrs.provider.ProviderFactory) ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) MediaType(jakarta.ws.rs.core.MediaType) Fault(org.apache.cxf.interceptor.Fault) IOException(java.io.IOException) WebApplicationException(jakarta.ws.rs.WebApplicationException) IOException(java.io.IOException) NoContentException(jakarta.ws.rs.core.NoContentException) ClientErrorException(jakarta.ws.rs.ClientErrorException)

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