Search in sources :

Example 21 with SoapVersion

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

the class SOAPHandlerInterceptorTest method testChangeSOAPBodyOutBound.

// SAAJ tree is created from DOMXMLStreamWriter. Any changes to SOAPMessage should be streamed back to
// outputStream
@Test
public void testChangeSOAPBodyOutBound() throws Exception {
    @SuppressWarnings("rawtypes") List<Handler> list = new ArrayList<>();
    list.add(new SOAPHandler<SOAPMessageContext>() {

        public boolean handleMessage(SOAPMessageContext smc) {
            Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
            if (outboundProperty.booleanValue()) {
                try {
                    smc.setMessage(prepareSOAPMessage("resources/greetMeRpcLitRespChanged.xml"));
                } 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();
    XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(originalEmptyOs);
    message.setContent(XMLStreamWriter.class, writer);
    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());
                writer.writeEndElement();
                writer.flush();
            } catch (Exception e) {
            // do nothing
            }
        }
    });
    chain.add(new SOAPHandlerInterceptor(binding));
    message.setInterceptorChain(chain);
    control.replay();
    chain.doIntercept(message);
    control.verify();
    writer.flush();
    // Verify SOAPMessage
    SOAPMessage resultedMessage = message.getContent(SOAPMessage.class);
    assertNotNull(resultedMessage);
    SOAPBody bodyNew = resultedMessage.getSOAPBody();
    Iterator<?> itNew = bodyNew.getChildElements(new QName("http://apache.org/hello_world_rpclit", "sendReceiveDataResponse"));
    SOAPBodyElement bodyElementNew = (SOAPBodyElement) itNew.next();
    Iterator<?> outIt = bodyElementNew.getChildElements(new QName("http://apache.org/hello_world_rpclit/types", "out"));
    Element outElement = (SOAPElement) outIt.next();
    assertNotNull(outElement);
    Element elem3Element = DOMUtils.findAllElementsByTagNameNS(outElement, "http://apache.org/hello_world_rpclit/types", "elem3").get(0);
    assertEquals("100", elem3Element.getTextContent());
}
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) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) IMocksControl(org.easymock.IMocksControl) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) SOAPElement(javax.xml.soap.SOAPElement) MessageContext(javax.xml.ws.handler.MessageContext) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) SOAPBodyElement(javax.xml.soap.SOAPBodyElement) 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) Exchange(org.apache.cxf.message.Exchange) SoapVersion(org.apache.cxf.binding.soap.SoapVersion) InterceptorChain(org.apache.cxf.interceptor.InterceptorChain) PhaseInterceptorChain(org.apache.cxf.phase.PhaseInterceptorChain) SOAPBody(javax.xml.soap.SOAPBody) 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)

Example 22 with SoapVersion

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

the class RMEndpoint method buildBindingInfo.

void buildBindingInfo(ServiceInfo si, ProtocolVariation protocol) {
    // explicitly
    if (null != applicationEndpoint) {
        final QName bindingQName = new QName(protocol.getWSRMNamespace(), BINDING_NAME);
        SoapBindingInfo sbi = (SoapBindingInfo) applicationEndpoint.getEndpointInfo().getBinding();
        SoapVersion sv = sbi.getSoapVersion();
        String bindingId = sbi.getBindingId();
        SoapBindingInfo bi = new SoapBindingInfo(si, bindingId, sv);
        bi.setName(bindingQName);
        BindingOperationInfo boi = null;
        RMConstants consts = protocol.getConstants();
        boi = bi.buildOperation(consts.getCreateSequenceOperationName(), consts.getCreateSequenceOperationName().getLocalPart(), null);
        addAction(boi, consts.getCreateSequenceAction(), consts.getCreateSequenceResponseAction());
        bi.addOperation(boi);
        boi = bi.buildOperation(consts.getTerminateSequenceOperationName(), consts.getTerminateSequenceOperationName().getLocalPart(), null);
        if (RM11Constants.NAMESPACE_URI.equals(protocol.getWSRMNamespace())) {
            addAction(boi, consts.getTerminateSequenceAction(), RM11Constants.INSTANCE.getTerminateSequenceResponseAction());
        } else {
            addAction(boi, consts.getTerminateSequenceAction());
        }
        bi.addOperation(boi);
        boi = bi.buildOperation(consts.getTerminateSequenceAnonymousOperationName(), null, consts.getTerminateSequenceAnonymousOperationName().getLocalPart());
        addAction(boi, consts.getTerminateSequenceAction());
        bi.addOperation(boi);
        boi = bi.buildOperation(consts.getSequenceAckOperationName(), null, null);
        addAction(boi, consts.getSequenceAckAction());
        bi.addOperation(boi);
        boi = bi.buildOperation(consts.getCloseSequenceOperationName(), null, null);
        if (RM11Constants.NAMESPACE_URI.equals(protocol.getWSRMNamespace())) {
            addAction(boi, consts.getCloseSequenceAction(), RM11Constants.INSTANCE.getCloseSequenceResponseAction());
        } else {
            addAction(boi, consts.getCloseSequenceAction());
        }
        bi.addOperation(boi);
        boi = bi.buildOperation(consts.getAckRequestedOperationName(), null, null);
        addAction(boi, consts.getAckRequestedAction());
        bi.addOperation(boi);
        boi = bi.buildOperation(consts.getCreateSequenceOnewayOperationName(), consts.getCreateSequenceOnewayOperationName().getLocalPart(), null);
        addAction(boi, consts.getCreateSequenceAction());
        bi.addOperation(boi);
        boi = bi.buildOperation(consts.getCreateSequenceResponseOnewayOperationName(), consts.getCreateSequenceResponseOnewayOperationName().getLocalPart(), null);
        addAction(boi, consts.getCreateSequenceResponseAction());
        bi.addOperation(boi);
        si.addBinding(bi);
    }
// TODO: BindingFaultInfo (SequenceFault)
}
Also used : SoapVersion(org.apache.cxf.binding.soap.SoapVersion) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) QName(javax.xml.namespace.QName) SoapBindingInfo(org.apache.cxf.binding.soap.model.SoapBindingInfo)

