Search in sources :

Example 91 with SOAPMessage

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

the class HandlerInvocationTest method testServerSOAPInboundHandlerThrowsSOAPFaultToClientHandlers.

@Test
public void testServerSOAPInboundHandlerThrowsSOAPFaultToClientHandlers() throws Exception {
    TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(false);
    TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(false) {

        public boolean handleFault(LogicalMessageContext ctx) {
            super.handleFault(ctx);
            try {
                Boolean outbound = (Boolean) ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
                if (!outbound) {
                    LogicalMessage msg = ctx.getMessage();
                    Source source = msg.getPayload();
                    assertNotNull(source);
                }
            } catch (Exception e) {
                e.printStackTrace();
                fail(e.toString());
            }
            return true;
        }
    };
    TestSOAPHandler soapHandler1 = new TestSOAPHandler(false);
    TestSOAPHandler soapHandler2 = new TestSOAPHandler(false) {

        public boolean handleFault(SOAPMessageContext ctx) {
            super.handleFault(ctx);
            Boolean outbound = (Boolean) ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
            if (!outbound) {
                try {
                    SOAPMessage msg = ctx.getMessage();
                    assertNotNull(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                    fail(e.toString());
                }
            }
            return true;
        }
    };
    addHandlersToChain((BindingProvider) handlerTest, handler1, handler2, soapHandler1, soapHandler2);
    try {
        handlerTest.pingWithArgs("soapHandler3 inbound throw SOAPFaultException");
        fail("did not get expected SOAPFaultException");
    } catch (SOAPFaultException e) {
    // e.printStackTrace();
    /*            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PrintStream ps = new PrintStream(baos, true);
            e.printStackTrace(ps);
            assertTrue("Did not get expected exception message",  baos.toString()
                .indexOf("HandleMessage throws exception") > -1);
            assertTrue("Did not get expected javax.xml.ws.soap.SOAPFaultException", baos.toString()
                .indexOf("javax.xml.ws.soap.SOAPFaultException") > -1);*/
    }
/*        assertEquals("handle message was not invoked", 1, handler1.getHandleMessageInvoked());
        assertEquals("handle message was not invoked", 1, handler2.getHandleMessageInvoked());
        assertEquals("handle message was not invoked", 1, soapHandler1.getHandleMessageInvoked());
        assertEquals("handle message was not invoked", 1, soapHandler2.getHandleMessageInvoked());

        assertEquals("handle message was not invoked", 1, handler1.getHandleFaultInvoked());
        assertEquals("handle message was not invoked", 1, handler2.getHandleFaultInvoked());
        assertEquals("handle message was not invoked", 1, soapHandler1.getHandleFaultInvoked());
        assertEquals("handle message was not invoked", 1, soapHandler2.getHandleFaultInvoked());

        assertEquals("close must be called", 1, handler1.getCloseInvoked());
        assertEquals("close must be called", 1, handler2.getCloseInvoked());
        assertEquals("close must be called", 1, soapHandler1.getCloseInvoked());
        assertEquals("close must be called", 1, soapHandler2.getCloseInvoked());

        assertTrue(soapHandler2.getInvokeOrderOfClose()
                   < soapHandler1.getInvokeOrderOfClose());
        assertTrue(soapHandler1.getInvokeOrderOfClose()
                   < handler2.getInvokeOrderOfClose());
        assertTrue(handler2.getInvokeOrderOfClose()
                   < handler1.getInvokeOrderOfClose());  */
}
Also used : LogicalMessageContext(javax.xml.ws.handler.LogicalMessageContext) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) LogicalMessage(javax.xml.ws.LogicalMessage) SOAPMessage(javax.xml.soap.SOAPMessage) Source(javax.xml.transform.Source) PingException(org.apache.handler_test.PingException) ProtocolException(javax.xml.ws.ProtocolException) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) TransformerException(javax.xml.transform.TransformerException) WebServiceException(javax.xml.ws.WebServiceException) HandlerTest(org.apache.handler_test.HandlerTest) Test(org.junit.Test)

Example 92 with SOAPMessage

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

the class HandlerInvocationUsingAddNumbersTest method testInvokeFromDispatchWithJAXBPayload.

