Search in sources :

Example 46 with MessageFactory

use of javax.xml.soap.MessageFactory 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 47 with MessageFactory

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

the class Client method main.

public static void main(String[] args) throws Exception {
    if (args.length == 0) {
        System.out.println("please specify wsdl");
        System.exit(1);
    }
    URL wsdlURL;
    File wsdlFile = new File(args[0]);
    if (wsdlFile.exists()) {
        wsdlURL = wsdlFile.toURI().toURL();
    } else {
        wsdlURL = new URL(args[0]);
    }
    MessageFactory factory = MessageFactory.newInstance();
    System.out.println(wsdlURL + "\n\n");
    final String ns = "http://apache.org/hello_world_soap_http";
    QName serviceName1 = new QName(ns, "SOAPService1");
    QName portName1 = new QName(ns, "SoapPort1");
    SOAPService1 service1 = new SOAPService1(wsdlURL, serviceName1);
    InputStream is1 = Client.class.getResourceAsStream("/GreetMeDocLiteralReq1.xml");
    SOAPMessage soapReq1 = factory.createMessage(null, is1);
    Dispatch<SOAPMessage> dispSOAPMsg = service1.createDispatch(portName1, SOAPMessage.class, Mode.MESSAGE);
    System.out.println("Invoking server through Dispatch interface using SOAPMessage");
    SOAPMessage soapResp = dispSOAPMsg.invoke(soapReq1);
    System.out.println("Response from server: " + soapResp.getSOAPBody().getTextContent());
    QName serviceName3 = new QName(ns, "SOAPService3");
    QName portName3 = new QName(ns, "SoapPort3");
    SOAPService3 service3 = new SOAPService3(wsdlURL, serviceName3);
    InputStream is3 = Client.class.getResourceAsStream("/GreetMeDocLiteralReq2.xml");
    SOAPMessage soapReq3 = MessageFactory.newInstance().createMessage(null, is3);
    DOMSource domReqPayload = new DOMSource(soapReq3.getSOAPBody().extractContentAsDocument());
    Dispatch<DOMSource> dispDOMSrcPayload = service3.createDispatch(portName3, DOMSource.class, Mode.PAYLOAD);
    System.out.println("Invoking server through Dispatch interface using DOMSource in PAYLOAD Mode");
    DOMSource domRespPayload = dispDOMSrcPayload.invoke(domReqPayload);
    System.out.println("Response from server: " + domRespPayload.getNode().getFirstChild().getTextContent());
    System.exit(0);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) MessageFactory(javax.xml.soap.MessageFactory) SOAPService1(org.apache.hello_world_soap_http.SOAPService1) QName(javax.xml.namespace.QName) InputStream(java.io.InputStream) SOAPService3(org.apache.hello_world_soap_http.SOAPService3) File(java.io.File) SOAPMessage(javax.xml.soap.SOAPMessage) URL(java.net.URL)

Example 48 with MessageFactory

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

the class DispatchHandlerInvocationTest method testInvokeWithSOAPMessageMessageMode.

@Test
public void testInvokeWithSOAPMessageMessageMode() throws Exception {
    URL wsdl = getClass().getResource("/wsdl/addNumbers.wsdl");
    assertNotNull(wsdl);
    AddNumbersService service = new AddNumbersService(wsdl, serviceName);
    assertNotNull(service);
    Dispatch<SOAPMessage> disp = service.createDispatch(portName, SOAPMessage.class, Mode.MESSAGE);
    setAddress(disp, addNumbersAddress);
    TestHandler handler = new TestHandler();
    TestSOAPHandler soapHandler = new TestSOAPHandler();
    addHandlersProgrammatically(disp, handler, soapHandler);
    InputStream is2 = this.getClass().getResourceAsStream("resources/GreetMeDocLiteralReq.xml");
    MessageFactory factory = MessageFactory.newInstance();
    SOAPMessage soapReq = factory.createMessage(null, is2);
    SOAPMessage response = disp.invoke(soapReq);
    assertNotNull(response);
// response.writeTo(System.out);
}
Also used : AddNumbersService(org.apache.handlers.AddNumbersService) MessageFactory(javax.xml.soap.MessageFactory) InputStream(java.io.InputStream) SOAPMessage(javax.xml.soap.SOAPMessage) URL(java.net.URL) Test(org.junit.Test)

