Search in sources :

Example 1 with AddNumbersResponse

use of org.apache.handlers.types.AddNumbersResponse 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 2 with AddNumbersResponse

use of org.apache.handlers.types.AddNumbersResponse in project cxf by apache.

the class SmallNumberHandler method handleMessage.

public final boolean handleMessage(LogicalMessageContext messageContext) {
    try {
        boolean outbound = (Boolean) messageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        if (outbound) {
            // get the LogicalMessage from our context
            LogicalMessage msg = messageContext.getMessage();
            // check the payload, if its an AddNumbers request, we'll intervene
            JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
            Object payload = msg.getPayload(jaxbContext);
            if (payload instanceof JAXBElement) {
                payload = ((JAXBElement<?>) payload).getValue();
            }
            if (payload instanceof AddNumbers) {
                AddNumbers req = (AddNumbers) payload;
                // now, if the arguments are small, let's do the calculation here
                int a = req.getArg0();
                int b = req.getArg1();
                if (isSmall(a) && isSmall(b)) {
                    int answer = a + b;
                    // System.out.printf("SmallNumberHandler addNumbers(%d, %d) == %d\n", a, b, answer);
                    // ok, we've done the calculation, so build the
                    // response and set it as the payload of the message
                    AddNumbersResponse resp = new AddNumbersResponse();
                    resp.setReturn(answer);
                    msg.setPayload(new ObjectFactory().createAddNumbersResponse(resp), jaxbContext);
                    Source src = msg.getPayload();
                    msg.setPayload(src);
                    payload = msg.getPayload(jaxbContext);
                    if (payload instanceof JAXBElement) {
                        payload = ((JAXBElement<?>) payload).getValue();
                    }
                    AddNumbersResponse resp2 = (AddNumbersResponse) payload;
                    if (resp2 == resp) {
                        throw new WebServiceException("Shouldn't be the same object");
                    }
                    // returned to the client
                    return false;
                }
            }
        }
        return true;
    } catch (JAXBException ex) {
        throw new ProtocolException(ex);
    }
}
Also used : ProtocolException(javax.xml.ws.ProtocolException) WebServiceException(javax.xml.ws.WebServiceException) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) AddNumbersResponse(org.apache.handlers.types.AddNumbersResponse) JAXBElement(javax.xml.bind.JAXBElement) Source(javax.xml.transform.Source) ObjectFactory(org.apache.handlers.types.ObjectFactory) AddNumbers(org.apache.handlers.types.AddNumbers) LogicalMessage(javax.xml.ws.LogicalMessage)

Example 3 with AddNumbersResponse

use of org.apache.handlers.types.AddNumbersResponse in project cxf by apache.

the class DispatchHandlerInvocationTest method testInvokeWithJAXBPayloadMode.

@Test
public void testInvokeWithJAXBPayloadMode() 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);
    TestHandler handler = new TestHandler();
    TestSOAPHandler soapHandler = new TestSOAPHandler();
    addHandlersProgrammatically(disp, handler, soapHandler);
    org.apache.handlers.types.AddNumbers req = new org.apache.handlers.types.AddNumbers();
    req.setArg0(10);
    req.setArg1(20);
    ObjectFactory factory = new ObjectFactory();
    JAXBElement<AddNumbers> e = factory.createAddNumbers(req);
    JAXBElement<?> response = (JAXBElement<?>) disp.invoke(e);
    assertNotNull(response);
    AddNumbersResponse value = (AddNumbersResponse) response.getValue();
    assertEquals(222, 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) URL(java.net.URL) ObjectFactory(org.apache.handlers.types.ObjectFactory) AddNumbers(org.apache.handlers.types.AddNumbers) AddNumbers(org.apache.handlers.types.AddNumbers) Test(org.junit.Test)

Example 4 with AddNumbersResponse

use of org.apache.handlers.types.AddNumbersResponse in project cxf by apache.

the class SmallNumberHandler method handleMessage.

// Implementation of javax.xml.ws.handler.Handler
public final boolean handleMessage(LogicalMessageContext messageContext) {
    System.out.println("LogicalMessageHandler handleMessage called");
    try {
        boolean outbound = (Boolean) messageContext.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        if (outbound) {
            // get the LogicalMessage from our context
            // 
            LogicalMessage msg = messageContext.getMessage();
            // check the payload, if its an AddNumbers request, we'll intervene
            // 
            JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
            Object payload = msg.getPayload(jaxbContext);
            if (payload instanceof JAXBElement) {
                payload = ((JAXBElement) payload).getValue();
            }
            if (payload instanceof AddNumbers) {
                AddNumbers req = (AddNumbers) payload;
                // now, if the arguments are small, let's do the calculation here
                // 
                int a = req.getArg0();
                int b = req.getArg1();
                if (isSmall(a) && isSmall(b)) {
                    int answer = a + b;
                    // System.out.printf("SmallNumberHandler addNumbers(%d, %d) == %d\n", a, b, answer);
                    // ok, we've done the calculation, so build the
                    // response and set it as the payload of the message
                    AddNumbersResponse resp = new AddNumbersResponse();
                    resp.setReturn(answer);
                    msg.setPayload(new ObjectFactory().createAddNumbersResponse(resp), jaxbContext);
                    Source src = msg.getPayload();
                    msg.setPayload(src);
                    payload = msg.getPayload(jaxbContext);
                    if (payload instanceof JAXBElement) {
                        payload = ((JAXBElement) payload).getValue();
                    }
                    AddNumbersResponse resp2 = (AddNumbersResponse) payload;
                    if (resp2 == resp) {
                        throw new WebServiceException("Shouldn't be the same object");
                    }
                    // returned to the client
                    return false;
                }
            }
        }
        return true;
    } catch (JAXBException ex) {
        throw new ProtocolException(ex);
    }
}
Also used : ProtocolException(javax.xml.ws.ProtocolException) WebServiceException(javax.xml.ws.WebServiceException) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) AddNumbersResponse(org.apache.handlers.types.AddNumbersResponse) JAXBElement(javax.xml.bind.JAXBElement) Source(javax.xml.transform.Source) ObjectFactory(org.apache.handlers.types.ObjectFactory) AddNumbers(org.apache.handlers.types.AddNumbers) LogicalMessage(javax.xml.ws.LogicalMessage)

