Search in sources :

Example 16 with SoapVersion

use of org.apache.cxf.binding.soap.SoapVersion in project cxf by apache.

the class SAAJOutInterceptor method handleMessage.

public void handleMessage(SoapMessage message) throws Fault {
    SOAPMessage saaj = message.getContent(SOAPMessage.class);
    try {
        if (message.hasHeaders() && saaj != null && saaj.getSOAPPart().getEnvelope().getHeader() == null) {
            // creating an empty SOAPHeader at this point in the
            // pre-existing SOAPMessage avoids the <soap:body> and
            // <soap:header> appearing in reverse order when the envolope
            // is written to the wire
            // 
            saaj.getSOAPPart().getEnvelope().addHeader();
        }
    } catch (SOAPException e) {
        throw new SoapFault(new Message("SOAPEXCEPTION", BUNDLE, e.getMessage()), e, message.getVersion().getSender());
    }
    if (saaj == null) {
        SoapVersion version = message.getVersion();
        try {
            MessageFactory factory = getFactory(message);
            SOAPMessage soapMessage = factory.createMessage();
            SOAPPart soapPart = soapMessage.getSOAPPart();
            XMLStreamWriter origWriter = (XMLStreamWriter) message.get(ORIGINAL_XML_WRITER);
            if (origWriter == null) {
                origWriter = message.getContent(XMLStreamWriter.class);
            }
            message.put(ORIGINAL_XML_WRITER, origWriter);
            W3CDOMStreamWriter writer = new SAAJStreamWriter(soapPart);
            // Replace stax writer with DomStreamWriter
            message.setContent(XMLStreamWriter.class, writer);
            message.setContent(SOAPMessage.class, soapMessage);
            message.setContent(Node.class, soapMessage.getSOAPPart());
        } catch (SOAPException e) {
            throw new SoapFault(new Message("SOAPEXCEPTION", BUNDLE, e.getMessage()), e, version.getSender());
        }
    } else if (!message.containsKey(ORIGINAL_XML_WRITER)) {
        // as the SOAPMessage already has everything in place, we do not need XMLStreamWriter to write
        // anything for us, so we just set XMLStreamWriter's output to a dummy output stream.
        XMLStreamWriter origWriter = message.getContent(XMLStreamWriter.class);
        message.put(ORIGINAL_XML_WRITER, origWriter);
        XMLStreamWriter dummyWriter = StaxUtils.createXMLStreamWriter(new OutputStream() {

            public void write(int b) throws IOException {
            }

            public void write(byte[] b, int off, int len) throws IOException {
            }
        });
        message.setContent(XMLStreamWriter.class, dummyWriter);
    }
    // Add a final interceptor to write the message
    message.getInterceptorChain().add(SAAJOutEndingInterceptor.INSTANCE);
}
Also used : SoapVersion(org.apache.cxf.binding.soap.SoapVersion) W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) SoapFault(org.apache.cxf.binding.soap.SoapFault) Message(org.apache.cxf.common.i18n.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) SOAPMessage(javax.xml.soap.SOAPMessage) MessageFactory(javax.xml.soap.MessageFactory) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) SOAPException(javax.xml.soap.SOAPException) OutputStream(java.io.OutputStream) SOAPPart(javax.xml.soap.SOAPPart) SOAPMessage(javax.xml.soap.SOAPMessage)

Example 17 with SoapVersion

use of org.apache.cxf.binding.soap.SoapVersion in project cxf by apache.

the class HandlerChainInvoker method setFaultMessage.

/*
     * When the message direction is reversed, if the message is not already a
     * fault message then it is replaced with a fault message
     */
