Search in sources :

Example 41 with W3CDOMStreamWriter

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

the class TunedDocumentLoader method loadFastinfosetDocument.

static Document loadFastinfosetDocument(URL url) throws IOException, ParserConfigurationException, XMLStreamException {
    try (InputStream in = new BufferedInputStream(url.openStream())) {
        XMLStreamReader staxReader = new StAXDocumentParser(in);
        W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
        StaxUtils.copy(staxReader, writer);
        staxReader.close();
        return writer.getDocument();
    }
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) XMLStreamReader(javax.xml.stream.XMLStreamReader) StAXDocumentParser(com.sun.xml.fastinfoset.stax.StAXDocumentParser) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream)

Example 42 with W3CDOMStreamWriter

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

the class FaultOutInterceptor method handleMessage.

public void handleMessage(Message message) {
    Fault f = (Fault) message.getContent(Exception.class);
    if (f == null) {
        return;
    }
    Throwable cause = f.getCause();
    if (cause == null) {
        return;
    }
    BindingOperationInfo bop = message.getExchange().getBindingOperationInfo();
    if (bop == null) {
        return;
    }
    FaultInfo fi = getFaultForClass(bop, cause.getClass());
    if (cause instanceof Exception && fi != null) {
        Exception ex = (Exception) cause;
        Object bean = getFaultBean(cause, fi, message);
        Service service = message.getExchange().getService();
        MessagePartInfo part = fi.getFirstMessagePart();
        DataBinding db = service.getDataBinding();
        try {
            if (isDOMSupported(db)) {
                DataWriter<Node> writer = db.createWriter(Node.class);
                if (f.hasDetails()) {
                    writer.write(bean, part, f.getDetail());
                } else {
                    writer.write(bean, part, f.getOrCreateDetail());
                    if (!f.getDetail().hasChildNodes()) {
                        f.setDetail(null);
                    }
                }
            } else {
                if (f.hasDetails()) {
                    XMLStreamWriter xsw = new W3CDOMStreamWriter(f.getDetail());
                    DataWriter<XMLStreamWriter> writer = db.createWriter(XMLStreamWriter.class);
                    writer.write(bean, part, xsw);
                } else {
                    XMLStreamWriter xsw = new W3CDOMStreamWriter(f.getOrCreateDetail());
                    DataWriter<XMLStreamWriter> writer = db.createWriter(XMLStreamWriter.class);
                    writer.write(bean, part, xsw);
                    if (!f.getDetail().hasChildNodes()) {
                        f.setDetail(null);
                    }
                }
            }
            f.setMessage(ex.getMessage());
        } catch (Exception fex) {
            // ignore - if any exceptions occur here, we'll ignore them
            // and let the default fault handling of the binding convert
            // the fault like it was an unchecked exception.
            LOG.log(Level.WARNING, "EXCEPTION_WHILE_WRITING_FAULT", fex);
        }
    }
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) BindingFaultInfo(org.apache.cxf.service.model.BindingFaultInfo) FaultInfo(org.apache.cxf.service.model.FaultInfo) Node(org.w3c.dom.Node) Service(org.apache.cxf.service.Service) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) InvocationTargetException(java.lang.reflect.InvocationTargetException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) DataBinding(org.apache.cxf.databinding.DataBinding)

Example 43 with W3CDOMStreamWriter

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

the class ReadHeadersInterceptor method handleMessage.

