Search in sources :

Example 66 with SOAPMessage

use of javax.xml.soap.SOAPMessage in project cxf by apache.

the class LogicalHandlerOutInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    if (binding.getHandlerChain().isEmpty()) {
        return;
    }
    HandlerChainInvoker invoker = getInvoker(message);
    if (invoker.getLogicalHandlers().isEmpty()) {
        return;
    }
    XMLStreamWriter origWriter = message.getContent(XMLStreamWriter.class);
    Node nd = message.getContent(Node.class);
    SOAPMessage m = message.getContent(SOAPMessage.class);
    Document document = null;
    if (m != null) {
        document = m.getSOAPPart();
    } else if (nd != null) {
        document = nd.getOwnerDocument();
    } else {
        document = DOMUtils.newDocument();
        message.setContent(Node.class, document);
    }
    W3CDOMStreamWriter writer = new W3CDOMStreamWriter(document.createDocumentFragment());
    // Replace stax writer with DomStreamWriter
    message.setContent(XMLStreamWriter.class, writer);
    message.put(ORIGINAL_WRITER, origWriter);
    message.getInterceptorChain().add(ending);
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) HandlerChainInvoker(org.apache.cxf.jaxws.handler.HandlerChainInvoker) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) SOAPMessage(javax.xml.soap.SOAPMessage)

Example 67 with SOAPMessage

use of javax.xml.soap.SOAPMessage in project cxf by apache.

the class LogicalMessageImpl method setPayload.

public void setPayload(Source s) {
    Message message = msgContext.getWrappedMessage();
    Service.Mode mode = (Service.Mode) msgContext.getWrappedMessage().getContextualProperty(Service.Mode.class.getName());
    SOAPMessage m = message.getContent(SOAPMessage.class);
    if (m != null && !MessageUtils.isOutbound(message)) {
        try {
            SAAJUtils.getBody(m).removeContents();
            W3CDOMStreamWriter writer = new W3CDOMStreamWriter(SAAJUtils.getBody(m));
            StaxUtils.copy(s, writer);
            writer.flush();
            writer.close();
            if (mode == Service.Mode.MESSAGE) {
                s = new DOMSource(m.getSOAPPart());
            } else {
                s = new DOMSource(SAAJUtils.getBody(m).getFirstChild());
            }
            W3CDOMStreamReader r = new W3CDOMStreamReader(DOMUtils.getFirstElement(SAAJUtils.getBody(m)));
            message.setContent(XMLStreamReader.class, r);
        } catch (Exception e) {
            throw new Fault(e);
        }
    } else if (mode != null) {
        if (message instanceof SoapMessage) {
            if (mode == Service.Mode.MESSAGE) {
                try {
                    // REVISIT: should try to use the original SOAPMessage
                    // instead of creating a new empty one.
                    SOAPMessage msg = initSOAPMessage(null);
                    write(s, SAAJUtils.getBody(msg));
                    s = new DOMSource(msg.getSOAPPart());
                } catch (Exception e) {
                    throw new Fault(e);
                }
            }
        } else if (message instanceof XMLMessage && message.getContent(DataSource.class) != null) {
            throw new Fault(new org.apache.cxf.common.i18n.Message("GETPAYLOAD_OF_DATASOURCE_NOT_VALID_XMLHTTPBINDING", LOG));
        }
    } else {
        XMLStreamReader reader = StaxUtils.createXMLStreamReader(s);
        msgContext.getWrappedMessage().setContent(XMLStreamReader.class, reader);
    }
    msgContext.getWrappedMessage().setContent(Source.class, s);
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) XMLMessage(org.apache.cxf.message.XMLMessage) DOMSource(javax.xml.transform.dom.DOMSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) LogicalMessage(javax.xml.ws.LogicalMessage) Message(org.apache.cxf.message.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) SOAPMessage(javax.xml.soap.SOAPMessage) XMLMessage(org.apache.cxf.message.XMLMessage) Service(javax.xml.ws.Service) Fault(org.apache.cxf.interceptor.Fault) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPException(javax.xml.soap.SOAPException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) WebServiceException(javax.xml.ws.WebServiceException) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) W3CDOMStreamReader(org.apache.cxf.staxutils.W3CDOMStreamReader)

Example 68 with SOAPMessage

use of javax.xml.soap.SOAPMessage in project cxf by apache.

the class LogicalMessageImpl method getPayload.

