Search in sources :

Example 1 with ClientReaderInterceptorContext

use of org.jboss.resteasy.core.interception.jaxrs.ClientReaderInterceptorContext 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 ClientReaderInterceptorContext

use of org.jboss.resteasy.core.interception.jaxrs.ClientReaderInterceptorContext 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 3 with ClientReaderInterceptorContext

use of org.jboss.resteasy.core.interception.jaxrs.ClientReaderInterceptorContext in project resteasy by resteasy.

the class ReaderUtility method doRead.

public <T> T doRead(Class<T> type, Type genericType, MediaType mediaType, Annotation[] annotations, MultivaluedMap<String, String> requestHeaders, InputStream inputStream) throws IOException {
    try {
        final Map<String, Object> attributes = new HashMap<String, Object>();
        AbstractReaderInterceptorContext messageBodyReaderContext = new ClientReaderInterceptorContext(interceptors, factory, type, genericType, annotations, mediaType, requestHeaders, inputStream, attributes, null);
        return (T) messageBodyReaderContext.proceed();
    } catch (Exception e) {
        if (e instanceof ReaderException) {
            throw (ReaderException) e;
        } else {
            throw new ReaderException(e);
        }
    }
}
Also used : AbstractReaderInterceptorContext(org.jboss.resteasy.core.interception.jaxrs.AbstractReaderInterceptorContext) HashMap(java.util.HashMap) ClientReaderInterceptorContext(org.jboss.resteasy.core.interception.jaxrs.ClientReaderInterceptorContext) IOException(java.io.IOException) ReaderException(org.jboss.resteasy.spi.ReaderException) ReaderException(org.jboss.resteasy.spi.ReaderException)

Aggregations

IOException (java.io.IOException)3 AbstractReaderInterceptorContext (org.jboss.resteasy.core.interception.jaxrs.AbstractReaderInterceptorContext)3 ClientReaderInterceptorContext (org.jboss.resteasy.core.interception.jaxrs.ClientReaderInterceptorContext)3 ProcessingException (jakarta.ws.rs.ProcessingException)2 MediaType (jakarta.ws.rs.core.MediaType)2 ReaderInterceptor (jakarta.ws.rs.ext.ReaderInterceptor)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2 MarshalledEntity (org.jboss.resteasy.spi.MarshalledEntity)2 InputStreamToByteArray (org.jboss.resteasy.util.InputStreamToByteArray)2 Providers (jakarta.ws.rs.ext.Providers)1 HashMap (java.util.HashMap)1 ProvidersContextRetainer (org.jboss.resteasy.core.ProvidersContextRetainer)1 ReaderException (org.jboss.resteasy.spi.ReaderException)1 ResteasyProviderFactory (org.jboss.resteasy.spi.ResteasyProviderFactory)1