Search in sources :

Example 1 with SAAJStreamWriter

use of org.apache.cxf.binding.soap.saaj.SAAJStreamWriter in project cxf by apache.

the class StaxSerializer method deserialize.

private Node deserialize(Node ctx, XMLStreamReader reader, boolean wrapped) throws XMLEncryptionException {
    Document contextDocument = null;
    if (Node.DOCUMENT_NODE == ctx.getNodeType()) {
        contextDocument = (Document) ctx;
    } else {
        contextDocument = ctx.getOwnerDocument();
    }
    XMLStreamWriter writer = null;
    try {
        if (ctx instanceof SOAPElement) {
            SOAPElement el = (SOAPElement) ctx;
            while (el != null && !(el instanceof SOAPEnvelope)) {
                el = el.getParentElement();
            }
            // cannot load into fragment due to a ClassCastException within SAAJ addChildElement
            // which only checks for Document as parent, not DocumentFragment
            Element element = ctx.getOwnerDocument().createElementNS("dummy", "dummy");
            writer = new SAAJStreamWriter((SOAPEnvelope) el, element);
            return appendNewChild(reader, wrapped, contextDocument, writer, element);
        }
        if (DOMUtils.isJava9SAAJ()) {
            // cannot load into fragment due to a ClassCastException within SAAJ addChildElement
            // which only checks for Document as parent, not DocumentFragment
            Element element = ctx.getOwnerDocument().createElementNS("dummy", "dummy");
            writer = new OverlayW3CDOMStreamWriter(ctx.getOwnerDocument(), element);
            return appendNewChild(reader, wrapped, contextDocument, writer, element);
        }
        // Import to a dummy fragment
        DocumentFragment dummyFragment = contextDocument.createDocumentFragment();
        writer = StaxUtils.createXMLStreamWriter(new DOMResult(dummyFragment));
        StaxUtils.copy(reader, writer);
        if (wrapped) {
            DocumentFragment result = contextDocument.createDocumentFragment();
            Node child = dummyFragment.getFirstChild().getFirstChild();
            if (child != null && child.getNextSibling() == null) {
                return child;
            }
            while (child != null) {
                Node nextChild = child.getNextSibling();
                result.appendChild(child);
                child = nextChild;
            }
            dummyFragment = result;
        }
        return dummyFragment;
    } catch (XMLStreamException ex) {
        throw new XMLEncryptionException(ex);
    }
}
Also used : OverlayW3CDOMStreamWriter(org.apache.cxf.staxutils.OverlayW3CDOMStreamWriter) DOMResult(javax.xml.transform.dom.DOMResult) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) SOAPElement(javax.xml.soap.SOAPElement) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) SOAPElement(javax.xml.soap.SOAPElement) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) Document(org.w3c.dom.Document) DocumentFragment(org.w3c.dom.DocumentFragment) SAAJStreamWriter(org.apache.cxf.binding.soap.saaj.SAAJStreamWriter) XMLEncryptionException(org.apache.xml.security.encryption.XMLEncryptionException)

Example 2 with SAAJStreamWriter

use of org.apache.cxf.binding.soap.saaj.SAAJStreamWriter in project cxf by apache.

the class AbstractSecurityTest method getSoapMessageForDom.

protected SoapMessage getSoapMessageForDom(Document doc, String protocol) throws Exception {
    SOAPMessage saajMsg = MessageFactory.newInstance(protocol).createMessage();
    SOAPPart part = saajMsg.getSOAPPart();
    SAAJStreamWriter writer = new SAAJStreamWriter(part);
    StaxUtils.copy(doc, writer);
    saajMsg.saveChanges();
    MessageImpl message = new MessageImpl();
    SoapMessage msg = new SoapMessage(message);
    Exchange ex = new ExchangeImpl();
    ex.setInMessage(msg);
    msg.setContent(SOAPMessage.class, saajMsg);
    return msg;
}
Also used : Exchange(org.apache.cxf.message.Exchange) SOAPPart(javax.xml.soap.SOAPPart) SOAPMessage(javax.xml.soap.SOAPMessage) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) SAAJStreamWriter(org.apache.cxf.binding.soap.saaj.SAAJStreamWriter) SoapMessage(org.apache.cxf.binding.soap.SoapMessage)

Aggregations

SAAJStreamWriter (org.apache.cxf.binding.soap.saaj.SAAJStreamWriter)2 SOAPElement (javax.xml.soap.SOAPElement)1 SOAPEnvelope (javax.xml.soap.SOAPEnvelope)1 SOAPMessage (javax.xml.soap.SOAPMessage)1 SOAPPart (javax.xml.soap.SOAPPart)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1 DOMResult (javax.xml.transform.dom.DOMResult)1 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)1 Exchange (org.apache.cxf.message.Exchange)1 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)1 MessageImpl (org.apache.cxf.message.MessageImpl)1 OverlayW3CDOMStreamWriter (org.apache.cxf.staxutils.OverlayW3CDOMStreamWriter)1 XMLEncryptionException (org.apache.xml.security.encryption.XMLEncryptionException)1 Document (org.w3c.dom.Document)1 DocumentFragment (org.w3c.dom.DocumentFragment)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1