Search in sources :

Example 1 with HeaderProcessor

use of org.apache.cxf.headers.HeaderProcessor in project cxf by apache.

the class SAAJInInterceptor method replaceHeaders.

public static void replaceHeaders(SOAPMessage soapMessage, SoapMessage message) throws SOAPException {
    SOAPHeader header = SAAJUtils.getHeader(soapMessage);
    if (header == null) {
        return;
    }
    Element elem = DOMUtils.getFirstElement(header);
    elem = (Element) DOMUtils.getDomElement(elem);
    while (elem != null) {
        Bus b = message.getExchange() == null ? null : message.getExchange().getBus();
        HeaderProcessor p = null;
        if (b != null && b.getExtension(HeaderManager.class) != null) {
            p = b.getExtension(HeaderManager.class).getHeaderProcessor(elem.getNamespaceURI());
        }
        Object obj;
        DataBinding dataBinding = null;
        if (p == null || p.getDataBinding() == null) {
            obj = elem;
        } else {
            dataBinding = p.getDataBinding();
            obj = p.getDataBinding().createReader(Node.class).read(elem);
        }
        SoapHeader shead = new SoapHeader(new QName(elem.getNamespaceURI(), elem.getLocalName()), obj, dataBinding);
        shead.setDirection(SoapHeader.Direction.DIRECTION_IN);
        String mu = elem.getAttributeNS(message.getVersion().getNamespace(), message.getVersion().getAttrNameMustUnderstand());
        String act = elem.getAttributeNS(message.getVersion().getNamespace(), message.getVersion().getAttrNameRole());
        shead.setActor(act);
        shead.setMustUnderstand(Boolean.valueOf(mu) || "1".equals(mu));
        Header oldHdr = message.getHeader(new QName(elem.getNamespaceURI(), elem.getLocalName()));
        if (oldHdr != null) {
            message.getHeaders().remove(oldHdr);
        }
        message.getHeaders().add(shead);
        elem = DOMUtils.getNextElement(elem);
    }
}
Also used : Bus(org.apache.cxf.Bus) SOAPHeader(javax.xml.soap.SOAPHeader) Header(org.apache.cxf.headers.Header) SoapHeader(org.apache.cxf.binding.soap.SoapHeader) HeaderProcessor(org.apache.cxf.headers.HeaderProcessor) QName(javax.xml.namespace.QName) SOAPElement(javax.xml.soap.SOAPElement) Element(org.w3c.dom.Element) DataBinding(org.apache.cxf.databinding.DataBinding) SoapHeader(org.apache.cxf.binding.soap.SoapHeader) SOAPHeader(javax.xml.soap.SOAPHeader)

Example 2 with HeaderProcessor

use of org.apache.cxf.headers.HeaderProcessor 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);
            Document doc = null;
            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);
                }
            }
            // Find header
            if (doc != null) {
                Element element = doc.getDocumentElement();
                QName header = soapVersion.getHeader();
                List<Element> elemList = DOMUtils.findAllElementsByTagNameNS(element, header.getNamespaceURI(), header.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());
            }
        }
    } 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 3 with HeaderProcessor

use of org.apache.cxf.headers.HeaderProcessor in project cxf by apache.

the class SoapOutInterceptor method writeSoapEnvelopeStart.

