Example usage for javax.xml.bind UnmarshalException getLinkedException

List of usage examples for javax.xml.bind UnmarshalException getLinkedException

Introduction

In this page you can find the example usage for javax.xml.bind UnmarshalException getLinkedException.

Prototype

public Throwable getLinkedException() 

Source Link

Document

Get the linked exception

Usage

From source file:ee.ria.xroad.opmonitordaemon.QueryRequestHandler.java

@SuppressWarnings("unchecked")
static <T> T getRequestData(SoapMessageImpl requestSoap, Class<?> clazz) throws Exception {
    Unmarshaller unmarshaller = createUnmarshaller(clazz);

    try {/*from w  ww.jav  a2  s . c  o m*/
        return (T) unmarshaller.unmarshal(SoapUtils.getFirstChild(requestSoap.getSoap().getSOAPBody()), clazz)
                .getValue();
    } catch (UnmarshalException e) {
        throw new CodedException(X_INVALID_REQUEST, e.getLinkedException()).withPrefix(CLIENT_X);
    }
}

From source file:com.rest4j.ApiFactory.java

/**
 * Create the API instance. The XML describing the API is read, preprocessed, analyzed for errors,
 * and the internal structures for Java object marshalling and unmarshalling are created, as
 * well as endpoint mappings.//from w ww  .  j av a 2 s .  c  o  m
 *
 * @return The API instance
 * @throws ConfigurationException When there is a problem with your XML description.
 */
public API createAPI() throws ConfigurationException {
    try {
        JAXBContext context;
        if (extObjectFactory == null)
            context = JAXBContext.newInstance(com.rest4j.impl.model.ObjectFactory.class);
        else
            context = JAXBContext.newInstance(com.rest4j.impl.model.ObjectFactory.class, extObjectFactory);
        Document xml = getDocument();

        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Source apiXsdSource = new StreamSource(getClass().getResourceAsStream("api.xsd"));
        List<Source> xsds = new ArrayList<Source>();
        xsds.add(apiXsdSource);
        Schema schema;
        if (!StringUtils.isEmpty(extSchema)) {
            xsds.add(new StreamSource(getClass().getClassLoader().getResourceAsStream(extSchema)));
        }
        xsds.add(new StreamSource(getClass().getResourceAsStream("html.xsd")));
        schema = schemaFactory.newSchema(xsds.toArray(new Source[xsds.size()]));

        Unmarshaller unmarshaller = context.createUnmarshaller();
        unmarshaller.setSchema(schema);
        JAXBElement<com.rest4j.impl.model.API> element = (JAXBElement<com.rest4j.impl.model.API>) unmarshaller
                .unmarshal(xml);
        com.rest4j.impl.model.API root = element.getValue();
        APIImpl api;
        api = new APIImpl(root, pathPrefix, serviceProvider,
                factories.toArray(new ObjectFactory[factories.size()]),
                fieldFilters.toArray(new FieldFilter[fieldFilters.size()]), permissionChecker);
        return api;
    } catch (javax.xml.bind.UnmarshalException e) {
        if (e.getLinkedException() instanceof SAXParseException) {
            SAXParseException spe = (SAXParseException) e.getLinkedException();
            throw new ConfigurationException("Cannot parse " + apiDescriptionXml, spe);
        }
        throw new AssertionError(e);
    } catch (ConfigurationException e) {
        throw e;
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new AssertionError(e);
    }
}

From source file:org.docx4j.openpackaging.parts.JaxbXmlPart.java

