Search in sources :

Example 6 with NoContentException

use of javax.ws.rs.core.NoContentException in project cxf by apache.

the class ResponseImpl method doReadEntity.

public <T> T doReadEntity(Class<T> cls, Type t, Annotation[] anns) throws ProcessingException, IllegalStateException {
    checkEntityIsClosed();
    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) {
        // By default, the response entity was never closed automatically which is not compliant
        // with JAX-RS specification and TCK. The auto-close behavior is controlled by
        // ResponseImpl#RESPONSE_STREAM_AUTO_CLOSE property which is set to "false" by default.
        // The autoCloseHint alters the default value of this property to be "true" for all
        // non-streaming and non-streaming like responses (to minimize the impact of the change
        // as much as possible) in order to follow the specification.
        final boolean autoCloseHint = !JAXRSUtils.isStreamingLikeOutType(cls, t);
        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();
            autoCloseWithHint(cls, autoCloseHint, false);
            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 {
                autoCloseWithHint(cls, autoCloseHint, false);
                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(javax.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(javax.ws.rs.core.NoContentException) NoContentException(javax.ws.rs.core.NoContentException) IOException(java.io.IOException) ProcessingException(javax.ws.rs.ProcessingException) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException) ReaderInputStream(org.apache.cxf.io.ReaderInputStream) ProviderFactory(org.apache.cxf.jaxrs.provider.ProviderFactory) MediaType(javax.ws.rs.core.MediaType)

Aggregations

NoContentException (javax.ws.rs.core.NoContentException)6 IOException (java.io.IOException)4 XMLStreamReader (javax.xml.stream.XMLStreamReader)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 PushbackInputStream (java.io.PushbackInputStream)2 Reader (java.io.Reader)2 BadRequestException (javax.ws.rs.BadRequestException)2 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)2 ProcessingException (javax.ws.rs.ProcessingException)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 ResponseProcessingException (javax.ws.rs.client.ResponseProcessingException)2 MediaType (javax.ws.rs.core.MediaType)2 ReaderInterceptor (javax.ws.rs.ext.ReaderInterceptor)2 JAXBException (javax.xml.bind.JAXBException)2 UnmarshalException (javax.xml.bind.UnmarshalException)2 ReaderInputStream (org.apache.cxf.io.ReaderInputStream)2 ProviderFactory (org.apache.cxf.jaxrs.provider.ProviderFactory)2 Message (org.apache.cxf.message.Message)2 EntityInputStream (org.glassfish.jersey.message.internal.EntityInputStream)2