Search in sources :

Example 21 with SOAPMessageContext

use of javax.xml.ws.handler.soap.SOAPMessageContext 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 22 with SOAPMessageContext

use of javax.xml.ws.handler.soap.SOAPMessageContext 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<>(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 23 with SOAPMessageContext

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

the class HandlerInvocationTest method testServerSOAPInboundHandlerThrowsSOAPFaultToClientHandlers.

@Test
public void testServerSOAPInboundHandlerThrowsSOAPFaultToClientHandlers() throws Exception {
    TestHandler<LogicalMessageContext> handler1 = new TestHandler<>(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 24 with SOAPMessageContext

use of javax.xml.ws.handler.soap.SOAPMessageContext in project nhin-d by DirectProject.

the class RepositorySOAPHandler method handleFault.

/**
     * Handles SOAP-Errors.
     * 
     * @param context
     *            the SOAPMessageContext object.
     * @return true for successful fault handling.
     */
@Override
public boolean handleFault(SOAPMessageContext context) {
    LOGGER.info("ServerSOAPHandler.handleFault");
    boolean outbound = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    if (outbound) {
        LOGGER.info("Direction=outbound (handleFault)");
    } else {
        LOGGER.info("Direction=inbound (handleFault)");
    }
    try {
        @SuppressWarnings("unused") SOAPMessage msg = ((SOAPMessageContext) context).getMessage();
        if (context.getMessage().getSOAPBody().getFault() != null) {
            String detailName = null;
            try {
                detailName = context.getMessage().getSOAPBody().getFault().getDetail().getFirstChild().getLocalName();
                LOGGER.info("detailName=" + detailName);
            } catch (Exception e) {
                LOGGER.warn("Error extracting detailName", e);
            }
        }
    } catch (SOAPException e) {
        LOGGER.warn("Error handling fault", e);
    }
    return true;
}
Also used : SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) SOAPException(javax.xml.soap.SOAPException) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPException(javax.xml.soap.SOAPException) IOException(java.io.IOException)

Example 25 with SOAPMessageContext

use of javax.xml.ws.handler.soap.SOAPMessageContext in project nhin-d by DirectProject.

the class DirectSOAPHandler method handleFault.

/*
     * (non-Javadoc)
     * 
     * @see javax.xml.ws.handler.Handler#handleFault(javax.xml.ws.handler.MessageContext)
     */
@Override
public boolean handleFault(SOAPMessageContext context) {
    LOGGER.info("ServerSOAPHandler.handleFault");
    boolean outbound = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    if (outbound) {
        LOGGER.info("Direction=outbound (handleFault)");
    } else {
        LOGGER.info("Direction=inbound (handleFault)");
    }
    try {
        @SuppressWarnings("unused") SOAPMessage msg = ((SOAPMessageContext) context).getMessage();
        if (context.getMessage().getSOAPBody().getFault() != null) {
            String detailName = null;
            try {
                detailName = context.getMessage().getSOAPBody().getFault().getDetail().getFirstChild().getLocalName();
                LOGGER.info("detailName=" + detailName);
            } catch (Exception e) {
                LOGGER.warn("Unable to extract detailName", e);
            }
        }
    } catch (SOAPException e) {
        LOGGER.warn("Error handling fault", e);
    }
    return true;
}
Also used : SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) SOAPException(javax.xml.soap.SOAPException) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPException(javax.xml.soap.SOAPException)

Aggregations

SOAPMessageContext (javax.xml.ws.handler.soap.SOAPMessageContext)45 SOAPMessage (javax.xml.soap.SOAPMessage)33 WebServiceException (javax.xml.ws.WebServiceException)24 SOAPException (javax.xml.soap.SOAPException)22 SOAPElement (javax.xml.soap.SOAPElement)18 Test (org.junit.Test)13 SOAPBody (javax.xml.soap.SOAPBody)12 SOAPBodyElement (javax.xml.soap.SOAPBodyElement)10 QName (javax.xml.namespace.QName)7 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)7 IOException (java.io.IOException)6 List (java.util.List)6 Set (java.util.Set)6 ContentType (javax.mail.internet.ContentType)6 SOAPEnvelope (javax.xml.soap.SOAPEnvelope)6 SOAPHeaderElement (javax.xml.soap.SOAPHeaderElement)6 Handler (javax.xml.ws.handler.Handler)6 MessageContext (javax.xml.ws.handler.MessageContext)6 SOAPHandler (javax.xml.ws.handler.soap.SOAPHandler)6 ArrayList (java.util.ArrayList)5