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