Example 23 with SoapVersion

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

the class MAPCodecTest method setUpMessage.

private SoapMessage setUpMessage(boolean requestor, boolean outbound, boolean invalidMAP, boolean preExistingSOAPAction, Boolean generateRelatesTo, String exposeAs) throws Exception {
    SoapMessage message = new SoapMessage(new MessageImpl());
    setUpOutbound(message, outbound);
    expectRelatesTo = generateRelatesTo != null ? generateRelatesTo : (requestor && !outbound) || (!requestor && outbound);
    message.put(REQUESTOR_ROLE, Boolean.valueOf(requestor));
    String mapProperty = getMAPProperty(requestor, outbound);
    AddressingProperties maps = getMAPs(requestor, outbound, exposeAs);
    final Element header = control.createMock(Element.class);
    codec.setHeaderFactory(new MAPCodec.HeaderFactory() {

        public Element getHeader(SoapVersion version) {
            return header;
        }
    });
    List<Header> headers = message.getHeaders();
    JAXBContext jaxbContext = control.createMock(JAXBContext.class);
    ContextUtils.setJAXBContext(jaxbContext);
    Names200408.setJAXBContext(jaxbContext);
    Names200403.setJAXBContext(jaxbContext);
    if (outbound) {
        setUpEncode(requestor, message, header, maps, mapProperty, invalidMAP, preExistingSOAPAction);
    } else {
        setUpDecode(message, headers, maps, mapProperty, requestor);
    }
    control.replay();
    return message;
}
Also used : SoapVersion(org.apache.cxf.binding.soap.SoapVersion) Header(org.apache.cxf.headers.Header) JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) AddressingProperties(org.apache.cxf.ws.addressing.AddressingProperties) JAXBContext(javax.xml.bind.JAXBContext) MessageImpl(org.apache.cxf.message.MessageImpl) SoapMessage(org.apache.cxf.binding.soap.SoapMessage)

Example 24 with SoapVersion

use of org.apache.cxf.binding.soap.SoapVersion 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);
    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 {
        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 25 with SoapVersion

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

the class HeaderVerifier method addPartialResponseHeader.

private void addPartialResponseHeader(SoapMessage message) {
    try {
        // add piggybacked wsa:From header to partial response
        List<Header> header = message.getHeaders();
        Document doc = DOMUtils.getEmptyDocument();
        SoapVersion ver = message.getVersion();
        Element hdr = doc.createElementNS(ver.getHeader().getNamespaceURI(), ver.getHeader().getLocalPart());
        hdr.setPrefix(ver.getHeader().getPrefix());
        marshallFrom("urn:piggyback_responder", hdr, getMarshaller());
        Element elem = DOMUtils.getFirstElement(hdr);
        while (elem != null) {
            Header holder = new Header(new QName(elem.getNamespaceURI(), elem.getLocalName()), elem, null);
            header.add(holder);
            elem = DOMUtils.getNextElement(elem);
        }
    } catch (Exception e) {
        verificationCache.put("SOAP header addition failed: " + e);
        e.printStackTrace();
    }
}
Also used : SoapVersion(org.apache.cxf.binding.soap.SoapVersion) Header(org.apache.cxf.headers.Header) QName(javax.xml.namespace.QName) JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) SOAPException(javax.xml.soap.SOAPException) JAXBException(javax.xml.bind.JAXBException)

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