Search in sources :

Example 1 with StaxStreamFilter

use of org.apache.cxf.staxutils.StaxStreamFilter in project cxf by apache.

the class XMLStreamDataReader method validate.

private Element validate(XMLStreamReader input) throws XMLStreamException, IOException {
    DOMSource ds = read(input);
    Element rootElement = null;
    if (ds.getNode() instanceof Document) {
        rootElement = ((Document) ds.getNode()).getDocumentElement();
    } else {
        rootElement = (Element) ds.getNode();
    }
    WoodstoxValidationImpl impl = new WoodstoxValidationImpl();
    XMLStreamWriter nullWriter = null;
    if (impl.canValidate()) {
        nullWriter = StaxUtils.createXMLStreamWriter(new NUllOutputStream());
        impl.setupValidation(nullWriter, message.getExchange().getEndpoint(), message.getExchange().getService().getServiceInfos().get(0));
    }
    // check if the impl can still validate after the setup, possible issue loading schemas or similar
    if (impl.canValidate()) {
        // Can use the MSV libs and woodstox to handle the schema validation during
        // parsing and processing.   Much faster and single traversal
        // filter xop node
        XMLStreamReader reader = StaxUtils.createXMLStreamReader(ds);
        XMLStreamReader filteredReader = StaxUtils.createFilteredReader(reader, new StaxStreamFilter(new QName[] { XOP }));
        StaxUtils.copy(filteredReader, nullWriter);
    } else {
        // MSV not available, use a slower method of cloning the data, replace the xop's, validate
        LOG.fine("NO_MSV_AVAILABLE");
        Element newElement = rootElement;
        if (DOMUtils.hasElementWithName(rootElement, "http://www.w3.org/2004/08/xop/include", "Include")) {
            newElement = (Element) rootElement.cloneNode(true);
            List<Element> elems = DOMUtils.findAllElementsByTagNameNS(newElement, "http://www.w3.org/2004/08/xop/include", "Include");
            for (Element include : elems) {
                Node parentNode = include.getParentNode();
                parentNode.removeChild(include);
                String cid = DOMUtils.getAttribute(include, "href");
                // set the fake base64Binary to validate instead of reading the attachment from message
                parentNode.setTextContent(javax.xml.bind.DatatypeConverter.printBase64Binary(cid.getBytes()));
            }
        }
        try {
            schema.newValidator().validate(new DOMSource(newElement));
        } catch (SAXException e) {
            throw new XMLStreamException(e.getMessage(), e);
        }
    }
    return rootElement;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) DepthXMLStreamReader(org.apache.cxf.staxutils.DepthXMLStreamReader) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) StaxStreamFilter(org.apache.cxf.staxutils.StaxStreamFilter) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) WoodstoxValidationImpl(org.apache.cxf.staxutils.validation.WoodstoxValidationImpl)

Example 2 with StaxStreamFilter

use of org.apache.cxf.staxutils.StaxStreamFilter in project cxf by apache.

the class JAXBEncoderDecoderTest method testUnmarshallFromStaxStreamReader.

@Test
public void testUnmarshallFromStaxStreamReader() throws Exception {
    QName elName = new QName(wrapperAnnotation.targetNamespace(), wrapperAnnotation.localName());
    MessagePartInfo part = new MessagePartInfo(elName, null);
    InputStream is = getClass().getResourceAsStream("resources/GreetMeDocLiteralReq.xml");
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLStreamReader reader = factory.createXMLStreamReader(is);
    QName[] tags = { SOAP_ENV, SOAP_BODY };
    StaxStreamFilter filter = new StaxStreamFilter(tags);
    FixNamespacesXMLStreamReader filteredReader = new FixNamespacesXMLStreamReader(factory.createFilteredReader(reader, filter));
    assertNull(filteredReader.getUnmarshaller());
    // Remove START_DOCUMENT & START_ELEMENT pertaining to Envelope and Body Tags.
    part.setTypeClass(GreetMe.class);
    Unmarshaller um = context.createUnmarshaller();
    Object val = JAXBEncoderDecoder.unmarshall(um, filteredReader, part, true);
    assertEquals(um, filteredReader.getUnmarshaller());
    assertNotNull(val);
    assertTrue(val instanceof GreetMe);
    assertEquals("TestSOAPInputPMessage", ((GreetMe) val).getRequestType());
    is.close();
}
Also used : StaxStreamFilter(org.apache.cxf.staxutils.StaxStreamFilter) GreetMe(org.apache.hello_world_soap_http.types.GreetMe) XMLStreamReader(javax.xml.stream.XMLStreamReader) QName(javax.xml.namespace.QName) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Unmarshaller(javax.xml.bind.Unmarshaller) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) XMLInputFactory(javax.xml.stream.XMLInputFactory) Test(org.junit.Test)

Aggregations

QName (javax.xml.namespace.QName)2 XMLStreamReader (javax.xml.stream.XMLStreamReader)2 StaxStreamFilter (org.apache.cxf.staxutils.StaxStreamFilter)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 XMLInputFactory (javax.xml.stream.XMLInputFactory)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1 DOMSource (javax.xml.transform.dom.DOMSource)1 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)1 DepthXMLStreamReader (org.apache.cxf.staxutils.DepthXMLStreamReader)1 WoodstoxValidationImpl (org.apache.cxf.staxutils.validation.WoodstoxValidationImpl)1 GreetMe (org.apache.hello_world_soap_http.types.GreetMe)1 Test (org.junit.Test)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1 SAXException (org.xml.sax.SAXException)1