Search in sources :

Example 1 with AddNumbersService

use of org.apache.handlers.AddNumbersService in project cxf by apache.

the class Client method main.

public static void main(String[] args) throws Exception {
    if (args.length == 0) {
        System.err.println("please provide wsdl");
        System.exit(0);
    }
    File wsdl = new File(args[0]);
    AddNumbersService service = new AddNumbersService(wsdl.toURI().toURL(), serviceName);
    AddNumbers port = (AddNumbers) service.getPort(portName, AddNumbers.class);
    // Add client side handlers programmatically
    SmallNumberHandler sh = new SmallNumberHandler();
    List<Handler> newHandlerChain = new ArrayList<>();
    newHandlerChain.add(sh);
    ((BindingProvider) port).getBinding().setHandlerChain(newHandlerChain);
    try {
        int number1 = 10;
        int number2 = 20;
        System.out.printf("Invoking addNumbers(%d, %d)\n", number1, number2);
        int result = port.addNumbers(number1, number2);
        System.out.printf("The result of adding %d and %d is %d.\n\n", number1, number2, result);
        number1 = 3;
        number2 = 5;
        System.out.printf("Invoking addNumbers(%d, %d)\n", number1, number2);
        result = port.addNumbers(number1, number2);
        System.out.printf("The result of adding %d and %d is %d.\n\n", number1, number2, result);
        number1 = -10;
        System.out.printf("Invoking addNumbers(%d, %d)\n", number1, number2);
        result = port.addNumbers(number1, number2);
        System.out.printf("The result of adding %d and %d is %d.\n", number1, number2, result);
    } catch (AddNumbersFault ex) {
        System.out.printf("Caught AddNumbersFault: %s\n", ex.getFaultInfo().getMessage());
    }
    System.exit(0);
}
Also used : SmallNumberHandler(demo.handlers.common.SmallNumberHandler) AddNumbersService(org.apache.handlers.AddNumbersService) AddNumbers(org.apache.handlers.AddNumbers) ArrayList(java.util.ArrayList) Handler(javax.xml.ws.handler.Handler) SmallNumberHandler(demo.handlers.common.SmallNumberHandler) File(java.io.File) AddNumbersFault(org.apache.handlers.AddNumbersFault)

Example 2 with AddNumbersService

use of org.apache.handlers.AddNumbersService 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 3 with AddNumbersService

use of org.apache.handlers.AddNumbersService in project cxf by apache.

the class HandlerInvocationUsingAddNumbersTest method testAddHandlerProgrammaticallyClientSide.

@Test
public void testAddHandlerProgrammaticallyClientSide() throws Exception {
    URL wsdl = getClass().getResource("/wsdl/addNumbers.wsdl");
    AddNumbersService service = new AddNumbersService(wsdl, serviceName);
    AddNumbers port = service.getPort(portName, AddNumbers.class);
    setAddress(port, addNumbersAddress);
    SmallNumberHandler sh = new SmallNumberHandler();
    addHandlersProgrammatically((BindingProvider) port, sh);
    int result = port.addNumbers(10, 20);
    assertEquals(200, result);
    int result1 = port.addNumbers(5, 6);
    assertEquals(11, result1);
}
Also used : AddNumbersService(org.apache.handlers.AddNumbersService) AddNumbers(org.apache.handlers.AddNumbers) URL(java.net.URL) Test(org.junit.Test)

Example 4 with AddNumbersService

use of org.apache.handlers.AddNumbersService in project cxf by apache.

the class DispatchHandlerInvocationTest method testInvokeWithSOAPMessagePayloadMode.

@Test
public void testInvokeWithSOAPMessagePayloadMode() 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.PAYLOAD);
    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);
    try {
        disp.invoke(soapReq);
        fail("Did not get expected exception");
    } catch (SOAPFaultException e) {
        assertTrue("Did not get expected exception message: " + e.getMessage(), e.getMessage().indexOf("is not valid in PAYLOAD mode with SOAP/HTTP binding") > -1);
    }
}
Also used : AddNumbersService(org.apache.handlers.AddNumbersService) MessageFactory(javax.xml.soap.MessageFactory) InputStream(java.io.InputStream) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) SOAPMessage(javax.xml.soap.SOAPMessage) URL(java.net.URL) Test(org.junit.Test)

Example 5 with AddNumbersService

use of org.apache.handlers.AddNumbersService in project cxf by apache.

the class DispatchHandlerInvocationTest method testInvokeWithDOMSourcPayloadMode.

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

Aggregations

AddNumbersService (org.apache.handlers.AddNumbersService)8 URL (java.net.URL)7 Test (org.junit.Test)7 SOAPMessage (javax.xml.soap.SOAPMessage)5 InputStream (java.io.InputStream)4 MessageFactory (javax.xml.soap.MessageFactory)4 AddNumbers (org.apache.handlers.AddNumbers)3 JAXBContext (javax.xml.bind.JAXBContext)2 JAXBElement (javax.xml.bind.JAXBElement)2 DOMSource (javax.xml.transform.dom.DOMSource)2 AddNumbersResponse (org.apache.handlers.types.AddNumbersResponse)2 ObjectFactory (org.apache.handlers.types.ObjectFactory)2 SmallNumberHandler (demo.handlers.common.SmallNumberHandler)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Handler (javax.xml.ws.handler.Handler)1 SOAPMessageContext (javax.xml.ws.handler.soap.SOAPMessageContext)1 SOAPFaultException (javax.xml.ws.soap.SOAPFaultException)1 AddNumbersFault (org.apache.handlers.AddNumbersFault)1 AddNumbers (org.apache.handlers.types.AddNumbers)1