use of javax.ws.rs.core.NoContentException in project jersey by jersey.
the class AbstractJaxbElementProvider method readFrom.
@Override
public final JAXBElement<?> readFrom(Class<JAXBElement<?>> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream inputStream) throws IOException {
final EntityInputStream entityStream = EntityInputStream.create(inputStream);
if (entityStream.isEmpty()) {
throw new NoContentException(LocalizationMessages.ERROR_READING_ENTITY_MISSING());
}
final ParameterizedType pt = (ParameterizedType) genericType;
final Class ta = (Class) pt.getActualTypeArguments()[0];
try {
return readFrom(ta, mediaType, getUnmarshaller(ta, mediaType), entityStream);
} catch (UnmarshalException ex) {
throw new BadRequestException(ex);
} catch (JAXBException ex) {
throw new InternalServerErrorException(ex);
}
}
use of javax.ws.rs.core.NoContentException in project jersey by jersey.
the class BasicTypesMessageProvider method readFrom.
@Override
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
final String entityString = readFromAsString(entityStream, mediaType);
if (entityString.isEmpty()) {
throw new NoContentException(LocalizationMessages.ERROR_READING_ENTITY_MISSING());
}
final PrimitiveTypes primitiveType = PrimitiveTypes.forType(type);
if (primitiveType != null) {
return primitiveType.convert(entityString);
}
final Constructor constructor = AccessController.doPrivileged(ReflectionHelper.getStringConstructorPA(type));
if (constructor != null) {
try {
return type.cast(constructor.newInstance(entityString));
} catch (Exception e) {
throw new MessageBodyProcessingException(LocalizationMessages.ERROR_ENTITY_PROVIDER_BASICTYPES_CONSTRUCTOR(type));
}
}
if (AtomicInteger.class.isAssignableFrom(type)) {
return new AtomicInteger((Integer) PrimitiveTypes.INTEGER.convert(entityString));
}
if (AtomicLong.class.isAssignableFrom(type)) {
return new AtomicLong((Long) PrimitiveTypes.LONG.convert(entityString));
}
throw new MessageBodyProcessingException(LocalizationMessages.ERROR_ENTITY_PROVIDER_BASICTYPES_UNKWNOWN(type));
}
use of javax.ws.rs.core.NoContentException in project tomee by apache.
the class AbstractJAXBProvider method handleExceptionEnd.
protected static void handleExceptionEnd(Throwable t, String message, boolean read) throws NoContentException {
if (t instanceof WstxEOFException && t.getMessage().startsWith("Unexpected EOF in prolog")) {
String noContent = new org.apache.cxf.common.i18n.Message("EMPTY_BODY", BUNDLE).toString();
LOG.warning(noContent);
throw new NoContentException(noContent);
}
Response.Status status = read ? Response.Status.BAD_REQUEST : Response.Status.INTERNAL_SERVER_ERROR;
Response r = JAXRSUtils.toResponseBuilder(status).type(MediaType.TEXT_PLAIN).entity(message).build();
throw read ? ExceptionUtils.toBadRequestException(t, r) : ExceptionUtils.toInternalServerErrorException(t, r);
}
use of javax.ws.rs.core.NoContentException in project tomee by apache.
the class ResponseImpl method doReadEntity.
public <T> T doReadEntity(Class<T> cls, Type t, Annotation[] anns) throws ProcessingException, IllegalStateException {
checkEntityIsClosed();
// according to javadoc, should close when is not buffered.
boolean shouldClose = !entityBufferred && !JAXRSUtils.isStreamingOutType(cls);
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);
// close the entity after readEntity is called.
T tCastLastEntity = castLastEntity();
shouldClose = shouldClose && !(tCastLastEntity instanceof AutoCloseable) && !(tCastLastEntity instanceof Source);
if (shouldClose) {
close();
}
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 {
if (shouldClose) {
close();
}
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()));
}
use of javax.ws.rs.core.NoContentException in project jersey by jersey.
the class AbstractCollectionJaxbProvider method readFrom.
@Override
@SuppressWarnings("unchecked")
public final Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream inputStream) throws IOException {
final EntityInputStream entityStream = EntityInputStream.create(inputStream);
if (entityStream.isEmpty()) {
throw new NoContentException(LocalizationMessages.ERROR_READING_ENTITY_MISSING());
}
try {
final Class<?> elementType = getElementClass(type, genericType);
final Unmarshaller u = getUnmarshaller(elementType, mediaType);
final XMLStreamReader r = getXMLStreamReader(elementType, mediaType, u, entityStream);
boolean jaxbElement = false;
Collection<Object> l = null;
if (type.isArray()) {
l = new ArrayList<Object>();
} else {
try {
l = (Collection<Object>) type.newInstance();
} catch (Exception e) {
for (Class<?> c : DEFAULT_IMPLS) {
if (type.isAssignableFrom(c)) {
try {
l = (Collection<Object>) c.newInstance();
break;
} catch (InstantiationException ex) {
LOGGER.log(Level.WARNING, LocalizationMessages.UNABLE_TO_INSTANTIATE_CLASS(c.getName()), ex);
} catch (IllegalAccessException ex) {
LOGGER.log(Level.WARNING, LocalizationMessages.UNABLE_TO_INSTANTIATE_CLASS(c.getName()), ex);
} catch (SecurityException ex) {
LOGGER.log(Level.WARNING, LocalizationMessages.UNABLE_TO_INSTANTIATE_CLASS(c.getName()), ex);
}
}
}
}
}
if (l == null) {
l = new ArrayList<Object>();
}
// Move to root element
int event = r.next();
while (event != XMLStreamReader.START_ELEMENT) {
event = r.next();
}
// Move to first child (if any)
event = r.next();
while (event != XMLStreamReader.START_ELEMENT && event != XMLStreamReader.END_DOCUMENT) {
event = r.next();
}
while (event != XMLStreamReader.END_DOCUMENT) {
if (elementType.isAnnotationPresent(XmlRootElement.class)) {
l.add(u.unmarshal(r));
} else if (elementType.isAnnotationPresent(XmlType.class)) {
l.add(u.unmarshal(r, elementType).getValue());
} else {
l.add(u.unmarshal(r, elementType));
jaxbElement = true;
}
// Move to next peer (if any)
event = r.getEventType();
while (event != XMLStreamReader.START_ELEMENT && event != XMLStreamReader.END_DOCUMENT) {
event = r.next();
}
}
return (type.isArray()) ? createArray(l, jaxbElement ? JAXBElement.class : elementType) : l;
} catch (UnmarshalException ex) {
throw new BadRequestException(ex);
} catch (XMLStreamException ex) {
throw new BadRequestException(ex);
} catch (JAXBException ex) {
throw new InternalServerErrorException(ex);
}
}
Aggregations