private void setFaultMessage(MessageContext mc, Exception exception) {
    Message msg = ((WrappedMessageContext) mc).getWrappedMessage();
    msg.setContent(Exception.class, exception);
    msg.removeContent(XMLStreamReader.class);
    msg.removeContent(Source.class);
    try {
        SOAPMessage soapMessage = null;
        SoapVersion version = null;
        if (msg instanceof SoapMessage) {
            version = ((SoapMessage) msg).getVersion();
        }
        soapMessage = SAAJFactoryResolver.createMessageFactory(version).createMessage();
        msg.setContent(SOAPMessage.class, soapMessage);
        SOAPBody body = SAAJUtils.getBody(soapMessage);
        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 = soapMessage.getSOAPPart().importNode(sf.getFault().getDetail(), true);
                nd = nd.getFirstChild();
                soapFault.addDetail();
                while (nd != null) {
                    soapFault.getDetail().appendChild(nd);
                    nd = nd.getNextSibling();
                }
            }
        } else if (exception instanceof Fault) {
            SoapFault sf = SoapFault.createFault((Fault) exception, ((SoapMessage) msg).getVersion());
            soapFault.setFaultString(sf.getReason());
            SAAJUtils.setFaultCode(soapFault, sf.getFaultCode());
            if (sf.hasDetails()) {
                soapFault.addDetail();
                Node nd = soapMessage.getSOAPPart().importNode(sf.getDetail(), true);
                nd = nd.getFirstChild();
                while (nd != null) {
                    soapFault.getDetail().appendChild(nd);
                    nd = nd.getNextSibling();
                }
            }
        } else {
            SAAJUtils.setFaultCode(soapFault, new QName("http://cxf.apache.org/faultcode", "HandlerFault"));
            soapFault.setFaultString(exception.getMessage());
        }
    } catch (SOAPException e) {
        e.printStackTrace();
    // do nothing
    }
}
Also used : SoapFault(org.apache.cxf.binding.soap.SoapFault) Message(org.apache.cxf.message.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) SOAPMessage(javax.xml.soap.SOAPMessage) QName(javax.xml.namespace.QName) Node(org.w3c.dom.Node) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) Fault(org.apache.cxf.interceptor.Fault) SOAPFault(javax.xml.soap.SOAPFault) SoapFault(org.apache.cxf.binding.soap.SoapFault) SOAPMessage(javax.xml.soap.SOAPMessage) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) SoapVersion(org.apache.cxf.binding.soap.SoapVersion) SOAPBody(javax.xml.soap.SOAPBody) SOAPException(javax.xml.soap.SOAPException) WrappedMessageContext(org.apache.cxf.jaxws.context.WrappedMessageContext) SOAPFault(javax.xml.soap.SOAPFault)

Example 18 with SoapVersion

use of org.apache.cxf.binding.soap.SoapVersion in project cxf by apache.

the class SOAPHandlerInterceptorTest method testChangeSOAPHeaderInBound.