// CHECKSTYLE:OFF MethodLength
public void handleMessage(SoapMessage message) {
    if (isGET(message)) {
        LOG.fine("ReadHeadersInterceptor skipped in HTTP GET method");
        return;
    }
    /*
         * Reject OPTIONS, and any other noise that is not allowed in SOAP.
         */
    final String verb = (String) message.get(org.apache.cxf.message.Message.HTTP_REQUEST_METHOD);
    if (verb != null && !"POST".equals(verb)) {
        Fault formula405 = new Fault("HTTP verb was not GET or POST", LOG);
        formula405.setStatusCode(405);
        throw formula405;
    }
    XMLStreamReader xmlReader = message.getContent(XMLStreamReader.class);
    boolean closeNeeded = false;
    if (xmlReader == null) {
        InputStream in = message.getContent(InputStream.class);
        if (in == null) {
            throw new RuntimeException("Can't find input stream in message");
        }
        xmlReader = StaxUtils.createXMLStreamReader(in);
        closeNeeded = true;
    }
    try {
        if (xmlReader.getEventType() == XMLStreamConstants.START_ELEMENT || xmlReader.nextTag() == XMLStreamConstants.START_ELEMENT) {
            SoapVersion soapVersion = readVersion(xmlReader, message);
            if (soapVersion == Soap12.getInstance() && version == Soap11.getInstance()) {
                message.setVersion(version);
                throw new SoapFault(new Message("INVALID_11_VERSION", LOG), version.getVersionMismatch());
            }
            XMLStreamReader filteredReader = new PartialXMLStreamReader(xmlReader, message.getVersion().getBody());
            Node nd = message.getContent(Node.class);
            W3CDOMStreamWriter writer = message.get(W3CDOMStreamWriter.class);
            final Document doc;
            if (writer != null) {
                StaxUtils.copy(filteredReader, writer);
                doc = writer.getDocument();
            } else if (nd instanceof Document) {
                doc = (Document) nd;
                StaxUtils.readDocElements(doc, doc, filteredReader, false, false);
            } else {
                final boolean addNC = MessageUtils.getContextualBoolean(message, "org.apache.cxf.binding.soap.addNamespaceContext", false);
                Map<String, String> bodyNC = addNC ? new HashMap<String, String>() : null;
                if (addNC) {
                    // add the Envelope-Level declarations
                    addCurrentNamespaceDecls(xmlReader, bodyNC);
                }
                HeadersProcessor processor = new HeadersProcessor(soapVersion);
                doc = processor.process(filteredReader);
                if (doc != null) {
                    message.setContent(Node.class, doc);
                } else {
                    message.put(ENVELOPE_EVENTS, processor.getEnvAttributeAndNamespaceEvents());
                    message.put(BODY_EVENTS, processor.getBodyAttributeAndNamespaceEvents());
                    message.put(ENVELOPE_PREFIX, processor.getEnvelopePrefix());
                    message.put(BODY_PREFIX, processor.getBodyPrefix());
                }
                if (addNC) {
                    // add the Body-level declarations
                    addCurrentNamespaceDecls(xmlReader, bodyNC);
                    message.put("soap.body.ns.context", bodyNC);
                }
            }
            List<Element> soapBody = null;
            // Find header
            if (doc != null) {
                Element element = doc.getDocumentElement();
                QName header = soapVersion.getHeader();
                QName body = soapVersion.getBody();
                List<Element> elemList = DOMUtils.findAllElementsByTagNameNS(element, header.getNamespaceURI(), header.getLocalPart());
                soapBody = DOMUtils.getChildrenWithName(element, body.getNamespaceURI(), body.getLocalPart());
                for (Element elem : elemList) {
                    Element hel = DOMUtils.getFirstElement(elem);
                    while (hel != null) {
                        // which otherwise would be lost.
                        if (elem.hasAttributes()) {
                            NamedNodeMap nnp = elem.getAttributes();
                            for (int ct = 0; ct < nnp.getLength(); ct++) {
                                Node attr = nnp.item(ct);
                                Node headerAttrNode = hel.hasAttributes() ? hel.getAttributes().getNamedItemNS(attr.getNamespaceURI(), attr.getLocalName()) : null;
                                if (headerAttrNode == null) {
                                    Attr attribute = hel.getOwnerDocument().createAttributeNS(attr.getNamespaceURI(), attr.getNodeName());
                                    attribute.setNodeValue(attr.getNodeValue());
                                    hel.setAttributeNodeNS(attribute);
                                }
                            }
                        }
                        HeaderProcessor p = bus == null ? null : bus.getExtension(HeaderManager.class).getHeaderProcessor(hel.getNamespaceURI());
                        Object obj;
                        DataBinding dataBinding = null;
                        if (p == null || p.getDataBinding() == null) {
                            obj = hel;
                        } else {
                            dataBinding = p.getDataBinding();
                            DataReader<Node> dataReader = dataBinding.createReader(Node.class);
                            dataReader.setAttachments(message.getAttachments());
                            dataReader.setProperty(DataReader.ENDPOINT, message.getExchange().getEndpoint());
                            dataReader.setProperty(Message.class.getName(), message);
                            obj = dataReader.read(hel);
                        }
                        SoapHeader shead = new SoapHeader(new QName(hel.getNamespaceURI(), hel.getLocalName()), obj, dataBinding);
                        String mu = hel.getAttributeNS(soapVersion.getNamespace(), soapVersion.getAttrNameMustUnderstand());
                        String act = hel.getAttributeNS(soapVersion.getNamespace(), soapVersion.getAttrNameRole());
                        if (!StringUtils.isEmpty(act)) {
                            shead.setActor(act);
                        }
                        shead.setMustUnderstand(Boolean.valueOf(mu) || "1".equals(mu));
                        // mark header as inbound header.(for distinguishing between the direction to
                        // avoid piggybacking of headers from request->server->response.
                        shead.setDirection(SoapHeader.Direction.DIRECTION_IN);
                        message.getHeaders().add(shead);
                        hel = DOMUtils.getNextElement(hel);
                    }
                }
            }
            if (ServiceUtils.isSchemaValidationEnabled(SchemaValidationType.IN, message)) {
                message.getInterceptorChain().add(new CheckClosingTagsInterceptor());
            }
            if (ServiceUtils.isSchemaValidationEnabled(SchemaValidationType.IN, message) && soapBody != null && soapBody.isEmpty()) {
                throw new SoapFault(new Message("NO_SOAP_BODY", LOG, "no soap body"), soapVersion.getSender());
            }
        }
    } catch (XMLStreamException e) {
        throw new SoapFault(new Message("XML_STREAM_EXC", LOG, e.getMessage()), e, message.getVersion().getSender());
    } finally {
        if (closeNeeded) {
            try {
                StaxUtils.close(xmlReader);
            } catch (XMLStreamException e) {
                throw new SoapFault(new Message("XML_STREAM_EXC", LOG, e.getMessage()), e, message.getVersion().getSender());
            }
        }
    }
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) SoapFault(org.apache.cxf.binding.soap.SoapFault) XMLStreamReader(javax.xml.stream.XMLStreamReader) PartialXMLStreamReader(org.apache.cxf.staxutils.PartialXMLStreamReader) PartialXMLStreamReader(org.apache.cxf.staxutils.PartialXMLStreamReader) Message(org.apache.cxf.common.i18n.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) HashMap(java.util.HashMap) HeaderProcessor(org.apache.cxf.headers.HeaderProcessor) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Fault(org.apache.cxf.interceptor.Fault) SoapFault(org.apache.cxf.binding.soap.SoapFault) Document(org.w3c.dom.Document) Attr(org.w3c.dom.Attr) NamedNodeMap(org.w3c.dom.NamedNodeMap) InputStream(java.io.InputStream) QName(javax.xml.namespace.QName) SoapVersion(org.apache.cxf.binding.soap.SoapVersion) XMLStreamException(javax.xml.stream.XMLStreamException) DataBinding(org.apache.cxf.databinding.DataBinding) SoapHeader(org.apache.cxf.binding.soap.SoapHeader) HashMap(java.util.HashMap) Map(java.util.Map) NamedNodeMap(org.w3c.dom.NamedNodeMap)