@Test
public void testInvokeFromDispatchWithJAXBPayload() throws Exception {
    URL wsdl = getClass().getResource("/wsdl/addNumbers.wsdl");
    assertNotNull(wsdl);
    AddNumbersService service = new AddNumbersService(wsdl, serviceName);
    assertNotNull(service);
    JAXBContext jc = JAXBContext.newInstance("org.apache.handlers.types");
    Dispatch<Object> disp = service.createDispatch(portName, jc, Service.Mode.PAYLOAD);
    setAddress(disp, addNumbersAddress);
    SmallNumberHandler sh = new SmallNumberHandler();
    TestSOAPHandler soapHandler = new TestSOAPHandler(false) {

        public boolean handleMessage(SOAPMessageContext ctx) {
            super.handleMessage(ctx);
            Boolean outbound = (Boolean) ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
            if (outbound) {
                try {
                    SOAPMessage msg = ctx.getMessage();
                    // System.out.println("aaaaaaaaaaaa");
                    // msg.writeTo(System.out);
                    assertNotNull(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                    fail(e.toString());
                }
            }
            return true;
        }
    };
    addHandlersProgrammatically(disp, sh, soapHandler);
    org.apache.handlers.types.AddNumbers req = new org.apache.handlers.types.AddNumbers();
    req.setArg0(10);
    req.setArg1(20);
    ObjectFactory factory = new ObjectFactory();
    JAXBElement<org.apache.handlers.types.AddNumbers> e = factory.createAddNumbers(req);
    JAXBElement<?> response = (JAXBElement<?>) disp.invoke(e);
    assertNotNull(response);
    AddNumbersResponse value = (AddNumbersResponse) response.getValue();
    assertEquals(200, value.getReturn());
}
Also used : AddNumbersService(org.apache.handlers.AddNumbersService) JAXBContext(javax.xml.bind.JAXBContext) AddNumbersResponse(org.apache.handlers.types.AddNumbersResponse) JAXBElement(javax.xml.bind.JAXBElement) SOAPMessage(javax.xml.soap.SOAPMessage) URL(java.net.URL) ObjectFactory(org.apache.handlers.types.ObjectFactory) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) AddNumbers(org.apache.handlers.AddNumbers) Test(org.junit.Test)

Example 93 with SOAPMessage

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

the class DispatchClientServerTest method testDOMSourcePAYLOAD.

@Test
public void testDOMSourcePAYLOAD() throws Exception {
    /*URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
        assertNotNull(wsdl);

        SOAPService service = new SOAPService(wsdl, serviceName);
        assertNotNull(service);*/
    Service service = Service.create(SERVICE_NAME);
    assertNotNull(service);
    service.addPort(PORT_NAME, "http://schemas.xmlsoap.org/soap/", "http://localhost:" + greeterPort + "/SOAPDispatchService/SoapDispatchPort");
    Dispatch<DOMSource> disp = service.createDispatch(PORT_NAME, DOMSource.class, Service.Mode.PAYLOAD);
    InputStream is = getClass().getResourceAsStream("resources/GreetMeDocLiteralReq.xml");
    SOAPMessage soapReqMsg = MessageFactory.newInstance().createMessage(null, is);
    DOMSource domReqMsg = new DOMSource(soapReqMsg.getSOAPBody().extractContentAsDocument());
    assertNotNull(domReqMsg);
    // invoke
    DOMSource domResMsg = disp.invoke(domReqMsg);
    assertNotNull(domResMsg);
    String expected = "Hello TestSOAPInputMessage";
    Node node = domResMsg.getNode();
    assertNotNull(node);
    if (node instanceof Document) {
        node = ((Document) node).getDocumentElement();
    }
    String content = node.getTextContent();
    assertNotNull(content);
    assertEquals("Response should be : Hello TestSOAPInputMessage", expected, content.trim());
    InputStream is1 = getClass().getResourceAsStream("resources/GreetMeDocLiteralReq1.xml");
    SOAPMessage soapReqMsg1 = MessageFactory.newInstance().createMessage(null, is1);
    DOMSource domReqMsg1 = new DOMSource(soapReqMsg1.getSOAPBody().extractContentAsDocument());
    assertNotNull(domReqMsg1);
    // invokeOneWay
    disp.invokeOneWay(domReqMsg1);
    InputStream is2 = getClass().getResourceAsStream("resources/GreetMeDocLiteralReq2.xml");
    SOAPMessage soapReqMsg2 = MessageFactory.newInstance().createMessage(null, is2);
    DOMSource domReqMsg2 = new DOMSource(soapReqMsg2.getSOAPBody().extractContentAsDocument());
    assertNotNull(domReqMsg2);
    // invokeAsync
    Response<DOMSource> response = disp.invokeAsync(domReqMsg2);
    DOMSource domRespMsg2 = response.get();
    assertNotNull(domRespMsg2);
    String expected2 = "Hello TestSOAPInputMessage2";
    node = domRespMsg2.getNode();
    assertNotNull(node);
    if (node instanceof Document) {
        node = ((Document) node).getDocumentElement();
    }
    content = node.getTextContent();
    assertNotNull(content);
    assertEquals("Response should be : Hello TestSOAPInputMessage2", expected2, content.trim());
    InputStream is3 = getClass().getResourceAsStream("resources/GreetMeDocLiteralReq3.xml");
    SOAPMessage soapReqMsg3 = MessageFactory.newInstance().createMessage(null, is3);
    DOMSource domReqMsg3 = new DOMSource(soapReqMsg3.getSOAPBody().extractContentAsDocument());
    assertNotNull(domReqMsg3);
    // invokeAsync with AsyncHandler
    TestDOMSourceHandler tdsh = new TestDOMSourceHandler();
    Future<?> fd = disp.invokeAsync(domReqMsg3, tdsh);
    assertNotNull(fd);
    waitForFuture(fd);
    String expected3 = "Hello TestSOAPInputMessage3";
    assertEquals("Response should be : Hello TestSOAPInputMessage3", expected3, tdsh.getReplyBuffer().trim());
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) InputStream(java.io.InputStream) Node(org.w3c.dom.Node) SOAPService(org.apache.hello_world_soap_http.SOAPService) Service(javax.xml.ws.Service) WebService(javax.jws.WebService) Document(org.w3c.dom.Document) SOAPMessage(javax.xml.soap.SOAPMessage) Test(org.junit.Test)

