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);
}
}
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()));
}
Aggregations