Example 44 with W3CDOMStreamWriter

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

the class RetransmissionQueueImpl method readHeaders.

private void readHeaders(XMLStreamReader xmlReader, SoapMessage message) throws XMLStreamException {
    // read header portion of SOAP document into DOM
    SoapVersion version = message.getVersion();
    XMLStreamReader filteredReader = new PartialXMLStreamReader(xmlReader, version.getBody());
    Node nd = message.getContent(Node.class);
    W3CDOMStreamWriter writer = message.get(W3CDOMStreamWriter.class);
    final Document doc;
    if (writer != null) {
        StaxUtils.copy(filteredReader, writer);
        doc = writer.getDocument();
    } else if (nd instanceof Document) {
        doc = (Document) nd;
        StaxUtils.readDocElements(doc, doc, filteredReader, false, false);
    } else {
        doc = StaxUtils.read(filteredReader);
        message.setContent(Node.class, doc);
    }
    // get the actual SOAP header
    Element element = doc.getDocumentElement();
    QName header = version.getHeader();
    List<Element> elemList = DOMUtils.findAllElementsByTagNameNS(element, header.getNamespaceURI(), header.getLocalPart());
    for (Element elem : elemList) {
        // set all child elements as headers for message transmission
        Element hel = DOMUtils.getFirstElement(elem);
        while (hel != null) {
            SoapHeader sheader = new SoapHeader(DOMUtils.getElementQName(hel), hel);
            message.getHeaders().add(sheader);
            hel = DOMUtils.getNextElement(hel);
        }
    }
}
Also used : SoapVersion(org.apache.cxf.binding.soap.SoapVersion) W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) XMLStreamReader(javax.xml.stream.XMLStreamReader) PartialXMLStreamReader(org.apache.cxf.staxutils.PartialXMLStreamReader) PartialXMLStreamReader(org.apache.cxf.staxutils.PartialXMLStreamReader) QName(javax.xml.namespace.QName) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) SoapHeader(org.apache.cxf.binding.soap.SoapHeader) Document(org.w3c.dom.Document)