Example 94 with SOAPMessage

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

the class DispatchClientServerWithHugeResponseTest method testStackOverflowErrorForSOAPMessageWithHugeResponse.

@Test
public void testStackOverflowErrorForSOAPMessageWithHugeResponse() throws Exception {
    HugeResponseInterceptor hugeResponseInterceptor = new HugeResponseInterceptor(ResponseInterceptorType.overflow);
    getBus().getInInterceptors().add(hugeResponseInterceptor);
    URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
    assertNotNull(wsdl);
    SOAPService service = new SOAPService(wsdl, SERVICE_NAME);
    assertNotNull(service);
    Dispatch<SOAPMessage> disp = service.createDispatch(PORT_NAME, SOAPMessage.class, Service.Mode.MESSAGE);
    disp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://localhost:" + greeterPort + "/SOAPDispatchService/SoapDispatchPort");
    StaxUtils.setInnerElementCountThreshold(12);
    StaxUtils.setInnerElementLevelThreshold(12);
    InputStream is3 = getClass().getResourceAsStream("resources/GreetMeDocLiteralReq3.xml");
    SOAPMessage soapReqMsg3 = MessageFactory.newInstance().createMessage(null, is3);
    assertNotNull(soapReqMsg3);
    Response<SOAPMessage> response = disp.invokeAsync(soapReqMsg3);
    try {
        response.get(300, TimeUnit.SECONDS);
    } catch (TimeoutException te) {
        fail("We should not have encountered a timeout, " + "should get some exception tell me stackoverflow");
    } catch (Throwable e) {
        assertTrue(e.getCause() instanceof StackOverflowError);
    } finally {
        getBus().getInInterceptors().remove(hugeResponseInterceptor);
    }
}
Also used : SOAPService(org.apache.hello_world_soap_http.SOAPService) InputStream(java.io.InputStream) SOAPMessage(javax.xml.soap.SOAPMessage) URL(java.net.URL) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 95 with SOAPMessage

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

the class DispatchClientServerWithHugeResponseTest method testElementCountThresholdfForSOAPMessageWithHugeResponse.

@Test
public void testElementCountThresholdfForSOAPMessageWithHugeResponse() throws Throwable {
    HugeResponseInterceptor hugeResponseInterceptor = new HugeResponseInterceptor(ResponseInterceptorType.ElementCountThreshold);
    getBus().getInInterceptors().add(hugeResponseInterceptor);
    URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
    assertNotNull(wsdl);
    SOAPService service = new SOAPService(wsdl, SERVICE_NAME);
    assertNotNull(service);
    Dispatch<SOAPMessage> disp = service.createDispatch(PORT_NAME, SOAPMessage.class, Service.Mode.MESSAGE);
    disp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://localhost:" + greeterPort + "/SOAPDispatchService/SoapDispatchPort");
    StaxUtils.setInnerElementCountThreshold(12);
    StaxUtils.setInnerElementLevelThreshold(12);
    InputStream is3 = getClass().getResourceAsStream("resources/GreetMeDocLiteralReq3.xml");
    SOAPMessage soapReqMsg3 = MessageFactory.newInstance().createMessage(null, is3);
    assertNotNull(soapReqMsg3);
    Response<SOAPMessage> response = disp.invokeAsync(soapReqMsg3);
    try {
        response.get(300, TimeUnit.SECONDS);
        fail("should catch exception");
    } catch (TimeoutException te) {
        fail("We should not have encountered a timeout, " + "should get some exception tell me stackoverflow");
    } catch (ExecutionException e) {
        if (e.getCause() == null) {
            throw e;
        }
        Throwable t = e.getCause();
        if (t instanceof SoapFault) {
            SoapFault sf = (SoapFault) e.getCause();
            if (sf.getCause() == null) {
                throw e;
            }
            t = sf.getCause();
        }
        if (t.getMessage() == null) {
            throw e;
        }
        String msg = t.getMessage();
        assertTrue(msg, msg.startsWith("reach the innerElementCountThreshold") || msg.contains("Maximum Number of Child Elements"));
    } finally {
        getBus().getInInterceptors().remove(hugeResponseInterceptor);
    }
}
Also used : SOAPService(org.apache.hello_world_soap_http.SOAPService) SoapFault(org.apache.cxf.binding.soap.SoapFault) InputStream(java.io.InputStream) ExecutionException(java.util.concurrent.ExecutionException) SOAPMessage(javax.xml.soap.SOAPMessage) URL(java.net.URL) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

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