/**
* Unmarshal XML data from the specified InputStream and return the
* resulting content tree. Validation event location information may be
* incomplete when using this form of the unmarshal API.
* 
* <p>/*from  w ww.  j a va 2s .  com*/
* Implements <a href="#unmarshalGlobal">Unmarshal Global Root Element</a>.
* 
* @param is
*            the InputStream to unmarshal XML data from
* @return the newly created root object of the java content tree
* 
* @throws JAXBException
*             If any unexpected errors occur while unmarshalling
*/
public E unmarshal(java.io.InputStream is) throws JAXBException {

    try {
        /* To avoid possible XML External Entity Injection attack,
         * we need to configure the processor.
         * 
         * We use STAX XMLInputFactory to do that.
         * 
         * createXMLStreamReader(is) is 40% slower than unmarshal(is).
         * 
         * But it seems to be the best we can do ... 
         * 
         *   org.w3c.dom.Document doc = XmlUtils.getNewDocumentBuilder().parse(is)
         *   unmarshal(doc)
         * 
         * ie DOM is 5x slower than unmarshal(is)
         * 
         */

        XMLInputFactory xif = XMLInputFactory.newInstance();
        xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
        xif.setProperty(XMLInputFactory.SUPPORT_DTD, false); // a DTD is merely ignored, its presence doesn't cause an exception
        XMLStreamReader xsr = xif.createXMLStreamReader(is);

        Unmarshaller u = jc.createUnmarshaller();

        JaxbValidationEventHandler eventHandler = new JaxbValidationEventHandler();
        if (is.markSupported()) {
            // Only fail hard if we know we can restart
            eventHandler.setContinue(false);
        }
        u.setEventHandler(eventHandler);

        try {
            jaxbElement = (E) XmlUtils.unwrap(u.unmarshal(xsr));
        } catch (UnmarshalException ue) {

            if (ue.getLinkedException() != null && ue.getLinkedException().getMessage().contains("entity")) {

                /*
                   Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[10,19]
                   Message: The entity "xxe" was referenced, but not declared.
                      at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source)
                      at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(Unknown Source)
                    */
                log.error(ue.getMessage(), ue);
                throw ue;
            }

            if (is.markSupported()) {
                // When reading from zip, we use a ByteArrayInputStream,
                // which does support this.

                log.info("encountered unexpected content; pre-processing");
                eventHandler.setContinue(true);

                try {
                    Templates mcPreprocessorXslt = JaxbValidationEventHandler.getMcPreprocessor();
                    is.reset();
                    JAXBResult result = XmlUtils.prepareJAXBResult(jc);
                    XmlUtils.transform(new StreamSource(is), mcPreprocessorXslt, null, result);
                    jaxbElement = (E) XmlUtils.unwrap(result.getResult());
                } catch (Exception e) {
                    throw new JAXBException("Preprocessing exception", e);
                }

            } else {
                log.error(ue.getMessage(), ue);
                log.error(".. and mark not supported");
                throw ue;
            }
        }

    } catch (XMLStreamException e1) {
        log.error(e1.getMessage(), e1);
        throw new JAXBException(e1);
    }

    return jaxbElement;

}

From source file:org.docx4j.XmlUtils.java

public static Object unmarshal(InputStream is, JAXBContext jc) throws JAXBException {

    // Guard against XXE
    XMLInputFactory xif = XMLInputFactory.newInstance();
    xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
    xif.setProperty(XMLInputFactory.SUPPORT_DTD, false); // a DTD is merely ignored, its presence doesn't cause an exception
    XMLStreamReader xsr = null;/*w w  w  .j av  a  2s.  c o m*/
    try {
        xsr = xif.createXMLStreamReader(is);
    } catch (XMLStreamException e) {
        throw new JAXBException(e);
    }

    Object o = null;
    Unmarshaller u = jc.createUnmarshaller();

    JaxbValidationEventHandler eventHandler = new JaxbValidationEventHandler();
    //      if (is.markSupported()) {
    //         // Only fail hard if we know we can restart
    //         eventHandler.setContinue(false);
    //      }
    u.setEventHandler(eventHandler);
    try {
        o = u.unmarshal(xsr);
        return o;
    } catch (UnmarshalException ue) {

        if (ue.getLinkedException() != null && ue.getLinkedException().getMessage().contains("entity")) {

            /*
               Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[10,19]
               Message: The entity "xxe" was referenced, but not declared.
                  at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source)
                  at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(Unknown Source)
                */
            log.error(ue.getMessage(), ue);
            throw ue;
        }

        if (is.markSupported()) {
            // When reading from zip, we use a ByteArrayInputStream,
            // which does support this.

            log.info("encountered unexpected content; pre-processing");
            eventHandler.setContinue(true);

            try {
                Templates mcPreprocessorXslt = JaxbValidationEventHandler.getMcPreprocessor();
                is.reset();
                JAXBResult result = XmlUtils.prepareJAXBResult(jc);
                XmlUtils.transform(new StreamSource(is), mcPreprocessorXslt, null, result);
                return //XmlUtils.unwrap(
                result.getResult();
            } catch (Exception e) {
                throw new JAXBException("Preprocessing exception", e);
            }

        } else {
            log.error(ue.getMessage(), ue);
            log.error(".. and mark not supported");
            throw ue;
        }
    }

}