Search in sources :

Example 6 with HandlerTest

use of org.apache.handler_test.HandlerTest in project cxf by apache.

the class HandlerInvocationTest method setUp.

@Before
public void setUp() throws Exception {
    super.createBus();
    wsdl = HandlerInvocationTest.class.getResource("/wsdl/handler_test.wsdl");
    service = new HandlerTestService(wsdl, serviceName);
    handlerTest = service.getPort(portName, HandlerTest.class);
    setAddress(handlerTest, "http://localhost:" + port + "/HandlerTest/SoapPort");
}
Also used : HandlerTest(org.apache.handler_test.HandlerTest) HandlerTestService(org.apache.handler_test.HandlerTestService) Before(org.junit.Before)

Example 7 with HandlerTest

use of org.apache.handler_test.HandlerTest in project cxf by apache.

the class HandlerInvocationTest method testServerEndpointRemoteFault.

@Test
public void testServerEndpointRemoteFault() throws PingException {
    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();
                    String payload = convertDOMToString(msg.getPayload());
                    assertTrue(payload.indexOf("<faultstring>" + "servant throws SOAPFaultException" + "</faultstring>") > -1);
                }
            } catch (Exception e) {
                e.printStackTrace();
                fail(e.toString());
            }
            return true;
        }

        private String convertDOMToString(Source s) throws TransformerException {
            StringWriter stringWriter = new StringWriter();
            StreamResult streamResult = new StreamResult(stringWriter);
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            transformerFactory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "no");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
            transformer.transform(s, streamResult);
            return stringWriter.toString();
        }
    };
    TestSOAPHandler soapHandler1 = new TestSOAPHandler(false);
    TestSOAPHandler soapHandler2 = new TestSOAPHandler(false) {

        public boolean handleFault(SOAPMessageContext ctx) {
            super.handleFault(ctx);
            try {
                Boolean outbound = (Boolean) ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
                if (!outbound) {
                    SOAPEnvelope env = ctx.getMessage().getSOAPPart().getEnvelope();
                    assertTrue("expected SOAPFault in SAAJ model", env.getBody().hasFault());
                }
            } catch (Exception e) {
                e.printStackTrace();
                fail(e.toString());
            }
            return true;
        }
    };
    addHandlersToChain((BindingProvider) handlerTest, handler1, handler2, soapHandler1, soapHandler2);
    try {
        handlerTest.pingWithArgs("servant throw SOAPFaultException");
        fail("did not get expected Exception");
    } catch (SOAPFaultException sfe) {
    // expected
    }
    assertEquals(1, handler1.getHandleMessageInvoked());
    assertEquals(1, handler2.getHandleMessageInvoked());
    assertEquals(1, soapHandler1.getHandleMessageInvoked());
    assertEquals(1, soapHandler2.getHandleMessageInvoked());
    assertEquals(1, handler2.getHandleFaultInvoked());
    assertEquals(1, handler1.getHandleFaultInvoked());
    assertEquals(1, soapHandler1.getHandleFaultInvoked());
    assertEquals(1, soapHandler2.getHandleFaultInvoked());
    assertEquals(1, handler1.getCloseInvoked());
    assertEquals(1, handler2.getCloseInvoked());
    assertEquals(1, soapHandler1.getCloseInvoked());
    assertEquals(1, soapHandler2.getCloseInvoked());
    assertTrue(handler2.getInvokeOrderOfClose() < handler1.getInvokeOrderOfClose());
}
Also used : LogicalMessageContext(javax.xml.ws.handler.LogicalMessageContext) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) 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) Source(javax.xml.transform.Source) StringWriter(java.io.StringWriter) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) LogicalMessage(javax.xml.ws.LogicalMessage) HandlerTest(org.apache.handler_test.HandlerTest) Test(org.junit.Test)

Example 8 with HandlerTest

use of org.apache.handler_test.HandlerTest in project cxf by apache.

the class HandlerInvocationTest method testLogicalHandlerHandleMessageReturnFalseClientOutBound.

