Search in sources :

Example 1 with LogicalMessageContext

use of javax.xml.ws.handler.LogicalMessageContext 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 2 with LogicalMessageContext

use of javax.xml.ws.handler.LogicalMessageContext in project cxf by apache.

the class HandlerInvocationTest method testHandlersInvokedForDispatch.

@Test
public void testHandlersInvokedForDispatch() throws Exception {
    Dispatch<SOAPMessage> disp = service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);
    setAddress(disp, "http://localhost:" + port + "/HandlerTest/SoapPort");
    TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(false);
    TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(false);
    TestSOAPHandler soapHandler1 = new TestSOAPHandler(false);
    TestSOAPHandler soapHandler2 = new TestSOAPHandler(false);
    addHandlersToChain(disp, handler1, handler2, soapHandler1, soapHandler2);
    InputStream is = getClass().getResourceAsStream("PingReq.xml");
    SOAPMessage outMsg = MessageFactory.newInstance().createMessage(null, is);
    SOAPMessage inMsg = disp.invoke(outMsg);
    assertNotNull(inMsg);
    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());
    // 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" };
    List<String> resp = getHandlerNames(inMsg.getSOAPPart().getEnvelope().getBody());
    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) InputStream(java.io.InputStream) SOAPMessage(javax.xml.soap.SOAPMessage) HandlerTest(org.apache.handler_test.HandlerTest) Test(org.junit.Test)

Example 3 with LogicalMessageContext

use of javax.xml.ws.handler.LogicalMessageContext in project cxf by apache.

the class HandlerInvocationTest method testSOAPHandlerHandleMessageReturnFalseClientOutbound.

@Test
public void testSOAPHandlerHandleMessageReturnFalseClientOutbound() throws Exception {
    final String clientHandlerMessage = "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);
                }
            } catch (Exception e) {
                e.printStackTrace();
                fail(e.toString());
            }
            return true;
        }
    };
    TestSOAPHandler soapHandler1 = new TestSOAPHandler(false);
    TestSOAPHandler soapHandler2 = new TestSOAPHandler(false) {

        public boolean handleMessage(SOAPMessageContext ctx) {
            super.handleMessage(ctx);
            Boolean outbound = (Boolean) ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
            if (outbound) {
                return false;
            }
            return true;
        }
    };
    addHandlersToChain((BindingProvider) handlerTest, handler1, handler2, soapHandler1, soapHandler2);
    List<String> resp = handlerTest.ping();
    assertEquals(clientHandlerMessage, resp.get(0));
    assertEquals(2, handler1.getHandleMessageInvoked());
    assertEquals(2, handler2.getHandleMessageInvoked());
    assertEquals(2, soapHandler1.getHandleMessageInvoked());
    assertEquals(1, 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());
}
Also used : LogicalMessageContext(javax.xml.ws.handler.LogicalMessageContext) JAXBContext(javax.xml.bind.JAXBContext) PingResponse(org.apache.handler_test.types.PingResponse) 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) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) LogicalMessage(javax.xml.ws.LogicalMessage) HandlerTest(org.apache.handler_test.HandlerTest) Test(org.junit.Test)

Example 4 with LogicalMessageContext

use of javax.xml.ws.handler.LogicalMessageContext in project cxf by apache.

the class HandlerInvocationTest method testAddHandlerThroughHandlerResolverClientSide.