@Test
public void testChangeSOAPHeaderInBound() throws Exception {
    @SuppressWarnings("rawtypes") List<Handler> list = new ArrayList<>();
    list.add(new SOAPHandler<SOAPMessageContext>() {

        public boolean handleMessage(SOAPMessageContext smc) {
            try {
                Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
                if (!outboundProperty.booleanValue()) {
                    // change mustUnderstand to false
                    SOAPMessage message = smc.getMessage();
                    SOAPHeader soapHeader = message.getSOAPHeader();
                    Element headerElementNew = (Element) soapHeader.getFirstChild();
                    SoapVersion soapVersion = Soap11.getInstance();
                    Attr attr = headerElementNew.getOwnerDocument().createAttributeNS(soapVersion.getNamespace(), "SOAP-ENV:mustUnderstand");
                    attr.setValue("false");
                    headerElementNew.setAttributeNodeNS(attr);
                }
            } catch (Exception e) {
                throw new Fault(e);
            }
            return true;
        }

        public boolean handleFault(SOAPMessageContext smc) {
            return true;
        }

        public Set<QName> getHeaders() {
            return null;
        }

        public void close(MessageContext messageContext) {
        }
    });
    HandlerChainInvoker invoker = new HandlerChainInvoker(list);
    IMocksControl control = createNiceControl();
    Binding binding = control.createMock(Binding.class);
    expect(binding.getHandlerChain()).andReturn(list).anyTimes();
    Exchange exchange = control.createMock(Exchange.class);
    expect(exchange.get(HandlerChainInvoker.class)).andReturn(invoker).anyTimes();
    // This is to set direction to inbound
    expect(exchange.getOutMessage()).andReturn(null);
    SoapMessage message = new SoapMessage(new MessageImpl());
    message.setExchange(exchange);
    XMLStreamReader reader = preparemXMLStreamReader("resources/greetMeRpcLitReq.xml");
    message.setContent(XMLStreamReader.class, reader);
    Object[] headerInfo = prepareSOAPHeader();
    message.setContent(Node.class, headerInfo[0]);
    Node node = ((Element) headerInfo[1]).getFirstChild();
    message.getHeaders().add(new Header(new QName(node.getNamespaceURI(), node.getLocalName()), node));
    control.replay();
    SOAPHandlerInterceptor li = new SOAPHandlerInterceptor(binding);
    li.handleMessage(message);
    control.verify();
    // Verify SOAPMessage header
    SOAPMessage soapMessageNew = message.getContent(SOAPMessage.class);
    Element headerElementNew = DOMUtils.getFirstElement(soapMessageNew.getSOAPHeader());
    SoapVersion soapVersion = Soap11.getInstance();
    assertEquals("false", headerElementNew.getAttributeNS(soapVersion.getNamespace(), "mustUnderstand"));
    // Verify XMLStreamReader
    XMLStreamReader xmlReader = message.getContent(XMLStreamReader.class);
    QName qn = xmlReader.getName();
    assertEquals("sendReceiveData", qn.getLocalPart());
    // Verify Header Element
    Iterator<Header> iter = message.getHeaders().iterator();
    Element requiredHeader = null;
    while (iter.hasNext()) {
        Header localHdr = iter.next();
        if (localHdr.getObject() instanceof Element) {
            Element elem = (Element) localHdr.getObject();
            if (elem.getNamespaceURI().equals("http://apache.org/hello_world_rpclit/types") && elem.getLocalName().equals("header1")) {
                requiredHeader = (Element) localHdr.getObject();
                break;
            }
        }
    }
    assertNotNull("Should have found header1", requiredHeader);
    assertEquals("false", requiredHeader.getAttributeNS(soapVersion.getNamespace(), "mustUnderstand"));
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) XMLStreamReader(javax.xml.stream.XMLStreamReader) PartialXMLStreamReader(org.apache.cxf.staxutils.PartialXMLStreamReader) SOAPHeaderElement(javax.xml.soap.SOAPHeaderElement) SOAPBodyElement(javax.xml.soap.SOAPBodyElement) SOAPElement(javax.xml.soap.SOAPElement) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Fault(org.apache.cxf.interceptor.Fault) SOAPMessage(javax.xml.soap.SOAPMessage) Attr(org.w3c.dom.Attr) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) IMocksControl(org.easymock.IMocksControl) MessageContext(javax.xml.ws.handler.MessageContext) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) SOAPHeader(javax.xml.soap.SOAPHeader) Binding(javax.xml.ws.Binding) QName(javax.xml.namespace.QName) SOAPHandler(javax.xml.ws.handler.soap.SOAPHandler) Handler(javax.xml.ws.handler.Handler) IOException(java.io.IOException) SoapVersion(org.apache.cxf.binding.soap.SoapVersion) Exchange(org.apache.cxf.message.Exchange) HandlerChainInvoker(org.apache.cxf.jaxws.handler.HandlerChainInvoker) SOAPHeader(javax.xml.soap.SOAPHeader) Header(org.apache.cxf.headers.Header) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 19 with SoapVersion

use of org.apache.cxf.binding.soap.SoapVersion in project cxf by apache.

the class SOAPHandlerInterceptorTest method preparemXMLStreamReader.

