Search in sources :

Example 46 with SOAPBody

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

the class HWSoapMessageProvider method invoke.

public SOAPMessage invoke(SOAPMessage request) {
    SOAPMessage response = null;
    try {
        SOAPBody body = SAAJUtils.getBody(request);
        Node n = body.getFirstChild();
        while (n.getNodeType() != Node.ELEMENT_NODE) {
            n = n.getNextSibling();
        }
        if (n.getLocalName().equals(sayHi.getLocalPart())) {
            response = sayHiResponse;
            if (request.countAttachments() > 0) {
                MessageFactory factory = MessageFactory.newInstance();
                InputStream is = getClass().getResourceAsStream("resources/sayHiRpcLiteralResp.xml");
                response = factory.createMessage(null, is);
                is.close();
                Iterator<AttachmentPart> it = CastUtils.cast(request.getAttachments(), AttachmentPart.class);
                while (it.hasNext()) {
                    response.addAttachmentPart(it.next());
                }
            }
        } else if (n.getLocalName().equals(greetMe.getLocalPart())) {
            response = greetMeResponse;
        } else {
            response = request;
        // response.writeTo(System.out);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return response;
}
Also used : SOAPBody(javax.xml.soap.SOAPBody) MessageFactory(javax.xml.soap.MessageFactory) InputStream(java.io.InputStream) Node(org.w3c.dom.Node) AttachmentPart(javax.xml.soap.AttachmentPart) SOAPMessage(javax.xml.soap.SOAPMessage)

Example 47 with SOAPBody

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

the class HWStreamSourceMessageProvider method invoke.

public StreamSource invoke(StreamSource request) {
    QName qn = (QName) ctx.getMessageContext().get(MessageContext.WSDL_OPERATION);
    if (qn == null) {
        throw new RuntimeException("No Operation Name");
    }
    StreamSource response = new StreamSource();
    try {
        SOAPMessage msg = factory.createMessage();
        msg.getSOAPPart().setContent(request);
        SOAPBody body = msg.getSOAPBody();
        Node n = body.getFirstChild();
        while (n.getNodeType() != Node.ELEMENT_NODE) {
            n = n.getNextSibling();
        }
        if (n.getLocalName().equals(sayHi.getLocalPart())) {
            response.setInputStream(sayHiInputStream);
        } else if (n.getLocalName().equals(greetMe.getLocalPart())) {
            response.setInputStream(greetMeInputStream);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return response;
}
Also used : SOAPBody(javax.xml.soap.SOAPBody) QName(javax.xml.namespace.QName) StreamSource(javax.xml.transform.stream.StreamSource) Node(org.w3c.dom.Node) SOAPMessage(javax.xml.soap.SOAPMessage)

Example 48 with SOAPBody

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

the class TestSOAPHandler method handleMessage.

public boolean handleMessage(SOAPMessageContext ctx) {
    try {
        SOAPMessage msg = ctx.getMessage();
        /*
             * System.out.println("-----------soap---------");
             * msg.writeTo(System.out);
             * System.out.println("-----------soap---------");
             */
        SOAPEnvelope env = msg.getSOAPPart().getEnvelope();
        SOAPBody body = env.getBody();
        Iterator<?> it = body.getChildElements();
        while (it.hasNext()) {
            Object elem = it.next();
            if (elem instanceof SOAPElement) {
                Iterator<?> it2 = ((SOAPElement) elem).getChildElements();
                while (it2.hasNext()) {
                    Object elem2 = it2.next();
                    if (elem2 instanceof SOAPElement) {
                        String value = ((SOAPElement) elem2).getValue();
                        if (value != null && (value.indexOf("Milestone-0") >= 0 || value.indexOf("TestGreetMeResponseServerLogicalHandler") >= 0)) {
                            value = value + "ServerSOAPHandler";
                            ((SOAPElement) elem2).setValue(value);
                        }
                    }
                }
            }
        }
        msg.saveChanges();
    /*
             * System.out.println("-----------soapaf---------");
             * msg.writeTo(System.out);
             * System.out.println("-----------soapaf---------");
             */
    } catch (Exception e) {
        e.printStackTrace();
    }
    return true;
}
Also used : SOAPBody(javax.xml.soap.SOAPBody) SOAPElement(javax.xml.soap.SOAPElement) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) SOAPMessage(javax.xml.soap.SOAPMessage)

Example 49 with SOAPBody

use of javax.xml.soap.SOAPBody 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 50 with SOAPBody

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

the class SOAPHandlerInterceptorTest method testGetSOAPMessageInBound.

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

        public boolean handleMessage(SOAPMessageContext smc) {
            try {
                smc.getMessage();
            } 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);
    Exchange exchange = control.createMock(Exchange.class);
    expect(binding.getHandlerChain()).andReturn(list).anyTimes();
    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);
    control.replay();
    SOAPHandlerInterceptor li = new SOAPHandlerInterceptor(binding);
    li.handleMessage(message);
    control.verify();
    // Verify SOAPMessage
    SOAPMessage soapMessageNew = message.getContent(SOAPMessage.class);
    SOAPBody bodyNew = soapMessageNew.getSOAPBody();
    Iterator<?> itNew = bodyNew.getChildElements();
    SOAPBodyElement bodyElementNew = (SOAPBodyElement) itNew.next();
    assertEquals("sendReceiveData", bodyElementNew.getLocalName());
    // Verify the XMLStreamReader
    XMLStreamReader xmlReader = message.getContent(XMLStreamReader.class);
    QName qn = xmlReader.getName();
    assertEquals("sendReceiveData", qn.getLocalPart());
}
Also used : Binding(javax.xml.ws.Binding) Set(java.util.Set) HashSet(java.util.HashSet) XMLStreamReader(javax.xml.stream.XMLStreamReader) PartialXMLStreamReader(org.apache.cxf.staxutils.PartialXMLStreamReader) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) SOAPHandler(javax.xml.ws.handler.soap.SOAPHandler) Handler(javax.xml.ws.handler.Handler) Fault(org.apache.cxf.interceptor.Fault) SOAPMessage(javax.xml.soap.SOAPMessage) IOException(java.io.IOException) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) IMocksControl(org.easymock.IMocksControl) Exchange(org.apache.cxf.message.Exchange) SOAPBody(javax.xml.soap.SOAPBody) HandlerChainInvoker(org.apache.cxf.jaxws.handler.HandlerChainInvoker) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) MessageContext(javax.xml.ws.handler.MessageContext) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) MessageImpl(org.apache.cxf.message.MessageImpl) SOAPBodyElement(javax.xml.soap.SOAPBodyElement) Test(org.junit.Test)

Aggregations

SOAPBody (javax.xml.soap.SOAPBody)54 SOAPMessage (javax.xml.soap.SOAPMessage)47 SOAPException (javax.xml.soap.SOAPException)26 SOAPElement (javax.xml.soap.SOAPElement)24 QName (javax.xml.namespace.QName)23 SOAPPart (javax.xml.soap.SOAPPart)18 Node (org.w3c.dom.Node)18 MessageFactory (javax.xml.soap.MessageFactory)15 SOAPEnvelope (javax.xml.soap.SOAPEnvelope)14 Element (org.w3c.dom.Element)13 NodeList (org.w3c.dom.NodeList)12 IOException (java.io.IOException)9 InputStream (java.io.InputStream)9 StreamSource (javax.xml.transform.stream.StreamSource)9 BufferedWriter (java.io.BufferedWriter)7 OutputStreamWriter (java.io.OutputStreamWriter)7 HttpURLConnection (java.net.HttpURLConnection)7 SOAPBodyElement (javax.xml.soap.SOAPBodyElement)7 SOAPHeader (javax.xml.soap.SOAPHeader)6 Test (org.testng.annotations.Test)6