Search in sources :

Example 1 with EntityInputStream

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);
    }
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) UnmarshalException(javax.xml.bind.UnmarshalException) JAXBException(javax.xml.bind.JAXBException) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) EntityInputStream(org.glassfish.jersey.message.internal.EntityInputStream) NoContentException(javax.ws.rs.core.NoContentException)

Example 2 with EntityInputStream

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);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) JAXBException(javax.xml.bind.JAXBException) NoContentException(javax.ws.rs.core.NoContentException) XMLStreamException(javax.xml.stream.XMLStreamException) BadRequestException(javax.ws.rs.BadRequestException) UnmarshalException(javax.xml.bind.UnmarshalException) NoContentException(javax.ws.rs.core.NoContentException) IOException(java.io.IOException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) JAXBException(javax.xml.bind.JAXBException) WebApplicationException(javax.ws.rs.WebApplicationException) XmlType(javax.xml.bind.annotation.XmlType) XMLStreamException(javax.xml.stream.XMLStreamException) UnmarshalException(javax.xml.bind.UnmarshalException) Collection(java.util.Collection) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) EntityInputStream(org.glassfish.jersey.message.internal.EntityInputStream) Unmarshaller(javax.xml.bind.Unmarshaller)

Aggregations

BadRequestException (javax.ws.rs.BadRequestException)2 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)2 NoContentException (javax.ws.rs.core.NoContentException)2 JAXBException (javax.xml.bind.JAXBException)2 UnmarshalException (javax.xml.bind.UnmarshalException)2 EntityInputStream (org.glassfish.jersey.message.internal.EntityInputStream)2 IOException (java.io.IOException)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Collection (java.util.Collection)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 XmlType (javax.xml.bind.annotation.XmlType)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1