Search in sources :

Example 1 with ReaderInputStream

use of org.apache.cxf.io.ReaderInputStream in project cxf by apache.

the class JAXRSUtils method processParameter.

private static Object processParameter(Class<?> parameterClass, Type parameterType, Annotation[] parameterAnns, Parameter parameter, MultivaluedMap<String, String> values, Message message, OperationResourceInfo ori) throws IOException, WebApplicationException {
    InputStream is = message.getContent(InputStream.class);
    if (is == null) {
        Reader reader = message.getContent(Reader.class);
        if (reader != null) {
            is = new ReaderInputStream(reader);
        }
    }
    if (parameter.getType() == ParameterType.REQUEST_BODY) {
        if (parameterClass == AsyncResponse.class) {
            return new AsyncResponseImpl(message);
        }
        String contentType = (String) message.get(Message.CONTENT_TYPE);
        if (contentType == null) {
            String defaultCt = (String) message.getContextualProperty(DEFAULT_CONTENT_TYPE);
            contentType = defaultCt == null ? MediaType.APPLICATION_OCTET_STREAM : defaultCt;
        }
        return readFromMessageBody(parameterClass, parameterType, parameterAnns, is, toMediaType(contentType), ori, message);
    } else if (parameter.getType() == ParameterType.CONTEXT) {
        return createContextValue(message, parameterType, parameterClass);
    } else if (parameter.getType() == ParameterType.BEAN) {
        return createBeanParamValue(message, parameterClass, ori);
    } else {
        return createHttpParameterValue(parameter, parameterClass, parameterType, parameterAnns, message, values, ori);
    }
}
Also used : ReaderInputStream(org.apache.cxf.io.ReaderInputStream) ReaderInputStream(org.apache.cxf.io.ReaderInputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) AsyncResponseImpl(org.apache.cxf.jaxrs.impl.AsyncResponseImpl)

Example 2 with ReaderInputStream

use of org.apache.cxf.io.ReaderInputStream 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) {
        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);
            autoClose(cls, false);
            return castLastEntity();
        } 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 : ReaderInputStream(org.apache.cxf.io.ReaderInputStream) ReaderInterceptor(javax.ws.rs.ext.ReaderInterceptor) Message(org.apache.cxf.message.Message) ReaderInputStream(org.apache.cxf.io.ReaderInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ProviderFactory(org.apache.cxf.jaxrs.provider.ProviderFactory) MediaType(javax.ws.rs.core.MediaType) XMLStreamReader(javax.xml.stream.XMLStreamReader) Reader(java.io.Reader) IOException(java.io.IOException) ProcessingException(javax.ws.rs.ProcessingException) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException)

Aggregations

InputStream (java.io.InputStream)2 Reader (java.io.Reader)2 ReaderInputStream (org.apache.cxf.io.ReaderInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ProcessingException (javax.ws.rs.ProcessingException)1 ResponseProcessingException (javax.ws.rs.client.ResponseProcessingException)1 MediaType (javax.ws.rs.core.MediaType)1 MessageBodyReader (javax.ws.rs.ext.MessageBodyReader)1 ReaderInterceptor (javax.ws.rs.ext.ReaderInterceptor)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 AsyncResponseImpl (org.apache.cxf.jaxrs.impl.AsyncResponseImpl)1 ProviderFactory (org.apache.cxf.jaxrs.provider.ProviderFactory)1 Message (org.apache.cxf.message.Message)1