public Source getPayload() {
    Source source = null;
    Service.Mode mode = msgContext.getWrappedMessage().getExchange().get(Service.Mode.class);
    if (mode != null) {
        // Dispatch/Provider case
        source = handleDispatchProviderCase(mode);
    } else {
        Message message = msgContext.getWrappedMessage();
        source = message.getContent(Source.class);
        if (source == null) {
            // need to convert
            SOAPMessage msg = message.getContent(SOAPMessage.class);
            XMLStreamReader reader = null;
            if (msg != null) {
                try {
                    Node node = SAAJUtils.getBody(msg).getFirstChild();
                    while (node != null && !(node instanceof Element)) {
                        node = node.getNextSibling();
                    }
                    source = new DOMSource(node);
                    reader = StaxUtils.createXMLStreamReader(source);
                } catch (SOAPException e) {
                    throw new Fault(e);
                }
            }
            if (source == null) {
                try {
                    DocumentFragment doc = DOMUtils.getEmptyDocument().createDocumentFragment();
                    W3CDOMStreamWriter writer = new W3CDOMStreamWriter(doc);
                    reader = message.getContent(XMLStreamReader.class);
                    // content must be an element thing, skip over any whitespace
                    StaxUtils.toNextTag(reader);
                    StaxUtils.copy(reader, writer, true);
                    doc.appendChild(DOMUtils.getFirstElement(writer.getCurrentFragment()));
                    source = new DOMSource(DOMUtils.getFirstElement(doc));
                    reader = StaxUtils.createXMLStreamReader(DOMUtils.getFirstElement(doc));
                } catch (XMLStreamException e) {
                    throw new Fault(e);
                }
            }
            message.setContent(XMLStreamReader.class, reader);
            message.setContent(Source.class, source);
        } else if (!(source instanceof DOMSource)) {
            W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
            XMLStreamReader reader = message.getContent(XMLStreamReader.class);
            if (reader == null) {
                reader = StaxUtils.createXMLStreamReader(source);
            }
            try {
                StaxUtils.copy(reader, writer);
            } catch (XMLStreamException e) {
                throw new Fault(e);
            }
            source = new DOMSource(writer.getDocument().getDocumentElement());
            reader = StaxUtils.createXMLStreamReader(writer.getDocument());
            message.setContent(XMLStreamReader.class, reader);
            message.setContent(Source.class, source);
        }
    }
    return source;
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) DOMSource(javax.xml.transform.dom.DOMSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) LogicalMessage(javax.xml.ws.LogicalMessage) Message(org.apache.cxf.message.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) SOAPMessage(javax.xml.soap.SOAPMessage) XMLMessage(org.apache.cxf.message.XMLMessage) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Service(javax.xml.ws.Service) Fault(org.apache.cxf.interceptor.Fault) SOAPMessage(javax.xml.soap.SOAPMessage) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) DataSource(javax.activation.DataSource) XMLStreamException(javax.xml.stream.XMLStreamException) SOAPException(javax.xml.soap.SOAPException) DocumentFragment(org.w3c.dom.DocumentFragment)

Example 69 with SOAPMessage

use of javax.xml.soap.SOAPMessage in project cxf by apache.

the class LogicalMessageImpl method handleDispatchProviderCase.

private Source handleDispatchProviderCase(Service.Mode mode) {
    Source source = null;
    Message message = msgContext.getWrappedMessage();
    Source obj = message.getContent(Source.class);
    if (message instanceof SoapMessage) {
        // StreamSource may only be used once, need to make a copy
        if (obj instanceof StreamSource) {
            try (CachedOutputStream cos = new CachedOutputStream()) {
                StaxUtils.copy(obj, cos);
                obj = new StreamSource(cos.getInputStream());
                message.setContent(Source.class, new StreamSource(cos.getInputStream()));
            } catch (Exception e) {
                throw new Fault(e);
            }
        }
        if (mode == Service.Mode.PAYLOAD) {
            source = obj;
        } else {
            try (CachedOutputStream cos = new CachedOutputStream()) {
                StaxUtils.copy(obj, cos);
                InputStream in = cos.getInputStream();
                SOAPMessage msg = initSOAPMessage(in);
                source = new DOMSource(SAAJUtils.getBody(msg).getFirstChild());
                in.close();
            } catch (Exception e) {
                throw new Fault(e);
            }
        }
    } else if (message instanceof XMLMessage) {
        if (obj != null) {
            source = obj;
        } else if (message.getContent(DataSource.class) != null) {
            throw new Fault(new org.apache.cxf.common.i18n.Message("GETPAYLOAD_OF_DATASOURCE_NOT_VALID_XMLHTTPBINDING", LOG));
        }
    }
    return source;
}
Also used : XMLMessage(org.apache.cxf.message.XMLMessage) DOMSource(javax.xml.transform.dom.DOMSource) LogicalMessage(javax.xml.ws.LogicalMessage) Message(org.apache.cxf.message.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) SOAPMessage(javax.xml.soap.SOAPMessage) XMLMessage(org.apache.cxf.message.XMLMessage) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Fault(org.apache.cxf.interceptor.Fault) SOAPMessage(javax.xml.soap.SOAPMessage) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) DataSource(javax.activation.DataSource) SOAPException(javax.xml.soap.SOAPException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) WebServiceException(javax.xml.ws.WebServiceException) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) DataSource(javax.activation.DataSource)

