use of org.glassfish.jersey.message.internal.EntityInputStream 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 org.glassfish.jersey.message.internal.EntityInputStream 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