Search in sources :

Example 1 with UnmarshalException

use of javax.xml.bind.UnmarshalException 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 UnmarshalException

use of javax.xml.bind.UnmarshalException in project openhab1-addons by openhab.

the class DenonConnector method getDocument.

private <T> T getDocument(String uri, Class<T> response) {
    try {
        String result = doHttpRequest("GET", uri, null);
        logger.trace("result of getDocument for uri '{}':\r\n{}", uri, result);
        if (StringUtils.isNotBlank(result)) {
            JAXBContext jc = JAXBContext.newInstance(response);
            XMLInputFactory xif = XMLInputFactory.newInstance();
            XMLStreamReader xsr = xif.createXMLStreamReader(IOUtils.toInputStream(result));
            xsr = new PropertyRenamerDelegate(xsr);
            @SuppressWarnings("unchecked") T obj = (T) jc.createUnmarshaller().unmarshal(xsr);
            return obj;
        }
    } catch (UnmarshalException e) {
        logger.debug("Failed to unmarshal xml document: {}", e.getMessage());
    } catch (JAXBException e) {
        logger.debug("Unexpected error occurred during unmarshalling of document: {}", e.getMessage());
    } catch (XMLStreamException e) {
        logger.debug("Communication error: {}", e.getMessage());
    }
    return null;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) UnmarshalException(javax.xml.bind.UnmarshalException) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 3 with UnmarshalException

use of javax.xml.bind.UnmarshalException in project OpenAM by OpenRock.

the class SAXUnmarshallerHandlerImpl method handleEvent.

public void handleEvent(ValidationEvent event, boolean canRecover) throws SAXException {
    ValidationEventHandler eventHandler;
    try {
        eventHandler = parent.getEventHandler();
    } catch (JAXBException e) {
        // impossible.
        throw new JAXBAssertionError();
    }
    boolean recover = eventHandler.handleEvent(event);
    // from the unmarshaller.getResult()
    if (!recover)
        aborted = true;
    if (!canRecover || !recover)
        throw new SAXException(new UnmarshalException(event.getMessage(), event.getLinkedException()));
}
Also used : ValidationEventHandler(javax.xml.bind.ValidationEventHandler) JAXBAssertionError(com.sun.xml.bind.JAXBAssertionError) UnmarshalException(javax.xml.bind.UnmarshalException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException)

Example 4 with UnmarshalException

use of javax.xml.bind.UnmarshalException in project spring-framework by spring-projects.

the class Jaxb2CollectionHttpMessageConverter method read.

@Override
@SuppressWarnings("unchecked")
public T read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
    ParameterizedType parameterizedType = (ParameterizedType) type;
    T result = createCollection((Class<?>) parameterizedType.getRawType());
    Class<?> elementClass = (Class<?>) parameterizedType.getActualTypeArguments()[0];
    try {
        Unmarshaller unmarshaller = createUnmarshaller(elementClass);
        XMLStreamReader streamReader = this.inputFactory.createXMLStreamReader(inputMessage.getBody());
        int event = moveToFirstChildOfRootElement(streamReader);
        while (event != XMLStreamReader.END_DOCUMENT) {
            if (elementClass.isAnnotationPresent(XmlRootElement.class)) {
                result.add(unmarshaller.unmarshal(streamReader));
            } else if (elementClass.isAnnotationPresent(XmlType.class)) {
                result.add(unmarshaller.unmarshal(streamReader, elementClass).getValue());
            } else {
                // should not happen, since we check in canRead(Type)
                throw new HttpMessageConversionException("Could not unmarshal to [" + elementClass + "]");
            }
            event = moveToNextElement(streamReader);
        }
        return result;
    } catch (UnmarshalException ex) {
        throw new HttpMessageNotReadableException("Could not unmarshal to [" + elementClass + "]: " + ex.getMessage(), ex);
    } catch (JAXBException ex) {
        throw new HttpMessageConversionException("Could not instantiate JAXBContext: " + ex.getMessage(), ex);
    } catch (XMLStreamException ex) {
        throw new HttpMessageConversionException(ex.getMessage(), ex);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) JAXBException(javax.xml.bind.JAXBException) XmlType(javax.xml.bind.annotation.XmlType) ParameterizedType(java.lang.reflect.ParameterizedType) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) XMLStreamException(javax.xml.stream.XMLStreamException) UnmarshalException(javax.xml.bind.UnmarshalException) HttpMessageConversionException(org.springframework.http.converter.HttpMessageConversionException) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 5 with UnmarshalException

use of javax.xml.bind.UnmarshalException in project Payara by payara.

the class SAXUnmarshallerHandlerImpl method handleEvent.

public void handleEvent(ValidationEvent event, boolean canRecover) throws SAXException {
    ValidationEventHandler eventHandler;
    try {
        eventHandler = parent.getEventHandler();
    } catch (JAXBException e) {
        // impossible.
        throw new JAXBAssertionError();
    }
    boolean recover = eventHandler.handleEvent(event);
    // from the unmarshaller.getResult()
    if (!recover)
        aborted = true;
    if (!canRecover || !recover)
        throw new SAXException(new UnmarshalException(event.getMessage(), event.getLinkedException()));
}
Also used : ValidationEventHandler(javax.xml.bind.ValidationEventHandler) JAXBAssertionError(com.sun.xml.bind.JAXBAssertionError) UnmarshalException(javax.xml.bind.UnmarshalException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException)

Aggregations

UnmarshalException (javax.xml.bind.UnmarshalException)25 JAXBException (javax.xml.bind.JAXBException)15 IOException (java.io.IOException)6 JAXBAssertionError (com.sun.xml.bind.JAXBAssertionError)5 ValidationEventHandler (javax.xml.bind.ValidationEventHandler)5 XMLStreamException (javax.xml.stream.XMLStreamException)5 Test (org.junit.Test)5 SAXException (org.xml.sax.SAXException)5 Unmarshaller (javax.xml.bind.Unmarshaller)4 File (java.io.File)3 URL (java.net.URL)3 XMLStreamReader (javax.xml.stream.XMLStreamReader)3 EjbDescriptor (com.sun.enterprise.deployment.EjbDescriptor)2 WebServiceEndpoint (com.sun.enterprise.deployment.WebServiceEndpoint)2 SecurityContext (com.sun.enterprise.security.SecurityContext)2 ClientSecurityContext (com.sun.enterprise.security.common.ClientSecurityContext)2 JavaMethod (com.sun.xml.ws.api.model.JavaMethod)2 StringReader (java.io.StringReader)2 Method (java.lang.reflect.Method)2 ParameterizedType (java.lang.reflect.ParameterizedType)2