private void writeSoapEnvelopeStart(final SoapMessage message) {
    final SoapVersion soapVersion = message.getVersion();
    try {
        XMLStreamWriter xtw = message.getContent(XMLStreamWriter.class);
        String soapPrefix = xtw.getPrefix(soapVersion.getNamespace());
        if (StringUtils.isEmpty(soapPrefix)) {
            soapPrefix = "soap";
        }
        if (message.hasAdditionalEnvNs()) {
            Map<String, String> nsMap = message.getEnvelopeNs();
            for (Map.Entry<String, String> entry : nsMap.entrySet()) {
                if (soapVersion.getNamespace().equals(entry.getValue())) {
                    soapPrefix = entry.getKey();
                }
            }
            xtw.setPrefix(soapPrefix, soapVersion.getNamespace());
            xtw.writeStartElement(soapPrefix, soapVersion.getEnvelope().getLocalPart(), soapVersion.getNamespace());
            xtw.writeNamespace(soapPrefix, soapVersion.getNamespace());
            for (Map.Entry<String, String> entry : nsMap.entrySet()) {
                if (!soapVersion.getNamespace().equals(entry.getValue())) {
                    xtw.writeNamespace(entry.getKey(), entry.getValue());
                }
            }
        } else {
            xtw.setPrefix(soapPrefix, soapVersion.getNamespace());
            xtw.writeStartElement(soapPrefix, soapVersion.getEnvelope().getLocalPart(), soapVersion.getNamespace());
            String s2 = xtw.getPrefix(soapVersion.getNamespace());
            if (StringUtils.isEmpty(s2) || soapPrefix.equals(s2)) {
                xtw.writeNamespace(soapPrefix, soapVersion.getNamespace());
            } else {
                soapPrefix = s2;
            }
        }
        boolean preexistingHeaders = message.hasHeaders();
        if (preexistingHeaders) {
            xtw.writeStartElement(soapPrefix, soapVersion.getHeader().getLocalPart(), soapVersion.getNamespace());
            List<Header> hdrList = message.getHeaders();
            for (Header header : hdrList) {
                XMLStreamWriter writer = xtw;
                if (xtw instanceof W3CDOMStreamWriter) {
                    Element nd = ((W3CDOMStreamWriter) xtw).getCurrentNode();
                    if (header.getObject() instanceof Element && nd.isSameNode(((Element) header.getObject()).getParentNode())) {
                        continue;
                    }
                }
                if (header instanceof SoapHeader) {
                    SoapHeader soapHeader = (SoapHeader) header;
                    writer = new SOAPHeaderWriter(xtw, soapHeader, soapVersion, soapPrefix);
                }
                DataBinding b = header.getDataBinding();
                if (b == null) {
                    HeaderProcessor hp = bus.getExtension(HeaderManager.class).getHeaderProcessor(header.getName().getNamespaceURI());
                    if (hp != null) {
                        b = hp.getDataBinding();
                    }
                }
                if (b != null) {
                    MessagePartInfo part = new MessagePartInfo(header.getName(), null);
                    part.setConcreteName(header.getName());
                    b.createWriter(XMLStreamWriter.class).write(header.getObject(), part, writer);
                } else {
                    Element node = (Element) header.getObject();
                    StaxUtils.copy(node, writer);
                }
            }
        }
        boolean endedHeader = handleHeaderPart(preexistingHeaders, message, soapPrefix);
        if (preexistingHeaders && !endedHeader) {
            xtw.writeEndElement();
        }
        xtw.writeStartElement(soapPrefix, soapVersion.getBody().getLocalPart(), soapVersion.getNamespace());
    // Interceptors followed such as Wrapped/RPC/Doc Interceptor will write SOAP body
    } catch (XMLStreamException e) {
        throw new SoapFault(new org.apache.cxf.common.i18n.Message("XML_WRITE_EXC", BUNDLE), e, soapVersion.getSender());
    }
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) SoapFault(org.apache.cxf.binding.soap.SoapFault) Message(org.apache.cxf.message.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) HeaderProcessor(org.apache.cxf.headers.HeaderProcessor) Element(org.w3c.dom.Element) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) HeaderManager(org.apache.cxf.headers.HeaderManager) SoapVersion(org.apache.cxf.binding.soap.SoapVersion) SoapHeader(org.apache.cxf.binding.soap.SoapHeader) Header(org.apache.cxf.headers.Header) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) DelegatingXMLStreamWriter(org.apache.cxf.staxutils.DelegatingXMLStreamWriter) DataBinding(org.apache.cxf.databinding.DataBinding) SoapHeader(org.apache.cxf.binding.soap.SoapHeader) Map(java.util.Map)

Aggregations

SoapHeader (org.apache.cxf.binding.soap.SoapHeader)3 DataBinding (org.apache.cxf.databinding.DataBinding)3 HeaderProcessor (org.apache.cxf.headers.HeaderProcessor)3 Element (org.w3c.dom.Element)3 Map (java.util.Map)2 QName (javax.xml.namespace.QName)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 SoapFault (org.apache.cxf.binding.soap.SoapFault)2 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)2 SoapVersion (org.apache.cxf.binding.soap.SoapVersion)2 Header (org.apache.cxf.headers.Header)2 W3CDOMStreamWriter (org.apache.cxf.staxutils.W3CDOMStreamWriter)2 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 SOAPElement (javax.xml.soap.SOAPElement)1 SOAPHeader (javax.xml.soap.SOAPHeader)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1 Bus (org.apache.cxf.Bus)1 Message (org.apache.cxf.common.i18n.Message)1