Example 49 with MessageFactory

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

the class DispatchHandlerInvocationTest method testInvokeWithDOMSourcPayloadModeXMLBinding.

@Test
public void testInvokeWithDOMSourcPayloadModeXMLBinding() throws Exception {
    URL wsdl = getClass().getResource("/wsdl/addNumbers.wsdl");
    assertNotNull(wsdl);
    XMLService service = new XMLService();
    assertNotNull(service);
    Dispatch<DOMSource> disp = service.createDispatch(portNameXML, DOMSource.class, Mode.PAYLOAD);
    setAddress(disp, addNumbersAddress);
    TestHandlerXMLBinding handler = new TestHandlerXMLBinding();
    addHandlersProgrammatically(disp, handler);
    InputStream is = getClass().getResourceAsStream("/messages/XML_GreetMeDocLiteralReq.xml");
    MessageFactory factory = MessageFactory.newInstance();
    SOAPMessage soapReq = factory.createMessage(null, is);
    DOMSource domReqMessage = new DOMSource(soapReq.getSOAPPart());
    DOMSource response = disp.invoke(domReqMessage);
    assertNotNull(response);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) MessageFactory(javax.xml.soap.MessageFactory) InputStream(java.io.InputStream) XMLService(org.apache.hello_world_xml_http.wrapped.XMLService) SOAPMessage(javax.xml.soap.SOAPMessage) URL(java.net.URL) Test(org.junit.Test)

Example 50 with MessageFactory

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

the class NBProviderClientServerTest method testSOAPMessageModeDocLit.

@Test
public void testSOAPMessageModeDocLit() throws Exception {
    QName serviceName = new QName("http://apache.org/hello_world_soap_http", "SOAPProviderService");
    QName portName = new QName("http://apache.org/hello_world_soap_http", "SoapProviderPort");
    Service service = Service.create(serviceName);
    assertNotNull(service);
    service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING, ADDRESS);
    try {
        Dispatch<SOAPMessage> dispatch = service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);
        MessageFactory factory = MessageFactory.newInstance();
        SOAPMessage request = encodeRequest(factory, "sayHi");
        SOAPMessage response;
        try {
            response = dispatch.invoke(request);
            fail("Should have thrown an exception");
        } catch (Exception ex) {
            // expected
            assertEquals("no body expected", ex.getMessage());
        }
        request = encodeRequest(factory, null);
        response = dispatch.invoke(request);
        String resp = decodeResponse(response);
        assertEquals("Bonjour", resp);
    } catch (UndeclaredThrowableException ex) {
        throw (Exception) ex.getCause();
    }
}
Also used : MessageFactory(javax.xml.soap.MessageFactory) QName(javax.xml.namespace.QName) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) Service(javax.xml.ws.Service) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPException(javax.xml.soap.SOAPException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) Test(org.junit.Test)

Aggregations

MessageFactory (javax.xml.soap.MessageFactory)70 SOAPMessage (javax.xml.soap.SOAPMessage)65 URL (java.net.URL)24 InputStream (java.io.InputStream)22 QName (javax.xml.namespace.QName)22 SOAPException (javax.xml.soap.SOAPException)22 Test (org.junit.Test)21 ByteArrayInputStream (java.io.ByteArrayInputStream)19 SOAPBody (javax.xml.soap.SOAPBody)18 SOAPPart (javax.xml.soap.SOAPPart)17 StreamSource (javax.xml.transform.stream.StreamSource)14 SOAPConnection (javax.xml.soap.SOAPConnection)13 IOException (java.io.IOException)11 SOAPElement (javax.xml.soap.SOAPElement)11 RunAsClient (org.jboss.arquillian.container.test.api.RunAsClient)10 JBossWSTest (org.jboss.wsf.test.JBossWSTest)10 NodeList (org.w3c.dom.NodeList)9 AttachmentPart (javax.xml.soap.AttachmentPart)8 SOAPEnvelope (javax.xml.soap.SOAPEnvelope)8 Element (org.w3c.dom.Element)8