Example 5 with AddNumbersResponse

use of org.apache.handlers.types.AddNumbersResponse in project cxf by apache.

the class LogicalHandlerInterceptorTest method xtestReturnFalseClientSide.

// JAX-WS spec: If handler returns false, for a request-response MEP, if the message
// direction is reversed during processing of a request message then the message
// becomes a response message.
// NOTE: commented out as this has been covered by other tests.
@Test
@org.junit.Ignore
public void xtestReturnFalseClientSide() throws Exception {
    @SuppressWarnings("rawtypes") List<Handler> list = new ArrayList<>();
    list.add(new LogicalHandler<LogicalMessageContext>() {

        public void close(MessageContext arg0) {
        }

        public boolean handleFault(LogicalMessageContext messageContext) {
            return true;
        }

        public boolean handleMessage(LogicalMessageContext messageContext) {
            LogicalMessage msg = messageContext.getMessage();
            AddNumbersResponse resp = new AddNumbersResponse();
            resp.setReturn(11);
            msg.setPayload(resp, null);
            return false;
        }
    });
    HandlerChainInvoker invoker1 = new HandlerChainInvoker(list);
    IMocksControl control1 = createNiceControl();
    Binding binding1 = control1.createMock(Binding.class);
    @SuppressWarnings("rawtypes") List<Handler> hList = CastUtils.cast(list);
    expect(binding1.getHandlerChain()).andReturn(hList).anyTimes();
    Exchange exchange1 = control1.createMock(Exchange.class);
    expect(exchange1.get(HandlerChainInvoker.class)).andReturn(invoker1).anyTimes();
    Message outMessage = new MessageImpl();
    outMessage.setExchange(exchange1);
    InterceptorChain chain = control1.createMock(InterceptorChain.class);
    outMessage.setInterceptorChain(chain);
    chain.abort();
    EasyMock.expectLastCall();
    MessageObserver observer = control1.createMock(MessageObserver.class);
    expect(exchange1.get(MessageObserver.class)).andReturn(observer).anyTimes();
    observer.onMessage(isA(Message.class));
    EasyMock.expectLastCall();
    control1.replay();
    LogicalHandlerInInterceptor li = new LogicalHandlerInInterceptor(binding1);
    li.handleMessage(outMessage);
    control1.verify();
}
Also used : Binding(javax.xml.ws.Binding) LogicalMessageContext(javax.xml.ws.handler.LogicalMessageContext) MessageObserver(org.apache.cxf.transport.MessageObserver) LogicalMessage(javax.xml.ws.LogicalMessage) Message(org.apache.cxf.message.Message) ArrayList(java.util.ArrayList) LogicalHandler(javax.xml.ws.handler.LogicalHandler) Handler(javax.xml.ws.handler.Handler) AddNumbersResponse(org.apache.handlers.types.AddNumbersResponse) IMocksControl(org.easymock.IMocksControl) Exchange(org.apache.cxf.message.Exchange) InterceptorChain(org.apache.cxf.interceptor.InterceptorChain) LogicalHandlerInInterceptor(org.apache.cxf.jaxws.handler.logical.LogicalHandlerInInterceptor) LogicalMessageContext(javax.xml.ws.handler.LogicalMessageContext) MessageContext(javax.xml.ws.handler.MessageContext) LogicalMessage(javax.xml.ws.LogicalMessage) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Aggregations

AddNumbersResponse (org.apache.handlers.types.AddNumbersResponse)5 JAXBContext (javax.xml.bind.JAXBContext)4 JAXBElement (javax.xml.bind.JAXBElement)4 ObjectFactory (org.apache.handlers.types.ObjectFactory)4 LogicalMessage (javax.xml.ws.LogicalMessage)3 AddNumbers (org.apache.handlers.types.AddNumbers)3 Test (org.junit.Test)3 URL (java.net.URL)2 JAXBException (javax.xml.bind.JAXBException)2 Source (javax.xml.transform.Source)2 ProtocolException (javax.xml.ws.ProtocolException)2 WebServiceException (javax.xml.ws.WebServiceException)2 AddNumbersService (org.apache.handlers.AddNumbersService)2 ArrayList (java.util.ArrayList)1 SOAPMessage (javax.xml.soap.SOAPMessage)1 Binding (javax.xml.ws.Binding)1 Handler (javax.xml.ws.handler.Handler)1 LogicalHandler (javax.xml.ws.handler.LogicalHandler)1 LogicalMessageContext (javax.xml.ws.handler.LogicalMessageContext)1 MessageContext (javax.xml.ws.handler.MessageContext)1