Example 45 with W3CDOMStreamWriter

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

the class AbstractBindingBuilder method cloneElement.

protected Element cloneElement(Element el) {
    Document doc = secHeader.getSecurityHeaderElement().getOwnerDocument();
    if (!doc.equals(el.getOwnerDocument())) {
        XMLStreamReader reader = StaxUtils.createXMLStreamReader(el);
        DocumentFragment fragment = doc.createDocumentFragment();
        W3CDOMStreamWriter writer = new W3CDOMStreamWriter(fragment);
        try {
            StaxUtils.copy(reader, writer);
            return (Element) fragment.getFirstChild();
        } catch (XMLStreamException ex) {
            LOG.log(Level.FINE, "Error cloning security element", ex);
        }
    }
    return el;
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) DocumentFragment(org.w3c.dom.DocumentFragment)

Aggregations

W3CDOMStreamWriter (org.apache.cxf.staxutils.W3CDOMStreamWriter)60 Element (org.w3c.dom.Element)29 DOMSource (javax.xml.transform.dom.DOMSource)24 XMLStreamException (javax.xml.stream.XMLStreamException)15 Document (org.w3c.dom.Document)14 WebClient (org.apache.cxf.jaxrs.client.WebClient)9 Node (org.w3c.dom.Node)9 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)8 Fault (org.apache.cxf.interceptor.Fault)8 XMLStreamReader (javax.xml.stream.XMLStreamReader)7 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)7 RequestSecurityTokenResponseType (org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenResponseType)7 SecurityToken (org.apache.cxf.ws.security.tokenstore.SecurityToken)7 JAXBElement (javax.xml.bind.JAXBElement)6 JAXBException (javax.xml.bind.JAXBException)5 SOAPMessage (javax.xml.soap.SOAPMessage)5 SoapFault (org.apache.cxf.binding.soap.SoapFault)5 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)5 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)4