@Test
public void testLogicalHandlerHandleMessageReturnFalseClientOutBound() throws Exception {
    final String clientHandlerMessage = "handler2 client side";
    TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(false);
    TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(false) {

        public boolean handleMessage(LogicalMessageContext ctx) {
            super.handleMessage(ctx);
            try {
                Boolean outbound = (Boolean) ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
                if (outbound) {
                    LogicalMessage msg = ctx.getMessage();
                    assertNotNull("logical message is null", msg);
                    JAXBContext jaxbCtx = JAXBContext.newInstance(PackageUtils.getPackageName(PingOneWay.class));
                    PingResponse resp = new PingResponse();
                    resp.getHandlersInfo().add(clientHandlerMessage);
                    msg.setPayload(resp, jaxbCtx);
                    return false;
                }
            } catch (Exception e) {
                e.printStackTrace();
                fail(e.toString());
            }
            return true;
        }
    };
    TestHandler<LogicalMessageContext> handler3 = new TestHandler<LogicalMessageContext>(false);
    TestSOAPHandler soapHandler1 = new TestSOAPHandler(false);
    addHandlersToChain((BindingProvider) handlerTest, handler1, handler2, handler3, soapHandler1);
    List<String> resp = handlerTest.ping();
    assertEquals(clientHandlerMessage, resp.get(0));
    assertEquals("the first handler must be invoked twice", 2, handler1.getHandleMessageInvoked());
    assertEquals("the second handler must be invoked once only on outbound", 1, handler2.getHandleMessageInvoked());
    assertEquals("the third handler must not be invoked", 0, handler3.getHandleMessageInvoked());
    assertEquals("the last handler must not be invoked", 0, soapHandler1.getHandleMessageInvoked());
    // outbound MEP processing ceased, the message direction was changed to inbound, essentially this is
    // only one MEP. So close is called only once at the end of inbound MEP, and the close order is
    // reversed to the outbound handler invoking order.
    assertEquals("close must be called", 1, handler1.getCloseInvoked());
    assertEquals("close must be called", 1, handler2.getCloseInvoked());
    assertEquals("close must be called", 0, handler3.getCloseInvoked());
    assertEquals("close must be called", 0, soapHandler1.getCloseInvoked());
    assertTrue(handler2.getInvokeOrderOfClose() < handler1.getInvokeOrderOfClose());
}
Also used : LogicalMessageContext(javax.xml.ws.handler.LogicalMessageContext) JAXBContext(javax.xml.bind.JAXBContext) PingResponse(org.apache.handler_test.types.PingResponse) LogicalMessage(javax.xml.ws.LogicalMessage) 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) PingOneWay(org.apache.handler_test.types.PingOneWay) HandlerTest(org.apache.handler_test.HandlerTest) Test(org.junit.Test)

Example 9 with HandlerTest

use of org.apache.handler_test.HandlerTest in project cxf by apache.

the class HandlerInvocationTest method testSOAPHandlerHandleMessageReturnTrueClient.

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

        public boolean handleMessage(LogicalMessageContext ctx) {
            super.handleMessage(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);
    addHandlersToChain((BindingProvider) handlerTest, handler1, handler2, soapHandler1, soapHandler2);
    List<String> resp = handlerTest.ping();
    assertNotNull(resp);
    assertEquals("handle message was not invoked", 2, handler1.getHandleMessageInvoked());
    assertEquals("handle message was not invoked", 2, handler2.getHandleMessageInvoked());
    assertEquals("handle message was not invoked", 2, soapHandler1.getHandleMessageInvoked());
    assertEquals("handle message was not invoked", 2, soapHandler2.getHandleMessageInvoked());
    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());
    // the server has encoded into the response the order in
    // which the handlers have been invoked, parse it and make
    // sure everything is ok expected order for inbound interceptors
    String[] handlerNames = { "soapHandler4", "soapHandler3", "handler2", "handler1", "servant", "handler1", "handler2", "soapHandler3", "soapHandler4" };
    assertEquals(handlerNames.length, resp.size());
    Iterator<String> iter = resp.iterator();
    for (String expected : handlerNames) {
        assertEquals(expected, iter.next());
    }
}
Also used : LogicalMessageContext(javax.xml.ws.handler.LogicalMessageContext) LogicalMessage(javax.xml.ws.LogicalMessage) 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)

Aggregations

HandlerTest (org.apache.handler_test.HandlerTest)9 Test (org.junit.Test)8 LogicalMessageContext (javax.xml.ws.handler.LogicalMessageContext)7 TransformerException (javax.xml.transform.TransformerException)5 LogicalMessage (javax.xml.ws.LogicalMessage)5 ProtocolException (javax.xml.ws.ProtocolException)5 WebServiceException (javax.xml.ws.WebServiceException)5 SOAPFaultException (javax.xml.ws.soap.SOAPFaultException)5 PingException (org.apache.handler_test.PingException)5 JAXBContext (javax.xml.bind.JAXBContext)3 Source (javax.xml.transform.Source)3 SOAPMessageContext (javax.xml.ws.handler.soap.SOAPMessageContext)3 PingOneWay (org.apache.handler_test.types.PingOneWay)3 SOAPMessage (javax.xml.soap.SOAPMessage)2 PingResponse (org.apache.handler_test.types.PingResponse)2 InputStream (java.io.InputStream)1 StringWriter (java.io.StringWriter)1 SOAPEnvelope (javax.xml.soap.SOAPEnvelope)1 Transformer (javax.xml.transform.Transformer)1 TransformerFactory (javax.xml.transform.TransformerFactory)1