Example 70 with SOAPMessage

use of javax.xml.soap.SOAPMessage in project cxf by apache.

the class SOAPHandlerFaultOutInterceptor method handleMessageInternal.

private void handleMessageInternal(SoapMessage message) {
    MessageContext context = createProtocolMessageContext(message);
    HandlerChainInvoker invoker = getInvoker(message);
    invoker.setProtocolMessageContext(context);
    try {
        if (!invoker.invokeProtocolHandlersHandleFault(isRequestor(message), context)) {
        // handleAbort(message, context);
        }
    } catch (RuntimeException exception) {
        /*
             * handleFault throws exception, in this case we need to replace
             * SOAPFault with the exception thrown from HandleFault so that the
             * exception can be dispatched.
             */
        try {
            SOAPMessage originalMsg = message.getContent(SOAPMessage.class);
            SOAPBody body = originalMsg.getSOAPPart().getEnvelope().getBody();
            body.removeContents();
            SOAPFault soapFault = body.addFault();
            if (exception instanceof SOAPFaultException) {
                SOAPFaultException sf = (SOAPFaultException) exception;
                soapFault.setFaultString(sf.getFault().getFaultString());
                SAAJUtils.setFaultCode(soapFault, sf.getFault().getFaultCodeAsQName());
                soapFault.setFaultActor(sf.getFault().getFaultActor());
                if (sf.getFault().hasDetail()) {
                    Node nd = originalMsg.getSOAPPart().importNode(sf.getFault().getDetail().getFirstChild(), true);
                    soapFault.addDetail().appendChild(nd);
                }
            } else if (exception instanceof Fault) {
                SoapFault sf = SoapFault.createFault((Fault) exception, message.getVersion());
                soapFault.setFaultString(sf.getReason());
                SAAJUtils.setFaultCode(soapFault, sf.getFaultCode());
                if (sf.hasDetails()) {
                    soapFault.addDetail();
                    Node nd = originalMsg.getSOAPPart().importNode(sf.getDetail(), true);
                    nd = nd.getFirstChild();
                    while (nd != null) {
                        soapFault.getDetail().appendChild(nd);
                        nd = nd.getNextSibling();
                    }
                }
            } else {
                soapFault.setFaultString(exception.getMessage());
                SAAJUtils.setFaultCode(soapFault, new QName("http://cxf.apache.org/faultcode", "HandleFault"));
            }
        } catch (SOAPException e) {
            // do nothing
            e.printStackTrace();
        }
    }
    onCompletion(message);
}
Also used : SOAPBody(javax.xml.soap.SOAPBody) SoapFault(org.apache.cxf.binding.soap.SoapFault) HandlerChainInvoker(org.apache.cxf.jaxws.handler.HandlerChainInvoker) QName(javax.xml.namespace.QName) Node(org.w3c.dom.Node) SOAPException(javax.xml.soap.SOAPException) SOAPFault(javax.xml.soap.SOAPFault) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) Fault(org.apache.cxf.interceptor.Fault) SOAPFault(javax.xml.soap.SOAPFault) SoapFault(org.apache.cxf.binding.soap.SoapFault) MessageContext(javax.xml.ws.handler.MessageContext) SOAPMessage(javax.xml.soap.SOAPMessage)

Aggregations

SOAPMessage (javax.xml.soap.SOAPMessage)219 SOAPException (javax.xml.soap.SOAPException)87 SOAPBody (javax.xml.soap.SOAPBody)47 Test (org.junit.Test)46 InputStream (java.io.InputStream)45 QName (javax.xml.namespace.QName)45 Element (org.w3c.dom.Element)44 IOException (java.io.IOException)40 MessageFactory (javax.xml.soap.MessageFactory)40 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)30 SOAPElement (javax.xml.soap.SOAPElement)28 ByteArrayInputStream (java.io.ByteArrayInputStream)26 SAML2Exception (com.sun.identity.saml2.common.SAML2Exception)25 XMLStreamReader (javax.xml.stream.XMLStreamReader)25 Node (org.w3c.dom.Node)24 Document (org.w3c.dom.Document)22 URL (java.net.URL)21 SOAPPart (javax.xml.soap.SOAPPart)21 Exchange (org.apache.cxf.message.Exchange)19 MessageImpl (org.apache.cxf.message.MessageImpl)19