private XMLStreamReader preparemXMLStreamReader(String resouceName) throws Exception {
    InputStream is = this.getClass().getResourceAsStream(resouceName);
    XMLStreamReader xmlReader = XMLInputFactory.newInstance().createXMLStreamReader(is);
    // skip until soap body
    if (xmlReader.nextTag() == XMLStreamConstants.START_ELEMENT) {
        String ns = xmlReader.getNamespaceURI();
        SoapVersion soapVersion = SoapVersionFactory.getInstance().getSoapVersion(ns);
        // message.setVersion(soapVersion);
        QName qn = xmlReader.getName();
        while (!qn.equals(soapVersion.getBody()) && !qn.equals(soapVersion.getHeader())) {
            while (xmlReader.nextTag() != XMLStreamConstants.START_ELEMENT) {
            // nothing to do
            }
            qn = xmlReader.getName();
        }
        if (qn.equals(soapVersion.getHeader())) {
            XMLStreamReader filteredReader = new PartialXMLStreamReader(xmlReader, soapVersion.getBody());
            StaxUtils.read(filteredReader);
        }
        // advance just past body.
        xmlReader.next();
        while (xmlReader.isWhiteSpace()) {
            xmlReader.next();
        }
    }
    return xmlReader;
}
Also used : SoapVersion(org.apache.cxf.binding.soap.SoapVersion) XMLStreamReader(javax.xml.stream.XMLStreamReader) PartialXMLStreamReader(org.apache.cxf.staxutils.PartialXMLStreamReader) PartialXMLStreamReader(org.apache.cxf.staxutils.PartialXMLStreamReader) InputStream(java.io.InputStream) QName(javax.xml.namespace.QName)

Example 20 with SoapVersion

use of org.apache.cxf.binding.soap.SoapVersion in project cxf by apache.

the class SOAPHandlerInterceptorTest method testChangeSOAPHeaderOutBound.