@Test
public void testAddHandlerThroughHandlerResolverClientSide() throws Exception {
    TestHandler<LogicalMessageContext> handler1 = new TestHandler<LogicalMessageContext>(false);
    TestHandler<LogicalMessageContext> handler2 = new TestHandler<LogicalMessageContext>(false);
    MyHandlerResolver myHandlerResolver = new MyHandlerResolver(handler1, handler2);
    service.setHandlerResolver(myHandlerResolver);
    HandlerTest handlerTestNew = service.getPort(portName, HandlerTest.class);
    setAddress(handlerTestNew, "http://localhost:" + port + "/HandlerTest/SoapPort");
    handlerTestNew.pingOneWay();
    String bindingID = myHandlerResolver.bindingID;
    assertEquals("http://schemas.xmlsoap.org/wsdl/soap/http", bindingID);
    assertEquals(1, handler1.getHandleMessageInvoked());
    assertEquals(1, handler2.getHandleMessageInvoked());
    // CXF-3956 - check to make sure a Dispatch can also get the handlers
    JAXBContext context = JAXBContext.newInstance(org.apache.handler_test.types.ObjectFactory.class);
    Dispatch<Object> disp = service.createDispatch(portName, context, Service.Mode.PAYLOAD);
    setAddress(disp, "http://localhost:" + port + "/HandlerTest/SoapPort");
    disp.invokeOneWay(new org.apache.handler_test.types.PingOneWay());
    assertEquals(2, handler1.getHandleMessageInvoked());
    assertEquals(2, handler2.getHandleMessageInvoked());
}
Also used : HandlerTest(org.apache.handler_test.HandlerTest) PingOneWay(org.apache.handler_test.types.PingOneWay) LogicalMessageContext(javax.xml.ws.handler.LogicalMessageContext) JAXBContext(javax.xml.bind.JAXBContext) HandlerTest(org.apache.handler_test.HandlerTest) Test(org.junit.Test)

Example 5 with LogicalMessageContext

use of javax.xml.ws.handler.LogicalMessageContext in project jbossws-cxf by jbossws.

the class LogicalSourceHandler method appendHandlerName.

public boolean appendHandlerName(MessageContext msgContext, String direction) {
    try {
        // Get the payload as Source
        LogicalMessageContext logicalContext = (LogicalMessageContext) msgContext;
        Source source = logicalContext.getMessage().getPayload();
        TransformerFactory tf = TransformerFactory.newInstance();
        ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
        tf.newTransformer().transform(source, new StreamResult(baos));
        // Parse the payload and extract the value
        Element root = DOMUtils.parse(new ByteArrayInputStream(baos.toByteArray()), getDocumentBuilder());
        String oldValue = DOMUtils.getTextContent(root);
        String newValue = oldValue + ":" + direction + "LogicalHandler";
        root.setTextContent(newValue);
        log.debug("oldValue: " + oldValue);
        log.debug("newValue: " + newValue);
        // Set the updated payload
        source = new DOMSource(root);
        logicalContext.getMessage().setPayload(source);
        return true;
    } catch (RuntimeException rte) {
        throw rte;
    } catch (Exception ex) {
        throw new WebServiceException(ex);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) LogicalMessageContext(javax.xml.ws.handler.LogicalMessageContext) TransformerFactory(javax.xml.transform.TransformerFactory) StreamResult(javax.xml.transform.stream.StreamResult) WebServiceException(javax.xml.ws.WebServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream) Element(org.w3c.dom.Element) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) WebServiceException(javax.xml.ws.WebServiceException)

Aggregations

LogicalMessageContext (javax.xml.ws.handler.LogicalMessageContext)13 WebServiceException (javax.xml.ws.WebServiceException)9 Test (org.junit.Test)9 HandlerTest (org.apache.handler_test.HandlerTest)7 Source (javax.xml.transform.Source)6 LogicalMessage (javax.xml.ws.LogicalMessage)6 TransformerException (javax.xml.transform.TransformerException)5 ProtocolException (javax.xml.ws.ProtocolException)5 SOAPFaultException (javax.xml.ws.soap.SOAPFaultException)5 PingException (org.apache.handler_test.PingException)5 JAXBContext (javax.xml.bind.JAXBContext)4 TransformerFactory (javax.xml.transform.TransformerFactory)4 StreamResult (javax.xml.transform.stream.StreamResult)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 DOMSource (javax.xml.transform.dom.DOMSource)3 SOAPMessageContext (javax.xml.ws.handler.soap.SOAPMessageContext)3 PingOneWay (org.apache.handler_test.types.PingOneWay)3 Element (org.w3c.dom.Element)3 ArrayList (java.util.ArrayList)2