Search in sources :

Example 1 with AddNumbers

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

the class LogicalMessageImplTest method testGetPayloadOfJAXB.

@Test
public void testGetPayloadOfJAXB() throws Exception {
    // using Dispatch
    JAXBContext ctx = JAXBContext.newInstance(ObjectFactory.class);
    Message message = new MessageImpl();
    Exchange e = new ExchangeImpl();
    message.setExchange(e);
    LogicalMessageContextImpl lmci = new LogicalMessageContextImpl(message);
    JAXBElement<AddNumbers> el = new ObjectFactory().createAddNumbers(req);
    LogicalMessageImpl lmi = new LogicalMessageImpl(lmci);
    lmi.setPayload(el, ctx);
    Object obj = lmi.getPayload(ctx);
    assertTrue(obj instanceof JAXBElement);
    JAXBElement<?> el2 = (JAXBElement<?>) obj;
    assertTrue(el2.getValue() instanceof AddNumbers);
    AddNumbers resp = (AddNumbers) el2.getValue();
    assertEquals(req.getArg0(), resp.getArg0());
    assertEquals(req.getArg1(), resp.getArg1());
}
Also used : Message(org.apache.cxf.message.Message) LogicalMessageImpl(org.apache.cxf.jaxws.handler.logical.LogicalMessageImpl) JAXBContext(javax.xml.bind.JAXBContext) JAXBElement(javax.xml.bind.JAXBElement) Exchange(org.apache.cxf.message.Exchange) LogicalMessageContextImpl(org.apache.cxf.jaxws.handler.logical.LogicalMessageContextImpl) ObjectFactory(org.apache.handlers.types.ObjectFactory) AddNumbers(org.apache.handlers.types.AddNumbers) MessageImpl(org.apache.cxf.message.MessageImpl) LogicalMessageImpl(org.apache.cxf.jaxws.handler.logical.LogicalMessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 2 with AddNumbers

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

the class ModifyNumberHandler method handleMessage.

public final boolean handleMessage(LogicalMessageContext messageContext) {
    try {
        // 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);
        Object value = payload;
        if (payload instanceof JAXBElement) {
            value = ((JAXBElement<?>) payload).getValue();
        }
        if (value instanceof AddNumbers) {
            AddNumbers req = (AddNumbers) value;
            int a = req.getArg0();
            req.setArg0(a * 10);
            msg.setPayload(payload, jaxbContext);
        }
        return true;
    } catch (JAXBException ex) {
        throw new ProtocolException(ex);
    }
}
Also used : ProtocolException(javax.xml.ws.ProtocolException) JAXBException(javax.xml.bind.JAXBException) AddNumbers(org.apache.handlers.types.AddNumbers) JAXBContext(javax.xml.bind.JAXBContext) LogicalMessage(javax.xml.ws.LogicalMessage) JAXBElement(javax.xml.bind.JAXBElement)

Example 3 with AddNumbers

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

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

the class LogicalMessageImplTest method setUp.

@Before
public void setUp() {
    req = new AddNumbers();
    req.setArg0(10);
    req.setArg1(20);
    args = new ArrayList<>();
    args.add(req);
}
Also used : AddNumbers(org.apache.handlers.types.AddNumbers) Before(org.junit.Before)

Example 5 with AddNumbers

use of org.apache.handlers.types.AddNumbers 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)

Aggregations

AddNumbers (org.apache.handlers.types.AddNumbers)6 JAXBContext (javax.xml.bind.JAXBContext)5 JAXBElement (javax.xml.bind.JAXBElement)5 ObjectFactory (org.apache.handlers.types.ObjectFactory)4 JAXBException (javax.xml.bind.JAXBException)3 LogicalMessage (javax.xml.ws.LogicalMessage)3 ProtocolException (javax.xml.ws.ProtocolException)3 AddNumbersResponse (org.apache.handlers.types.AddNumbersResponse)3 Source (javax.xml.transform.Source)2 WebServiceException (javax.xml.ws.WebServiceException)2 Test (org.junit.Test)2 URL (java.net.URL)1 LogicalMessageContextImpl (org.apache.cxf.jaxws.handler.logical.LogicalMessageContextImpl)1 LogicalMessageImpl (org.apache.cxf.jaxws.handler.logical.LogicalMessageImpl)1 Exchange (org.apache.cxf.message.Exchange)1 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)1 Message (org.apache.cxf.message.Message)1 MessageImpl (org.apache.cxf.message.MessageImpl)1 AddNumbersService (org.apache.handlers.AddNumbersService)1 Before (org.junit.Before)1