@Test
public void testChangeSOAPHeaderOutBound() throws Exception {
    @SuppressWarnings("rawtypes") List<Handler> list = new ArrayList<>();
    list.add(new SOAPHandler<SOAPMessageContext>() {

        public boolean handleMessage(SOAPMessageContext smc) {
            try {
                Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
                if (outboundProperty.booleanValue()) {
                    // change mustUnderstand to false
                    SOAPMessage message = smc.getMessage();
                    SOAPHeader soapHeader = message.getSOAPHeader();
                    Iterator<?> it = soapHeader.getChildElements(new QName("http://apache.org/hello_world_rpclit/types", "header1"));
                    SOAPHeaderElement headerElementNew = (SOAPHeaderElement) it.next();
                    SoapVersion soapVersion = Soap11.getInstance();
                    Attr attr = headerElementNew.getOwnerDocument().createAttributeNS(soapVersion.getNamespace(), "SOAP-ENV:mustUnderstand");
                    attr.setValue("false");
                    headerElementNew.setAttributeNodeNS(attr);
                }
            } catch (Exception e) {
                throw new Fault(e);
            }
            return true;
        }

        public boolean handleFault(SOAPMessageContext smc) {
            return true;
        }

        public Set<QName> getHeaders() {
            return null;
        }

        public void close(MessageContext messageContext) {
        }
    });
    HandlerChainInvoker invoker = new HandlerChainInvoker(list);
    IMocksControl control = createNiceControl();
    Binding binding = control.createMock(Binding.class);
    expect(binding.getHandlerChain()).andReturn(list).anyTimes();
    Exchange exchange = control.createMock(Exchange.class);
    expect(exchange.get(HandlerChainInvoker.class)).andReturn(invoker).anyTimes();
    SoapMessage message = new SoapMessage(new MessageImpl());
    message.setExchange(exchange);
    // This is to set direction to outbound
    expect(exchange.getOutMessage()).andReturn(message).anyTimes();
    CachedStream originalEmptyOs = new CachedStream();
    message.setContent(OutputStream.class, originalEmptyOs);
    InterceptorChain chain = new PhaseInterceptorChain((new PhaseManagerImpl()).getOutPhases());
    // Interceptors after SOAPHandlerInterceptor DOMXMLStreamWriter to write
    chain.add(new AbstractProtocolHandlerInterceptor<SoapMessage>(binding, Phase.MARSHAL) {

        public void handleMessage(SoapMessage message) throws Fault {
            try {
                XMLStreamWriter writer = message.getContent(XMLStreamWriter.class);
                SoapVersion soapVersion = Soap11.getInstance();
                writer.setPrefix("soap", soapVersion.getNamespace());
                writer.writeStartElement("soap", soapVersion.getEnvelope().getLocalPart(), soapVersion.getNamespace());
                writer.writeNamespace("soap", soapVersion.getNamespace());
                Object[] headerInfo = prepareSOAPHeader();
                StaxUtils.writeElement((Element) headerInfo[1], writer, true, false);
                writer.writeEndElement();
                writer.flush();
            } catch (Exception e) {
            // do nothing
            }
        }
    });
    chain.add(new SOAPHandlerInterceptor(binding));
    message.setInterceptorChain(chain);
    control.replay();
    chain.doIntercept(message);
    control.verify();
    // Verify SOAPMessage header
    SOAPMessage soapMessageNew = message.getContent(SOAPMessage.class);
    SOAPHeader soapHeader = soapMessageNew.getSOAPHeader();
    Iterator<?> itNew = soapHeader.getChildElements(new QName("http://apache.org/hello_world_rpclit/types", "header1"));
    SOAPHeaderElement headerElementNew = (SOAPHeaderElement) itNew.next();
    SoapVersion soapVersion = Soap11.getInstance();
    assertEquals("false", headerElementNew.getAttributeNS(soapVersion.getNamespace(), "mustUnderstand"));
    originalEmptyOs.close();
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) SOAPHeaderElement(javax.xml.soap.SOAPHeaderElement) SOAPBodyElement(javax.xml.soap.SOAPBodyElement) SOAPElement(javax.xml.soap.SOAPElement) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Fault(org.apache.cxf.interceptor.Fault) SOAPMessage(javax.xml.soap.SOAPMessage) Attr(org.w3c.dom.Attr) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) IMocksControl(org.easymock.IMocksControl) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Iterator(java.util.Iterator) MessageContext(javax.xml.ws.handler.MessageContext) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) SOAPHeader(javax.xml.soap.SOAPHeader) SOAPHeaderElement(javax.xml.soap.SOAPHeaderElement) Binding(javax.xml.ws.Binding) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) QName(javax.xml.namespace.QName) SOAPHandler(javax.xml.ws.handler.soap.SOAPHandler) Handler(javax.xml.ws.handler.Handler) IOException(java.io.IOException) SoapVersion(org.apache.cxf.binding.soap.SoapVersion) Exchange(org.apache.cxf.message.Exchange) InterceptorChain(org.apache.cxf.interceptor.InterceptorChain) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) HandlerChainInvoker(org.apache.cxf.jaxws.handler.HandlerChainInvoker) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) MessageImpl(org.apache.cxf.message.MessageImpl) PhaseManagerImpl(org.apache.cxf.bus.managers.PhaseManagerImpl) Test(org.junit.Test)

Aggregations

SoapVersion (org.apache.cxf.binding.soap.SoapVersion)26 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)15 QName (javax.xml.namespace.QName)12 Element (org.w3c.dom.Element)11 SoapFault (org.apache.cxf.binding.soap.SoapFault)10 Header (org.apache.cxf.headers.Header)7 Fault (org.apache.cxf.interceptor.Fault)7 SOAPMessage (javax.xml.soap.SOAPMessage)6 XMLStreamException (javax.xml.stream.XMLStreamException)6 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)6 XMLStreamReader (javax.xml.stream.XMLStreamReader)5 SoapHeader (org.apache.cxf.binding.soap.SoapHeader)5 Message (org.apache.cxf.common.i18n.Message)5 Exchange (org.apache.cxf.message.Exchange)5 Node (org.w3c.dom.Node)5 IOException (java.io.IOException)4 HashSet (java.util.HashSet)4 SOAPBodyElement (javax.xml.soap.SOAPBodyElement)4 SOAPElement (javax.xml.soap.SOAPElement)4 SOAPException (javax.xml.soap.SOAPException)4